blob: e1c391d774fe03b8422113c17951f4a7a8ebf4d4 [file] [log] [blame]
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -07001package gophercloud
2
3import (
4 "testing"
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -07005 "net/http"
6 "io/ioutil"
7 "strings"
Samuel A. Falvo II978fed82013-06-25 17:59:19 -07008 "encoding/json"
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -07009)
10
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070011
12type testTransport struct {
13 called int
14 response string
15}
16
17func (t *testTransport) RoundTrip(req *http.Request) (rsp *http.Response, err error) {
18 t.called++;
19
20 headers := make(http.Header)
21 headers.Add("Content-Type", "application/xml; charset=UTF-8")
22
23 body := ioutil.NopCloser(strings.NewReader(t.response))
24
25 rsp = &http.Response{
26 Status: "200 OK",
27 StatusCode: 200,
28 Proto: "HTTP/1.1",
29 ProtoMajor: 1,
30 ProtoMinor: 1,
31 Header: headers,
32 Body: body,
33 ContentLength: -1,
34 TransferEncoding: nil,
35 Close: true,
36 Trailer: nil,
37 Request: req,
38 }
39 return
40}
41
Samuel A. Falvo II978fed82013-06-25 17:59:19 -070042type tenantIdCheckTransport struct {
43 expectTenantId bool
44 tenantIdFound bool
45}
46
47func (t *tenantIdCheckTransport) RoundTrip(req *http.Request) (rsp *http.Response, err error) {
48 var authContainer *AuthContainer
49
50 headers := make(http.Header)
51 headers.Add("Content-Type", "application/xml; charset=UTF-8")
52
53 body := ioutil.NopCloser(strings.NewReader("t.response"))
54
55 rsp = &http.Response{
56 Status: "200 OK",
57 StatusCode: 200,
58 Proto: "HTTP/1.1",
59 ProtoMajor: 1,
60 ProtoMinor: 1,
61 Header: headers,
62 Body: body,
63 ContentLength: -1,
64 TransferEncoding: nil,
65 Close: true,
66 Trailer: nil,
67 Request: req,
68 }
69
70 bytes, err := ioutil.ReadAll(req.Body)
71 if err != nil {
72 return nil, err
73 }
74 err = json.Unmarshal(bytes, &authContainer)
75 if err != nil {
76 return nil, err
77 }
78 t.tenantIdFound = (authContainer.Auth.TenantId != "")
79
80 if t.tenantIdFound != t.expectTenantId {
81 rsp.Status = "500 Internal Server Error"
82 rsp.StatusCode = 500
83 }
84 return
85}
86
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070087func TestAuthProvider(t *testing.T) {
88 c := TestContext()
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070089 tt := &testTransport{}
90 c.UseCustomClient(&http.Client{
91 Transport: tt,
92 })
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070093
94 _, err := c.Authenticate("", AuthOptions{})
95 if err == nil {
96 t.Error("Expected error for empty provider string")
97 return
98 }
99 _, err = c.Authenticate("unknown-provider", AuthOptions{Username: "u", Password: "p"})
100 if err == nil {
101 t.Error("Expected error for unknown service provider")
102 return
103 }
104
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700105 err = c.RegisterProvider("provider", &Provider{AuthEndpoint: "/"})
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700106 if err != nil {
107 t.Error(err)
108 return
109 }
110 _, err = c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
111 if err != nil {
112 t.Error(err)
113 return
114 }
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700115 if tt.called != 1 {
116 t.Error("Expected transport to be called once.")
117 return
118 }
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700119}
120
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700121func TestTenantIdEncoding(t *testing.T) {
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700122 c := TestContext()
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700123 tt := &tenantIdCheckTransport{}
124 c.UseCustomClient(&http.Client{
125 Transport: tt,
126 })
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700127 c.RegisterProvider("provider", &Provider{AuthEndpoint: "/"})
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700128
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700129 tt.expectTenantId = false
130 _, err := c.Authenticate("provider", AuthOptions{
131 Username: "u",
132 Password: "p",
133 })
134 if err != nil {
135 t.Error(err)
136 return
137 }
138 if tt.tenantIdFound {
139 t.Error("Tenant ID should not have been encoded")
140 return
141 }
142
143 tt.expectTenantId = true
144 _, err = c.Authenticate("provider", AuthOptions{
145 Username: "u",
146 Password: "p",
147 TenantId: "t",
148 })
149 if err != nil {
150 t.Error(err)
151 return
152 }
153 if !tt.tenantIdFound {
154 t.Error("Tenant ID should have been encoded")
155 return
156 }
157}
158
159func TestUserNameAndPassword(t *testing.T) {
160 c := TestContext()
161 c.UseCustomClient(&http.Client{Transport: &testTransport{}})
162 c.RegisterProvider("provider", &Provider{AuthEndpoint: "http://localhost/"})
163
164 credentials := []AuthOptions{
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700165 AuthOptions{},
166 AuthOptions{Username: "u"},
167 AuthOptions{Password: "p"},
168 }
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700169 for i, auth := range credentials {
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700170 _, err := c.Authenticate("provider", auth)
171 if err == nil {
172 t.Error("Expected error from missing credentials (%d)", i)
173 return
174 }
175 }
176
177 _, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
178 if err != nil {
179 t.Error(err)
180 return
181 }
182}