blob: a39a6f4be1ed8f62840b931a40d77f7732a987fe [file] [log] [blame]
Ash Wilsoncde68122014-08-28 16:15:43 -04001package tokens
2
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"
10 "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsoncde68122014-08-28 16:15:43 -040011)
12
Ash Wilson417d9222014-08-29 07:58:35 -040013// authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure.
Jon Perrittdb0ae142016-03-13 00:33:41 -060014func authTokenPost(t *testing.T, options AuthOptionsBuilder, scope *gophercloud.ScopeOptsV3, requestJSON string) {
Ash Wilson0ab4d612014-08-29 11:10:13 -040015 testhelper.SetupHTTP()
16 defer testhelper.TeardownHTTP()
Ash Wilsoncde68122014-08-28 16:15:43 -040017
Ash Wilson6425a412014-08-29 12:30:35 -040018 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -040019 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson49f0f562014-09-03 08:58:20 -040020 TokenID: "12345abcdef",
Ash Wilson6425a412014-08-29 12:30:35 -040021 },
Ash Wilson0ab4d612014-08-29 11:10:13 -040022 Endpoint: testhelper.Endpoint(),
Ash Wilsoncde68122014-08-28 16:15:43 -040023 }
24
Ash Wilson0ab4d612014-08-29 11:10:13 -040025 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsoncde68122014-08-28 16:15:43 -040026 testhelper.TestMethod(t, r, "POST")
27 testhelper.TestHeader(t, r, "Content-Type", "application/json")
28 testhelper.TestHeader(t, r, "Accept", "application/json")
Ash Wilson417d9222014-08-29 07:58:35 -040029 testhelper.TestJSONRequest(t, r, requestJSON)
Ash Wilsoncde68122014-08-28 16:15:43 -040030
Ash Wilson4a52e2a2014-08-29 09:28:00 -040031 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -040032 fmt.Fprintf(w, `{
33 "token": {
34 "expires_at": "2014-10-02T13:45:00.000000Z"
35 }
36 }`)
Ash Wilsoncde68122014-08-28 16:15:43 -040037 })
38
Ash Wilson3f59ade2014-10-02 09:22:23 -040039 _, err := Create(&client, options, scope).Extract()
Ash Wilsoncde68122014-08-28 16:15:43 -040040 if err != nil {
41 t.Errorf("Create returned an error: %v", err)
42 }
43}
Ash Wilson417d9222014-08-29 07:58:35 -040044
Jon Perrittdb0ae142016-03-13 00:33:41 -060045func authTokenPostErr(t *testing.T, options AuthOptionsBuilder, scope *gophercloud.ScopeOptsV3, includeToken bool, expectedErr error) {
Ash Wilson0ab4d612014-08-29 11:10:13 -040046 testhelper.SetupHTTP()
47 defer testhelper.TeardownHTTP()
Ash Wilsona8855ff2014-08-29 08:26:29 -040048
Ash Wilson6425a412014-08-29 12:30:35 -040049 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -040050 ProviderClient: &gophercloud.ProviderClient{},
51 Endpoint: testhelper.Endpoint(),
Ash Wilsona8855ff2014-08-29 08:26:29 -040052 }
53 if includeToken {
Ash Wilsond7f73e92014-10-22 09:11:49 -040054 client.TokenID = "abcdef123456"
Ash Wilsona8855ff2014-08-29 08:26:29 -040055 }
56
Ash Wilson3f59ade2014-10-02 09:22:23 -040057 _, err := Create(&client, options, scope).Extract()
Ash Wilsona8855ff2014-08-29 08:26:29 -040058 if err == nil {
59 t.Errorf("Create did NOT return an error")
60 }
61 if err != expectedErr {
62 t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err)
63 }
64}
65
Ash Wilson417d9222014-08-29 07:58:35 -040066func TestCreateUserIDAndPassword(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060067 ao := gophercloud.AuthOptions{}
68 ao.UserID = "me"
69 ao.Password = "squirrel!"
70 authTokenPost(t, ao, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040071 {
72 "auth": {
73 "identity": {
74 "methods": ["password"],
75 "password": {
76 "user": { "id": "me", "password": "squirrel!" }
77 }
78 }
79 }
80 }
81 `)
82}
83
84func TestCreateUsernameDomainIDPassword(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060085 ao := gophercloud.AuthOptions{DomainID: "abc123"}
86 ao.Username = "fakey"
87 ao.Password = "notpassword"
88 authTokenPost(t, ao, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040089 {
90 "auth": {
91 "identity": {
92 "methods": ["password"],
93 "password": {
94 "user": {
95 "domain": {
96 "id": "abc123"
97 },
98 "name": "fakey",
99 "password": "notpassword"
100 }
101 }
102 }
103 }
104 }
105 `)
106}
Ash Wilsond8da9e42014-08-29 08:01:06 -0400107
108func TestCreateUsernameDomainNamePassword(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600109 ao := gophercloud.AuthOptions{DomainName: "spork.net"}
110 ao.Username = "frank"
111 ao.Password = "swordfish"
112 authTokenPost(t, ao, 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) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600133 authTokenPost(t, gophercloud.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) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600148 ao := gophercloud.AuthOptions{}
149 ao.UserID = "fenris"
150 ao.Password = "g0t0h311"
151 scope := &gophercloud.ScopeOptsV3{ProjectID: "123456"}
152 authTokenPost(t, ao, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400153 {
154 "auth": {
155 "identity": {
156 "methods": ["password"],
157 "password": {
158 "user": {
159 "id": "fenris",
160 "password": "g0t0h311"
161 }
162 }
163 },
164 "scope": {
165 "project": {
166 "id": "123456"
167 }
168 }
169 }
170 }
171 `)
172}
173
174func TestCreateDomainIDScope(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600175 ao := gophercloud.AuthOptions{}
176 ao.UserID = "fenris"
177 ao.Password = "g0t0h311"
178 scope := &gophercloud.ScopeOptsV3{DomainID: "1000"}
179 authTokenPost(t, ao, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400180 {
181 "auth": {
182 "identity": {
183 "methods": ["password"],
184 "password": {
185 "user": {
186 "id": "fenris",
187 "password": "g0t0h311"
188 }
189 }
190 },
191 "scope": {
192 "domain": {
193 "id": "1000"
194 }
195 }
196 }
197 }
198 `)
199}
200
201func TestCreateProjectNameAndDomainIDScope(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600202 ao := gophercloud.AuthOptions{}
203 ao.UserID = "fenris"
204 ao.Password = "g0t0h311"
205 scope := &gophercloud.ScopeOptsV3{ProjectName: "world-domination", DomainID: "1000"}
206 authTokenPost(t, ao, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400207 {
208 "auth": {
209 "identity": {
210 "methods": ["password"],
211 "password": {
212 "user": {
213 "id": "fenris",
214 "password": "g0t0h311"
215 }
216 }
217 },
218 "scope": {
219 "project": {
220 "domain": {
221 "id": "1000"
222 },
223 "name": "world-domination"
224 }
225 }
226 }
227 }
228 `)
229}
230
231func TestCreateProjectNameAndDomainNameScope(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600232 ao := gophercloud.AuthOptions{}
233 ao.UserID = "fenris"
234 ao.Password = "g0t0h311"
235 scope := &gophercloud.ScopeOptsV3{ProjectName: "world-domination", DomainName: "evil-plans"}
236 authTokenPost(t, ao, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400237 {
238 "auth": {
239 "identity": {
240 "methods": ["password"],
241 "password": {
242 "user": {
243 "id": "fenris",
244 "password": "g0t0h311"
245 }
246 }
247 },
248 "scope": {
249 "project": {
250 "domain": {
251 "name": "evil-plans"
252 },
253 "name": "world-domination"
254 }
255 }
256 }
257 }
258 `)
259}
Ash Wilsona8855ff2014-08-29 08:26:29 -0400260
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400261func TestCreateExtractsTokenFromResponse(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400262 testhelper.SetupHTTP()
263 defer testhelper.TeardownHTTP()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400264
Ash Wilson6425a412014-08-29 12:30:35 -0400265 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400266 ProviderClient: &gophercloud.ProviderClient{},
267 Endpoint: testhelper.Endpoint(),
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400268 }
269
Ash Wilson0ab4d612014-08-29 11:10:13 -0400270 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400271 w.Header().Add("X-Subject-Token", "aaa111")
272
273 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -0400274 fmt.Fprintf(w, `{
275 "token": {
276 "expires_at": "2014-10-02T13:45:00.000000Z"
277 }
278 }`)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400279 })
280
Jon Perrittdb0ae142016-03-13 00:33:41 -0600281 ao := gophercloud.AuthOptions{}
282 ao.UserID = "me"
283 ao.Password = "shhh"
284 token, err := Create(&client, ao, nil).Extract()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400285 if err != nil {
Ash Wilson63b2a292014-10-02 09:29:06 -0400286 t.Fatalf("Create returned an error: %v", err)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400287 }
288
Ash Wilson3f59ade2014-10-02 09:22:23 -0400289 if token.ID != "aaa111" {
290 t.Errorf("Expected token to be aaa111, but was %s", token.ID)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400291 }
292}
293
Ash Wilsona8855ff2014-08-29 08:26:29 -0400294func TestCreateFailureEmptyAuth(t *testing.T) {
295 authTokenPostErr(t, gophercloud.AuthOptions{}, nil, false, ErrMissingPassword)
296}
297
Ash Wilsona8855ff2014-08-29 08:26:29 -0400298func TestCreateFailureTokenIDUsername(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600299 ao := gophercloud.AuthOptions{}
300 ao.Username = "somthing"
301 authTokenPostErr(t, ao, nil, true, ErrUsernameWithToken)
Ash Wilsona8855ff2014-08-29 08:26:29 -0400302}
303
304func TestCreateFailureTokenIDUserID(t *testing.T) {
305 authTokenPostErr(t, gophercloud.AuthOptions{UserID: "something"}, nil, true, ErrUserIDWithToken)
306}
307
308func TestCreateFailureTokenIDDomainID(t *testing.T) {
309 authTokenPostErr(t, gophercloud.AuthOptions{DomainID: "something"}, nil, true, ErrDomainIDWithToken)
310}
311
312func TestCreateFailureTokenIDDomainName(t *testing.T) {
313 authTokenPostErr(t, gophercloud.AuthOptions{DomainName: "something"}, nil, true, ErrDomainNameWithToken)
314}
Ash Wilsonaed3db42014-08-29 08:59:56 -0400315
316func TestCreateFailureMissingUser(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600317 ao := gophercloud.AuthOptions{}
318 ao.Password = "supersecure"
319 authTokenPostErr(t, ao, nil, false, ErrUsernameOrUserID)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400320}
321
322func TestCreateFailureBothUser(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600323 ao := gophercloud.AuthOptions{}
324 ao.UserID = "redundancy"
325 ao.Username = "oops"
326 ao.Password = "supersecure"
327 authTokenPostErr(t, ao, nil, false, ErrUsernameOrUserID)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400328}
329
330func TestCreateFailureMissingDomain(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600331 ao := gophercloud.AuthOptions{}
332 ao.Username = "notuniqueenough"
333 ao.Password = "supersecure"
334 authTokenPostErr(t, ao, nil, false, ErrDomainIDOrDomainName)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400335}
336
337func TestCreateFailureBothDomain(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600338 ao := gophercloud.AuthOptions{}
339 ao.Username = "someone"
340 ao.Password = "supersecure"
341 ao.DomainID = "hurf"
342 ao.DomainName = "durf"
343 authTokenPostErr(t, ao, nil, false, ErrDomainIDOrDomainName)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400344}
345
346func TestCreateFailureUserIDDomainID(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600347 ao := gophercloud.AuthOptions{}
348 ao.UserID = "100"
349 ao.Password = "stuff"
350 ao.DomainID = "oops"
351 authTokenPostErr(t, ao, nil, false, ErrDomainIDWithUserID)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400352}
353
354func TestCreateFailureUserIDDomainName(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600355 ao := gophercloud.AuthOptions{}
356 ao.UserID = "100"
357 ao.Password = "sssh"
358 ao.DomainName = "oops"
359 authTokenPostErr(t, ao, nil, false, ErrDomainNameWithUserID)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400360}
361
362func TestCreateFailureScopeProjectNameAlone(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600363 ao := gophercloud.AuthOptions{}
364 ao.UserID = "myself"
365 ao.Password = "swordfish"
366 scope := &gophercloud.ScopeOptsV3{ProjectName: "notenough"}
367 authTokenPostErr(t, ao, scope, false, ErrScopeDomainIDOrDomainName)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400368}
369
370func TestCreateFailureScopeProjectNameAndID(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600371 ao := gophercloud.AuthOptions{}
372 ao.UserID = "myself"
373 ao.Password = "swordfish"
374 scope := &gophercloud.ScopeOptsV3{ProjectName: "whoops", ProjectID: "toomuch", DomainID: "1234"}
375 authTokenPostErr(t, ao, scope, false, ErrScopeProjectIDOrProjectName)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400376}
377
378func TestCreateFailureScopeProjectIDAndDomainID(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600379 ao := gophercloud.AuthOptions{}
380 ao.UserID = "myself"
381 ao.Password = "swordfish"
382 scope := &gophercloud.ScopeOptsV3{ProjectID: "toomuch", DomainID: "notneeded"}
383 authTokenPostErr(t, ao, scope, false, ErrScopeProjectIDAlone)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400384}
385
386func TestCreateFailureScopeProjectIDAndDomainNAme(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600387 ao := gophercloud.AuthOptions{}
388 ao.UserID = "myself"
389 ao.Password = "swordfish"
390 scope := &gophercloud.ScopeOptsV3{ProjectID: "toomuch", DomainName: "notneeded"}
391 authTokenPostErr(t, ao, scope, false, ErrScopeProjectIDAlone)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400392}
393
394func TestCreateFailureScopeDomainIDAndDomainName(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600395 ao := gophercloud.AuthOptions{}
396 ao.UserID = "myself"
397 ao.Password = "swordfish"
398 scope := &gophercloud.ScopeOptsV3{DomainID: "toomuch", DomainName: "notneeded"}
399 authTokenPostErr(t, ao, scope, false, ErrScopeDomainIDOrDomainName)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400400}
401
402func TestCreateFailureScopeDomainNameAlone(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600403 ao := gophercloud.AuthOptions{}
404 ao.UserID = "myself"
405 ao.Password = "swordfish"
406 scope := &gophercloud.ScopeOptsV3{DomainName: "notenough"}
407 authTokenPostErr(t, ao, scope, false, ErrScopeDomainName)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400408}
409
410func TestCreateFailureEmptyScope(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600411 ao := gophercloud.AuthOptions{}
412 ao.UserID = "myself"
413 ao.Password = "swordfish"
414 scope := &gophercloud.ScopeOptsV3{}
415 authTokenPostErr(t, ao, scope, false, ErrScopeEmpty)
Ash Wilsonaed3db42014-08-29 08:59:56 -0400416}
Ash Wilson46d913f2014-08-29 11:00:11 -0400417
Ash Wilson5266e492014-09-09 15:44:30 -0400418func TestGetRequest(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400419 testhelper.SetupHTTP()
420 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400421
Ash Wilson6425a412014-08-29 12:30:35 -0400422 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400423 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400424 TokenID: "12345abcdef",
425 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400426 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400427 }
428
Ash Wilson0ab4d612014-08-29 11:10:13 -0400429 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400430 testhelper.TestMethod(t, r, "GET")
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500431 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400432 testhelper.TestHeader(t, r, "Accept", "application/json")
433 testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef")
434 testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345")
435
436 w.WriteHeader(http.StatusOK)
437 fmt.Fprintf(w, `
438 { "token": { "expires_at": "2014-08-29T13:10:01.000000Z" } }
439 `)
440 })
441
Ash Wilson3f59ade2014-10-02 09:22:23 -0400442 token, err := Get(&client, "abcdef12345").Extract()
Ash Wilson46d913f2014-08-29 11:00:11 -0400443 if err != nil {
444 t.Errorf("Info returned an error: %v", err)
445 }
446
Ash Wilson46d913f2014-08-29 11:00:11 -0400447 expected, _ := time.Parse(time.UnixDate, "Fri Aug 29 13:10:01 UTC 2014")
Ash Wilson3f59ade2014-10-02 09:22:23 -0400448 if token.ExpiresAt != expected {
449 t.Errorf("Expected expiration time %s, but was %s", expected.Format(time.UnixDate), token.ExpiresAt.Format(time.UnixDate))
Ash Wilson46d913f2014-08-29 11:00:11 -0400450 }
451}
452
Ash Wilson6425a412014-08-29 12:30:35 -0400453func prepareAuthTokenHandler(t *testing.T, expectedMethod string, status int) gophercloud.ServiceClient {
454 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400455 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400456 TokenID: "12345abcdef",
457 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400458 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400459 }
460
Ash Wilson0ab4d612014-08-29 11:10:13 -0400461 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400462 testhelper.TestMethod(t, r, expectedMethod)
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500463 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400464 testhelper.TestHeader(t, r, "Accept", "application/json")
465 testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef")
466 testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345")
467
468 w.WriteHeader(status)
469 })
470
471 return client
472}
473
474func TestValidateRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400475 testhelper.SetupHTTP()
476 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400477 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNoContent)
478
479 ok, err := Validate(&client, "abcdef12345")
480 if err != nil {
481 t.Errorf("Unexpected error from Validate: %v", err)
482 }
483
484 if !ok {
485 t.Errorf("Validate returned false for a valid token")
486 }
487}
488
489func TestValidateRequestFailure(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400490 testhelper.SetupHTTP()
491 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400492 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNotFound)
493
494 ok, err := Validate(&client, "abcdef12345")
495 if err != nil {
496 t.Errorf("Unexpected error from Validate: %v", err)
497 }
498
499 if ok {
500 t.Errorf("Validate returned true for an invalid token")
501 }
502}
503
504func TestValidateRequestError(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, "HEAD", http.StatusUnauthorized)
508
509 _, err := Validate(&client, "abcdef12345")
510 if err == nil {
511 t.Errorf("Missing expected error from Validate")
512 }
513}
514
515func TestRevokeRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400516 testhelper.SetupHTTP()
517 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400518 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNoContent)
519
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100520 res := Revoke(&client, "abcdef12345")
521 testhelper.AssertNoErr(t, res.Err)
Ash Wilson46d913f2014-08-29 11:00:11 -0400522}
523
524func TestRevokeRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400525 testhelper.SetupHTTP()
526 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400527 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNotFound)
528
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100529 res := Revoke(&client, "abcdef12345")
530 if res.Err == nil {
Ash Wilson46d913f2014-08-29 11:00:11 -0400531 t.Errorf("Missing expected error from Revoke")
532 }
533}