blob: 0e6269fb49de81df6bd8bd7f1a7e4acff148cd68 [file] [log] [blame]
Ash Wilsonaa197a92014-10-03 11:38:08 -04001package tokens
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7 "time"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
11 th "github.com/rackspace/gophercloud/testhelper"
12)
13
14var expectedToken = &Token{
15 ID: "aaaabbbbccccdddd",
16 ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC),
17 Tenant: tenants.Tenant{
18 ID: "fc394f2ab2df4114bde39905f800dc57",
19 Name: "test",
20 Description: "There are many tenants. This one is yours.",
21 Enabled: true,
22 },
23}
24
25var expectedServiceCatalog = &ServiceCatalog{
26 Entries: []CatalogEntry{
27 CatalogEntry{
28 Name: "inscrutablewalrus",
29 Type: "something",
30 Endpoints: []Endpoint{
31 Endpoint{
32 PublicURL: "http://something0:1234/v2/",
33 Region: "region0",
34 },
35 Endpoint{
36 PublicURL: "http://something1:1234/v2/",
37 Region: "region1",
38 },
39 },
40 },
41 CatalogEntry{
42 Name: "arbitrarypenguin",
43 Type: "else",
44 Endpoints: []Endpoint{
45 Endpoint{
46 PublicURL: "http://else0:4321/v3/",
47 Region: "region0",
48 },
49 },
50 },
51 },
52}
53
54func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult {
55 th.SetupHTTP()
56 defer th.TeardownHTTP()
57
58 client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
59
60 th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
61 th.TestMethod(t, r, "POST")
62 th.TestHeader(t, r, "Content-Type", "application/json")
63 th.TestHeader(t, r, "Accept", "application/json")
64 th.TestJSONRequest(t, r, requestJSON)
65
66 w.WriteHeader(http.StatusOK)
67 fmt.Fprintf(w, `
68{
69 "access": {
70 "token": {
71 "issued_at": "2014-01-30T15:30:58.000000Z",
72 "expires": "2014-01-31T15:30:58Z",
73 "id": "aaaabbbbccccdddd",
74 "tenant": {
75 "description": "There are many tenants. This one is yours.",
76 "enabled": true,
77 "id": "fc394f2ab2df4114bde39905f800dc57",
78 "name": "test"
79 }
80 },
81 "serviceCatalog": [
82 {
83 "endpoints": [
84 {
85 "publicURL": "http://something0:1234/v2/",
86 "region": "region0"
87 },
88 {
89 "publicURL": "http://something1:1234/v2/",
90 "region": "region1"
91 }
92 ],
93 "type": "something",
94 "name": "inscrutablewalrus"
95 },
96 {
97 "endpoints": [
98 {
99 "publicURL": "http://else0:4321/v3/",
100 "region": "region0"
101 }
102 ],
103 "type": "else",
104 "name": "arbitrarypenguin"
105 }
106 ]
107 }
108}
109 `)
110 })
111
112 return Create(&client, options)
113}
114
115func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
116 th.SetupHTTP()
117 defer th.TeardownHTTP()
118
119 client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
120
121 th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
122 th.TestMethod(t, r, "POST")
123 th.TestHeader(t, r, "Content-Type", "application/json")
124 th.TestHeader(t, r, "Accept", "application/json")
125
126 w.WriteHeader(http.StatusOK)
127 fmt.Fprintf(w, `{}`)
128 })
129
Ash Wilson27d29e22014-10-03 11:57:14 -0400130 actualErr := Create(&client, options).Err
Ash Wilsonaa197a92014-10-03 11:38:08 -0400131 th.CheckEquals(t, expectedErr, actualErr)
132}
133
134func isSuccessful(t *testing.T, result CreateResult) {
135 token, err := result.ExtractToken()
136 th.AssertNoErr(t, err)
137 th.CheckDeepEquals(t, expectedToken, token)
138
139 serviceCatalog, err := result.ExtractServiceCatalog()
140 th.AssertNoErr(t, err)
141 th.CheckDeepEquals(t, expectedServiceCatalog, serviceCatalog)
142}
143
144func TestCreateWithPassword(t *testing.T) {
145 options := gophercloud.AuthOptions{
146 Username: "me",
147 Password: "swordfish",
148 }
149
150 isSuccessful(t, tokenPost(t, options, `
151 {
152 "auth": {
153 "passwordCredentials": {
154 "username": "me",
155 "password": "swordfish"
156 }
157 }
158 }
159 `))
160}
161
162func TestCreateTokenWithAPIKey(t *testing.T) {
163 options := gophercloud.AuthOptions{
164 Username: "me",
165 APIKey: "1234567890abcdef",
166 }
167
168 isSuccessful(t, tokenPost(t, options, `
169 {
170 "auth": {
171 "RAX-KSKEY:apiKeyCredentials": {
172 "username": "me",
173 "apiKey": "1234567890abcdef"
174 }
175 }
176 }
177 `))
178}
Ash Wilson29f23172014-10-03 11:45:06 -0400179
180func TestCreateTokenWithTenantID(t *testing.T) {
181 options := gophercloud.AuthOptions{
182 Username: "me",
183 Password: "opensesame",
184 TenantID: "fc394f2ab2df4114bde39905f800dc57",
185 }
186
187 isSuccessful(t, tokenPost(t, options, `
188 {
189 "auth": {
190 "tenantId": "fc394f2ab2df4114bde39905f800dc57",
191 "passwordCredentials": {
192 "username": "me",
193 "password": "opensesame"
194 }
195 }
196 }
197 `))
198}
199
200func TestCreateTokenWithTenantName(t *testing.T) {
201 options := gophercloud.AuthOptions{
202 Username: "me",
203 Password: "opensesame",
204 TenantName: "demo",
205 }
206
207 isSuccessful(t, tokenPost(t, options, `
208 {
209 "auth": {
210 "tenantName": "demo",
211 "passwordCredentials": {
212 "username": "me",
213 "password": "opensesame"
214 }
215 }
216 }
217 `))
218}
Ash Wilson27d29e22014-10-03 11:57:14 -0400219
220func TestProhibitUserID(t *testing.T) {
221 options := gophercloud.AuthOptions{
222 Username: "me",
223 UserID: "1234",
224 Password: "thing",
225 }
226 tokenPostErr(t, options, ErrUserIDProvided)
227}
228
229func TestProhibitDomainID(t *testing.T) {
230 options := gophercloud.AuthOptions{
231 Username: "me",
232 Password: "thing",
233 DomainID: "1234",
234 }
235 tokenPostErr(t, options, ErrDomainIDProvided)
236}
237
238func TestProhibitDomainName(t *testing.T) {
239 options := gophercloud.AuthOptions{
240 Username: "me",
241 Password: "thing",
242 DomainName: "wat",
243 }
244 tokenPostErr(t, options, ErrDomainNameProvided)
245}
246
247func TestRequireUsername(t *testing.T) {
248 options := gophercloud.AuthOptions{
249 Password: "thing",
250 }
251 tokenPostErr(t, options, ErrUsernameRequired)
252}
253
254func TestProhibitBothPasswordAndAPIKey(t *testing.T) {
255 options := gophercloud.AuthOptions{
256 Username: "me",
257 Password: "thing",
258 APIKey: "123412341234",
259 }
260 tokenPostErr(t, options, ErrPasswordOrAPIKey)
261}
262
263func TestRequirePasswordOrAPIKey(t *testing.T) {
264 options := gophercloud.AuthOptions{
265 Username: "me",
266 }
267 tokenPostErr(t, options, ErrPasswordOrAPIKey)
268}