github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/extensions/oauth1/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/oauth1" 10 tokens "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens/testing" 11 "github.com/gophercloud/gophercloud/testhelper" 12 "github.com/gophercloud/gophercloud/testhelper/client" 13 ) 14 15 const CreateConsumerRequest = ` 16 { 17 "consumer": { 18 "description": "My consumer" 19 } 20 } 21 ` 22 23 const CreateConsumerResponse = ` 24 { 25 "consumer": { 26 "secret": "secretsecret", 27 "description": "My consumer", 28 "id": "7fea2d", 29 "links": { 30 "self": "http://example.com/identity/v3/OS-OAUTH1/consumers/7fea2d" 31 } 32 } 33 } 34 ` 35 36 const UpdateConsumerRequest = ` 37 { 38 "consumer": { 39 "description": "My new consumer" 40 } 41 } 42 ` 43 44 const UpdateConsumerResponse = ` 45 { 46 "consumer": { 47 "description": "My new consumer", 48 "id": "7fea2d", 49 "links": { 50 "self": "http://example.com/identity/v3/OS-OAUTH1/consumers/7fea2d" 51 } 52 } 53 } 54 ` 55 56 // GetConsumerOutput provides a Get result. 57 const GetConsumerResponse = ` 58 { 59 "consumer": { 60 "id": "7fea2d", 61 "description": "My consumer", 62 "links": { 63 "self": "http://example.com/identity/v3/OS-OAUTH1/consumers/7fea2d" 64 } 65 } 66 } 67 ` 68 69 // ListConsumersResponse provides a single page of Consumers results. 70 const ListConsumersResponse = ` 71 { 72 "consumers": [ 73 { 74 "description": "My consumer", 75 "id": "7fea2d", 76 "links": { 77 "self": "http://example.com/identity/v3/OS-OAUTH1/consumers/7fea2d" 78 } 79 }, 80 { 81 "id": "0c2a74", 82 "links": { 83 "self": "http://example.com/identity/v3/OS-OAUTH1/consumers/0c2a74" 84 } 85 } 86 ], 87 "links": { 88 "next": null, 89 "previous": null, 90 "self": "http://example.com/identity/v3/OS-OAUTH1/consumers" 91 } 92 } 93 ` 94 95 const AuthorizeTokenRequest = ` 96 { 97 "roles": [ 98 { 99 "id": "a3b29b" 100 }, 101 { 102 "id": "49993e" 103 } 104 ] 105 } 106 ` 107 108 const AuthorizeTokenResponse = ` 109 { 110 "token": { 111 "oauth_verifier": "8171" 112 } 113 } 114 ` 115 116 const GetUserAccessTokenResponse = ` 117 { 118 "access_token": { 119 "consumer_id": "7fea2d", 120 "id": "6be26a", 121 "expires_at": "2013-09-11T06:07:51.501805Z", 122 "links": { 123 "roles": "http://example.com/identity/v3/users/ce9e07/OS-OAUTH1/access_tokens/6be26a/roles", 124 "self": "http://example.com/identity/v3/users/ce9e07/OS-OAUTH1/access_tokens/6be26a" 125 }, 126 "project_id": "b9fca3", 127 "authorizing_user_id": "ce9e07" 128 } 129 } 130 ` 131 132 const ListUserAccessTokensResponse = ` 133 { 134 "access_tokens": [ 135 { 136 "consumer_id": "7fea2d", 137 "id": "6be26a", 138 "expires_at": "2013-09-11T06:07:51.501805Z", 139 "links": { 140 "roles": "http://example.com/identity/v3/users/ce9e07/OS-OAUTH1/access_tokens/6be26a/roles", 141 "self": "http://example.com/identity/v3/users/ce9e07/OS-OAUTH1/access_tokens/6be26a" 142 }, 143 "project_id": "b9fca3", 144 "authorizing_user_id": "ce9e07" 145 } 146 ], 147 "links": { 148 "next": null, 149 "previous": null, 150 "self": "http://example.com/identity/v3/users/ce9e07/OS-OAUTH1/access_tokens" 151 } 152 } 153 ` 154 155 const ListUserAccessTokenRolesResponse = ` 156 { 157 "roles": [ 158 { 159 "id": "5ad150", 160 "domain_id": "7cf37b", 161 "links": { 162 "self": "http://example.com/identity/v3/roles/5ad150" 163 }, 164 "name": "admin" 165 }, 166 { 167 "id": "a62eb6", 168 "domain_id": "7cf37b", 169 "links": { 170 "self": "http://example.com/identity/v3/roles/a62eb6" 171 }, 172 "name": "member" 173 } 174 ], 175 "links": { 176 "next": null, 177 "previous": null, 178 "self": "http://example.com/identity/v3/users/ce9e07/OS-OAUTH1/access_tokens/6be26a/roles" 179 } 180 } 181 ` 182 183 const ListUserAccessTokenRoleResponse = ` 184 { 185 "role": { 186 "id": "5ad150", 187 "domain_id": "7cf37b", 188 "links": { 189 "self": "http://example.com/identity/v3/roles/5ad150" 190 }, 191 "name": "admin" 192 } 193 } 194 ` 195 196 var tokenExpiresAt = time.Date(2013, time.September, 11, 06, 07, 51, 501805000, time.UTC) 197 var UserAccessToken = oauth1.AccessToken{ 198 ID: "6be26a", 199 ConsumerID: "7fea2d", 200 ProjectID: "b9fca3", 201 AuthorizingUserID: "ce9e07", 202 ExpiresAt: &tokenExpiresAt, 203 } 204 205 var UserAccessTokenRole = oauth1.AccessTokenRole{ 206 ID: "5ad150", 207 DomainID: "7cf37b", 208 Name: "admin", 209 } 210 211 var UserAccessTokenRoleSecond = oauth1.AccessTokenRole{ 212 ID: "a62eb6", 213 DomainID: "7cf37b", 214 Name: "member", 215 } 216 217 var ExpectedUserAccessTokensSlice = []oauth1.AccessToken{UserAccessToken} 218 219 var ExpectedUserAccessTokenRolesSlice = []oauth1.AccessTokenRole{UserAccessTokenRole, UserAccessTokenRoleSecond} 220 221 // HandleCreateConsumer creates an HTTP handler at `/OS-OAUTH1/consumers` on the 222 // test handler mux that tests consumer creation. 223 func HandleCreateConsumer(t *testing.T) { 224 testhelper.Mux.HandleFunc("/OS-OAUTH1/consumers", func(w http.ResponseWriter, r *http.Request) { 225 testhelper.TestMethod(t, r, "POST") 226 testhelper.TestHeader(t, r, "Content-Type", "application/json") 227 testhelper.TestHeader(t, r, "Accept", "application/json") 228 testhelper.TestJSONRequest(t, r, CreateConsumerRequest) 229 230 w.WriteHeader(http.StatusCreated) 231 _, err := fmt.Fprintf(w, CreateConsumerResponse) 232 testhelper.AssertNoErr(t, err) 233 }) 234 } 235 236 // HandleUpdateConsumer creates an HTTP handler at `/OS-OAUTH1/consumers/7fea2d` on the 237 // test handler mux that tests consumer update. 238 func HandleUpdateConsumer(t *testing.T) { 239 testhelper.Mux.HandleFunc("/OS-OAUTH1/consumers/7fea2d", func(w http.ResponseWriter, r *http.Request) { 240 testhelper.TestMethod(t, r, "PATCH") 241 testhelper.TestHeader(t, r, "Content-Type", "application/json") 242 testhelper.TestHeader(t, r, "Accept", "application/json") 243 testhelper.TestJSONRequest(t, r, UpdateConsumerRequest) 244 245 w.WriteHeader(http.StatusOK) 246 _, err := fmt.Fprintf(w, UpdateConsumerResponse) 247 testhelper.AssertNoErr(t, err) 248 }) 249 } 250 251 // HandleDeleteConsumer creates an HTTP handler at `/OS-OAUTH1/consumers/7fea2d` on the 252 // test handler mux that tests consumer deletion. 253 func HandleDeleteConsumer(t *testing.T) { 254 testhelper.Mux.HandleFunc("/OS-OAUTH1/consumers/7fea2d", func(w http.ResponseWriter, r *http.Request) { 255 testhelper.TestMethod(t, r, "DELETE") 256 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 257 258 w.WriteHeader(http.StatusNoContent) 259 }) 260 } 261 262 // HandleGetConsumer creates an HTTP handler at `/OS-OAUTH1/consumers/7fea2d` on the 263 // test handler mux that responds with a single consumer. 264 func HandleGetConsumer(t *testing.T) { 265 testhelper.Mux.HandleFunc("/OS-OAUTH1/consumers/7fea2d", func(w http.ResponseWriter, r *http.Request) { 266 testhelper.TestMethod(t, r, "GET") 267 testhelper.TestHeader(t, r, "Accept", "application/json") 268 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 269 270 w.Header().Set("Content-Type", "application/json") 271 w.WriteHeader(http.StatusOK) 272 fmt.Fprintf(w, GetConsumerResponse) 273 }) 274 } 275 276 var Consumer = oauth1.Consumer{ 277 ID: "7fea2d", 278 Description: "My consumer", 279 Secret: "secretsecret", 280 } 281 282 var UpdatedConsumer = oauth1.Consumer{ 283 ID: "7fea2d", 284 Description: "My new consumer", 285 } 286 287 var FirstConsumer = oauth1.Consumer{ 288 ID: "7fea2d", 289 Description: "My consumer", 290 } 291 292 var SecondConsumer = oauth1.Consumer{ 293 ID: "0c2a74", 294 } 295 296 // ExpectedConsumersSlice is the slice of consumers expected to be returned from ListOutput. 297 var ExpectedConsumersSlice = []oauth1.Consumer{FirstConsumer, SecondConsumer} 298 299 // HandleListConsumers creates an HTTP handler at `/OS-OAUTH1/consumers` on the 300 // test handler mux that responds with a list of two consumers. 301 func HandleListConsumers(t *testing.T) { 302 testhelper.Mux.HandleFunc("/OS-OAUTH1/consumers", func(w http.ResponseWriter, r *http.Request) { 303 testhelper.TestMethod(t, r, "GET") 304 testhelper.TestHeader(t, r, "Accept", "application/json") 305 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 306 307 w.Header().Set("Content-Type", "application/json") 308 w.WriteHeader(http.StatusOK) 309 fmt.Fprintf(w, ListConsumersResponse) 310 }) 311 } 312 313 var Token = oauth1.Token{ 314 OAuthToken: "29971f", 315 OAuthTokenSecret: "238eb8", 316 OAuthExpiresAt: &tokenExpiresAt, 317 } 318 319 // HandleRequestToken creates an HTTP handler at `/OS-OAUTH1/request_token` on the 320 // test handler mux that responds with a OAuth1 unauthorized token. 321 func HandleRequestToken(t *testing.T) { 322 testhelper.Mux.HandleFunc("/OS-OAUTH1/request_token", func(w http.ResponseWriter, r *http.Request) { 323 testhelper.TestMethod(t, r, "POST") 324 testhelper.TestHeader(t, r, "Accept", "application/json") 325 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 326 testhelper.TestHeader(t, r, "Authorization", `OAuth oauth_callback="oob", oauth_consumer_key="7fea2d", oauth_nonce="71416001758914252991586795052", oauth_signature_method="HMAC-SHA1", oauth_timestamp="0", oauth_version="1.0", oauth_signature="jCSPVryCYF52Ks0VNNmBmeKSGuw%3D"`) 327 testhelper.TestHeader(t, r, "Requested-Project-Id", "1df927e8a466498f98788ed73d3c8ab4") 328 testhelper.TestBody(t, r, "") 329 330 w.Header().Set("Content-Type", oauth1.OAuth1TokenContentType) 331 w.WriteHeader(http.StatusCreated) 332 fmt.Fprintf(w, `oauth_token=29971f&oauth_token_secret=238eb8&oauth_expires_at=2013-09-11T06:07:51.501805Z`) 333 }) 334 } 335 336 // HandleAuthorizeToken creates an HTTP handler at `/OS-OAUTH1/authorize/29971f` on the 337 // test handler mux that tests unauthorized token authorization. 338 func HandleAuthorizeToken(t *testing.T) { 339 testhelper.Mux.HandleFunc("/OS-OAUTH1/authorize/29971f", func(w http.ResponseWriter, r *http.Request) { 340 testhelper.TestMethod(t, r, "PUT") 341 testhelper.TestHeader(t, r, "Content-Type", "application/json") 342 testhelper.TestHeader(t, r, "Accept", "application/json") 343 testhelper.TestJSONRequest(t, r, AuthorizeTokenRequest) 344 345 w.WriteHeader(http.StatusOK) 346 _, err := fmt.Fprintf(w, AuthorizeTokenResponse) 347 testhelper.AssertNoErr(t, err) 348 }) 349 } 350 351 var AccessToken = oauth1.Token{ 352 OAuthToken: "accd36", 353 OAuthTokenSecret: "aa47da", 354 OAuthExpiresAt: &tokenExpiresAt, 355 } 356 357 // HandleCreateAccessToken creates an HTTP handler at `/OS-OAUTH1/access_token` on the 358 // test handler mux that responds with a OAuth1 access token. 359 func HandleCreateAccessToken(t *testing.T) { 360 testhelper.Mux.HandleFunc("/OS-OAUTH1/access_token", func(w http.ResponseWriter, r *http.Request) { 361 testhelper.TestMethod(t, r, "POST") 362 testhelper.TestHeader(t, r, "Accept", "application/json") 363 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 364 testhelper.TestHeader(t, r, "Authorization", `OAuth oauth_consumer_key="7fea2d", oauth_nonce="66148873158553341551586804894", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1586804894", oauth_token="29971f", oauth_verifier="8171", oauth_version="1.0", oauth_signature="usQ89Y3IYG0IBE7%2Ft8aVsc8XgEk%3D"`) 365 testhelper.TestBody(t, r, "") 366 367 w.Header().Set("Content-Type", oauth1.OAuth1TokenContentType) 368 w.WriteHeader(http.StatusCreated) 369 fmt.Fprintf(w, `oauth_token=accd36&oauth_token_secret=aa47da&oauth_expires_at=2013-09-11T06:07:51.501805Z`) 370 }) 371 } 372 373 // HandleGetAccessToken creates an HTTP handler at `/users/ce9e07/OS-OAUTH1/access_tokens/6be26a` on the 374 // test handler mux that responds with a single access token. 375 func HandleGetAccessToken(t *testing.T) { 376 testhelper.Mux.HandleFunc("/users/ce9e07/OS-OAUTH1/access_tokens/6be26a", func(w http.ResponseWriter, r *http.Request) { 377 testhelper.TestMethod(t, r, "GET") 378 testhelper.TestHeader(t, r, "Accept", "application/json") 379 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 380 381 w.Header().Set("Content-Type", "application/json") 382 w.WriteHeader(http.StatusOK) 383 fmt.Fprintf(w, GetUserAccessTokenResponse) 384 }) 385 } 386 387 // HandleRevokeAccessToken creates an HTTP handler at `/users/ce9e07/OS-OAUTH1/access_tokens/6be26a` on the 388 // test handler mux that tests access token deletion. 389 func HandleRevokeAccessToken(t *testing.T) { 390 testhelper.Mux.HandleFunc("/users/ce9e07/OS-OAUTH1/access_tokens/6be26a", func(w http.ResponseWriter, r *http.Request) { 391 testhelper.TestMethod(t, r, "DELETE") 392 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 393 394 w.WriteHeader(http.StatusNoContent) 395 }) 396 } 397 398 // HandleListAccessTokens creates an HTTP handler at `/users/ce9e07/OS-OAUTH1/access_tokens` on the 399 // test handler mux that responds with a slice of access tokens. 400 func HandleListAccessTokens(t *testing.T) { 401 testhelper.Mux.HandleFunc("/users/ce9e07/OS-OAUTH1/access_tokens", func(w http.ResponseWriter, r *http.Request) { 402 testhelper.TestMethod(t, r, "GET") 403 testhelper.TestHeader(t, r, "Accept", "application/json") 404 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 405 406 w.Header().Set("Content-Type", "application/json") 407 w.WriteHeader(http.StatusOK) 408 fmt.Fprintf(w, ListUserAccessTokensResponse) 409 }) 410 } 411 412 // HandleListAccessTokenRoles creates an HTTP handler at `/users/ce9e07/OS-OAUTH1/access_tokens/6be26a/roles` on the 413 // test handler mux that responds with a slice of access token roles. 414 func HandleListAccessTokenRoles(t *testing.T) { 415 testhelper.Mux.HandleFunc("/users/ce9e07/OS-OAUTH1/access_tokens/6be26a/roles", func(w http.ResponseWriter, r *http.Request) { 416 testhelper.TestMethod(t, r, "GET") 417 testhelper.TestHeader(t, r, "Accept", "application/json") 418 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 419 420 w.Header().Set("Content-Type", "application/json") 421 w.WriteHeader(http.StatusOK) 422 fmt.Fprintf(w, ListUserAccessTokenRolesResponse) 423 }) 424 } 425 426 // HandleGetAccessTokenRole creates an HTTP handler at `/users/ce9e07/OS-OAUTH1/access_tokens/6be26a/roles/5ad150` on the 427 // test handler mux that responds with an access token role. 428 func HandleGetAccessTokenRole(t *testing.T) { 429 testhelper.Mux.HandleFunc("/users/ce9e07/OS-OAUTH1/access_tokens/6be26a/roles/5ad150", func(w http.ResponseWriter, r *http.Request) { 430 testhelper.TestMethod(t, r, "GET") 431 testhelper.TestHeader(t, r, "Accept", "application/json") 432 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 433 434 w.Header().Set("Content-Type", "application/json") 435 w.WriteHeader(http.StatusOK) 436 fmt.Fprintf(w, ListUserAccessTokenRoleResponse) 437 }) 438 } 439 440 // HandleAuthenticate creates an HTTP handler at `/auth/tokens` on the 441 // test handler mux that responds with an OpenStack token. 442 func HandleAuthenticate(t *testing.T) { 443 testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { 444 testhelper.TestMethod(t, r, "POST") 445 testhelper.TestHeader(t, r, "Content-Type", "application/json") 446 testhelper.TestHeader(t, r, "Accept", "application/json") 447 testhelper.TestHeader(t, r, "Authorization", `OAuth oauth_consumer_key="7fea2d", oauth_nonce="66148873158553341551586804894", oauth_signature_method="HMAC-SHA1", oauth_timestamp="0", oauth_token="accd36", oauth_version="1.0", oauth_signature="JgMHu4e7rXGlqz3A%2FLhHDMvtjp8%3D"`) 448 testhelper.TestJSONRequest(t, r, `{"auth": {"identity": {"oauth1": {}, "methods": ["oauth1"]}}}`) 449 450 w.Header().Set("Content-Type", "application/json") 451 w.WriteHeader(http.StatusCreated) 452 fmt.Fprintf(w, tokens.TokenOutput) 453 }) 454 }