Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 5 | "net/http" |
| 6 | "io/ioutil" |
| 7 | "strings" |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 8 | "encoding/json" |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 11 | |
| 12 | type testTransport struct { |
| 13 | called int |
| 14 | response string |
| 15 | } |
| 16 | |
| 17 | func (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 II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 42 | type tenantIdCheckTransport struct { |
| 43 | expectTenantId bool |
| 44 | tenantIdFound bool |
| 45 | } |
| 46 | |
| 47 | func (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 II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 87 | func TestAuthProvider(t *testing.T) { |
| 88 | c := TestContext() |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 89 | tt := &testTransport{} |
| 90 | c.UseCustomClient(&http.Client{ |
| 91 | Transport: tt, |
| 92 | }) |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 93 | |
| 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 II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 105 | err = c.RegisterProvider("provider", &Provider{AuthEndpoint: "/"}) |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 106 | 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 II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 115 | if tt.called != 1 { |
| 116 | t.Error("Expected transport to be called once.") |
| 117 | return |
| 118 | } |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 121 | func TestTenantIdEncoding(t *testing.T) { |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 122 | c := TestContext() |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 123 | tt := &tenantIdCheckTransport{} |
| 124 | c.UseCustomClient(&http.Client{ |
| 125 | Transport: tt, |
| 126 | }) |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 127 | c.RegisterProvider("provider", &Provider{AuthEndpoint: "/"}) |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 128 | |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 129 | 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 | |
| 159 | func 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 II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 165 | AuthOptions{}, |
| 166 | AuthOptions{Username: "u"}, |
| 167 | AuthOptions{Password: "p"}, |
| 168 | } |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 169 | for i, auth := range credentials { |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 170 | _, 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 | } |