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