blob: e08a0a6ab6828f4c0c86307736f8038eb0217df5 [file] [log] [blame]
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -07001package gophercloud
2
3import (
Samuel A. Falvo II839428e2013-06-25 18:02:24 -07004 "net/http"
Samuel A. Falvo II839428e2013-06-25 18:02:24 -07005 "testing"
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -07006)
7
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -07008const SUCCESSFUL_RESPONSE = `{
9 "access": {
10 "serviceCatalog": [{
11 "endpoints": [{
12 "publicURL": "https://ord.servers.api.rackspacecloud.com/v2/12345",
13 "region": "ORD",
14 "tenantId": "12345",
15 "versionId": "2",
16 "versionInfo": "https://ord.servers.api.rackspacecloud.com/v2",
17 "versionList": "https://ord.servers.api.rackspacecloud.com/"
18 },{
19 "publicURL": "https://dfw.servers.api.rackspacecloud.com/v2/12345",
20 "region": "DFW",
21 "tenantId": "12345",
22 "versionId": "2",
23 "versionInfo": "https://dfw.servers.api.rackspacecloud.com/v2",
24 "versionList": "https://dfw.servers.api.rackspacecloud.com/"
25 }],
26 "name": "cloudServersOpenStack",
27 "type": "compute"
28 },{
29 "endpoints": [{
30 "publicURL": "https://ord.databases.api.rackspacecloud.com/v1.0/12345",
31 "region": "ORD",
32 "tenantId": "12345"
33 }],
34 "name": "cloudDatabases",
35 "type": "rax:database"
36 }],
37 "token": {
38 "expires": "2012-04-13T13:15:00.000-05:00",
39 "id": "aaaaa-bbbbb-ccccc-dddd"
40 },
41 "user": {
42 "RAX-AUTH:defaultRegion": "DFW",
43 "id": "161418",
44 "name": "demoauthor",
45 "roles": [{
46 "description": "User Admin Role.",
47 "id": "3",
48 "name": "identity:user-admin"
49 }]
50 }
51 }
52}
53`
54
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070055func TestAuthProvider(t *testing.T) {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070056 tt := newTransport()
57 c := TestContext().UseCustomClient(&http.Client{
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070058 Transport: tt,
59 })
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070060
61 _, err := c.Authenticate("", AuthOptions{})
62 if err == nil {
63 t.Error("Expected error for empty provider string")
64 return
65 }
66 _, err = c.Authenticate("unknown-provider", AuthOptions{Username: "u", Password: "p"})
67 if err == nil {
68 t.Error("Expected error for unknown service provider")
69 return
70 }
71
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070072 err = c.RegisterProvider("provider", Provider{AuthEndpoint: "/"})
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070073 if err != nil {
74 t.Error(err)
75 return
76 }
77 _, err = c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
78 if err != nil {
79 t.Error(err)
80 return
81 }
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070082 if tt.called != 1 {
83 t.Error("Expected transport to be called once.")
84 return
85 }
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070086}
87
Samuel A. Falvo II978fed82013-06-25 17:59:19 -070088func TestTenantIdEncoding(t *testing.T) {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070089 tt := newTransport()
90 c := TestContext().
91 UseCustomClient(&http.Client{
Samuel A. Falvo II978fed82013-06-25 17:59:19 -070092 Transport: tt,
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070093 }).
94 WithProvider("provider", Provider{AuthEndpoint: "/"})
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070095
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070096 tt.IgnoreTenantId()
Samuel A. Falvo II978fed82013-06-25 17:59:19 -070097 _, err := c.Authenticate("provider", AuthOptions{
98 Username: "u",
99 Password: "p",
100 })
101 if err != nil {
102 t.Error(err)
103 return
104 }
105 if tt.tenantIdFound {
106 t.Error("Tenant ID should not have been encoded")
107 return
108 }
109
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700110 tt.ExpectTenantId()
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700111 _, err = c.Authenticate("provider", AuthOptions{
112 Username: "u",
113 Password: "p",
114 TenantId: "t",
115 })
116 if err != nil {
117 t.Error(err)
118 return
119 }
120 if !tt.tenantIdFound {
121 t.Error("Tenant ID should have been encoded")
122 return
123 }
124}
125
126func TestUserNameAndPassword(t *testing.T) {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700127 c := TestContext().
128 WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"}).
129 UseCustomClient(&http.Client{Transport: newTransport()})
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700130
131 credentials := []AuthOptions{
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700132 AuthOptions{},
133 AuthOptions{Username: "u"},
134 AuthOptions{Password: "p"},
135 }
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700136 for i, auth := range credentials {
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700137 _, err := c.Authenticate("provider", auth)
138 if err == nil {
139 t.Error("Expected error from missing credentials (%d)", i)
140 return
141 }
142 }
143
144 _, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
145 if err != nil {
146 t.Error(err)
147 return
148 }
149}
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700150
151func TestTokenAcquisition(t *testing.T) {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700152 c := TestContext().
153 UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}).
154 WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"})
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700155
156 acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
157 if err != nil {
158 t.Error(err)
159 return
160 }
161
162 tok := acc.Token
163 if (tok.Id == "") || (tok.Expires == "") {
164 t.Error("Expected a valid token for successful login; got %s, %s", tok.Id, tok.Expires)
165 return
166 }
167}
168
169func TestServiceCatalogAcquisition(t *testing.T) {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700170 c := TestContext().
171 UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}).
172 WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"})
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700173
174 acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
175 if err != nil {
176 t.Error(err)
177 return
178 }
179
180 svcs := acc.ServiceCatalog
181 if len(svcs) < 2 {
182 t.Error("Expected 2 service catalog entries; got %d", len(svcs))
183 return
184 }
185
Samuel A. Falvo II4e895182013-06-26 15:44:18 -0700186 types := map[string]bool{
187 "compute": true,
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700188 "rax:database": true,
189 }
190 for _, entry := range svcs {
191 if !types[entry.Type] {
192 t.Error("Expected to find type %s.", entry.Type)
193 return
194 }
195 }
196}
197
198func TestUserAcquisition(t *testing.T) {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700199 c := TestContext().
200 UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}).
201 WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"})
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700202
203 acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
204 if err != nil {
205 t.Error(err)
206 return
207 }
208
209 u := acc.User
210 if u.Id != "161418" {
211 t.Error("Expected user ID of 16148; got", u.Id)
212 return
213 }
Samuel A. Falvo II4e895182013-06-26 15:44:18 -0700214}
Samuel A. Falvo II2d0f6da2013-07-15 16:41:52 -0700215
216func TestAuthenticationNeverReauths(t *testing.T) {
217 tt := newTransport().WithError(401)
218 c := TestContext().
219 UseCustomClient(&http.Client{Transport: tt}).
220 WithProvider("provider", Provider{AuthEndpoint: "http://localhost"})
221
222 _, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
223 if err == nil {
224 t.Error("Expected an error from a 401 Unauthorized response")
225 return
226 }
227
228 rc, _ := ActualResponseCode(err)
229 if rc != 401 {
230 t.Error("Expected a 401 error code")
231 return
232 }
233
234 err = tt.VerifyCalls(t, 1)
235 if err != nil {
236 // Test object already flagged.
237 return
238 }
239}