jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 4 | "fmt" |
| 5 | "net/http" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/openstack" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | th "github.com/gophercloud/gophercloud/testhelper" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
jrperritt | 93b4a3c | 2016-07-20 20:29:30 -0500 | [diff] [blame^] | 13 | const ID = "0123456789" |
| 14 | |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 15 | func TestAuthenticatedClientV3(t *testing.T) { |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 16 | th.SetupHTTP() |
| 17 | defer th.TeardownHTTP() |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 18 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 19 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 20 | fmt.Fprintf(w, ` |
| 21 | { |
| 22 | "versions": { |
| 23 | "values": [ |
| 24 | { |
| 25 | "status": "stable", |
| 26 | "id": "v3.0", |
| 27 | "links": [ |
| 28 | { "href": "%s", "rel": "self" } |
| 29 | ] |
| 30 | }, |
| 31 | { |
| 32 | "status": "stable", |
| 33 | "id": "v2.0", |
| 34 | "links": [ |
| 35 | { "href": "%s", "rel": "self" } |
| 36 | ] |
| 37 | } |
| 38 | ] |
| 39 | } |
| 40 | } |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 41 | `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 42 | }) |
| 43 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 44 | th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 45 | w.Header().Add("X-Subject-Token", ID) |
| 46 | |
| 47 | w.WriteHeader(http.StatusCreated) |
| 48 | fmt.Fprintf(w, `{ "token": { "expires_at": "2013-02-02T18:30:59.000000Z" } }`) |
| 49 | }) |
| 50 | |
| 51 | options := gophercloud.AuthOptions{ |
| 52 | UserID: "me", |
| 53 | Password: "secret", |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 54 | IdentityEndpoint: th.Endpoint(), |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 55 | } |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 56 | client, err := openstack.AuthenticatedClient(options) |
Ash Wilson | 3431e56 | 2014-10-03 14:18:17 -0400 | [diff] [blame] | 57 | th.AssertNoErr(t, err) |
| 58 | th.CheckEquals(t, ID, client.TokenID) |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 59 | } |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 60 | |
| 61 | func TestAuthenticatedClientV2(t *testing.T) { |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 62 | th.SetupHTTP() |
| 63 | defer th.TeardownHTTP() |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 64 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 65 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 66 | fmt.Fprintf(w, ` |
| 67 | { |
| 68 | "versions": { |
| 69 | "values": [ |
| 70 | { |
| 71 | "status": "experimental", |
| 72 | "id": "v3.0", |
| 73 | "links": [ |
| 74 | { "href": "%s", "rel": "self" } |
| 75 | ] |
| 76 | }, |
| 77 | { |
| 78 | "status": "stable", |
| 79 | "id": "v2.0", |
| 80 | "links": [ |
| 81 | { "href": "%s", "rel": "self" } |
| 82 | ] |
| 83 | } |
| 84 | ] |
| 85 | } |
| 86 | } |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 87 | `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 88 | }) |
| 89 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 90 | th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 91 | fmt.Fprintf(w, ` |
| 92 | { |
| 93 | "access": { |
| 94 | "token": { |
Ash Wilson | 6cc0070 | 2014-10-03 14:00:26 -0400 | [diff] [blame] | 95 | "id": "01234567890", |
| 96 | "expires": "2014-10-01T10:00:00.000000Z" |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 97 | }, |
| 98 | "serviceCatalog": [ |
| 99 | { |
| 100 | "name": "Cloud Servers", |
| 101 | "type": "compute", |
| 102 | "endpoints": [ |
| 103 | { |
| 104 | "tenantId": "t1000", |
| 105 | "publicURL": "https://compute.north.host.com/v1/t1000", |
| 106 | "internalURL": "https://compute.north.internal/v1/t1000", |
| 107 | "region": "North", |
| 108 | "versionId": "1", |
| 109 | "versionInfo": "https://compute.north.host.com/v1/", |
| 110 | "versionList": "https://compute.north.host.com/" |
| 111 | }, |
| 112 | { |
| 113 | "tenantId": "t1000", |
| 114 | "publicURL": "https://compute.north.host.com/v1.1/t1000", |
| 115 | "internalURL": "https://compute.north.internal/v1.1/t1000", |
| 116 | "region": "North", |
| 117 | "versionId": "1.1", |
| 118 | "versionInfo": "https://compute.north.host.com/v1.1/", |
| 119 | "versionList": "https://compute.north.host.com/" |
| 120 | } |
| 121 | ], |
| 122 | "endpoints_links": [] |
| 123 | }, |
| 124 | { |
| 125 | "name": "Cloud Files", |
| 126 | "type": "object-store", |
| 127 | "endpoints": [ |
| 128 | { |
| 129 | "tenantId": "t1000", |
| 130 | "publicURL": "https://storage.north.host.com/v1/t1000", |
| 131 | "internalURL": "https://storage.north.internal/v1/t1000", |
| 132 | "region": "North", |
| 133 | "versionId": "1", |
| 134 | "versionInfo": "https://storage.north.host.com/v1/", |
| 135 | "versionList": "https://storage.north.host.com/" |
| 136 | }, |
| 137 | { |
| 138 | "tenantId": "t1000", |
| 139 | "publicURL": "https://storage.south.host.com/v1/t1000", |
| 140 | "internalURL": "https://storage.south.internal/v1/t1000", |
| 141 | "region": "South", |
| 142 | "versionId": "1", |
| 143 | "versionInfo": "https://storage.south.host.com/v1/", |
| 144 | "versionList": "https://storage.south.host.com/" |
| 145 | } |
| 146 | ] |
| 147 | } |
| 148 | ] |
| 149 | } |
| 150 | } |
| 151 | `) |
| 152 | }) |
| 153 | |
| 154 | options := gophercloud.AuthOptions{ |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 155 | Username: "me", |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 156 | Password: "secret", |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 157 | IdentityEndpoint: th.Endpoint(), |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 158 | } |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 159 | client, err := openstack.AuthenticatedClient(options) |
Ash Wilson | 3431e56 | 2014-10-03 14:18:17 -0400 | [diff] [blame] | 160 | th.AssertNoErr(t, err) |
| 161 | th.CheckEquals(t, "01234567890", client.TokenID) |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 162 | } |
jrperritt | 93b4a3c | 2016-07-20 20:29:30 -0500 | [diff] [blame^] | 163 | |
| 164 | func TestIdentityAdminV3Client(t *testing.T) { |
| 165 | th.SetupHTTP() |
| 166 | defer th.TeardownHTTP() |
| 167 | |
| 168 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 169 | fmt.Fprintf(w, ` |
| 170 | { |
| 171 | "versions": { |
| 172 | "values": [ |
| 173 | { |
| 174 | "status": "stable", |
| 175 | "id": "v3.0", |
| 176 | "links": [ |
| 177 | { "href": "%s", "rel": "self" } |
| 178 | ] |
| 179 | }, |
| 180 | { |
| 181 | "status": "stable", |
| 182 | "id": "v2.0", |
| 183 | "links": [ |
| 184 | { "href": "%s", "rel": "self" } |
| 185 | ] |
| 186 | } |
| 187 | ] |
| 188 | } |
| 189 | } |
| 190 | `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") |
| 191 | }) |
| 192 | |
| 193 | th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 194 | w.Header().Add("X-Subject-Token", ID) |
| 195 | |
| 196 | w.WriteHeader(http.StatusCreated) |
| 197 | fmt.Fprintf(w, ` |
| 198 | { |
| 199 | "token": { |
| 200 | "audit_ids": ["VcxU2JYqT8OzfUVvrjEITQ", "qNUTIJntTzO1-XUk5STybw"], |
| 201 | "catalog": [ |
| 202 | { |
| 203 | "endpoints": [ |
| 204 | { |
| 205 | "id": "39dc322ce86c4111b4f06c2eeae0841b", |
| 206 | "interface": "public", |
| 207 | "region": "RegionOne", |
| 208 | "url": "http://localhost:5000" |
| 209 | }, |
| 210 | { |
| 211 | "id": "ec642f27474842e78bf059f6c48f4e99", |
| 212 | "interface": "internal", |
| 213 | "region": "RegionOne", |
| 214 | "url": "http://localhost:5000" |
| 215 | }, |
| 216 | { |
| 217 | "id": "c609fc430175452290b62a4242e8a7e8", |
| 218 | "interface": "admin", |
| 219 | "region": "RegionOne", |
| 220 | "url": "http://localhost:35357" |
| 221 | } |
| 222 | ], |
| 223 | "id": "4363ae44bdf34a3981fde3b823cb9aa2", |
| 224 | "type": "identity", |
| 225 | "name": "keystone" |
| 226 | } |
| 227 | ], |
| 228 | "expires_at": "2013-02-27T18:30:59.999999Z", |
| 229 | "is_domain": false, |
| 230 | "issued_at": "2013-02-27T16:30:59.999999Z", |
| 231 | "methods": [ |
| 232 | "password" |
| 233 | ], |
| 234 | "project": { |
| 235 | "domain": { |
| 236 | "id": "1789d1", |
| 237 | "name": "example.com" |
| 238 | }, |
| 239 | "id": "263fd9", |
| 240 | "name": "project-x" |
| 241 | }, |
| 242 | "roles": [ |
| 243 | { |
| 244 | "id": "76e72a", |
| 245 | "name": "admin" |
| 246 | }, |
| 247 | { |
| 248 | "id": "f4f392", |
| 249 | "name": "member" |
| 250 | } |
| 251 | ], |
| 252 | "service_providers": [ |
| 253 | { |
| 254 | "auth_url":"https://example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth", |
| 255 | "id": "sp1", |
| 256 | "sp_url": "https://example.com:5000/Shibboleth.sso/SAML2/ECP" |
| 257 | }, |
| 258 | { |
| 259 | "auth_url":"https://other.example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth", |
| 260 | "id": "sp2", |
| 261 | "sp_url": "https://other.example.com:5000/Shibboleth.sso/SAML2/ECP" |
| 262 | } |
| 263 | ], |
| 264 | "user": { |
| 265 | "domain": { |
| 266 | "id": "1789d1", |
| 267 | "name": "example.com" |
| 268 | }, |
| 269 | "id": "0ca8f6", |
| 270 | "name": "Joe", |
| 271 | "password_expires_at": "2016-11-06T15:32:17.000000" |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | `) |
| 276 | }) |
| 277 | |
| 278 | options := gophercloud.AuthOptions{ |
| 279 | Username: "me", |
| 280 | Password: "secret", |
| 281 | DomainID: "12345", |
| 282 | IdentityEndpoint: th.Endpoint(), |
| 283 | } |
| 284 | pc, err := openstack.AuthenticatedClient(options) |
| 285 | th.AssertNoErr(t, err) |
| 286 | sc, err := openstack.NewIdentityV3(pc, gophercloud.EndpointOpts{ |
| 287 | Availability: gophercloud.AvailabilityAdmin, |
| 288 | }) |
| 289 | th.AssertNoErr(t, err) |
| 290 | th.CheckEquals(t, "http://localhost:35357/", sc.Endpoint) |
| 291 | } |