Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 1 | package v3 |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 7 | "time" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 8 | |
| 9 | "github.com/rackspace/gophercloud" |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/testhelper" |
| 12 | ) |
| 13 | |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 14 | func TestNewClient(t *testing.T) { |
| 15 | testhelper.SetupHTTP() |
| 16 | defer testhelper.TeardownHTTP() |
| 17 | |
Ash Wilson | 001cfa5 | 2014-09-02 14:23:23 -0400 | [diff] [blame^] | 18 | provider := &gophercloud.ProviderClient{} |
| 19 | client := NewClient(provider, testhelper.Endpoint()+"v3/") |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 20 | |
| 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 Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 27 | func TestGetToken(t *testing.T) { |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 28 | testhelper.SetupHTTP() |
| 29 | defer testhelper.TeardownHTTP() |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 30 | const ID = "aaaa1111" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 31 | |
| 32 | testhelper.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 33 | w.Header().Add("X-Subject-Token", ID) |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 34 | |
| 35 | w.WriteHeader(http.StatusCreated) |
| 36 | fmt.Fprintf(w, `{ "token": { "expires_at": "2013-02-02T18:30:59.000000Z" } }`) |
| 37 | }) |
| 38 | |
Ash Wilson | 001cfa5 | 2014-09-02 14:23:23 -0400 | [diff] [blame^] | 39 | provider := &gophercloud.ProviderClient{} |
| 40 | client := NewClient(provider, testhelper.Endpoint()+"v3/") |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 41 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 42 | token, err := client.GetToken(gophercloud.AuthOptions{UserID: "me", Password: "swordfish"}) |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 43 | 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 Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 54 | } |
| 55 | } |