blob: 7274dcb7f6234beac72805ca5a51c8d011c92347 [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
38func TestCreateUserIDAndPassword(t *testing.T) {
39 authTokenPost(t, gophercloud.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, `
40 {
41 "auth": {
42 "identity": {
43 "methods": ["password"],
44 "password": {
45 "user": { "id": "me", "password": "squirrel!" }
46 }
47 }
48 }
49 }
50 `)
51}
52
53func TestCreateUsernameDomainIDPassword(t *testing.T) {
54 authTokenPost(t, gophercloud.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, `
55 {
56 "auth": {
57 "identity": {
58 "methods": ["password"],
59 "password": {
60 "user": {
61 "domain": {
62 "id": "abc123"
63 },
64 "name": "fakey",
65 "password": "notpassword"
66 }
67 }
68 }
69 }
70 }
71 `)
72}
Ash Wilsond8da9e42014-08-29 08:01:06 -040073
74func TestCreateUsernameDomainNamePassword(t *testing.T) {
75 authTokenPost(t, gophercloud.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, `
76 {
77 "auth": {
78 "identity": {
79 "methods": ["password"],
80 "password": {
81 "user": {
82 "domain": {
83 "name": "spork.net"
84 },
85 "name": "frank",
86 "password": "swordfish"
87 }
88 }
89 }
90 }
91 }
92 `)
93}
Ash Wilson053fcb02014-08-29 08:04:35 -040094
95func TestCreateTokenID(t *testing.T) {
96 authTokenPost(t, gophercloud.AuthOptions{}, nil, `
97 {
98 "auth": {
99 "identity": {
100 "methods": ["token"],
101 "token": {
102 "id": "12345abcdef"
103 }
104 }
105 }
106 }
107 `)
108}
Ash Wilson1fde6162014-08-29 08:13:06 -0400109
110func TestCreateProjectIDScope(t *testing.T) {
111 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
112 scope := &Scope{ProjectID: "123456"}
113 authTokenPost(t, options, scope, `
114 {
115 "auth": {
116 "identity": {
117 "methods": ["password"],
118 "password": {
119 "user": {
120 "id": "fenris",
121 "password": "g0t0h311"
122 }
123 }
124 },
125 "scope": {
126 "project": {
127 "id": "123456"
128 }
129 }
130 }
131 }
132 `)
133}
134
135func TestCreateDomainIDScope(t *testing.T) {
136 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
137 scope := &Scope{DomainID: "1000"}
138 authTokenPost(t, options, scope, `
139 {
140 "auth": {
141 "identity": {
142 "methods": ["password"],
143 "password": {
144 "user": {
145 "id": "fenris",
146 "password": "g0t0h311"
147 }
148 }
149 },
150 "scope": {
151 "domain": {
152 "id": "1000"
153 }
154 }
155 }
156 }
157 `)
158}
159
160func TestCreateProjectNameAndDomainIDScope(t *testing.T) {
161 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
162 scope := &Scope{ProjectName: "world-domination", DomainID: "1000"}
163 authTokenPost(t, options, scope, `
164 {
165 "auth": {
166 "identity": {
167 "methods": ["password"],
168 "password": {
169 "user": {
170 "id": "fenris",
171 "password": "g0t0h311"
172 }
173 }
174 },
175 "scope": {
176 "project": {
177 "domain": {
178 "id": "1000"
179 },
180 "name": "world-domination"
181 }
182 }
183 }
184 }
185 `)
186}
187
188func TestCreateProjectNameAndDomainNameScope(t *testing.T) {
189 options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"}
190 scope := &Scope{ProjectName: "world-domination", DomainName: "evil-plans"}
191 authTokenPost(t, options, scope, `
192 {
193 "auth": {
194 "identity": {
195 "methods": ["password"],
196 "password": {
197 "user": {
198 "id": "fenris",
199 "password": "g0t0h311"
200 }
201 }
202 },
203 "scope": {
204 "project": {
205 "domain": {
206 "name": "evil-plans"
207 },
208 "name": "world-domination"
209 }
210 }
211 }
212 }
213 `)
214}