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" |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 13 | thclient "github.com/rackspace/gophercloud/testhelper/client" |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // ExpectedToken is the token that should be parsed from TokenCreationResponse. |
| 17 | var ExpectedToken = &Token{ |
| 18 | ID: "aaaabbbbccccdddd", |
| 19 | ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC), |
| 20 | Tenant: tenants.Tenant{ |
| 21 | ID: "fc394f2ab2df4114bde39905f800dc57", |
| 22 | Name: "test", |
| 23 | Description: "There are many tenants. This one is yours.", |
| 24 | Enabled: true, |
| 25 | }, |
| 26 | } |
| 27 | |
| 28 | // ExpectedServiceCatalog is the service catalog that should be parsed from TokenCreationResponse. |
| 29 | var ExpectedServiceCatalog = &ServiceCatalog{ |
| 30 | Entries: []CatalogEntry{ |
| 31 | CatalogEntry{ |
| 32 | Name: "inscrutablewalrus", |
| 33 | Type: "something", |
| 34 | Endpoints: []Endpoint{ |
| 35 | Endpoint{ |
| 36 | PublicURL: "http://something0:1234/v2/", |
| 37 | Region: "region0", |
| 38 | }, |
| 39 | Endpoint{ |
| 40 | PublicURL: "http://something1:1234/v2/", |
| 41 | Region: "region1", |
| 42 | }, |
| 43 | }, |
| 44 | }, |
| 45 | CatalogEntry{ |
| 46 | Name: "arbitrarypenguin", |
| 47 | Type: "else", |
| 48 | Endpoints: []Endpoint{ |
| 49 | Endpoint{ |
| 50 | PublicURL: "http://else0:4321/v3/", |
| 51 | Region: "region0", |
| 52 | }, |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | } |
| 57 | |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 58 | // ExpectedUser is the token that should be parsed from TokenGetResponse. |
| 59 | var ExpectedUser = &User{ |
| 60 | ID: "a530fefc3d594c4ba2693a4ecd6be74e", |
| 61 | Name: "apiserver", |
| 62 | Roles: []Role{{"member"}, {"service"}}, |
| 63 | UserName: "apiserver", |
| 64 | } |
| 65 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 66 | // TokenCreationResponse is a JSON response that contains ExpectedToken and ExpectedServiceCatalog. |
| 67 | const TokenCreationResponse = ` |
| 68 | { |
| 69 | "access": { |
| 70 | "token": { |
| 71 | "issued_at": "2014-01-30T15:30:58.000000Z", |
| 72 | "expires": "2014-01-31T15:30:58Z", |
| 73 | "id": "aaaabbbbccccdddd", |
| 74 | "tenant": { |
| 75 | "description": "There are many tenants. This one is yours.", |
| 76 | "enabled": true, |
| 77 | "id": "fc394f2ab2df4114bde39905f800dc57", |
| 78 | "name": "test" |
| 79 | } |
| 80 | }, |
| 81 | "serviceCatalog": [ |
| 82 | { |
| 83 | "endpoints": [ |
| 84 | { |
| 85 | "publicURL": "http://something0:1234/v2/", |
| 86 | "region": "region0" |
| 87 | }, |
| 88 | { |
| 89 | "publicURL": "http://something1:1234/v2/", |
| 90 | "region": "region1" |
| 91 | } |
| 92 | ], |
| 93 | "type": "something", |
| 94 | "name": "inscrutablewalrus" |
| 95 | }, |
| 96 | { |
| 97 | "endpoints": [ |
| 98 | { |
| 99 | "publicURL": "http://else0:4321/v3/", |
| 100 | "region": "region0" |
| 101 | } |
| 102 | ], |
| 103 | "type": "else", |
| 104 | "name": "arbitrarypenguin" |
| 105 | } |
| 106 | ] |
| 107 | } |
| 108 | } |
| 109 | ` |
| 110 | |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 111 | // TokenGetResponse is a JSON response that contains ExpectedToken and ExpectedUser. |
| 112 | const TokenGetResponse = ` |
| 113 | { |
| 114 | "access": { |
| 115 | "token": { |
| 116 | "issued_at": "2014-01-30T15:30:58.000000Z", |
| 117 | "expires": "2014-01-31T15:30:58Z", |
| 118 | "id": "aaaabbbbccccdddd", |
| 119 | "tenant": { |
| 120 | "description": "There are many tenants. This one is yours.", |
| 121 | "enabled": true, |
| 122 | "id": "fc394f2ab2df4114bde39905f800dc57", |
| 123 | "name": "test" |
| 124 | } |
| 125 | }, |
| 126 | "serviceCatalog": [], |
| 127 | "user": { |
| 128 | "id": "a530fefc3d594c4ba2693a4ecd6be74e", |
| 129 | "name": "apiserver", |
| 130 | "roles": [ |
| 131 | { |
| 132 | "name": "member" |
| 133 | }, |
| 134 | { |
| 135 | "name": "service" |
| 136 | } |
| 137 | ], |
| 138 | "roles_links": [], |
| 139 | "username": "apiserver" |
| 140 | } |
| 141 | } |
| 142 | }` |
| 143 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 144 | // HandleTokenPost expects a POST against a /tokens handler, ensures that the request body has been |
| 145 | // constructed properly given certain auth options, and returns the result. |
| 146 | func HandleTokenPost(t *testing.T, requestJSON string) { |
| 147 | th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 148 | th.TestMethod(t, r, "POST") |
| 149 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 150 | th.TestHeader(t, r, "Accept", "application/json") |
| 151 | if requestJSON != "" { |
| 152 | th.TestJSONRequest(t, r, requestJSON) |
| 153 | } |
| 154 | |
| 155 | w.WriteHeader(http.StatusOK) |
| 156 | fmt.Fprintf(w, TokenCreationResponse) |
| 157 | }) |
| 158 | } |
| 159 | |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 160 | // HandleTokenGet expects a Get against a /tokens handler, ensures that the request body has been |
| 161 | // constructed properly given certain auth options, and returns the result. |
| 162 | func HandleTokenGet(t *testing.T, token string) { |
| 163 | th.Mux.HandleFunc("/tokens/"+token, func(w http.ResponseWriter, r *http.Request) { |
| 164 | th.TestMethod(t, r, "GET") |
| 165 | th.TestHeader(t, r, "Accept", "application/json") |
| 166 | th.TestHeader(t, r, "X-Auth-Token", thclient.TokenID) |
| 167 | |
| 168 | w.WriteHeader(http.StatusOK) |
| 169 | fmt.Fprintf(w, TokenGetResponse) |
| 170 | }) |
| 171 | } |
| 172 | |
Ash Wilson | c72e362 | 2014-10-10 14:44:19 -0400 | [diff] [blame] | 173 | // IsSuccessful ensures that a CreateResult was successful and contains the correct token and |
| 174 | // service catalog. |
| 175 | func IsSuccessful(t *testing.T, result CreateResult) { |
| 176 | token, err := result.ExtractToken() |
| 177 | th.AssertNoErr(t, err) |
| 178 | th.CheckDeepEquals(t, ExpectedToken, token) |
| 179 | |
| 180 | serviceCatalog, err := result.ExtractServiceCatalog() |
| 181 | th.AssertNoErr(t, err) |
| 182 | th.CheckDeepEquals(t, ExpectedServiceCatalog, serviceCatalog) |
| 183 | } |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 184 | |
| 185 | // GetIsSuccessful ensures that a GetResult was successful and contains the correct token and |
| 186 | // User Info. |
| 187 | func GetIsSuccessful(t *testing.T, result GetResult) { |
| 188 | token, err := result.ExtractToken() |
| 189 | th.AssertNoErr(t, err) |
| 190 | th.CheckDeepEquals(t, ExpectedToken, token) |
| 191 | |
| 192 | user, err := result.ExtractUser() |
| 193 | th.AssertNoErr(t, err) |
| 194 | th.CheckDeepEquals(t, ExpectedUser, user) |
| 195 | } |