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