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