Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 4 | "net/http" |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 5 | "testing" |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 6 | ) |
| 7 | |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 8 | const 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 II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 55 | func TestAuthProvider(t *testing.T) { |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 56 | tt := newTransport().WithResponse(SUCCESSFUL_RESPONSE) |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 57 | c := TestContext().UseCustomClient(&http.Client{ |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 58 | Transport: tt, |
| 59 | }) |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 60 | |
| 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 II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 72 | err = c.RegisterProvider("provider", Provider{AuthEndpoint: "/"}) |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 73 | 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 II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 82 | if tt.called != 1 { |
| 83 | t.Error("Expected transport to be called once.") |
| 84 | return |
| 85 | } |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 88 | func TestTenantIdEncoding(t *testing.T) { |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 89 | tt := newTransport().WithResponse(SUCCESSFUL_RESPONSE) |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 90 | c := TestContext(). |
| 91 | UseCustomClient(&http.Client{ |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 92 | Transport: tt, |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 93 | }). |
| 94 | WithProvider("provider", Provider{AuthEndpoint: "/"}) |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 95 | |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 96 | tt.IgnoreTenantId() |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 97 | _, 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 II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 110 | tt.ExpectTenantId() |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 111 | _, 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 | |
| 126 | func TestUserNameAndPassword(t *testing.T) { |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 127 | c := TestContext(). |
| 128 | WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"}). |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 129 | UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}) |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 130 | |
| 131 | credentials := []AuthOptions{ |
Samuel A. Falvo II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 132 | {}, |
| 133 | {Username: "u"}, |
| 134 | {Password: "p"}, |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 135 | } |
Samuel A. Falvo II | 978fed8 | 2013-06-25 17:59:19 -0700 | [diff] [blame] | 136 | for i, auth := range credentials { |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 137 | _, 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 II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 150 | |
Rafael Garcia | e4a550e | 2013-12-06 17:00:32 -0300 | [diff] [blame] | 151 | func TestUserNameAndApiKey(t *testing.T) { |
| 152 | c := TestContext(). |
| 153 | WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"}). |
| 154 | UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}) |
| 155 | |
| 156 | credentials := []AuthOptions{ |
| 157 | {}, |
| 158 | {Username: "u"}, |
| 159 | {ApiKey: "a"}, |
| 160 | } |
| 161 | for i, auth := range credentials { |
| 162 | _, err := c.Authenticate("provider", auth) |
| 163 | if err == nil { |
| 164 | t.Error("Expected error from missing credentials (%d)", i) |
| 165 | return |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | _, err := c.Authenticate("provider", AuthOptions{Username: "u", ApiKey: "a"}) |
| 170 | if err != nil { |
| 171 | t.Error(err) |
| 172 | return |
| 173 | } |
| 174 | } |
| 175 | |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 176 | func TestTokenAcquisition(t *testing.T) { |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 177 | c := TestContext(). |
| 178 | UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}). |
| 179 | WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"}) |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 180 | |
| 181 | acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"}) |
| 182 | if err != nil { |
| 183 | t.Error(err) |
| 184 | return |
| 185 | } |
| 186 | |
| 187 | tok := acc.Token |
| 188 | if (tok.Id == "") || (tok.Expires == "") { |
| 189 | t.Error("Expected a valid token for successful login; got %s, %s", tok.Id, tok.Expires) |
| 190 | return |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestServiceCatalogAcquisition(t *testing.T) { |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 195 | c := TestContext(). |
| 196 | UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}). |
| 197 | WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"}) |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 198 | |
| 199 | acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"}) |
| 200 | if err != nil { |
| 201 | t.Error(err) |
| 202 | return |
| 203 | } |
| 204 | |
| 205 | svcs := acc.ServiceCatalog |
| 206 | if len(svcs) < 2 { |
| 207 | t.Error("Expected 2 service catalog entries; got %d", len(svcs)) |
| 208 | return |
| 209 | } |
| 210 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 211 | types := map[string]bool{ |
| 212 | "compute": true, |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 213 | "rax:database": true, |
| 214 | } |
| 215 | for _, entry := range svcs { |
| 216 | if !types[entry.Type] { |
| 217 | t.Error("Expected to find type %s.", entry.Type) |
| 218 | return |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func TestUserAcquisition(t *testing.T) { |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 224 | c := TestContext(). |
| 225 | UseCustomClient(&http.Client{Transport: newTransport().WithResponse(SUCCESSFUL_RESPONSE)}). |
| 226 | WithProvider("provider", Provider{AuthEndpoint: "http://localhost/"}) |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 227 | |
| 228 | acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"}) |
| 229 | if err != nil { |
| 230 | t.Error(err) |
| 231 | return |
| 232 | } |
| 233 | |
| 234 | u := acc.User |
| 235 | if u.Id != "161418" { |
| 236 | t.Error("Expected user ID of 16148; got", u.Id) |
| 237 | return |
| 238 | } |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 239 | } |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 240 | |
| 241 | func TestAuthenticationNeverReauths(t *testing.T) { |
| 242 | tt := newTransport().WithError(401) |
| 243 | c := TestContext(). |
| 244 | UseCustomClient(&http.Client{Transport: tt}). |
| 245 | WithProvider("provider", Provider{AuthEndpoint: "http://localhost"}) |
| 246 | |
| 247 | _, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"}) |
| 248 | if err == nil { |
| 249 | t.Error("Expected an error from a 401 Unauthorized response") |
| 250 | return |
| 251 | } |
| 252 | |
| 253 | rc, _ := ActualResponseCode(err) |
| 254 | if rc != 401 { |
| 255 | t.Error("Expected a 401 error code") |
| 256 | return |
| 257 | } |
| 258 | |
| 259 | err = tt.VerifyCalls(t, 1) |
| 260 | if err != nil { |
| 261 | // Test object already flagged. |
| 262 | return |
| 263 | } |
| 264 | } |