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