blob: 70aa5ff82f89bdbd4177b86581be11ec666f2770 [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"
9
10 _ "fmt"
11 _ "os"
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070012)
13
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070014
15type testTransport struct {
16 called int
17 response string
18}
19
20func (t *testTransport) RoundTrip(req *http.Request) (rsp *http.Response, err error) {
21 t.called++;
22
23 headers := make(http.Header)
24 headers.Add("Content-Type", "application/xml; charset=UTF-8")
25
26 body := ioutil.NopCloser(strings.NewReader(t.response))
27
28 rsp = &http.Response{
29 Status: "200 OK",
30 StatusCode: 200,
31 Proto: "HTTP/1.1",
32 ProtoMajor: 1,
33 ProtoMinor: 1,
34 Header: headers,
35 Body: body,
36 ContentLength: -1,
37 TransferEncoding: nil,
38 Close: true,
39 Trailer: nil,
40 Request: req,
41 }
42 return
43}
44
Samuel A. Falvo II978fed82013-06-25 17:59:19 -070045type tenantIdCheckTransport struct {
46 expectTenantId bool
47 tenantIdFound bool
48}
49
50func (t *tenantIdCheckTransport) RoundTrip(req *http.Request) (rsp *http.Response, err error) {
51 var authContainer *AuthContainer
52
53 headers := make(http.Header)
54 headers.Add("Content-Type", "application/xml; charset=UTF-8")
55
56 body := ioutil.NopCloser(strings.NewReader("t.response"))
57
58 rsp = &http.Response{
59 Status: "200 OK",
60 StatusCode: 200,
61 Proto: "HTTP/1.1",
62 ProtoMajor: 1,
63 ProtoMinor: 1,
64 Header: headers,
65 Body: body,
66 ContentLength: -1,
67 TransferEncoding: nil,
68 Close: true,
69 Trailer: nil,
70 Request: req,
71 }
72
73 bytes, err := ioutil.ReadAll(req.Body)
74 if err != nil {
75 return nil, err
76 }
77 err = json.Unmarshal(bytes, &authContainer)
78 if err != nil {
79 return nil, err
80 }
81 t.tenantIdFound = (authContainer.Auth.TenantId != "")
82
83 if t.tenantIdFound != t.expectTenantId {
84 rsp.Status = "500 Internal Server Error"
85 rsp.StatusCode = 500
86 }
87 return
88}
89
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070090func TestAuthProvider(t *testing.T) {
91 c := TestContext()
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070092 tt := &testTransport{}
93 c.UseCustomClient(&http.Client{
94 Transport: tt,
95 })
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070096
97 _, err := c.Authenticate("", AuthOptions{})
98 if err == nil {
99 t.Error("Expected error for empty provider string")
100 return
101 }
102 _, err = c.Authenticate("unknown-provider", AuthOptions{Username: "u", Password: "p"})
103 if err == nil {
104 t.Error("Expected error for unknown service provider")
105 return
106 }
107
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700108 err = c.RegisterProvider("provider", &Provider{AuthEndpoint: "/"})
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700109 if err != nil {
110 t.Error(err)
111 return
112 }
113 _, err = c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
114 if err != nil {
115 t.Error(err)
116 return
117 }
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700118 if tt.called != 1 {
119 t.Error("Expected transport to be called once.")
120 return
121 }
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700122}
123
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700124func TestTenantIdEncoding(t *testing.T) {
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700125 c := TestContext()
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700126 tt := &tenantIdCheckTransport{}
127 c.UseCustomClient(&http.Client{
128 Transport: tt,
129 })
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700130 c.RegisterProvider("provider", &Provider{AuthEndpoint: "/"})
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700131
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700132 tt.expectTenantId = false
133 _, err := c.Authenticate("provider", AuthOptions{
134 Username: "u",
135 Password: "p",
136 })
137 if err != nil {
138 t.Error(err)
139 return
140 }
141 if tt.tenantIdFound {
142 t.Error("Tenant ID should not have been encoded")
143 return
144 }
145
146 tt.expectTenantId = true
147 _, err = c.Authenticate("provider", AuthOptions{
148 Username: "u",
149 Password: "p",
150 TenantId: "t",
151 })
152 if err != nil {
153 t.Error(err)
154 return
155 }
156 if !tt.tenantIdFound {
157 t.Error("Tenant ID should have been encoded")
158 return
159 }
160}
161
162func TestUserNameAndPassword(t *testing.T) {
163 c := TestContext()
164 c.UseCustomClient(&http.Client{Transport: &testTransport{}})
165 c.RegisterProvider("provider", &Provider{AuthEndpoint: "http://localhost/"})
166
167 credentials := []AuthOptions{
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700168 AuthOptions{},
169 AuthOptions{Username: "u"},
170 AuthOptions{Password: "p"},
171 }
Samuel A. Falvo II978fed82013-06-25 17:59:19 -0700172 for i, auth := range credentials {
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700173 _, err := c.Authenticate("provider", auth)
174 if err == nil {
175 t.Error("Expected error from missing credentials (%d)", i)
176 return
177 }
178 }
179
180 _, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
181 if err != nil {
182 t.Error(err)
183 return
184 }
185}