blob: b6476df0c3eca3f625b90ecc7b27f6d7edb57e4b [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
Ash Wilson40095f02014-10-07 15:46:40 -040054func tokenPost(t *testing.T, options AuthOptions, requestJSON string) CreateResult {
Ash Wilsonaa197a92014-10-03 11:38:08 -040055 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
Ash Wilson40095f02014-10-07 15:46:40 -0400115func tokenPostErr(t *testing.T, options AuthOptions, expectedErr error) {
Ash Wilsonaa197a92014-10-03 11:38:08 -0400116 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) {
Ash Wilson40095f02014-10-07 15:46:40 -0400145 options := AuthOptions{
146 gophercloud.AuthOptions{
147 Username: "me",
148 Password: "swordfish",
149 },
Ash Wilsonaa197a92014-10-03 11:38:08 -0400150 }
151
152 isSuccessful(t, tokenPost(t, options, `
153 {
154 "auth": {
155 "passwordCredentials": {
156 "username": "me",
157 "password": "swordfish"
158 }
159 }
160 }
161 `))
162}
163
Ash Wilson29f23172014-10-03 11:45:06 -0400164func TestCreateTokenWithTenantID(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400165 options := AuthOptions{
166 gophercloud.AuthOptions{
167 Username: "me",
168 Password: "opensesame",
169 TenantID: "fc394f2ab2df4114bde39905f800dc57",
170 },
Ash Wilson29f23172014-10-03 11:45:06 -0400171 }
172
173 isSuccessful(t, tokenPost(t, options, `
174 {
175 "auth": {
176 "tenantId": "fc394f2ab2df4114bde39905f800dc57",
177 "passwordCredentials": {
178 "username": "me",
179 "password": "opensesame"
180 }
181 }
182 }
183 `))
184}
185
186func TestCreateTokenWithTenantName(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400187 options := AuthOptions{
188 gophercloud.AuthOptions{
189 Username: "me",
190 Password: "opensesame",
191 TenantName: "demo",
192 },
Ash Wilson29f23172014-10-03 11:45:06 -0400193 }
194
195 isSuccessful(t, tokenPost(t, options, `
196 {
197 "auth": {
198 "tenantName": "demo",
199 "passwordCredentials": {
200 "username": "me",
201 "password": "opensesame"
202 }
203 }
204 }
205 `))
206}
Ash Wilson27d29e22014-10-03 11:57:14 -0400207
208func TestProhibitUserID(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400209 options := AuthOptions{
210 gophercloud.AuthOptions{
211 Username: "me",
212 UserID: "1234",
213 Password: "thing",
214 },
Ash Wilson27d29e22014-10-03 11:57:14 -0400215 }
216 tokenPostErr(t, options, ErrUserIDProvided)
217}
218
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400219func TestProhibitAPIKey(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400220 options := AuthOptions{
221 gophercloud.AuthOptions{
222 Username: "me",
223 Password: "thing",
224 APIKey: "123412341234",
225 },
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400226 }
227 tokenPostErr(t, options, ErrAPIKeyProvided)
228}
229
Ash Wilson27d29e22014-10-03 11:57:14 -0400230func TestProhibitDomainID(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400231 options := AuthOptions{
232 gophercloud.AuthOptions{
233 Username: "me",
234 Password: "thing",
235 DomainID: "1234",
236 },
Ash Wilson27d29e22014-10-03 11:57:14 -0400237 }
238 tokenPostErr(t, options, ErrDomainIDProvided)
239}
240
241func TestProhibitDomainName(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400242 options := AuthOptions{
243 gophercloud.AuthOptions{
244 Username: "me",
245 Password: "thing",
246 DomainName: "wat",
247 },
Ash Wilson27d29e22014-10-03 11:57:14 -0400248 }
249 tokenPostErr(t, options, ErrDomainNameProvided)
250}
251
252func TestRequireUsername(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400253 options := AuthOptions{
254 gophercloud.AuthOptions{
255 Password: "thing",
256 },
Ash Wilson27d29e22014-10-03 11:57:14 -0400257 }
258 tokenPostErr(t, options, ErrUsernameRequired)
259}
260
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400261func TestRequirePassword(t *testing.T) {
Ash Wilson40095f02014-10-07 15:46:40 -0400262 options := AuthOptions{
263 gophercloud.AuthOptions{
264 Username: "me",
265 },
Ash Wilson27d29e22014-10-03 11:57:14 -0400266 }
Ash Wilson1cf4d5f2014-10-07 14:16:18 -0400267 tokenPostErr(t, options, ErrPasswordRequired)
Ash Wilson27d29e22014-10-03 11:57:14 -0400268}