blob: 3ac49cd569dac341e69641b35d79c15cd0c20fea [file] [log] [blame]
Ash Wilsoncde68122014-08-28 16:15:43 -04001package tokens
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/testhelper"
10)
11
Ash Wilson417d9222014-08-29 07:58:35 -040012// authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure.
13func authTokenPost(t *testing.T, options gophercloud.AuthOptions, scope *Scope, requestJSON string) {
Ash Wilsoncde68122014-08-28 16:15:43 -040014 setup()
15 defer teardown()
16
17 client := gophercloud.ServiceClient{
18 Endpoint: endpoint(),
Ash Wilson417d9222014-08-29 07:58:35 -040019 Options: options,
Ash Wilson053fcb02014-08-29 08:04:35 -040020 TokenID: "12345abcdef",
Ash Wilsoncde68122014-08-28 16:15:43 -040021 }
22
23 mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
24 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
29 fmt.Fprintf(w, `{}`)
30 })
31
Ash Wilson417d9222014-08-29 07:58:35 -040032 _, err := Create(&client, scope)
Ash Wilsoncde68122014-08-28 16:15:43 -040033 if err != nil {
34 t.Errorf("Create returned an error: %v", err)
35 }
36}
Ash Wilson417d9222014-08-29 07:58:35 -040037
Ash Wilsona8855ff2014-08-29 08:26:29 -040038func authTokenPostErr(t *testing.T, options gophercloud.AuthOptions, scope *Scope, includeToken bool, expectedErr error) {
39 setup()
40 defer teardown()
41
42 client := gophercloud.ServiceClient{
43 Endpoint: endpoint(),
44 Options: options,
45 }
46 if includeToken {
47 client.TokenID = "abcdef123456"
48 }
49
50 _, err := Create(&client, scope)
51 if err == nil {
52 t.Errorf("Create did NOT return an error")
53 }
54 if err != expectedErr {
55 t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err)
56 }
57}
58
Ash Wilson417d9222014-08-29 07:58:35 -040059func TestCreateUserIDAndPassword(t *testing.T) {
60 authTokenPost(t, gophercloud.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, `
61 {
62 "auth": {
63 "identity": {
64 "methods": ["password"],
65 "password": {
66 "user": { "id": "me", "password": "squirrel!" }
67 }
68 }
69 }
70 }
71 `)
72}
73
74func TestCreateUsernameDomainIDPassword(t *testing.T) {
75 authTokenPost(t, gophercloud.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, `
76 {
77 "auth": {
78 "identity": {
79 "methods": ["password"],
80 "password": {
81 "user": {
82 "domain": {
83 "id": "abc123"
84 },
85 "name": "fakey",
86 "password": "notpassword"
87 }
88 }
89 }
90 }
91 }
92 `)
93}
Ash Wilsond8da9e42014-08-29 08:01:06 -040094
95func TestCreateUsernameDomainNamePassword(t *testing.T) {
96 authTokenPost(t, gophercloud.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, `
97 {
98 "auth": {
99 "identity": {
100 "methods": ["password"],
101 "password": {
102 "user": {
103 "domain": {
104 "name": "spork.net"
105 },
106 "name": "frank",
107 "password": "swordfish"
108 }
109 }
110 }
111 }
112 }
113 `)
114}
Ash Wilson053fcb02014-08-29 08:04:35 -0400115
116func TestCreateTokenID(t *testing.T) {
117 authTokenPost(t, gophercloud.AuthOptions{}, nil, `
118 {
119 "auth": {
120 "identity": {
121 "methods": ["token"],
122 "token": {
123 "id": "12345abcdef"
124 }
125 }
126 }
127 }
128 `)
129}
Ash Wilson1fde6162014-08-29 08:13:06 -0400130
131func TestCreateProjectIDScope(t *testing.T) {
132 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
133 scope := &Scope{ProjectID: "123456"}
134 authTokenPost(t, options, scope, `
135 {
136 "auth": {
137 "identity": {
138 "methods": ["password"],
139 "password": {
140 "user": {
141 "id": "fenris",
142 "password": "g0t0h311"
143 }
144 }
145 },
146 "scope": {
147 "project": {
148 "id": "123456"
149 }
150 }
151 }
152 }
153 `)
154}
155
156func TestCreateDomainIDScope(t *testing.T) {
157 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
158 scope := &Scope{DomainID: "1000"}
159 authTokenPost(t, options, scope, `
160 {
161 "auth": {
162 "identity": {
163 "methods": ["password"],
164 "password": {
165 "user": {
166 "id": "fenris",
167 "password": "g0t0h311"
168 }
169 }
170 },
171 "scope": {
172 "domain": {
173 "id": "1000"
174 }
175 }
176 }
177 }
178 `)
179}
180
181func TestCreateProjectNameAndDomainIDScope(t *testing.T) {
182 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
183 scope := &Scope{ProjectName: "world-domination", DomainID: "1000"}
184 authTokenPost(t, options, scope, `
185 {
186 "auth": {
187 "identity": {
188 "methods": ["password"],
189 "password": {
190 "user": {
191 "id": "fenris",
192 "password": "g0t0h311"
193 }
194 }
195 },
196 "scope": {
197 "project": {
198 "domain": {
199 "id": "1000"
200 },
201 "name": "world-domination"
202 }
203 }
204 }
205 }
206 `)
207}
208
209func TestCreateProjectNameAndDomainNameScope(t *testing.T) {
210 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
211 scope := &Scope{ProjectName: "world-domination", DomainName: "evil-plans"}
212 authTokenPost(t, options, scope, `
213 {
214 "auth": {
215 "identity": {
216 "methods": ["password"],
217 "password": {
218 "user": {
219 "id": "fenris",
220 "password": "g0t0h311"
221 }
222 }
223 },
224 "scope": {
225 "project": {
226 "domain": {
227 "name": "evil-plans"
228 },
229 "name": "world-domination"
230 }
231 }
232 }
233 }
234 `)
235}
Ash Wilsona8855ff2014-08-29 08:26:29 -0400236
237func TestCreateFailureEmptyAuth(t *testing.T) {
238 authTokenPostErr(t, gophercloud.AuthOptions{}, nil, false, ErrMissingPassword)
239}
240
241func TestCreateFailureAPIKey(t *testing.T) {
242 authTokenPostErr(t, gophercloud.AuthOptions{APIKey: "something"}, nil, false, ErrAPIKeyProvided)
243}
244
245func TestCreateFailureTenantID(t *testing.T) {
246 authTokenPostErr(t, gophercloud.AuthOptions{TenantID: "something"}, nil, false, ErrTenantIDProvided)
247}
248
249func TestCreateFailureTenantName(t *testing.T) {
250 authTokenPostErr(t, gophercloud.AuthOptions{TenantName: "something"}, nil, false, ErrTenantNameProvided)
251}
252
253func TestCreateFailureTokenIDUsername(t *testing.T) {
254 authTokenPostErr(t, gophercloud.AuthOptions{Username: "something"}, nil, true, ErrUsernameWithToken)
255}
256
257func TestCreateFailureTokenIDUserID(t *testing.T) {
258 authTokenPostErr(t, gophercloud.AuthOptions{UserID: "something"}, nil, true, ErrUserIDWithToken)
259}
260
261func TestCreateFailureTokenIDDomainID(t *testing.T) {
262 authTokenPostErr(t, gophercloud.AuthOptions{DomainID: "something"}, nil, true, ErrDomainIDWithToken)
263}
264
265func TestCreateFailureTokenIDDomainName(t *testing.T) {
266 authTokenPostErr(t, gophercloud.AuthOptions{DomainName: "something"}, nil, true, ErrDomainNameWithToken)
267}
Ash Wilsonaed3db42014-08-29 08:59:56 -0400268
269func TestCreateFailureMissingUser(t *testing.T) {
270 options := gophercloud.AuthOptions{Password: "supersecure"}
271 authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID)
272}
273
274func TestCreateFailureBothUser(t *testing.T) {
275 options := gophercloud.AuthOptions{
276 Password: "supersecure",
277 Username: "oops",
278 UserID: "redundancy",
279 }
280 authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID)
281}
282
283func TestCreateFailureMissingDomain(t *testing.T) {
284 options := gophercloud.AuthOptions{
285 Password: "supersecure",
286 Username: "notuniqueenough",
287 }
288 authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName)
289}
290
291func TestCreateFailureBothDomain(t *testing.T) {
292 options := gophercloud.AuthOptions{
293 Password: "supersecure",
294 Username: "someone",
295 DomainID: "hurf",
296 DomainName: "durf",
297 }
298 authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName)
299}
300
301func TestCreateFailureUserIDDomainID(t *testing.T) {
302 options := gophercloud.AuthOptions{
303 UserID: "100",
304 Password: "stuff",
305 DomainID: "oops",
306 }
307 authTokenPostErr(t, options, nil, false, ErrDomainIDWithUserID)
308}
309
310func TestCreateFailureUserIDDomainName(t *testing.T) {
311 options := gophercloud.AuthOptions{
312 UserID: "100",
313 Password: "sssh",
314 DomainName: "oops",
315 }
316 authTokenPostErr(t, options, nil, false, ErrDomainNameWithUserID)
317}
318
319func TestCreateFailureScopeProjectNameAlone(t *testing.T) {
320 options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"}
321 scope := &Scope{ProjectName: "notenough"}
322 authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName)
323}
324
325func TestCreateFailureScopeProjectNameAndID(t *testing.T) {
326 options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"}
327 scope := &Scope{ProjectName: "whoops", ProjectID: "toomuch", DomainID: "1234"}
328 authTokenPostErr(t, options, scope, false, ErrScopeProjectIDOrProjectName)
329}
330
331func TestCreateFailureScopeProjectIDAndDomainID(t *testing.T) {
332 options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"}
333 scope := &Scope{ProjectID: "toomuch", DomainID: "notneeded"}
334 authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone)
335}
336
337func TestCreateFailureScopeProjectIDAndDomainNAme(t *testing.T) {
338 options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"}
339 scope := &Scope{ProjectID: "toomuch", DomainName: "notneeded"}
340 authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone)
341}
342
343func TestCreateFailureScopeDomainIDAndDomainName(t *testing.T) {
344 options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"}
345 scope := &Scope{DomainID: "toomuch", DomainName: "notneeded"}
346 authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName)
347}
348
349func TestCreateFailureScopeDomainNameAlone(t *testing.T) {
350 options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"}
351 scope := &Scope{DomainName: "notenough"}
352 authTokenPostErr(t, options, scope, false, ErrScopeDomainName)
353}
354
355func TestCreateFailureEmptyScope(t *testing.T) {
356 options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"}
357 scope := &Scope{}
358 authTokenPostErr(t, options, scope, false, ErrScopeEmpty)
359}