|
| 1 | +package trust |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/rackspace/gophercloud" |
| 9 | + "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
| 10 | + "github.com/rackspace/gophercloud/testhelper" |
| 11 | +) |
| 12 | + |
| 13 | +// authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure. |
| 14 | +func authTokenPost(t *testing.T, options gophercloud.AuthOptions, scope *tokens.Scope, requestJSON string) { |
| 15 | + testhelper.SetupHTTP() |
| 16 | + defer testhelper.TeardownHTTP() |
| 17 | + |
| 18 | + client := gophercloud.ServiceClient{ |
| 19 | + ProviderClient: &gophercloud.ProviderClient{ |
| 20 | + TokenID: "12345abcdef", |
| 21 | + }, |
| 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, `{ |
| 33 | + "token": { |
| 34 | + "expires_at": "2014-10-02T13:45:00.000000Z" |
| 35 | + } |
| 36 | + }`) |
| 37 | + }) |
| 38 | + |
| 39 | + _, err := tokens.Create(&client, AuthOptionsExt{AuthOptions: tokens.AuthOptions{options}, TrustID: "123456"}, scope).Extract() |
| 40 | + if err != nil { |
| 41 | + t.Errorf("Create returned an error: %v", err) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func authTokenPostErr(t *testing.T, options gophercloud.AuthOptions, scope *tokens.Scope, includeToken bool, expectedErr error) { |
| 46 | + testhelper.SetupHTTP() |
| 47 | + defer testhelper.TeardownHTTP() |
| 48 | + |
| 49 | + client := gophercloud.ServiceClient{ |
| 50 | + ProviderClient: &gophercloud.ProviderClient{}, |
| 51 | + Endpoint: testhelper.Endpoint(), |
| 52 | + } |
| 53 | + if includeToken { |
| 54 | + client.TokenID = "abcdef123456" |
| 55 | + } |
| 56 | + |
| 57 | + _, err := tokens.Create(&client, AuthOptionsExt{AuthOptions: tokens.AuthOptions{options}, TrustID: "123456"}, scope).Extract() |
| 58 | + if err == nil { |
| 59 | + t.Errorf("Create did NOT return an error") |
| 60 | + } |
| 61 | + if err != expectedErr { |
| 62 | + t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestTrustIDTokenID(t *testing.T) { |
| 67 | + options := gophercloud.AuthOptions{TokenID: "old_trustee"} |
| 68 | + var scope *tokens.Scope |
| 69 | + authTokenPost(t, options, scope, ` |
| 70 | + { |
| 71 | + "auth": { |
| 72 | + "identity": { |
| 73 | + "methods": [ |
| 74 | + "token" |
| 75 | + ], |
| 76 | + "token": { |
| 77 | + "id": "12345abcdef" |
| 78 | + } |
| 79 | + }, |
| 80 | + "scope": { |
| 81 | + "OS-TRUST:trust": { |
| 82 | + "id": "123456" |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +
|
| 88 | + `) |
| 89 | +} |
| 90 | + |
| 91 | +func TestFailurePassword(t *testing.T) { |
| 92 | + options := gophercloud.AuthOptions{TokenID: "fakeidnopass"} |
| 93 | + //Service Client must have tokenId or password, |
| 94 | + //setting include tokenId to false |
| 95 | + //scope := &Scope{TrustID: "notenough"} |
| 96 | + var scope *tokens.Scope |
| 97 | + authTokenPostErr(t, options, scope, false, tokens.ErrMissingPassword) |
| 98 | +} |
0 commit comments