blob: d549f0967b3b7c8d94fc98c64e9f5b4612a52de5 [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
Ash Wilson001cfa52014-09-02 14:23:23 -040018 provider := &gophercloud.ProviderClient{}
19 client := NewClient(provider, testhelper.Endpoint()+"v3/")
Ash Wilson131b7752014-08-29 12:53:55 -040020
21 expected := testhelper.Endpoint() + "v3/"
22 if client.Endpoint != expected {
23 t.Errorf("Expected endpoint to be %s, but was %s", expected, client.Endpoint)
24 }
25}
26
Ash Wilsonccd020b2014-09-02 10:40:54 -040027func TestGetToken(t *testing.T) {
Ash Wilson6425a412014-08-29 12:30:35 -040028 testhelper.SetupHTTP()
29 defer testhelper.TeardownHTTP()
Ash Wilson131b7752014-08-29 12:53:55 -040030 const ID = "aaaa1111"
Ash Wilson6425a412014-08-29 12:30:35 -040031
32 testhelper.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson131b7752014-08-29 12:53:55 -040033 w.Header().Add("X-Subject-Token", ID)
Ash Wilson6425a412014-08-29 12:30:35 -040034
35 w.WriteHeader(http.StatusCreated)
36 fmt.Fprintf(w, `{ "token": { "expires_at": "2013-02-02T18:30:59.000000Z" } }`)
37 })
38
Ash Wilson001cfa52014-09-02 14:23:23 -040039 provider := &gophercloud.ProviderClient{}
40 client := NewClient(provider, testhelper.Endpoint()+"v3/")
Ash Wilson6425a412014-08-29 12:30:35 -040041
Ash Wilsonccd020b2014-09-02 10:40:54 -040042 token, err := client.GetToken(gophercloud.AuthOptions{UserID: "me", Password: "swordfish"})
Ash Wilson131b7752014-08-29 12:53:55 -040043 if err != nil {
44 t.Errorf("Unexpected error from authentication: %v", err)
45 }
46
47 if token.ID != ID {
48 t.Errorf("Expected token ID [%s], but got [%s]", ID, token.ID)
49 }
50
51 expectedExpiration, _ := time.Parse(tokens.RFC3339Milli, "2013-02-02T18:30:59.000000Z")
52 if token.ExpiresAt != expectedExpiration {
53 t.Errorf("Expected token expiration [%v], but got [%v]", expectedExpiration, token.ExpiresAt)
Ash Wilson6425a412014-08-29 12:30:35 -040054 }
55}