blob: faa79e0bb41f331df073af65e84eac8bb3b5707c [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.
jrperritt29ae6b32016-04-13 12:59:37 -050014func authTokenPost(t *testing.T, options AuthOptions, scope *Scope, 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{
jrperritt29ae6b32016-04-13 12:59:37 -050019 ProviderClient: &gophercloud.ProviderClient{},
20 Endpoint: testhelper.Endpoint(),
Ash Wilsoncde68122014-08-28 16:15:43 -040021 }
22
Ash Wilson0ab4d612014-08-29 11:10:13 -040023 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsoncde68122014-08-28 16:15:43 -040024 testhelper.TestMethod(t, r, "POST")
25 testhelper.TestHeader(t, r, "Content-Type", "application/json")
26 testhelper.TestHeader(t, r, "Accept", "application/json")
Ash Wilson417d9222014-08-29 07:58:35 -040027 testhelper.TestJSONRequest(t, r, requestJSON)
Ash Wilsoncde68122014-08-28 16:15:43 -040028
Ash Wilson4a52e2a2014-08-29 09:28:00 -040029 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -040030 fmt.Fprintf(w, `{
31 "token": {
32 "expires_at": "2014-10-02T13:45:00.000000Z"
33 }
34 }`)
Ash Wilsoncde68122014-08-28 16:15:43 -040035 })
36
Ash Wilson3f59ade2014-10-02 09:22:23 -040037 _, err := Create(&client, options, scope).Extract()
Ash Wilsoncde68122014-08-28 16:15:43 -040038 if err != nil {
39 t.Errorf("Create returned an error: %v", err)
40 }
41}
Ash Wilson417d9222014-08-29 07:58:35 -040042
jrperritt29ae6b32016-04-13 12:59:37 -050043func authTokenPostErr(t *testing.T, options AuthOptions, scope *Scope, includeToken bool, expectedErr error) {
Ash Wilson0ab4d612014-08-29 11:10:13 -040044 testhelper.SetupHTTP()
45 defer testhelper.TeardownHTTP()
Ash Wilsona8855ff2014-08-29 08:26:29 -040046
Ash Wilson6425a412014-08-29 12:30:35 -040047 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -040048 ProviderClient: &gophercloud.ProviderClient{},
49 Endpoint: testhelper.Endpoint(),
Ash Wilsona8855ff2014-08-29 08:26:29 -040050 }
51 if includeToken {
Ash Wilsond7f73e92014-10-22 09:11:49 -040052 client.TokenID = "abcdef123456"
Ash Wilsona8855ff2014-08-29 08:26:29 -040053 }
54
Ash Wilson3f59ade2014-10-02 09:22:23 -040055 _, err := Create(&client, options, scope).Extract()
Ash Wilsona8855ff2014-08-29 08:26:29 -040056 if err == nil {
57 t.Errorf("Create did NOT return an error")
58 }
59 if err != expectedErr {
60 t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err)
61 }
62}
63
Ash Wilson417d9222014-08-29 07:58:35 -040064func TestCreateUserIDAndPassword(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -050065 authTokenPost(t, AuthOptions{UserID: "me", Password: "squirrel!"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040066 {
67 "auth": {
68 "identity": {
69 "methods": ["password"],
70 "password": {
71 "user": { "id": "me", "password": "squirrel!" }
72 }
73 }
74 }
75 }
76 `)
77}
78
79func TestCreateUsernameDomainIDPassword(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -050080 authTokenPost(t, AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040081 {
82 "auth": {
83 "identity": {
84 "methods": ["password"],
85 "password": {
86 "user": {
87 "domain": {
88 "id": "abc123"
89 },
90 "name": "fakey",
91 "password": "notpassword"
92 }
93 }
94 }
95 }
96 }
97 `)
98}
Ash Wilsond8da9e42014-08-29 08:01:06 -040099
100func TestCreateUsernameDomainNamePassword(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500101 authTokenPost(t, AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, `
Ash Wilsond8da9e42014-08-29 08:01:06 -0400102 {
103 "auth": {
104 "identity": {
105 "methods": ["password"],
106 "password": {
107 "user": {
108 "domain": {
109 "name": "spork.net"
110 },
111 "name": "frank",
112 "password": "swordfish"
113 }
114 }
115 }
116 }
117 }
118 `)
119}
Ash Wilson053fcb02014-08-29 08:04:35 -0400120
121func TestCreateTokenID(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500122 authTokenPost(t, AuthOptions{TokenID: "12345abcdef"}, nil, `
Ash Wilson053fcb02014-08-29 08:04:35 -0400123 {
124 "auth": {
125 "identity": {
126 "methods": ["token"],
127 "token": {
128 "id": "12345abcdef"
129 }
130 }
131 }
132 }
133 `)
134}
Ash Wilson1fde6162014-08-29 08:13:06 -0400135
136func TestCreateProjectIDScope(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500137 options := AuthOptions{UserID: "fenris", Password: "g0t0h311"}
138 scope := &Scope{ProjectID: "123456"}
139 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400140 {
141 "auth": {
142 "identity": {
143 "methods": ["password"],
144 "password": {
145 "user": {
146 "id": "fenris",
147 "password": "g0t0h311"
148 }
149 }
150 },
151 "scope": {
152 "project": {
153 "id": "123456"
154 }
155 }
156 }
157 }
158 `)
159}
160
161func TestCreateDomainIDScope(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500162 options := AuthOptions{UserID: "fenris", Password: "g0t0h311"}
163 scope := &Scope{DomainID: "1000"}
164 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400165 {
166 "auth": {
167 "identity": {
168 "methods": ["password"],
169 "password": {
170 "user": {
171 "id": "fenris",
172 "password": "g0t0h311"
173 }
174 }
175 },
176 "scope": {
177 "domain": {
178 "id": "1000"
179 }
180 }
181 }
182 }
183 `)
184}
185
186func TestCreateProjectNameAndDomainIDScope(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500187 options := AuthOptions{UserID: "fenris", Password: "g0t0h311"}
188 scope := &Scope{ProjectName: "world-domination", DomainID: "1000"}
189 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400190 {
191 "auth": {
192 "identity": {
193 "methods": ["password"],
194 "password": {
195 "user": {
196 "id": "fenris",
197 "password": "g0t0h311"
198 }
199 }
200 },
201 "scope": {
202 "project": {
203 "domain": {
204 "id": "1000"
205 },
206 "name": "world-domination"
207 }
208 }
209 }
210 }
211 `)
212}
213
214func TestCreateProjectNameAndDomainNameScope(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500215 options := AuthOptions{UserID: "fenris", Password: "g0t0h311"}
216 scope := &Scope{ProjectName: "world-domination", DomainName: "evil-plans"}
217 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400218 {
219 "auth": {
220 "identity": {
221 "methods": ["password"],
222 "password": {
223 "user": {
224 "id": "fenris",
225 "password": "g0t0h311"
226 }
227 }
228 },
229 "scope": {
230 "project": {
231 "domain": {
232 "name": "evil-plans"
233 },
234 "name": "world-domination"
235 }
236 }
237 }
238 }
239 `)
240}
Ash Wilsona8855ff2014-08-29 08:26:29 -0400241
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400242func TestCreateExtractsTokenFromResponse(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400243 testhelper.SetupHTTP()
244 defer testhelper.TeardownHTTP()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400245
Ash Wilson6425a412014-08-29 12:30:35 -0400246 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400247 ProviderClient: &gophercloud.ProviderClient{},
248 Endpoint: testhelper.Endpoint(),
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400249 }
250
Ash Wilson0ab4d612014-08-29 11:10:13 -0400251 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400252 w.Header().Add("X-Subject-Token", "aaa111")
253
254 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -0400255 fmt.Fprintf(w, `{
256 "token": {
257 "expires_at": "2014-10-02T13:45:00.000000Z"
258 }
259 }`)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400260 })
261
jrperritt29ae6b32016-04-13 12:59:37 -0500262 options := AuthOptions{UserID: "me", Password: "shhh"}
263 token, err := Create(&client, options, nil).Extract()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400264 if err != nil {
Ash Wilson63b2a292014-10-02 09:29:06 -0400265 t.Fatalf("Create returned an error: %v", err)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400266 }
267
Ash Wilson3f59ade2014-10-02 09:22:23 -0400268 if token.ID != "aaa111" {
269 t.Errorf("Expected token to be aaa111, but was %s", token.ID)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400270 }
271}
272
Ash Wilsona8855ff2014-08-29 08:26:29 -0400273func TestCreateFailureEmptyAuth(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500274 authTokenPostErr(t, AuthOptions{}, nil, false, ErrMissingPassword{})
275}
276
277func TestCreateFailureTenantID(t *testing.T) {
278 authTokenPostErr(t, AuthOptions{TenantID: "something"}, nil, false, ErrTenantIDProvided{})
279}
280
281func TestCreateFailureTenantName(t *testing.T) {
282 authTokenPostErr(t, AuthOptions{TenantName: "something"}, nil, false, ErrTenantNameProvided{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400283}
284
Ash Wilsona8855ff2014-08-29 08:26:29 -0400285func TestCreateFailureTokenIDUsername(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500286 authTokenPostErr(t, AuthOptions{Username: "something", TokenID: "12345"}, nil, true, ErrUsernameWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400287}
288
289func TestCreateFailureTokenIDUserID(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500290 authTokenPostErr(t, AuthOptions{UserID: "something", TokenID: "12345"}, nil, true, ErrUserIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400291}
292
293func TestCreateFailureTokenIDDomainID(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500294 authTokenPostErr(t, AuthOptions{DomainID: "something", TokenID: "12345"}, nil, true, ErrDomainIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400295}
296
297func TestCreateFailureTokenIDDomainName(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500298 authTokenPostErr(t, AuthOptions{DomainName: "something", TokenID: "12345"}, nil, true, ErrDomainNameWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400299}
Ash Wilsonaed3db42014-08-29 08:59:56 -0400300
301func TestCreateFailureMissingUser(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500302 options := AuthOptions{Password: "supersecure"}
303 authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400304}
305
306func TestCreateFailureBothUser(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500307 options := AuthOptions{
308 Password: "supersecure",
309 Username: "oops",
310 UserID: "redundancy",
311 }
312 authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400313}
314
315func TestCreateFailureMissingDomain(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500316 options := AuthOptions{
317 Password: "supersecure",
318 Username: "notuniqueenough",
319 }
320 authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400321}
322
323func TestCreateFailureBothDomain(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500324 options := AuthOptions{
325 Password: "supersecure",
326 Username: "someone",
327 DomainID: "hurf",
328 DomainName: "durf",
329 }
330 authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400331}
332
333func TestCreateFailureUserIDDomainID(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500334 options := AuthOptions{
335 UserID: "100",
336 Password: "stuff",
337 DomainID: "oops",
338 }
339 authTokenPostErr(t, options, nil, false, ErrDomainIDWithUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400340}
341
342func TestCreateFailureUserIDDomainName(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500343 options := AuthOptions{
344 UserID: "100",
345 Password: "sssh",
346 DomainName: "oops",
347 }
348 authTokenPostErr(t, options, nil, false, ErrDomainNameWithUserID{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400349}
350
351func TestCreateFailureScopeProjectNameAlone(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500352 options := AuthOptions{UserID: "myself", Password: "swordfish"}
353 scope := &Scope{ProjectName: "notenough"}
354 authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400355}
356
357func TestCreateFailureScopeProjectNameAndID(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500358 options := AuthOptions{UserID: "myself", Password: "swordfish"}
359 scope := &Scope{ProjectName: "whoops", ProjectID: "toomuch", DomainID: "1234"}
360 authTokenPostErr(t, options, scope, false, ErrScopeProjectIDOrProjectName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400361}
362
363func TestCreateFailureScopeProjectIDAndDomainID(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500364 options := AuthOptions{UserID: "myself", Password: "swordfish"}
365 scope := &Scope{ProjectID: "toomuch", DomainID: "notneeded"}
366 authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400367}
368
369func TestCreateFailureScopeProjectIDAndDomainNAme(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500370 options := AuthOptions{UserID: "myself", Password: "swordfish"}
371 scope := &Scope{ProjectID: "toomuch", DomainName: "notneeded"}
372 authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400373}
374
375func TestCreateFailureScopeDomainIDAndDomainName(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500376 options := AuthOptions{UserID: "myself", Password: "swordfish"}
377 scope := &Scope{DomainID: "toomuch", DomainName: "notneeded"}
378 authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400379}
380
381func TestCreateFailureScopeDomainNameAlone(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500382 options := AuthOptions{UserID: "myself", Password: "swordfish"}
383 scope := &Scope{DomainName: "notenough"}
384 authTokenPostErr(t, options, scope, false, ErrScopeDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400385}
386
387func TestCreateFailureEmptyScope(t *testing.T) {
jrperritt29ae6b32016-04-13 12:59:37 -0500388 options := AuthOptions{UserID: "myself", Password: "swordfish"}
389 scope := &Scope{}
390 authTokenPostErr(t, options, scope, false, ErrScopeEmpty{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400391}
Ash Wilson46d913f2014-08-29 11:00:11 -0400392
Ash Wilson5266e492014-09-09 15:44:30 -0400393func TestGetRequest(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400394 testhelper.SetupHTTP()
395 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400396
Ash Wilson6425a412014-08-29 12:30:35 -0400397 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400398 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400399 TokenID: "12345abcdef",
400 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400401 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400402 }
403
Ash Wilson0ab4d612014-08-29 11:10:13 -0400404 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400405 testhelper.TestMethod(t, r, "GET")
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500406 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400407 testhelper.TestHeader(t, r, "Accept", "application/json")
408 testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef")
409 testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345")
410
411 w.WriteHeader(http.StatusOK)
412 fmt.Fprintf(w, `
413 { "token": { "expires_at": "2014-08-29T13:10:01.000000Z" } }
414 `)
415 })
416
Ash Wilson3f59ade2014-10-02 09:22:23 -0400417 token, err := Get(&client, "abcdef12345").Extract()
Ash Wilson46d913f2014-08-29 11:00:11 -0400418 if err != nil {
419 t.Errorf("Info returned an error: %v", err)
420 }
421
Ash Wilson46d913f2014-08-29 11:00:11 -0400422 expected, _ := time.Parse(time.UnixDate, "Fri Aug 29 13:10:01 UTC 2014")
Ash Wilson3f59ade2014-10-02 09:22:23 -0400423 if token.ExpiresAt != expected {
424 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 -0400425 }
426}
427
Ash Wilson6425a412014-08-29 12:30:35 -0400428func prepareAuthTokenHandler(t *testing.T, expectedMethod string, status int) gophercloud.ServiceClient {
429 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400430 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400431 TokenID: "12345abcdef",
432 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400433 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400434 }
435
Ash Wilson0ab4d612014-08-29 11:10:13 -0400436 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400437 testhelper.TestMethod(t, r, expectedMethod)
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500438 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400439 testhelper.TestHeader(t, r, "Accept", "application/json")
440 testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef")
441 testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345")
442
443 w.WriteHeader(status)
444 })
445
446 return client
447}
448
449func TestValidateRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400450 testhelper.SetupHTTP()
451 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400452 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNoContent)
453
454 ok, err := Validate(&client, "abcdef12345")
455 if err != nil {
456 t.Errorf("Unexpected error from Validate: %v", err)
457 }
458
459 if !ok {
460 t.Errorf("Validate returned false for a valid token")
461 }
462}
463
464func TestValidateRequestFailure(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400465 testhelper.SetupHTTP()
466 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400467 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNotFound)
468
469 ok, err := Validate(&client, "abcdef12345")
470 if err != nil {
471 t.Errorf("Unexpected error from Validate: %v", err)
472 }
473
474 if ok {
475 t.Errorf("Validate returned true for an invalid token")
476 }
477}
478
479func TestValidateRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400480 testhelper.SetupHTTP()
481 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400482 client := prepareAuthTokenHandler(t, "HEAD", http.StatusUnauthorized)
483
484 _, err := Validate(&client, "abcdef12345")
485 if err == nil {
486 t.Errorf("Missing expected error from Validate")
487 }
488}
489
490func TestRevokeRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400491 testhelper.SetupHTTP()
492 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400493 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNoContent)
494
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100495 res := Revoke(&client, "abcdef12345")
496 testhelper.AssertNoErr(t, res.Err)
Ash Wilson46d913f2014-08-29 11:00:11 -0400497}
498
499func TestRevokeRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400500 testhelper.SetupHTTP()
501 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400502 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNotFound)
503
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100504 res := Revoke(&client, "abcdef12345")
505 if res.Err == nil {
Ash Wilson46d913f2014-08-29 11:00:11 -0400506 t.Errorf("Missing expected error from Revoke")
507 }
508}