gopkg.in/goose.v2@v2.0.1/testservices/identityservice/userpass_test.go (about)

     1  package identityservice
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"gopkg.in/goose.v2/testing/httpsuite"
    13  )
    14  
    15  type UserPassSuite struct {
    16  	httpsuite.HTTPSuite
    17  }
    18  
    19  var _ = gc.Suite(&UserPassSuite{})
    20  
    21  func makeUserPass(user, secret string) (identity *UserPass) {
    22  	identity = NewUserPass()
    23  	// Ensure that it conforms to the interface
    24  	var _ IdentityService = identity
    25  	if user != "" {
    26  		identity.AddUser(user, secret, "tenant", "default")
    27  	}
    28  	return
    29  }
    30  
    31  func (s *UserPassSuite) setupUserPass(user, secret string) {
    32  	var identity *UserPass
    33  	identity = makeUserPass(user, secret)
    34  	identity.SetupHTTP(s.Mux)
    35  	return
    36  }
    37  
    38  func (s *UserPassSuite) setupUserPassWithServices(user, secret string, services []Service) {
    39  	var identity *UserPass
    40  	identity = makeUserPass(user, secret)
    41  	for _, service := range services {
    42  		identity.AddService(service)
    43  	}
    44  	identity.SetupHTTP(s.Mux)
    45  	return
    46  }
    47  
    48  var authTemplate = `{
    49      "auth": {
    50          "tenantName": "tenant-something", 
    51          "passwordCredentials": {
    52              "username": "%s", 
    53              "password": "%s"
    54          }
    55      }
    56  }`
    57  
    58  func userPassAuthRequest(URL, user, key string) (*http.Response, error) {
    59  	client := http.DefaultClient
    60  	body := strings.NewReader(fmt.Sprintf(authTemplate, user, key))
    61  	request, err := http.NewRequest("POST", URL+"/tokens", body)
    62  	request.Header.Set("Content-Type", "application/json")
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return client.Do(request)
    67  }
    68  
    69  func CheckErrorResponse(c *gc.C, r *http.Response, status int, msg string) {
    70  	c.Check(r.StatusCode, gc.Equals, status)
    71  	c.Assert(r.Header.Get("Content-Type"), gc.Equals, "application/json")
    72  	body, err := ioutil.ReadAll(r.Body)
    73  	c.Assert(err, gc.IsNil)
    74  	var errmsg ErrorWrapper
    75  	err = json.Unmarshal(body, &errmsg)
    76  	c.Assert(err, gc.IsNil)
    77  	c.Check(errmsg.Error.Code, gc.Equals, status)
    78  	c.Check(errmsg.Error.Title, gc.Equals, http.StatusText(status))
    79  	if msg != "" {
    80  		c.Check(errmsg.Error.Message, gc.Equals, msg)
    81  	}
    82  }
    83  
    84  func (s *UserPassSuite) TestNotJSON(c *gc.C) {
    85  	// We do everything in userPassAuthRequest, except set the Content-Type
    86  	s.setupUserPass("user", "secret")
    87  	client := http.DefaultClient
    88  	body := strings.NewReader(fmt.Sprintf(authTemplate, "user", "secret"))
    89  	request, err := http.NewRequest("POST", s.Server.URL+"/tokens", body)
    90  	c.Assert(err, gc.IsNil)
    91  	res, err := client.Do(request)
    92  	c.Assert(err, gc.IsNil)
    93  	defer res.Body.Close()
    94  	CheckErrorResponse(c, res, http.StatusBadRequest, notJSON)
    95  }
    96  
    97  func (s *UserPassSuite) TestBadJSON(c *gc.C) {
    98  	// We do everything in userPassAuthRequest, except set the Content-Type
    99  	s.setupUserPass("user", "secret")
   100  	res, err := userPassAuthRequest(s.Server.URL, "garbage\"in", "secret")
   101  	c.Assert(err, gc.IsNil)
   102  	defer res.Body.Close()
   103  	CheckErrorResponse(c, res, http.StatusBadRequest, notJSON)
   104  }
   105  
   106  func (s *UserPassSuite) TestNoSuchUser(c *gc.C) {
   107  	s.setupUserPass("user", "secret")
   108  	res, err := userPassAuthRequest(s.Server.URL, "not-user", "secret")
   109  	c.Assert(err, gc.IsNil)
   110  	defer res.Body.Close()
   111  	CheckErrorResponse(c, res, http.StatusUnauthorized, notAuthorized)
   112  }
   113  
   114  func (s *UserPassSuite) TestBadPassword(c *gc.C) {
   115  	s.setupUserPass("user", "secret")
   116  	res, err := userPassAuthRequest(s.Server.URL, "user", "not-secret")
   117  	c.Assert(err, gc.IsNil)
   118  	defer res.Body.Close()
   119  	CheckErrorResponse(c, res, http.StatusUnauthorized, invalidUser)
   120  }
   121  
   122  func (s *UserPassSuite) TestValidAuthorization(c *gc.C) {
   123  	compute_url := "http://testing.invalid/compute"
   124  	s.setupUserPassWithServices("user", "secret", []Service{
   125  		{V2: V2Service{"nova", "compute", []Endpoint{
   126  			{PublicURL: compute_url},
   127  		}}}})
   128  	res, err := userPassAuthRequest(s.Server.URL, "user", "secret")
   129  	c.Assert(err, gc.IsNil)
   130  	defer res.Body.Close()
   131  	c.Check(res.StatusCode, gc.Equals, http.StatusOK)
   132  	c.Check(res.Header.Get("Content-Type"), gc.Equals, "application/json")
   133  	content, err := ioutil.ReadAll(res.Body)
   134  	c.Assert(err, gc.IsNil)
   135  	var response AccessResponse
   136  	err = json.Unmarshal(content, &response)
   137  	c.Assert(err, gc.IsNil)
   138  	c.Check(response.Access.Token.Id, gc.NotNil)
   139  	novaURL := ""
   140  	for _, service := range response.Access.ServiceCatalog {
   141  		if service.Type == "compute" {
   142  			novaURL = service.Endpoints[0].PublicURL
   143  			break
   144  		}
   145  	}
   146  	c.Assert(novaURL, gc.Equals, compute_url)
   147  }