blob: cbb675fb712255253e89f573970c0ebd37e3b85f [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
jrperritt3d966162016-06-06 14:08:54 -050038 _, err := tokens.Create(&client, options, scope).Extract()
Ash Wilsoncde68122014-08-28 16:15:43 -040039 if err != nil {
40 t.Errorf("Create returned an error: %v", err)
41 }
42}
Ash Wilson417d9222014-08-29 07:58:35 -040043
jrperritt3d966162016-06-06 14:08:54 -050044func authTokenPostErr(t *testing.T, options tokens.AuthOptions, scope *tokens.Scope, includeToken bool, expectedErr error) {
Ash Wilson0ab4d612014-08-29 11:10:13 -040045 testhelper.SetupHTTP()
46 defer testhelper.TeardownHTTP()
Ash Wilsona8855ff2014-08-29 08:26:29 -040047
Ash Wilson6425a412014-08-29 12:30:35 -040048 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -040049 ProviderClient: &gophercloud.ProviderClient{},
50 Endpoint: testhelper.Endpoint(),
Ash Wilsona8855ff2014-08-29 08:26:29 -040051 }
52 if includeToken {
Ash Wilsond7f73e92014-10-22 09:11:49 -040053 client.TokenID = "abcdef123456"
Ash Wilsona8855ff2014-08-29 08:26:29 -040054 }
55
jrperritt3d966162016-06-06 14:08:54 -050056 _, err := tokens.Create(&client, options, scope).Extract()
Ash Wilsona8855ff2014-08-29 08:26:29 -040057 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 Wilson417d9222014-08-29 07:58:35 -040065func TestCreateUserIDAndPassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050066 authTokenPost(t, tokens.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040067 {
68 "auth": {
69 "identity": {
70 "methods": ["password"],
71 "password": {
72 "user": { "id": "me", "password": "squirrel!" }
73 }
74 }
75 }
76 }
77 `)
78}
79
80func TestCreateUsernameDomainIDPassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050081 authTokenPost(t, tokens.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, `
Ash Wilson417d9222014-08-29 07:58:35 -040082 {
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 Wilsond8da9e42014-08-29 08:01:06 -0400100
101func TestCreateUsernameDomainNamePassword(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500102 authTokenPost(t, tokens.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, `
Ash Wilsond8da9e42014-08-29 08:01:06 -0400103 {
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 Wilson053fcb02014-08-29 08:04:35 -0400121
122func TestCreateTokenID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500123 authTokenPost(t, tokens.AuthOptions{TokenID: "12345abcdef"}, nil, `
Ash Wilson053fcb02014-08-29 08:04:35 -0400124 {
125 "auth": {
126 "identity": {
127 "methods": ["token"],
128 "token": {
129 "id": "12345abcdef"
130 }
131 }
132 }
133 }
134 `)
135}
Ash Wilson1fde6162014-08-29 08:13:06 -0400136
137func TestCreateProjectIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500138 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
139 scope := &tokens.Scope{ProjectID: "123456"}
jrperritt29ae6b32016-04-13 12:59:37 -0500140 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400141 {
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
162func TestCreateDomainIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500163 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
164 scope := &tokens.Scope{DomainID: "1000"}
jrperritt29ae6b32016-04-13 12:59:37 -0500165 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400166 {
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
187func TestCreateProjectNameAndDomainIDScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500188 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
189 scope := &tokens.Scope{ProjectName: "world-domination", DomainID: "1000"}
jrperritt29ae6b32016-04-13 12:59:37 -0500190 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400191 {
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
215func TestCreateProjectNameAndDomainNameScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500216 options := tokens.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
217 scope := &tokens.Scope{ProjectName: "world-domination", DomainName: "evil-plans"}
jrperritt29ae6b32016-04-13 12:59:37 -0500218 authTokenPost(t, options, scope, `
Ash Wilson1fde6162014-08-29 08:13:06 -0400219 {
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 Wilsona8855ff2014-08-29 08:26:29 -0400242
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400243func TestCreateExtractsTokenFromResponse(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400244 testhelper.SetupHTTP()
245 defer testhelper.TeardownHTTP()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400246
Ash Wilson6425a412014-08-29 12:30:35 -0400247 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400248 ProviderClient: &gophercloud.ProviderClient{},
249 Endpoint: testhelper.Endpoint(),
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400250 }
251
Ash Wilson0ab4d612014-08-29 11:10:13 -0400252 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400253 w.Header().Add("X-Subject-Token", "aaa111")
254
255 w.WriteHeader(http.StatusCreated)
Ash Wilson63b2a292014-10-02 09:29:06 -0400256 fmt.Fprintf(w, `{
257 "token": {
258 "expires_at": "2014-10-02T13:45:00.000000Z"
259 }
260 }`)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400261 })
262
jrperritt3d966162016-06-06 14:08:54 -0500263 options := tokens.AuthOptions{UserID: "me", Password: "shhh"}
264 token, err := tokens.Create(&client, options, nil).Extract()
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400265 if err != nil {
Ash Wilson63b2a292014-10-02 09:29:06 -0400266 t.Fatalf("Create returned an error: %v", err)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400267 }
268
Ash Wilson3f59ade2014-10-02 09:22:23 -0400269 if token.ID != "aaa111" {
270 t.Errorf("Expected token to be aaa111, but was %s", token.ID)
Ash Wilson4a52e2a2014-08-29 09:28:00 -0400271 }
272}
273
Ash Wilsona8855ff2014-08-29 08:26:29 -0400274func TestCreateFailureEmptyAuth(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500275 authTokenPostErr(t, tokens.AuthOptions{}, nil, false, tokens.ErrMissingPassword{})
jrperritt29ae6b32016-04-13 12:59:37 -0500276}
277
278func TestCreateFailureTenantID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500279 authTokenPostErr(t, tokens.AuthOptions{TenantID: "something"}, nil, false, tokens.ErrTenantIDProvided{})
jrperritt29ae6b32016-04-13 12:59:37 -0500280}
281
282func TestCreateFailureTenantName(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500283 authTokenPostErr(t, tokens.AuthOptions{TenantName: "something"}, nil, false, tokens.ErrTenantNameProvided{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400284}
285
Ash Wilsona8855ff2014-08-29 08:26:29 -0400286func TestCreateFailureTokenIDUsername(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500287 authTokenPostErr(t, tokens.AuthOptions{Username: "something", TokenID: "12345"}, nil, true, tokens.ErrUsernameWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400288}
289
290func TestCreateFailureTokenIDUserID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500291 authTokenPostErr(t, tokens.AuthOptions{UserID: "something", TokenID: "12345"}, nil, true, tokens.ErrUserIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400292}
293
294func TestCreateFailureTokenIDDomainID(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500295 authTokenPostErr(t, tokens.AuthOptions{DomainID: "something", TokenID: "12345"}, nil, true, tokens.ErrDomainIDWithToken{})
Ash Wilsona8855ff2014-08-29 08:26:29 -0400296}
297
298func TestCreateFailureTokenIDDomainName(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500299 authTokenPostErr(t, tokens.AuthOptions{DomainName: "something", TokenID: "12345"}, nil, true, tokens.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"}
304 authTokenPostErr(t, options, nil, false, tokens.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 }
jrperritt3d966162016-06-06 14:08:54 -0500313 authTokenPostErr(t, options, nil, false, tokens.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 }
jrperritt3d966162016-06-06 14:08:54 -0500321 authTokenPostErr(t, options, nil, false, tokens.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 }
jrperritt3d966162016-06-06 14:08:54 -0500331 authTokenPostErr(t, options, nil, false, tokens.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 }
jrperritt3d966162016-06-06 14:08:54 -0500340 authTokenPostErr(t, options, nil, false, tokens.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 }
jrperritt3d966162016-06-06 14:08:54 -0500349 authTokenPostErr(t, options, nil, false, tokens.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"}
355 authTokenPostErr(t, options, scope, false, tokens.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"}
361 authTokenPostErr(t, options, scope, false, tokens.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"}
367 authTokenPostErr(t, options, scope, false, tokens.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"}
373 authTokenPostErr(t, options, scope, false, tokens.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"}
379 authTokenPostErr(t, options, scope, false, tokens.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"}
385 authTokenPostErr(t, options, scope, false, tokens.ErrScopeDomainName{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400386}
387
388func TestCreateFailureEmptyScope(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500389 options := tokens.AuthOptions{UserID: "myself", Password: "swordfish"}
390 scope := &tokens.Scope{}
391 authTokenPostErr(t, options, scope, false, tokens.ErrScopeEmpty{})
Ash Wilsonaed3db42014-08-29 08:59:56 -0400392}
Ash Wilson46d913f2014-08-29 11:00:11 -0400393
Ash Wilson5266e492014-09-09 15:44:30 -0400394func TestGetRequest(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400395 testhelper.SetupHTTP()
396 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400397
Ash Wilson6425a412014-08-29 12:30:35 -0400398 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400399 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400400 TokenID: "12345abcdef",
401 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400402 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400403 }
404
Ash Wilson0ab4d612014-08-29 11:10:13 -0400405 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400406 testhelper.TestMethod(t, r, "GET")
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500407 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400408 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
jrperritt3d966162016-06-06 14:08:54 -0500418 token, err := tokens.Get(&client, "abcdef12345").Extract()
Ash Wilson46d913f2014-08-29 11:00:11 -0400419 if err != nil {
420 t.Errorf("Info returned an error: %v", err)
421 }
422
Ash Wilson46d913f2014-08-29 11:00:11 -0400423 expected, _ := time.Parse(time.UnixDate, "Fri Aug 29 13:10:01 UTC 2014")
Ash Wilson3f59ade2014-10-02 09:22:23 -0400424 if token.ExpiresAt != expected {
425 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 -0400426 }
427}
428
Ash Wilson6425a412014-08-29 12:30:35 -0400429func prepareAuthTokenHandler(t *testing.T, expectedMethod string, status int) gophercloud.ServiceClient {
430 client := gophercloud.ServiceClient{
Ash Wilsond7f73e92014-10-22 09:11:49 -0400431 ProviderClient: &gophercloud.ProviderClient{
Ash Wilson6425a412014-08-29 12:30:35 -0400432 TokenID: "12345abcdef",
433 },
Ash Wilson0ab4d612014-08-29 11:10:13 -0400434 Endpoint: testhelper.Endpoint(),
Ash Wilson46d913f2014-08-29 11:00:11 -0400435 }
436
Ash Wilson0ab4d612014-08-29 11:10:13 -0400437 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson46d913f2014-08-29 11:00:11 -0400438 testhelper.TestMethod(t, r, expectedMethod)
Jon Perrittd8aef1b2014-09-11 17:50:04 -0500439 testhelper.TestHeader(t, r, "Content-Type", "")
Ash Wilson46d913f2014-08-29 11:00:11 -0400440 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
450func TestValidateRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400451 testhelper.SetupHTTP()
452 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400453 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNoContent)
454
jrperritt3d966162016-06-06 14:08:54 -0500455 ok, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400456 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
465func TestValidateRequestFailure(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400466 testhelper.SetupHTTP()
467 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400468 client := prepareAuthTokenHandler(t, "HEAD", http.StatusNotFound)
469
jrperritt3d966162016-06-06 14:08:54 -0500470 ok, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400471 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
480func TestValidateRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400481 testhelper.SetupHTTP()
482 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400483 client := prepareAuthTokenHandler(t, "HEAD", http.StatusUnauthorized)
484
jrperritt3d966162016-06-06 14:08:54 -0500485 _, err := tokens.Validate(&client, "abcdef12345")
Ash Wilson46d913f2014-08-29 11:00:11 -0400486 if err == nil {
487 t.Errorf("Missing expected error from Validate")
488 }
489}
490
491func TestRevokeRequestSuccessful(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400492 testhelper.SetupHTTP()
493 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400494 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNoContent)
495
jrperritt3d966162016-06-06 14:08:54 -0500496 res := tokens.Revoke(&client, "abcdef12345")
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100497 testhelper.AssertNoErr(t, res.Err)
Ash Wilson46d913f2014-08-29 11:00:11 -0400498}
499
500func TestRevokeRequestError(t *testing.T) {
Ash Wilson0ab4d612014-08-29 11:10:13 -0400501 testhelper.SetupHTTP()
502 defer testhelper.TeardownHTTP()
Ash Wilson46d913f2014-08-29 11:00:11 -0400503 client := prepareAuthTokenHandler(t, "DELETE", http.StatusNotFound)
504
jrperritt3d966162016-06-06 14:08:54 -0500505 res := tokens.Revoke(&client, "abcdef12345")
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100506 if res.Err == nil {
Ash Wilson46d913f2014-08-29 11:00:11 -0400507 t.Errorf("Missing expected error from Revoke")
508 }
509}