blob: 8c41a91976e3b9769c87d04dfa2ee3e67c79c6ff [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonc72e3622014-10-10 14:44:19 -04002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7 "time"
8
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02009 "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 Wilsonc72e3622014-10-10 14:44:19 -040013)
14
15// ExpectedToken is the token that should be parsed from TokenCreationResponse.
jrperritt3d966162016-06-06 14:08:54 -050016var ExpectedToken = &tokens.Token{
Ash Wilsonc72e3622014-10-10 14:44:19 -040017 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.
jrperritt3d966162016-06-06 14:08:54 -050028var ExpectedServiceCatalog = &tokens.ServiceCatalog{
29 Entries: []tokens.CatalogEntry{
30 {
Ash Wilsonc72e3622014-10-10 14:44:19 -040031 Name: "inscrutablewalrus",
32 Type: "something",
jrperritt3d966162016-06-06 14:08:54 -050033 Endpoints: []tokens.Endpoint{
34 {
Ash Wilsonc72e3622014-10-10 14:44:19 -040035 PublicURL: "http://something0:1234/v2/",
36 Region: "region0",
37 },
jrperritt3d966162016-06-06 14:08:54 -050038 {
Ash Wilsonc72e3622014-10-10 14:44:19 -040039 PublicURL: "http://something1:1234/v2/",
40 Region: "region1",
41 },
42 },
43 },
jrperritt3d966162016-06-06 14:08:54 -050044 {
Ash Wilsonc72e3622014-10-10 14:44:19 -040045 Name: "arbitrarypenguin",
46 Type: "else",
jrperritt3d966162016-06-06 14:08:54 -050047 Endpoints: []tokens.Endpoint{
48 {
Ash Wilsonc72e3622014-10-10 14:44:19 -040049 PublicURL: "http://else0:4321/v3/",
50 Region: "region0",
51 },
52 },
53 },
54 },
55}
56
hzlouchao04543602015-11-30 18:44:15 +080057// ExpectedUser is the token that should be parsed from TokenGetResponse.
jrperritt3d966162016-06-06 14:08:54 -050058var ExpectedUser = &tokens.User{
hzlouchao04543602015-11-30 18:44:15 +080059 ID: "a530fefc3d594c4ba2693a4ecd6be74e",
60 Name: "apiserver",
jrperritt3d966162016-06-06 14:08:54 -050061 Roles: []tokens.Role{{"member"}, {"service"}},
hzlouchao04543602015-11-30 18:44:15 +080062 UserName: "apiserver",
63}
64
Ash Wilsonc72e3622014-10-10 14:44:19 -040065// TokenCreationResponse is a JSON response that contains ExpectedToken and ExpectedServiceCatalog.
66const 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
hzlouchao04543602015-11-30 18:44:15 +0800110// TokenGetResponse is a JSON response that contains ExpectedToken and ExpectedUser.
111const 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 },
jrperritt3d966162016-06-06 14:08:54 -0500125 "serviceCatalog": [],
hzlouchao04543602015-11-30 18:44:15 +0800126 "user": {
jrperritt3d966162016-06-06 14:08:54 -0500127 "id": "a530fefc3d594c4ba2693a4ecd6be74e",
128 "name": "apiserver",
hzlouchao04543602015-11-30 18:44:15 +0800129 "roles": [
130 {
131 "name": "member"
jrperritt3d966162016-06-06 14:08:54 -0500132 },
hzlouchao04543602015-11-30 18:44:15 +0800133 {
134 "name": "service"
135 }
jrperritt3d966162016-06-06 14:08:54 -0500136 ],
137 "roles_links": [],
hzlouchao04543602015-11-30 18:44:15 +0800138 "username": "apiserver"
139 }
140 }
141}`
142
Ash Wilsonc72e3622014-10-10 14:44:19 -0400143// 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.
145func 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
hzlouchao04543602015-11-30 18:44:15 +0800159// 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.
161func 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 Wilsonc72e3622014-10-10 14:44:19 -0400172// IsSuccessful ensures that a CreateResult was successful and contains the correct token and
173// service catalog.
jrperritt3d966162016-06-06 14:08:54 -0500174func IsSuccessful(t *testing.T, result tokens.CreateResult) {
Ash Wilsonc72e3622014-10-10 14:44:19 -0400175 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}
hzlouchao04543602015-11-30 18:44:15 +0800183
184// GetIsSuccessful ensures that a GetResult was successful and contains the correct token and
185// User Info.
jrperritt3d966162016-06-06 14:08:54 -0500186func GetIsSuccessful(t *testing.T, result tokens.GetResult) {
hzlouchao04543602015-11-30 18:44:15 +0800187 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}