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 | |
| 18 | provider := &gophercloud.ProviderClient{ |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 19 | IdentityEndpoint: testhelper.Endpoint() + "v3/", |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 20 | } |
| 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 Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 29 | func TestAuthentication(t *testing.T) { |
| 30 | testhelper.SetupHTTP() |
| 31 | defer testhelper.TeardownHTTP() |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 32 | const ID = "aaaa1111" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 33 | |
| 34 | 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] | 35 | w.Header().Add("X-Subject-Token", ID) |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 36 | |
| 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 Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 42 | IdentityEndpoint: testhelper.Endpoint() + "v3/", |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 43 | } |
| 44 | client := NewClient(provider) |
| 45 | |
Ash Wilson | 131b775 | 2014-08-29 12:53:55 -0400 | [diff] [blame] | 46 | 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 Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 58 | } |
| 59 | } |