jrperritt | 0bc5578 | 2016-07-27 13:50:14 -0500 | [diff] [blame] | 1 | package testing |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame] | 7 | "time" |
jrperritt | 0bc5578 | 2016-07-27 13:50:14 -0500 | [diff] [blame] | 8 | |
| 9 | "github.com/gophercloud/gophercloud" |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/trusts" |
jrperritt | 0bc5578 | 2016-07-27 13:50:14 -0500 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens" |
| 12 | "github.com/gophercloud/gophercloud/testhelper" |
| 13 | ) |
| 14 | |
| 15 | // HandleCreateTokenWithTrustID verifies that providing certain AuthOptions and Scope results in an expected JSON structure. |
| 16 | func HandleCreateTokenWithTrustID(t *testing.T, options tokens.AuthOptionsBuilder, requestJSON string) { |
| 17 | testhelper.SetupHTTP() |
| 18 | defer testhelper.TeardownHTTP() |
| 19 | |
| 20 | client := gophercloud.ServiceClient{ |
| 21 | ProviderClient: &gophercloud.ProviderClient{}, |
| 22 | Endpoint: testhelper.Endpoint(), |
| 23 | } |
| 24 | |
| 25 | testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 26 | testhelper.TestMethod(t, r, "POST") |
| 27 | testhelper.TestHeader(t, r, "Content-Type", "application/json") |
| 28 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 29 | testhelper.TestJSONRequest(t, r, requestJSON) |
| 30 | |
| 31 | w.WriteHeader(http.StatusCreated) |
| 32 | fmt.Fprintf(w, `{ |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame] | 33 | "token": { |
| 34 | "expires_at": "2013-02-27T18:30:59.999999Z", |
| 35 | "issued_at": "2013-02-27T16:30:59.999999Z", |
| 36 | "methods": [ |
| 37 | "password" |
| 38 | ], |
| 39 | "OS-TRUST:trust": { |
| 40 | "id": "fe0aef", |
| 41 | "impersonation": false, |
| 42 | "redelegated_trust_id": "3ba234", |
| 43 | "redelegation_count": 2, |
| 44 | "links": { |
| 45 | "self": "http://example.com/identity/v3/trusts/fe0aef" |
| 46 | }, |
| 47 | "trustee_user": { |
| 48 | "id": "0ca8f6", |
| 49 | "links": { |
| 50 | "self": "http://example.com/identity/v3/users/0ca8f6" |
| 51 | } |
| 52 | }, |
| 53 | "trustor_user": { |
| 54 | "id": "bd263c", |
| 55 | "links": { |
| 56 | "self": "http://example.com/identity/v3/users/bd263c" |
| 57 | } |
| 58 | } |
| 59 | }, |
| 60 | "user": { |
| 61 | "domain": { |
| 62 | "id": "1789d1", |
| 63 | "links": { |
| 64 | "self": "http://example.com/identity/v3/domains/1789d1" |
| 65 | }, |
| 66 | "name": "example.com" |
| 67 | }, |
| 68 | "email": "joe@example.com", |
| 69 | "id": "0ca8f6", |
| 70 | "links": { |
| 71 | "self": "http://example.com/identity/v3/users/0ca8f6" |
| 72 | }, |
| 73 | "name": "Joe" |
| 74 | } |
| 75 | } |
| 76 | }`) |
jrperritt | 0bc5578 | 2016-07-27 13:50:14 -0500 | [diff] [blame] | 77 | }) |
| 78 | |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame] | 79 | var actual trusts.TokenExt |
| 80 | err := tokens.Create(&client, options).ExtractInto(&actual) |
jrperritt | 0bc5578 | 2016-07-27 13:50:14 -0500 | [diff] [blame] | 81 | if err != nil { |
| 82 | t.Errorf("Create returned an error: %v", err) |
| 83 | } |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame] | 84 | expected := trusts.TokenExt{ |
| 85 | Token: trusts.Token{ |
| 86 | Token: tokens.Token{ |
| 87 | ExpiresAt: gophercloud.JSONRFC3339Milli(time.Date(2013, 02, 27, 18, 30, 59, 999999000, time.UTC)), |
| 88 | }, |
| 89 | Trust: trusts.Trust{ |
| 90 | ID: "fe0aef", |
| 91 | Impersonation: false, |
| 92 | TrusteeUser: trusts.TrusteeUser{ |
| 93 | ID: "0ca8f6", |
| 94 | }, |
| 95 | TrustorUser: trusts.TrustorUser{ |
| 96 | ID: "bd263c", |
| 97 | }, |
| 98 | RedelegatedTrustID: "3ba234", |
| 99 | RedelegationCount: 2, |
| 100 | }, |
| 101 | }, |
| 102 | } |
| 103 | testhelper.AssertDeepEquals(t, expected, actual) |
jrperritt | 0bc5578 | 2016-07-27 13:50:14 -0500 | [diff] [blame] | 104 | } |