Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 7 | "time" |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 13 | // authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure. |
| 14 | func authTokenPost(t *testing.T, options gophercloud.AuthOptions, scope *Scope, requestJSON string) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 15 | testhelper.SetupHTTP() |
| 16 | defer testhelper.TeardownHTTP() |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 17 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 18 | client := gophercloud.ServiceClient{ |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 19 | ProviderClient: &gophercloud.ProviderClient{ |
Ash Wilson | 49f0f56 | 2014-09-03 08:58:20 -0400 | [diff] [blame] | 20 | TokenID: "12345abcdef", |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 21 | }, |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 22 | Endpoint: testhelper.Endpoint(), |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 23 | } |
| 24 | |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 25 | testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 26 | testhelper.TestMethod(t, r, "POST") |
| 27 | testhelper.TestHeader(t, r, "Content-Type", "application/json") |
| 28 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 29 | testhelper.TestJSONRequest(t, r, requestJSON) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 30 | |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 31 | w.WriteHeader(http.StatusCreated) |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 32 | fmt.Fprintf(w, `{ |
| 33 | "token": { |
| 34 | "expires_at": "2014-10-02T13:45:00.000000Z" |
| 35 | } |
| 36 | }`) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 37 | }) |
| 38 | |
Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 39 | _, err := Create(&client, options, scope).Extract() |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 40 | if err != nil { |
| 41 | t.Errorf("Create returned an error: %v", err) |
| 42 | } |
| 43 | } |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 44 | |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 45 | func authTokenPostErr(t *testing.T, options gophercloud.AuthOptions, scope *Scope, includeToken bool, expectedErr error) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 46 | testhelper.SetupHTTP() |
| 47 | defer testhelper.TeardownHTTP() |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 48 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 49 | client := gophercloud.ServiceClient{ |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 50 | ProviderClient: &gophercloud.ProviderClient{}, |
| 51 | Endpoint: testhelper.Endpoint(), |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 52 | } |
| 53 | if includeToken { |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 54 | client.TokenID = "abcdef123456" |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 57 | _, err := Create(&client, options, scope).Extract() |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 58 | if err == nil { |
| 59 | t.Errorf("Create did NOT return an error") |
| 60 | } |
| 61 | if err != expectedErr { |
| 62 | t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err) |
| 63 | } |
| 64 | } |
| 65 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 66 | func TestCreateUserIDAndPassword(t *testing.T) { |
| 67 | authTokenPost(t, gophercloud.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, ` |
| 68 | { |
| 69 | "auth": { |
| 70 | "identity": { |
| 71 | "methods": ["password"], |
| 72 | "password": { |
| 73 | "user": { "id": "me", "password": "squirrel!" } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | `) |
| 79 | } |
| 80 | |
| 81 | func TestCreateUsernameDomainIDPassword(t *testing.T) { |
| 82 | authTokenPost(t, gophercloud.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, ` |
| 83 | { |
| 84 | "auth": { |
| 85 | "identity": { |
| 86 | "methods": ["password"], |
| 87 | "password": { |
| 88 | "user": { |
| 89 | "domain": { |
| 90 | "id": "abc123" |
| 91 | }, |
| 92 | "name": "fakey", |
| 93 | "password": "notpassword" |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | `) |
| 100 | } |
Ash Wilson | d8da9e4 | 2014-08-29 08:01:06 -0400 | [diff] [blame] | 101 | |
| 102 | func TestCreateUsernameDomainNamePassword(t *testing.T) { |
| 103 | authTokenPost(t, gophercloud.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, ` |
| 104 | { |
| 105 | "auth": { |
| 106 | "identity": { |
| 107 | "methods": ["password"], |
| 108 | "password": { |
| 109 | "user": { |
| 110 | "domain": { |
| 111 | "name": "spork.net" |
| 112 | }, |
| 113 | "name": "frank", |
| 114 | "password": "swordfish" |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | `) |
| 121 | } |
Ash Wilson | 053fcb0 | 2014-08-29 08:04:35 -0400 | [diff] [blame] | 122 | |
| 123 | func TestCreateTokenID(t *testing.T) { |
| 124 | authTokenPost(t, gophercloud.AuthOptions{}, nil, ` |
| 125 | { |
| 126 | "auth": { |
| 127 | "identity": { |
| 128 | "methods": ["token"], |
| 129 | "token": { |
| 130 | "id": "12345abcdef" |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | `) |
| 136 | } |
Ash Wilson | 1fde616 | 2014-08-29 08:13:06 -0400 | [diff] [blame] | 137 | |
| 138 | func TestCreateProjectIDScope(t *testing.T) { |
| 139 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 140 | scope := &Scope{ProjectID: "123456"} |
| 141 | authTokenPost(t, options, scope, ` |
| 142 | { |
| 143 | "auth": { |
| 144 | "identity": { |
| 145 | "methods": ["password"], |
| 146 | "password": { |
| 147 | "user": { |
| 148 | "id": "fenris", |
| 149 | "password": "g0t0h311" |
| 150 | } |
| 151 | } |
| 152 | }, |
| 153 | "scope": { |
| 154 | "project": { |
| 155 | "id": "123456" |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | `) |
| 161 | } |
| 162 | |
| 163 | func TestCreateDomainIDScope(t *testing.T) { |
| 164 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 165 | scope := &Scope{DomainID: "1000"} |
| 166 | authTokenPost(t, options, scope, ` |
| 167 | { |
| 168 | "auth": { |
| 169 | "identity": { |
| 170 | "methods": ["password"], |
| 171 | "password": { |
| 172 | "user": { |
| 173 | "id": "fenris", |
| 174 | "password": "g0t0h311" |
| 175 | } |
| 176 | } |
| 177 | }, |
| 178 | "scope": { |
| 179 | "domain": { |
| 180 | "id": "1000" |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | `) |
| 186 | } |
| 187 | |
| 188 | func TestCreateProjectNameAndDomainIDScope(t *testing.T) { |
| 189 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 190 | scope := &Scope{ProjectName: "world-domination", DomainID: "1000"} |
| 191 | authTokenPost(t, options, scope, ` |
| 192 | { |
| 193 | "auth": { |
| 194 | "identity": { |
| 195 | "methods": ["password"], |
| 196 | "password": { |
| 197 | "user": { |
| 198 | "id": "fenris", |
| 199 | "password": "g0t0h311" |
| 200 | } |
| 201 | } |
| 202 | }, |
| 203 | "scope": { |
| 204 | "project": { |
| 205 | "domain": { |
| 206 | "id": "1000" |
| 207 | }, |
| 208 | "name": "world-domination" |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | `) |
| 214 | } |
| 215 | |
| 216 | func TestCreateProjectNameAndDomainNameScope(t *testing.T) { |
| 217 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 218 | scope := &Scope{ProjectName: "world-domination", DomainName: "evil-plans"} |
| 219 | authTokenPost(t, options, scope, ` |
| 220 | { |
| 221 | "auth": { |
| 222 | "identity": { |
| 223 | "methods": ["password"], |
| 224 | "password": { |
| 225 | "user": { |
| 226 | "id": "fenris", |
| 227 | "password": "g0t0h311" |
| 228 | } |
| 229 | } |
| 230 | }, |
| 231 | "scope": { |
| 232 | "project": { |
| 233 | "domain": { |
| 234 | "name": "evil-plans" |
| 235 | }, |
| 236 | "name": "world-domination" |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | `) |
| 242 | } |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 243 | |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 244 | func TestCreateExtractsTokenFromResponse(t *testing.T) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 245 | testhelper.SetupHTTP() |
| 246 | defer testhelper.TeardownHTTP() |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 247 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 248 | client := gophercloud.ServiceClient{ |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 249 | ProviderClient: &gophercloud.ProviderClient{}, |
| 250 | Endpoint: testhelper.Endpoint(), |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 253 | testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 254 | w.Header().Add("X-Subject-Token", "aaa111") |
| 255 | |
| 256 | w.WriteHeader(http.StatusCreated) |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 257 | fmt.Fprintf(w, `{ |
| 258 | "token": { |
| 259 | "expires_at": "2014-10-02T13:45:00.000000Z" |
| 260 | } |
| 261 | }`) |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 262 | }) |
| 263 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 264 | options := gophercloud.AuthOptions{UserID: "me", Password: "shhh"} |
Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 265 | token, err := Create(&client, options, nil).Extract() |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 266 | if err != nil { |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 267 | t.Fatalf("Create returned an error: %v", err) |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 270 | if token.ID != "aaa111" { |
| 271 | t.Errorf("Expected token to be aaa111, but was %s", token.ID) |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 275 | func TestCreateFailureEmptyAuth(t *testing.T) { |
| 276 | authTokenPostErr(t, gophercloud.AuthOptions{}, nil, false, ErrMissingPassword) |
| 277 | } |
| 278 | |
| 279 | func TestCreateFailureAPIKey(t *testing.T) { |
| 280 | authTokenPostErr(t, gophercloud.AuthOptions{APIKey: "something"}, nil, false, ErrAPIKeyProvided) |
| 281 | } |
| 282 | |
| 283 | func TestCreateFailureTenantID(t *testing.T) { |
| 284 | authTokenPostErr(t, gophercloud.AuthOptions{TenantID: "something"}, nil, false, ErrTenantIDProvided) |
| 285 | } |
| 286 | |
| 287 | func TestCreateFailureTenantName(t *testing.T) { |
| 288 | authTokenPostErr(t, gophercloud.AuthOptions{TenantName: "something"}, nil, false, ErrTenantNameProvided) |
| 289 | } |
| 290 | |
| 291 | func TestCreateFailureTokenIDUsername(t *testing.T) { |
| 292 | authTokenPostErr(t, gophercloud.AuthOptions{Username: "something"}, nil, true, ErrUsernameWithToken) |
| 293 | } |
| 294 | |
| 295 | func TestCreateFailureTokenIDUserID(t *testing.T) { |
| 296 | authTokenPostErr(t, gophercloud.AuthOptions{UserID: "something"}, nil, true, ErrUserIDWithToken) |
| 297 | } |
| 298 | |
| 299 | func TestCreateFailureTokenIDDomainID(t *testing.T) { |
| 300 | authTokenPostErr(t, gophercloud.AuthOptions{DomainID: "something"}, nil, true, ErrDomainIDWithToken) |
| 301 | } |
| 302 | |
| 303 | func TestCreateFailureTokenIDDomainName(t *testing.T) { |
| 304 | authTokenPostErr(t, gophercloud.AuthOptions{DomainName: "something"}, nil, true, ErrDomainNameWithToken) |
| 305 | } |
Ash Wilson | aed3db4 | 2014-08-29 08:59:56 -0400 | [diff] [blame] | 306 | |
| 307 | func TestCreateFailureMissingUser(t *testing.T) { |
| 308 | options := gophercloud.AuthOptions{Password: "supersecure"} |
| 309 | authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID) |
| 310 | } |
| 311 | |
| 312 | func TestCreateFailureBothUser(t *testing.T) { |
| 313 | options := gophercloud.AuthOptions{ |
| 314 | Password: "supersecure", |
| 315 | Username: "oops", |
| 316 | UserID: "redundancy", |
| 317 | } |
| 318 | authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID) |
| 319 | } |
| 320 | |
| 321 | func TestCreateFailureMissingDomain(t *testing.T) { |
| 322 | options := gophercloud.AuthOptions{ |
| 323 | Password: "supersecure", |
| 324 | Username: "notuniqueenough", |
| 325 | } |
| 326 | authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName) |
| 327 | } |
| 328 | |
| 329 | func TestCreateFailureBothDomain(t *testing.T) { |
| 330 | options := gophercloud.AuthOptions{ |
| 331 | Password: "supersecure", |
| 332 | Username: "someone", |
| 333 | DomainID: "hurf", |
| 334 | DomainName: "durf", |
| 335 | } |
| 336 | authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName) |
| 337 | } |
| 338 | |
| 339 | func TestCreateFailureUserIDDomainID(t *testing.T) { |
| 340 | options := gophercloud.AuthOptions{ |
| 341 | UserID: "100", |
| 342 | Password: "stuff", |
| 343 | DomainID: "oops", |
| 344 | } |
| 345 | authTokenPostErr(t, options, nil, false, ErrDomainIDWithUserID) |
| 346 | } |
| 347 | |
| 348 | func TestCreateFailureUserIDDomainName(t *testing.T) { |
| 349 | options := gophercloud.AuthOptions{ |
| 350 | UserID: "100", |
| 351 | Password: "sssh", |
| 352 | DomainName: "oops", |
| 353 | } |
| 354 | authTokenPostErr(t, options, nil, false, ErrDomainNameWithUserID) |
| 355 | } |
| 356 | |
| 357 | func TestCreateFailureScopeProjectNameAlone(t *testing.T) { |
| 358 | options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} |
| 359 | scope := &Scope{ProjectName: "notenough"} |
| 360 | authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName) |
| 361 | } |
| 362 | |
| 363 | func TestCreateFailureScopeProjectNameAndID(t *testing.T) { |
| 364 | options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} |
| 365 | scope := &Scope{ProjectName: "whoops", ProjectID: "toomuch", DomainID: "1234"} |
| 366 | authTokenPostErr(t, options, scope, false, ErrScopeProjectIDOrProjectName) |
| 367 | } |
| 368 | |
| 369 | func TestCreateFailureScopeProjectIDAndDomainID(t *testing.T) { |
| 370 | options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} |
| 371 | scope := &Scope{ProjectID: "toomuch", DomainID: "notneeded"} |
| 372 | authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone) |
| 373 | } |
| 374 | |
| 375 | func TestCreateFailureScopeProjectIDAndDomainNAme(t *testing.T) { |
| 376 | options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} |
| 377 | scope := &Scope{ProjectID: "toomuch", DomainName: "notneeded"} |
| 378 | authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone) |
| 379 | } |
| 380 | |
| 381 | func TestCreateFailureScopeDomainIDAndDomainName(t *testing.T) { |
| 382 | options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} |
| 383 | scope := &Scope{DomainID: "toomuch", DomainName: "notneeded"} |
| 384 | authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName) |
| 385 | } |
| 386 | |
| 387 | func TestCreateFailureScopeDomainNameAlone(t *testing.T) { |
| 388 | options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} |
| 389 | scope := &Scope{DomainName: "notenough"} |
| 390 | authTokenPostErr(t, options, scope, false, ErrScopeDomainName) |
| 391 | } |
| 392 | |
| 393 | func TestCreateFailureEmptyScope(t *testing.T) { |
| 394 | options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} |
| 395 | scope := &Scope{} |
| 396 | authTokenPostErr(t, options, scope, false, ErrScopeEmpty) |
| 397 | } |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 398 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 399 | func TestGetRequest(t *testing.T) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 400 | testhelper.SetupHTTP() |
| 401 | defer testhelper.TeardownHTTP() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 402 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 403 | client := gophercloud.ServiceClient{ |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 404 | ProviderClient: &gophercloud.ProviderClient{ |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 405 | TokenID: "12345abcdef", |
| 406 | }, |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 407 | Endpoint: testhelper.Endpoint(), |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 410 | testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 411 | testhelper.TestMethod(t, r, "GET") |
Jon Perritt | d8aef1b | 2014-09-11 17:50:04 -0500 | [diff] [blame] | 412 | testhelper.TestHeader(t, r, "Content-Type", "") |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 413 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 414 | testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef") |
| 415 | testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345") |
| 416 | |
| 417 | w.WriteHeader(http.StatusOK) |
| 418 | fmt.Fprintf(w, ` |
| 419 | { "token": { "expires_at": "2014-08-29T13:10:01.000000Z" } } |
| 420 | `) |
| 421 | }) |
| 422 | |
Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 423 | token, err := Get(&client, "abcdef12345").Extract() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 424 | if err != nil { |
| 425 | t.Errorf("Info returned an error: %v", err) |
| 426 | } |
| 427 | |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 428 | expected, _ := time.Parse(time.UnixDate, "Fri Aug 29 13:10:01 UTC 2014") |
Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 429 | if token.ExpiresAt != expected { |
| 430 | t.Errorf("Expected expiration time %s, but was %s", expected.Format(time.UnixDate), token.ExpiresAt.Format(time.UnixDate)) |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 434 | func prepareAuthTokenHandler(t *testing.T, expectedMethod string, status int) gophercloud.ServiceClient { |
| 435 | client := gophercloud.ServiceClient{ |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 436 | ProviderClient: &gophercloud.ProviderClient{ |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 437 | TokenID: "12345abcdef", |
| 438 | }, |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 439 | Endpoint: testhelper.Endpoint(), |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 442 | testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 443 | testhelper.TestMethod(t, r, expectedMethod) |
Jon Perritt | d8aef1b | 2014-09-11 17:50:04 -0500 | [diff] [blame] | 444 | testhelper.TestHeader(t, r, "Content-Type", "") |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 445 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 446 | testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef") |
| 447 | testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345") |
| 448 | |
| 449 | w.WriteHeader(status) |
| 450 | }) |
| 451 | |
| 452 | return client |
| 453 | } |
| 454 | |
| 455 | func TestValidateRequestSuccessful(t *testing.T) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 456 | testhelper.SetupHTTP() |
| 457 | defer testhelper.TeardownHTTP() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 458 | client := prepareAuthTokenHandler(t, "HEAD", http.StatusNoContent) |
| 459 | |
| 460 | ok, err := Validate(&client, "abcdef12345") |
| 461 | if err != nil { |
| 462 | t.Errorf("Unexpected error from Validate: %v", err) |
| 463 | } |
| 464 | |
| 465 | if !ok { |
| 466 | t.Errorf("Validate returned false for a valid token") |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | func TestValidateRequestFailure(t *testing.T) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 471 | testhelper.SetupHTTP() |
| 472 | defer testhelper.TeardownHTTP() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 473 | client := prepareAuthTokenHandler(t, "HEAD", http.StatusNotFound) |
| 474 | |
| 475 | ok, err := Validate(&client, "abcdef12345") |
| 476 | if err != nil { |
| 477 | t.Errorf("Unexpected error from Validate: %v", err) |
| 478 | } |
| 479 | |
| 480 | if ok { |
| 481 | t.Errorf("Validate returned true for an invalid token") |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | func TestValidateRequestError(t *testing.T) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 486 | testhelper.SetupHTTP() |
| 487 | defer testhelper.TeardownHTTP() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 488 | client := prepareAuthTokenHandler(t, "HEAD", http.StatusUnauthorized) |
| 489 | |
| 490 | _, err := Validate(&client, "abcdef12345") |
| 491 | if err == nil { |
| 492 | t.Errorf("Missing expected error from Validate") |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | func TestRevokeRequestSuccessful(t *testing.T) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 497 | testhelper.SetupHTTP() |
| 498 | defer testhelper.TeardownHTTP() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 499 | client := prepareAuthTokenHandler(t, "DELETE", http.StatusNoContent) |
| 500 | |
| 501 | err := Revoke(&client, "abcdef12345") |
| 502 | if err != nil { |
| 503 | t.Errorf("Unexpected error from Revoke: %v", err) |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | func TestRevokeRequestError(t *testing.T) { |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 508 | testhelper.SetupHTTP() |
| 509 | defer testhelper.TeardownHTTP() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 510 | client := prepareAuthTokenHandler(t, "DELETE", http.StatusNotFound) |
| 511 | |
| 512 | err := Revoke(&client, "abcdef12345") |
| 513 | if err == nil { |
| 514 | t.Errorf("Missing expected error from Revoke") |
| 515 | } |
| 516 | } |