blob: c9cf3c28417dcc1a79cfa54eef024f19b0d691a1 [file] [log] [blame]
Ash Wilson6425a412014-08-29 12:30:35 -04001package v3
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
Ash Wilson131b7752014-08-29 12:53:55 -04007 "time"
Ash Wilson6425a412014-08-29 12:30:35 -04008
9 "github.com/rackspace/gophercloud"
Ash Wilson131b7752014-08-29 12:53:55 -040010 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
Ash Wilson6425a412014-08-29 12:30:35 -040011 "github.com/rackspace/gophercloud/testhelper"
12)
13
Ash Wilson131b7752014-08-29 12:53:55 -040014func TestNewClient(t *testing.T) {
15 testhelper.SetupHTTP()
16 defer testhelper.TeardownHTTP()
17
18 provider := &gophercloud.ProviderClient{
Ash Wilson4dee1b82014-08-29 14:56:45 -040019 IdentityEndpoint: testhelper.Endpoint() + "v3/",
Ash Wilson131b7752014-08-29 12:53:55 -040020 }
21 client := NewClient(provider)
22
23 expected := testhelper.Endpoint() + "v3/"
24 if client.Endpoint != expected {
25 t.Errorf("Expected endpoint to be %s, but was %s", expected, client.Endpoint)
26 }
27}
28
Ash Wilson6425a412014-08-29 12:30:35 -040029func TestAuthentication(t *testing.T) {
30 testhelper.SetupHTTP()
31 defer testhelper.TeardownHTTP()
Ash Wilson131b7752014-08-29 12:53:55 -040032 const ID = "aaaa1111"
Ash Wilson6425a412014-08-29 12:30:35 -040033
34 testhelper.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson131b7752014-08-29 12:53:55 -040035 w.Header().Add("X-Subject-Token", ID)
Ash Wilson6425a412014-08-29 12:30:35 -040036
37 w.WriteHeader(http.StatusCreated)
38 fmt.Fprintf(w, `{ "token": { "expires_at": "2013-02-02T18:30:59.000000Z" } }`)
39 })
40
41 provider := &gophercloud.ProviderClient{
Ash Wilson4dee1b82014-08-29 14:56:45 -040042 IdentityEndpoint: testhelper.Endpoint() + "v3/",
Ash Wilson6425a412014-08-29 12:30:35 -040043 }
44 client := NewClient(provider)
45
Ash Wilson131b7752014-08-29 12:53:55 -040046 token, err := client.Authenticate(gophercloud.AuthOptions{UserID: "me", Password: "swordfish"})
47 if err != nil {
48 t.Errorf("Unexpected error from authentication: %v", err)
49 }
50
51 if token.ID != ID {
52 t.Errorf("Expected token ID [%s], but got [%s]", ID, token.ID)
53 }
54
55 expectedExpiration, _ := time.Parse(tokens.RFC3339Milli, "2013-02-02T18:30:59.000000Z")
56 if token.ExpiresAt != expectedExpiration {
57 t.Errorf("Expected token expiration [%v], but got [%v]", expectedExpiration, token.ExpiresAt)
Ash Wilson6425a412014-08-29 12:30:35 -040058 }
59}