Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
| 3 | package tokens |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/rackspace/gophercloud/openstack/identity/v2/tenants" |
| 12 | th "github.com/rackspace/gophercloud/testhelper" |
| 13 | ) |
| 14 | |
| 15 | // ExpectedToken is the token that should be parsed from TokenCreationResponse. |
| 16 | var ExpectedToken = &Token{ |
| 17 | ID: "aaaabbbbccccdddd", |
| 18 | ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC), |
| 19 | Tenant: tenants.Tenant{ |
| 20 | ID: "fc394f2ab2df4114bde39905f800dc57", |
| 21 | Name: "test", |
| 22 | Description: "There are many tenants. This one is yours.", |
| 23 | Enabled: true, |
| 24 | }, |
| 25 | } |
| 26 | |
| 27 | // ExpectedServiceCatalog is the service catalog that should be parsed from TokenCreationResponse. |
| 28 | var ExpectedServiceCatalog = &ServiceCatalog{ |
| 29 | Entries: []CatalogEntry{ |
| 30 | CatalogEntry{ |
| 31 | Name: "inscrutablewalrus", |
| 32 | Type: "something", |
| 33 | Endpoints: []Endpoint{ |
| 34 | Endpoint{ |
| 35 | PublicURL: "http://something0:1234/v2/", |
| 36 | Region: "region0", |
| 37 | }, |
| 38 | Endpoint{ |
| 39 | PublicURL: "http://something1:1234/v2/", |
| 40 | Region: "region1", |
| 41 | }, |
| 42 | }, |
| 43 | }, |
| 44 | CatalogEntry{ |
| 45 | Name: "arbitrarypenguin", |
| 46 | Type: "else", |
| 47 | Endpoints: []Endpoint{ |
| 48 | Endpoint{ |
| 49 | PublicURL: "http://else0:4321/v3/", |
| 50 | Region: "region0", |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | }, |
| 55 | } |
| 56 | |
| 57 | // TokenCreationResponse is a JSON response that contains ExpectedToken and ExpectedServiceCatalog. |
| 58 | const TokenCreationResponse = ` |
| 59 | { |
| 60 | "access": { |
| 61 | "token": { |
| 62 | "issued_at": "2014-01-30T15:30:58.000000Z", |
| 63 | "expires": "2014-01-31T15:30:58Z", |
| 64 | "id": "aaaabbbbccccdddd", |
| 65 | "tenant": { |
| 66 | "description": "There are many tenants. This one is yours.", |
| 67 | "enabled": true, |
| 68 | "id": "fc394f2ab2df4114bde39905f800dc57", |
| 69 | "name": "test" |
| 70 | } |
| 71 | }, |
| 72 | "serviceCatalog": [ |
| 73 | { |
| 74 | "endpoints": [ |
| 75 | { |
| 76 | "publicURL": "http://something0:1234/v2/", |
| 77 | "region": "region0" |
| 78 | }, |
| 79 | { |
| 80 | "publicURL": "http://something1:1234/v2/", |
| 81 | "region": "region1" |
| 82 | } |
| 83 | ], |
| 84 | "type": "something", |
| 85 | "name": "inscrutablewalrus" |
| 86 | }, |
| 87 | { |
| 88 | "endpoints": [ |
| 89 | { |
| 90 | "publicURL": "http://else0:4321/v3/", |
| 91 | "region": "region0" |
| 92 | } |
| 93 | ], |
| 94 | "type": "else", |
| 95 | "name": "arbitrarypenguin" |
| 96 | } |
| 97 | ] |
| 98 | } |
| 99 | } |
| 100 | ` |
| 101 | |
| 102 | // HandleTokenPost expects a POST against a /tokens handler, ensures that the request body has been |
| 103 | // constructed properly given certain auth options, and returns the result. |
| 104 | func HandleTokenPost(t *testing.T, requestJSON string) { |
| 105 | th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 106 | th.TestMethod(t, r, "POST") |
| 107 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 108 | th.TestHeader(t, r, "Accept", "application/json") |
| 109 | if requestJSON != "" { |
| 110 | th.TestJSONRequest(t, r, requestJSON) |
| 111 | } |
| 112 | |
| 113 | w.WriteHeader(http.StatusOK) |
| 114 | fmt.Fprintf(w, TokenCreationResponse) |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | // IsSuccessful ensures that a CreateResult was successful and contains the correct token and |
| 119 | // service catalog. |
| 120 | func IsSuccessful(t *testing.T, result CreateResult) { |
| 121 | token, err := result.ExtractToken() |
| 122 | th.AssertNoErr(t, err) |
| 123 | th.CheckDeepEquals(t, ExpectedToken, token) |
| 124 | |
| 125 | serviceCatalog, err := result.ExtractServiceCatalog() |
| 126 | th.AssertNoErr(t, err) |
| 127 | th.CheckDeepEquals(t, ExpectedServiceCatalog, serviceCatalog) |
| 128 | } |