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