github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/testing/client_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/opentelekomcloud/gophertelekomcloud" 9 "github.com/opentelekomcloud/gophertelekomcloud/openstack" 10 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 11 ) 12 13 const ID = "0123456789" 14 15 func TestAuthenticatedClientV3(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 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 } 41 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") 42 }) 43 44 th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { 45 w.Header().Add("X-Subject-Token", ID) 46 47 w.WriteHeader(http.StatusCreated) 48 _, _ = fmt.Fprint(w, ` 49 { 50 "token": { 51 "expires_at": "2013-02-02T18:30:59.000000Z", 52 "project": { 53 "id": "" 54 } 55 } 56 } 57 `) 58 }) 59 60 options := golangsdk.AuthOptions{ 61 Username: "me", 62 Password: "secret", 63 DomainName: "default", 64 TenantName: "project", 65 IdentityEndpoint: th.Endpoint(), 66 } 67 client, err := openstack.AuthenticatedClient(options) 68 th.AssertNoErr(t, err) 69 th.CheckEquals(t, ID, client.TokenID) 70 } 71 72 func TestIdentityAdminV3Client(t *testing.T) { 73 th.SetupHTTP() 74 defer th.TeardownHTTP() 75 76 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 77 _, _ = fmt.Fprintf(w, ` 78 { 79 "versions": { 80 "values": [ 81 { 82 "status": "stable", 83 "id": "v3.0", 84 "links": [ 85 { "href": "%s", "rel": "self" } 86 ] 87 }, 88 { 89 "status": "stable", 90 "id": "v2.0", 91 "links": [ 92 { "href": "%s", "rel": "self" } 93 ] 94 } 95 ] 96 } 97 } 98 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") 99 }) 100 101 th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { 102 w.Header().Add("X-Subject-Token", ID) 103 104 w.WriteHeader(http.StatusCreated) 105 _, _ = fmt.Fprint(w, ` 106 { 107 "token": { 108 "audit_ids": ["VcxU2JYqT8OzfUVvrjEITQ", "qNUTIJntTzO1-XUk5STybw"], 109 "catalog": [ 110 { 111 "endpoints": [ 112 { 113 "id": "39dc322ce86c4111b4f06c2eeae0841b", 114 "interface": "public", 115 "region": "RegionOne", 116 "url": "http://localhost:5000" 117 }, 118 { 119 "id": "ec642f27474842e78bf059f6c48f4e99", 120 "interface": "internal", 121 "region": "RegionOne", 122 "url": "http://localhost:5000" 123 }, 124 { 125 "id": "c609fc430175452290b62a4242e8a7e8", 126 "interface": "admin", 127 "region": "RegionOne", 128 "url": "http://localhost:35357" 129 } 130 ], 131 "id": "4363ae44bdf34a3981fde3b823cb9aa2", 132 "type": "identity", 133 "name": "keystone" 134 } 135 ], 136 "expires_at": "2013-02-27T18:30:59.999999Z", 137 "is_domain": false, 138 "issued_at": "2013-02-27T16:30:59.999999Z", 139 "methods": [ 140 "password" 141 ], 142 "project": { 143 "domain": { 144 "id": "1789d1", 145 "name": "example.com" 146 }, 147 "id": "263fd9", 148 "name": "project-x" 149 }, 150 "roles": [ 151 { 152 "id": "76e72a", 153 "name": "admin" 154 }, 155 { 156 "id": "f4f392", 157 "name": "member" 158 } 159 ], 160 "service_providers": [ 161 { 162 "auth_url":"https://example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth", 163 "id": "sp1", 164 "sp_url": "https://example.com:5000/Shibboleth.sso/SAML2/ECP" 165 }, 166 { 167 "auth_url":"https://other.example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth", 168 "id": "sp2", 169 "sp_url": "https://other.example.com:5000/Shibboleth.sso/SAML2/ECP" 170 } 171 ], 172 "user": { 173 "domain": { 174 "id": "1789d1", 175 "name": "example.com" 176 }, 177 "id": "0ca8f6", 178 "name": "Joe", 179 "password_expires_at": "2016-11-06T15:32:17.000000" 180 } 181 } 182 } 183 `) 184 }) 185 186 options := golangsdk.AuthOptions{ 187 Username: "me", 188 Password: "secret", 189 DomainID: "12345", 190 IdentityEndpoint: th.Endpoint(), 191 } 192 pc, err := openstack.AuthenticatedClient(options) 193 th.AssertNoErr(t, err) 194 sc, err := openstack.NewIdentityV3(pc, golangsdk.EndpointOpts{ 195 Region: "RegionOne", 196 Availability: golangsdk.AvailabilityAdmin, 197 }) 198 th.AssertNoErr(t, err) 199 th.CheckEquals(t, "http://localhost:35357/v3/", sc.Endpoint) 200 } 201 202 func testAuthenticatedClientFails(t *testing.T, endpoint string) { 203 options := golangsdk.AuthOptions{ 204 Username: "me", 205 Password: "secret", 206 DomainName: "default", 207 TenantName: "project", 208 IdentityEndpoint: endpoint, 209 } 210 _, err := openstack.AuthenticatedClient(options) 211 if err == nil { 212 t.Fatal("expected error but call succeeded") 213 } 214 } 215 216 func TestAuthenticatedClientV3Fails(t *testing.T) { 217 testAuthenticatedClientFails(t, "http://bad-address.example.com/v3") 218 } 219 220 func TestAuthenticatedClientV2Fails(t *testing.T) { 221 testAuthenticatedClientFails(t, "http://bad-address.example.com/v2.0") 222 }