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