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