blob: 27f3fdcad3b979c45cbca9ea915194ba9dda1eac [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsoncde68122014-08-28 16:15:43 -04002
3import (
4 "fmt"
5 "net/http"
6 "testing"
Ash Wilson46d913f2014-08-29 11:00:11 -04007 "time"
Ash Wilsoncde68122014-08-28 16:15:43 -04008
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -050010 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsoncde68122014-08-28 16:15:43 -040012)
13
Ash Wilson417d9222014-08-29 07:58:35 -040014// authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure.
jrperritt3d966162016-06-06 14:08:54 -050015func authTokenPost(t *testing.T, options tokens.AuthOptions, scope *tokens.Scope, requestJSON string) {
Ash Wilson0ab4d612014-08-29 11:10:13 -040016 testhelper.SetupHTTP()
17 defer testhelper.TeardownHTTP()
Ash Wilsoncde68122014-08-28 16:15:43 -040018
Ash Wilson6425a412014-08-29 12:30:35 -040019 client := gophercloud.ServiceClient{
jrperritt29ae6b32016-04-13 12:59:37 -050020 ProviderClient: &gophercloud.ProviderClient{},
21 Endpoint: testhelper.Endpoint(),
Ash Wilsoncde68122014-08-28 16:15:43 -040022 }
23
Ash Wilson0ab4d612014-08-29 11:10:13 -040024 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsoncde68122014-08-28 16:15:43 -040025 testhelper.TestMethod(t, r, "POST")
26 testhelper.TestHeader(t, r, "Content-Type", "application/json")
27 testhelper.TestHeader(t, r, "Accept", "application/json")
Ash Wilson417d9222014-08-29 07:58:35 -040028 testhelper.TestJSONRequest(t, r, requestJSON)
Ash Wilsoncde68122014-08-28 16:15:43 -040029
Ash Wilson4a52e2a2014-08-29 09:28:00 -040030 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -040031 fmt.Fprintf(w, `{
32 "token": {
33 "expires_at": "2014-10-02T13:45:00.000000Z"
34 }
35 }`)
Ash Wilsoncde68122014-08-28 16:15:43 -040036 })
37
jrperritt0bc55782016-07-27 13:50:14 -050038 if scope != nil {
39 options.Scope = *scope
40 }
41
42 _, err := tokens.Create(&client, &options).Extract()
Ash Wilsoncde68122014-08-28 16:15:43 -040043 if err != nil {
44 t.Errorf("Create returned an error: %v", err)
45 }
46}
Ash Wilson417d9222014-08-29 07:58:35 -040047
jrperritt3d966162016-06-06 14:08:54 -050048func authTokenPostErr(t *testing.T, options tokens.AuthOptions, scope *tokens.Scope, includeToken bool, expectedErr error) {
Ash Wilson0ab4d612014-08-29 11:10:13 -040049 testhelper.SetupHTTP()
50 defer testhelper.TeardownHTTP()
Ash Wilsona8855ff2014-08-29 08:26:29 -040051
Ash Wilson6425a412014-08-29 12:30:35 -040052 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -040053 ProviderClient: &gophercloud.ProviderClient{},
54 Endpoint: testhelper.Endpoint(),
Ash Wilsona8855ff2014-08-29 08:26:29 -040055 }
56 if includeToken {
Ash Wilsond7f73e92014-10-22 09:11:49 -040057 client.TokenID = "abcdef123456"
Ash Wilsona8855ff2014-08-29 08:26:29 -040058 }
59
jrperritt0bc55782016-07-27 13:50:14 -050060 if scope != nil {
61 options.Scope = *scope
62 }
63
64 _, err := tokens.Create(&client, &options).Extract()
Ash Wilsona8855ff2014-08-29 08:26:29 -040065 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 Wilson417d9222014-08-29 07:58:35 -040073func TestCreateUserIDAndPassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050074 authTokenPost(t, tokens.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040075 {
76 "auth": {
77 "identity": {
78 "methods": ["password"],
79 "password": {
80 "user": { "id": "me", "password": "squirrel!" }
81 }
82 }
83 }
84 }
85 `)
86}
87
88func TestCreateUsernameDomainIDPassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050089 authTokenPost(t, tokens.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040090 {
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 Wilsond8da9e42014-08-29 08:01:06 -0400108
109func TestCreateUsernameDomainNamePassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500110 authTokenPost(t, tokens.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, `
Ash Wilsond8da9e42014-08-29 08:01:06 -0400111 {
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 Wilson053fcb02014-08-29 08:04:35 -0400129
130func TestCreateTokenID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500131 authTokenPost(t, tokens.AuthOptions{TokenID: "12345abcdef"}, nil, `
Ash Wilson053fcb02014-08-29 08:04:35 -0400132 {
133 "auth": {
134 "identity": {
135 "methods": ["token"],
136 "token": {
137 "id": "12345abcdef"
138 }
139 }
140 }
141 }
142 `)
143}
Ash Wilson1fde6162014-08-29 08:13:06 -0400144
145func TestCreateProjectIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500146 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
147 scope := &tokens.Scope{ProjectID: "123456"}
jrperritt29ae6b32016-04-13 12:59:37 -0500148 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400149 {
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
170func TestCreateDomainIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500171 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
172 scope := &tokens.Scope{DomainID: "1000"}
jrperritt29ae6b32016-04-13 12:59:37 -0500173 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400174 {
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
195func TestCreateProjectNameAndDomainIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500196 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
197 scope := &tokens.Scope{ProjectName: "world-domination", DomainID: "1000"}
jrperritt29ae6b32016-04-13 12:59:37 -0500198 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400199 {
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
223func TestCreateProjectNameAndDomainNameScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500224 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
225 scope := &tokens.Scope{ProjectName: "world-domination", DomainName: "evil-plans"}
jrperritt29ae6b32016-04-13 12:59:37 -0500226 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400227 {
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 Wilsona8855ff2014-08-29 08:26:29 -0400250
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400251func TestCreateExtractsTokenFromResponse(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400252 testhelper.SetupHTTP()
253 defer testhelper.TeardownHTTP()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400254
Ash Wilson6425a412014-08-29 12:30:35 -0400255 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400256 ProviderClient: &gophercloud.ProviderClient{},
257 Endpoint: testhelper.Endpoint(),
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400258 }
259
Ash Wilson0ab4d612014-08-29 11:10:13 -0400260 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400261 w.Header().Add("X-Subject-Token", "aaa111")
262
263 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -0400264 fmt.Fprintf(w, `{
265 "token": {
266 "expires_at": "2014-10-02T13:45:00.000000Z"
267 }
268 }`)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400269 })
270
jrperritt3d966162016-06-06 14:08:54 -0500271 options := tokens.AuthOptions{UserID: "me", Password: "shhh"}
jrperritt0bc55782016-07-27 13:50:14 -0500272 token, err := tokens.Create(&client, &options).Extract()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400273 if err != nil {
Ash Wilson63b2a292014-10-02 09:29:06 -0400274 t.Fatalf("Create returned an error: %v", err)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400275 }
276
Ash Wilson3f59ade2014-10-02 09:22:23 -0400277 if token.ID != "aaa111" {
278 t.Errorf("Expected token to be aaa111, but was %s", token.ID)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400279 }
280}
281
Ash Wilsona8855ff2014-08-29 08:26:29 -0400282func TestCreateFailureEmptyAuth(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500283 authTokenPostErr(t, tokens.AuthOptions{}, nil, false, gophercloud.ErrMissingPassword{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400284}
285
Ash Wilsona8855ff2014-08-29 08:26:29 -0400286func TestCreateFailureTokenIDUsername(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500287 authTokenPostErr(t, tokens.AuthOptions{Username: "something", TokenID: "12345"}, nil, true, gophercloud.ErrUsernameWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400288}
289
290func TestCreateFailureTokenIDUserID(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500291 authTokenPostErr(t, tokens.AuthOptions{UserID: "something", TokenID: "12345"}, nil, true, gophercloud.ErrUserIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400292}
293
294func TestCreateFailureTokenIDDomainID(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500295 authTokenPostErr(t, tokens.AuthOptions{DomainID: "something", TokenID: "12345"}, nil, true, gophercloud.ErrDomainIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400296}
297
298func TestCreateFailureTokenIDDomainName(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500299 authTokenPostErr(t, tokens.AuthOptions{DomainName: "something", TokenID: "12345"}, nil, true, gophercloud.ErrDomainNameWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400300}
Ash Wilsonaed3db42014-08-29 08:59:56 -0400301
302func TestCreateFailureMissingUser(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500303 options := tokens.AuthOptions{Password: "supersecure"}
jrperritt0bc55782016-07-27 13:50:14 -0500304 authTokenPostErr(t, options, nil, false, gophercloud.ErrUsernameOrUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400305}
306
307func TestCreateFailureBothUser(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500308 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500309 Password: "supersecure",
310 Username: "oops",
311 UserID: "redundancy",
312 }
jrperritt0bc55782016-07-27 13:50:14 -0500313 authTokenPostErr(t, options, nil, false, gophercloud.ErrUsernameOrUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400314}
315
316func TestCreateFailureMissingDomain(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500317 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500318 Password: "supersecure",
319 Username: "notuniqueenough",
320 }
jrperritt0bc55782016-07-27 13:50:14 -0500321 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400322}
323
324func TestCreateFailureBothDomain(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500325 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500326 Password: "supersecure",
327 Username: "someone",
328 DomainID: "hurf",
329 DomainName: "durf",
330 }
jrperritt0bc55782016-07-27 13:50:14 -0500331 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400332}
333
334func TestCreateFailureUserIDDomainID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500335 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500336 UserID: "100",
337 Password: "stuff",
338 DomainID: "oops",
339 }
jrperritt0bc55782016-07-27 13:50:14 -0500340 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainIDWithUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400341}
342
343func TestCreateFailureUserIDDomainName(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500344 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500345 UserID: "100",
346 Password: "sssh",
347 DomainName: "oops",
348 }
jrperritt0bc55782016-07-27 13:50:14 -0500349 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainNameWithUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400350}
351
352func TestCreateFailureScopeProjectNameAlone(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500353 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
354 scope := &tokens.Scope{ProjectName: "notenough"}
jrperritt0bc55782016-07-27 13:50:14 -0500355 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400356}
357
358func TestCreateFailureScopeProjectNameAndID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500359 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
360 scope := &tokens.Scope{ProjectName: "whoops", ProjectID: "toomuch", DomainID: "1234"}
jrperritt0bc55782016-07-27 13:50:14 -0500361 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeProjectIDOrProjectName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400362}
363
364func TestCreateFailureScopeProjectIDAndDomainID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500365 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
366 scope := &tokens.Scope{ProjectID: "toomuch", DomainID: "notneeded"}
jrperritt0bc55782016-07-27 13:50:14 -0500367 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeProjectIDAlone{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400368}
369
370func TestCreateFailureScopeProjectIDAndDomainNAme(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500371 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
372 scope := &tokens.Scope{ProjectID: "toomuch", DomainName: "notneeded"}
jrperritt0bc55782016-07-27 13:50:14 -0500373 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeProjectIDAlone{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400374}
375
376func TestCreateFailureScopeDomainIDAndDomainName(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500377 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
378 scope := &tokens.Scope{DomainID: "toomuch", DomainName: "notneeded"}
jrperritt0bc55782016-07-27 13:50:14 -0500379 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400380}
381
382func TestCreateFailureScopeDomainNameAlone(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500383 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
384 scope := &tokens.Scope{DomainName: "notenough"}
jrperritt0bc55782016-07-27 13:50:14 -0500385 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400386}
387
jrperritt0bc55782016-07-27 13:50:14 -0500388/*
Ash Wilsonaed3db42014-08-29 08:59:56 -0400389func TestCreateFailureEmptyScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500390 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
391 scope := &tokens.Scope{}
jrperritt0bc55782016-07-27 13:50:14 -0500392 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeEmpty{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400393}
jrperritt0bc55782016-07-27 13:50:14 -0500394*/
Ash Wilson46d913f2014-08-29 11:00:11 -0400395
Ash Wilson5266e492014-09-09 15:44:30 -0400396func TestGetRequest(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400397 testhelper.SetupHTTP()
398 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400399
Ash Wilson6425a412014-08-29 12:30:35 -0400400 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400401 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400402 TokenID: "12345abcdef",
403 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400404 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400405 }
406
Ash Wilson0ab4d612014-08-29 11:10:13 -0400407 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400408 testhelper.TestMethod(t, r, "GET")
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500409 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400410 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
jrperritt3d966162016-06-06 14:08:54 -0500420 token, err := tokens.Get(&client, "abcdef12345").Extract()
Ash Wilson46d913f2014-08-29 11:00:11 -0400421 if err != nil {
422 t.Errorf("Info returned an error: %v", err)
423 }
424
Ash Wilson46d913f2014-08-29 11:00:11 -0400425 expected, _ := time.Parse(time.UnixDate, "Fri Aug 29 13:10:01 UTC 2014")
jrperrittc8834c12016-08-03 16:06:16 -0500426 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 Wilson46d913f2014-08-29 11:00:11 -0400428 }
429}
430
Ash Wilson6425a412014-08-29 12:30:35 -0400431func prepareAuthTokenHandler(t *testing.T, expectedMethod string, status int) gophercloud.ServiceClient {
432 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400433 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400434 TokenID: "12345abcdef",
435 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400436 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400437 }
438
Ash Wilson0ab4d612014-08-29 11:10:13 -0400439 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400440 testhelper.TestMethod(t, r, expectedMethod)
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500441 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400442 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
452func TestValidateRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400453 testhelper.SetupHTTP()
454 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400455 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNoContent)
456
jrperritt3d966162016-06-06 14:08:54 -0500457 ok, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400458 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
467func TestValidateRequestFailure(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400468 testhelper.SetupHTTP()
469 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400470 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNotFound)
471
jrperritt3d966162016-06-06 14:08:54 -0500472 ok, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400473 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
482func TestValidateRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400483 testhelper.SetupHTTP()
484 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400485 client := prepareAuthTokenHandler(t, "HEAD", http.StatusUnauthorized)
486
jrperritt3d966162016-06-06 14:08:54 -0500487 _, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400488 if err == nil {
489 t.Errorf("Missing expected error from Validate")
490 }
491}
492
493func TestRevokeRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400494 testhelper.SetupHTTP()
495 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400496 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNoContent)
497
jrperritt3d966162016-06-06 14:08:54 -0500498 res := tokens.Revoke(&client, "abcdef12345")
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100499 testhelper.AssertNoErr(t, res.Err)
Ash Wilson46d913f2014-08-29 11:00:11 -0400500}
501
502func TestRevokeRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400503 testhelper.SetupHTTP()
504 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400505 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNotFound)
506
jrperritt3d966162016-06-06 14:08:54 -0500507 res := tokens.Revoke(&client, "abcdef12345")
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100508 if res.Err == nil {
Ash Wilson46d913f2014-08-29 11:00:11 -0400509 t.Errorf("Missing expected error from Revoke")
510 }
511}
Eugene Yakubovich8e3f2502016-10-06 07:15:46 -0700512
513func 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}