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