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