blob: ddffeb48a5c514e520c900341efc374e4a2066d8 [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
jrperritt98d01622017-01-12 14:24:42 -060042 expected := &tokens.Token{
43 ExpiresAt: time.Date(2014, 10, 2, 13, 45, 0, 0, time.UTC),
Ash Wilsoncde68122014-08-28 16:15:43 -040044 }
jrperritt98d01622017-01-12 14:24:42 -060045 actual, err := tokens.Create(&client, &options).Extract()
46 testhelper.AssertNoErr(t, err)
47 testhelper.CheckDeepEquals(t, expected, actual)
Ash Wilsoncde68122014-08-28 16:15:43 -040048}
Ash Wilson417d9222014-08-29 07:58:35 -040049
jrperritt3d966162016-06-06 14:08:54 -050050func authTokenPostErr(t *testing.T, options tokens.AuthOptions, scope *tokens.Scope, includeToken bool, expectedErr error) {
Ash Wilson0ab4d612014-08-29 11:10:13 -040051 testhelper.SetupHTTP()
52 defer testhelper.TeardownHTTP()
Ash Wilsona8855ff2014-08-29 08:26:29 -040053
Ash Wilson6425a412014-08-29 12:30:35 -040054 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -040055 ProviderClient: &gophercloud.ProviderClient{},
56 Endpoint: testhelper.Endpoint(),
Ash Wilsona8855ff2014-08-29 08:26:29 -040057 }
58 if includeToken {
Ash Wilsond7f73e92014-10-22 09:11:49 -040059 client.TokenID = "abcdef123456"
Ash Wilsona8855ff2014-08-29 08:26:29 -040060 }
61
jrperritt0bc55782016-07-27 13:50:14 -050062 if scope != nil {
63 options.Scope = *scope
64 }
65
66 _, err := tokens.Create(&client, &options).Extract()
Ash Wilsona8855ff2014-08-29 08:26:29 -040067 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 Wilson417d9222014-08-29 07:58:35 -040075func TestCreateUserIDAndPassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050076 authTokenPost(t, tokens.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040077 {
78 "auth": {
79 "identity": {
80 "methods": ["password"],
81 "password": {
82 "user": { "id": "me", "password": "squirrel!" }
83 }
84 }
85 }
86 }
87 `)
88}
89
90func TestCreateUsernameDomainIDPassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050091 authTokenPost(t, tokens.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040092 {
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 Wilsond8da9e42014-08-29 08:01:06 -0400110
111func TestCreateUsernameDomainNamePassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500112 authTokenPost(t, tokens.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, `
Ash Wilsond8da9e42014-08-29 08:01:06 -0400113 {
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 Wilson053fcb02014-08-29 08:04:35 -0400131
132func TestCreateTokenID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500133 authTokenPost(t, tokens.AuthOptions{TokenID: "12345abcdef"}, nil, `
Ash Wilson053fcb02014-08-29 08:04:35 -0400134 {
135 "auth": {
136 "identity": {
137 "methods": ["token"],
138 "token": {
139 "id": "12345abcdef"
140 }
141 }
142 }
143 }
144 `)
145}
Ash Wilson1fde6162014-08-29 08:13:06 -0400146
147func TestCreateProjectIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500148 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
149 scope := &tokens.Scope{ProjectID: "123456"}
jrperritt29ae6b32016-04-13 12:59:37 -0500150 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400151 {
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
172func TestCreateDomainIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500173 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
174 scope := &tokens.Scope{DomainID: "1000"}
jrperritt29ae6b32016-04-13 12:59:37 -0500175 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400176 {
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
197func TestCreateProjectNameAndDomainIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500198 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
199 scope := &tokens.Scope{ProjectName: "world-domination", DomainID: "1000"}
jrperritt29ae6b32016-04-13 12:59:37 -0500200 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400201 {
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
225func TestCreateProjectNameAndDomainNameScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500226 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
227 scope := &tokens.Scope{ProjectName: "world-domination", DomainName: "evil-plans"}
jrperritt29ae6b32016-04-13 12:59:37 -0500228 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400229 {
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 Wilsona8855ff2014-08-29 08:26:29 -0400252
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400253func TestCreateExtractsTokenFromResponse(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400254 testhelper.SetupHTTP()
255 defer testhelper.TeardownHTTP()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400256
Ash Wilson6425a412014-08-29 12:30:35 -0400257 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400258 ProviderClient: &gophercloud.ProviderClient{},
259 Endpoint: testhelper.Endpoint(),
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400260 }
261
Ash Wilson0ab4d612014-08-29 11:10:13 -0400262 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400263 w.Header().Add("X-Subject-Token", "aaa111")
264
265 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -0400266 fmt.Fprintf(w, `{
267 "token": {
268 "expires_at": "2014-10-02T13:45:00.000000Z"
269 }
270 }`)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400271 })
272
jrperritt3d966162016-06-06 14:08:54 -0500273 options := tokens.AuthOptions{UserID: "me", Password: "shhh"}
jrperritt0bc55782016-07-27 13:50:14 -0500274 token, err := tokens.Create(&client, &options).Extract()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400275 if err != nil {
Ash Wilson63b2a292014-10-02 09:29:06 -0400276 t.Fatalf("Create returned an error: %v", err)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400277 }
278
Ash Wilson3f59ade2014-10-02 09:22:23 -0400279 if token.ID != "aaa111" {
280 t.Errorf("Expected token to be aaa111, but was %s", token.ID)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400281 }
282}
283
Ash Wilsona8855ff2014-08-29 08:26:29 -0400284func TestCreateFailureEmptyAuth(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500285 authTokenPostErr(t, tokens.AuthOptions{}, nil, false, gophercloud.ErrMissingPassword{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400286}
287
Ash Wilsona8855ff2014-08-29 08:26:29 -0400288func TestCreateFailureTokenIDUsername(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500289 authTokenPostErr(t, tokens.AuthOptions{Username: "something", TokenID: "12345"}, nil, true, gophercloud.ErrUsernameWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400290}
291
292func TestCreateFailureTokenIDUserID(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500293 authTokenPostErr(t, tokens.AuthOptions{UserID: "something", TokenID: "12345"}, nil, true, gophercloud.ErrUserIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400294}
295
296func TestCreateFailureTokenIDDomainID(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500297 authTokenPostErr(t, tokens.AuthOptions{DomainID: "something", TokenID: "12345"}, nil, true, gophercloud.ErrDomainIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400298}
299
300func TestCreateFailureTokenIDDomainName(t *testing.T) {
jrperritt0bc55782016-07-27 13:50:14 -0500301 authTokenPostErr(t, tokens.AuthOptions{DomainName: "something", TokenID: "12345"}, nil, true, gophercloud.ErrDomainNameWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400302}
Ash Wilsonaed3db42014-08-29 08:59:56 -0400303
304func TestCreateFailureMissingUser(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500305 options := tokens.AuthOptions{Password: "supersecure"}
jrperritt0bc55782016-07-27 13:50:14 -0500306 authTokenPostErr(t, options, nil, false, gophercloud.ErrUsernameOrUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400307}
308
309func TestCreateFailureBothUser(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500310 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500311 Password: "supersecure",
312 Username: "oops",
313 UserID: "redundancy",
314 }
jrperritt0bc55782016-07-27 13:50:14 -0500315 authTokenPostErr(t, options, nil, false, gophercloud.ErrUsernameOrUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400316}
317
318func TestCreateFailureMissingDomain(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500319 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500320 Password: "supersecure",
321 Username: "notuniqueenough",
322 }
jrperritt0bc55782016-07-27 13:50:14 -0500323 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400324}
325
326func TestCreateFailureBothDomain(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500327 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500328 Password: "supersecure",
329 Username: "someone",
330 DomainID: "hurf",
331 DomainName: "durf",
332 }
jrperritt0bc55782016-07-27 13:50:14 -0500333 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400334}
335
336func TestCreateFailureUserIDDomainID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500337 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500338 UserID: "100",
339 Password: "stuff",
340 DomainID: "oops",
341 }
jrperritt0bc55782016-07-27 13:50:14 -0500342 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainIDWithUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400343}
344
345func TestCreateFailureUserIDDomainName(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500346 options := tokens.AuthOptions{
jrperritt29ae6b32016-04-13 12:59:37 -0500347 UserID: "100",
348 Password: "sssh",
349 DomainName: "oops",
350 }
jrperritt0bc55782016-07-27 13:50:14 -0500351 authTokenPostErr(t, options, nil, false, gophercloud.ErrDomainNameWithUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400352}
353
354func TestCreateFailureScopeProjectNameAlone(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500355 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
356 scope := &tokens.Scope{ProjectName: "notenough"}
jrperritt0bc55782016-07-27 13:50:14 -0500357 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400358}
359
360func TestCreateFailureScopeProjectNameAndID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500361 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
362 scope := &tokens.Scope{ProjectName: "whoops", ProjectID: "toomuch", DomainID: "1234"}
jrperritt0bc55782016-07-27 13:50:14 -0500363 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeProjectIDOrProjectName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400364}
365
366func TestCreateFailureScopeProjectIDAndDomainID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500367 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
368 scope := &tokens.Scope{ProjectID: "toomuch", DomainID: "notneeded"}
jrperritt0bc55782016-07-27 13:50:14 -0500369 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeProjectIDAlone{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400370}
371
372func TestCreateFailureScopeProjectIDAndDomainNAme(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500373 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
374 scope := &tokens.Scope{ProjectID: "toomuch", DomainName: "notneeded"}
jrperritt0bc55782016-07-27 13:50:14 -0500375 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeProjectIDAlone{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400376}
377
378func TestCreateFailureScopeDomainIDAndDomainName(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500379 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
380 scope := &tokens.Scope{DomainID: "toomuch", DomainName: "notneeded"}
jrperritt0bc55782016-07-27 13:50:14 -0500381 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400382}
383
384func TestCreateFailureScopeDomainNameAlone(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500385 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
386 scope := &tokens.Scope{DomainName: "notenough"}
jrperritt0bc55782016-07-27 13:50:14 -0500387 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400388}
389
jrperritt0bc55782016-07-27 13:50:14 -0500390/*
Ash Wilsonaed3db42014-08-29 08:59:56 -0400391func TestCreateFailureEmptyScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500392 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
393 scope := &tokens.Scope{}
jrperritt0bc55782016-07-27 13:50:14 -0500394 authTokenPostErr(t, options, scope, false, gophercloud.ErrScopeEmpty{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400395}
jrperritt0bc55782016-07-27 13:50:14 -0500396*/
Ash Wilson46d913f2014-08-29 11:00:11 -0400397
Ash Wilson5266e492014-09-09 15:44:30 -0400398func TestGetRequest(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400399 testhelper.SetupHTTP()
400 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400401
Ash Wilson6425a412014-08-29 12:30:35 -0400402 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400403 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400404 TokenID: "12345abcdef",
405 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400406 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400407 }
408
Ash Wilson0ab4d612014-08-29 11:10:13 -0400409 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400410 testhelper.TestMethod(t, r, "GET")
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500411 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400412 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
jrperritt3d966162016-06-06 14:08:54 -0500422 token, err := tokens.Get(&client, "abcdef12345").Extract()
Ash Wilson46d913f2014-08-29 11:00:11 -0400423 if err != nil {
424 t.Errorf("Info returned an error: %v", err)
425 }
426
Ash Wilson46d913f2014-08-29 11:00:11 -0400427 expected, _ := time.Parse(time.UnixDate, "Fri Aug 29 13:10:01 UTC 2014")
jrperritt98d01622017-01-12 14:24:42 -0600428 if token.ExpiresAt != expected {
jrperrittc8834c12016-08-03 16:06:16 -0500429 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 -0400430 }
431}
432
Ash Wilson6425a412014-08-29 12:30:35 -0400433func prepareAuthTokenHandler(t *testing.T, expectedMethod string, status int) gophercloud.ServiceClient {
434 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400435 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400436 TokenID: "12345abcdef",
437 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400438 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400439 }
440
Ash Wilson0ab4d612014-08-29 11:10:13 -0400441 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400442 testhelper.TestMethod(t, r, expectedMethod)
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500443 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400444 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
454func TestValidateRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400455 testhelper.SetupHTTP()
456 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400457 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNoContent)
458
jrperritt3d966162016-06-06 14:08:54 -0500459 ok, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400460 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
469func TestValidateRequestFailure(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400470 testhelper.SetupHTTP()
471 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400472 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNotFound)
473
jrperritt3d966162016-06-06 14:08:54 -0500474 ok, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400475 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
484func TestValidateRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400485 testhelper.SetupHTTP()
486 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400487 client := prepareAuthTokenHandler(t, "HEAD", http.StatusUnauthorized)
488
jrperritt3d966162016-06-06 14:08:54 -0500489 _, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400490 if err == nil {
491 t.Errorf("Missing expected error from Validate")
492 }
493}
494
495func TestRevokeRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400496 testhelper.SetupHTTP()
497 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400498 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNoContent)
499
jrperritt3d966162016-06-06 14:08:54 -0500500 res := tokens.Revoke(&client, "abcdef12345")
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100501 testhelper.AssertNoErr(t, res.Err)
Ash Wilson46d913f2014-08-29 11:00:11 -0400502}
503
504func TestRevokeRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400505 testhelper.SetupHTTP()
506 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400507 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNotFound)
508
jrperritt3d966162016-06-06 14:08:54 -0500509 res := tokens.Revoke(&client, "abcdef12345")
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100510 if res.Err == nil {
Ash Wilson46d913f2014-08-29 11:00:11 -0400511 t.Errorf("Missing expected error from Revoke")
512 }
513}
Eugene Yakubovich8e3f2502016-10-06 07:15:46 -0700514
515func 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()
jrperritt98d01622017-01-12 14:24:42 -0600531 testhelper.AssertNoErr(t, err)
Eugene Yakubovich8e3f2502016-10-06 07:15:46 -0700532}