/
testcase.go
360 lines (301 loc) · 8.25 KB
/
testcase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package testrunner
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"strings"
"testing"
texttemplate "text/template"
"github.com/Masterminds/sprig"
authflowclient "github.com/authgear/authgear-server/e2e/pkg/e2eclient"
"github.com/authgear/authgear-server/pkg/util/httputil"
)
var _ = TestCaseSchema.Add("TestCase", `
{
"type": "object",
"properties": {
"name": { "type": "string" },
"focus": { "type": "boolean" },
"authgear.yaml": { "$ref": "#/$defs/AuthgearYAMLSource" },
"steps": { "type": "array", "items": { "$ref": "#/$defs/Step" } },
"before": { "type": "array", "items": { "$ref": "#/$defs/BeforeHook" } }
},
"required": ["name", "steps"]
}
`)
type TestCase struct {
Name string `json:"name"`
Path string `json:"path"`
// Applying focus to a test case will make it the only test case to run,
// mainly used for debugging new test cases.
Focus bool `json:"focus"`
AuthgearYAMLSource AuthgearYAMLSource `json:"authgear.yaml"`
Steps []Step `json:"steps"`
Before []BeforeHook `json:"before"`
}
func (tc *TestCase) FullName() string {
return tc.Path + "/" + tc.Name
}
func (tc *TestCase) Run(t *testing.T) {
ctx := context.Background()
appID := generateAppID()
cmd := &End2EndCmd{
AppID: appID,
TestCase: *tc,
}
// Create project per test case
err := cmd.CreateConfigSource()
if err != nil {
t.Errorf("failed to create config source: %v", err)
return
}
ok := tc.executeBeforeAll(t, cmd)
if !ok {
return
}
client := authflowclient.NewClient(
ctx,
"localhost:4000",
httputil.HTTPHost(fmt.Sprintf("%s.portal.localhost:4000", appID)),
)
var stepResults []StepResult
var state string
for i, step := range tc.Steps {
if step.Name == "" {
step.Name = fmt.Sprintf("step %d", i+1)
}
var result *StepResult
result, state, ok = tc.executeStep(t, cmd, client, stepResults, state, step)
if !ok {
return
}
stepResults = append(stepResults, *result)
}
}
// Execute before hooks to prepare fixtures
func (tc *TestCase) executeBeforeAll(t *testing.T, cmd *End2EndCmd) (ok bool) {
for _, beforeHook := range tc.Before {
switch beforeHook.Type {
case BeforeHookTypeUserImport:
err := cmd.ImportUsers(beforeHook.UserImport)
if err != nil {
t.Errorf("failed to import users: %v", err)
return false
}
case BeforeHookTypeCustomSQL:
err := cmd.ExecuteCustomSQL(beforeHook.CustomSQL.Path)
if err != nil {
t.Errorf("failed to execute custom SQL: %v", err)
return false
}
default:
t.Errorf("unknown before hook type: %s", beforeHook.Type)
return false
}
}
return true
}
func (tc *TestCase) executeStep(
t *testing.T,
cmd *End2EndCmd,
client *authflowclient.Client,
prevSteps []StepResult,
state string,
step Step,
) (result *StepResult, nextState string, ok bool) {
var flowResponse *authflowclient.FlowResponse
var flowErr error
switch step.Action {
case StepActionCreate:
var flowReference authflowclient.FlowReference
err := json.Unmarshal([]byte(step.Input), &flowReference)
if err != nil {
t.Errorf("failed to parse input in '%s': %v\n", step.Name, err)
return
}
flowResponse, flowErr = client.Create(flowReference, "")
if step.Output != nil {
ok := validateOutput(t, step, flowResponse, flowErr)
if !ok {
return nil, state, false
}
}
nextState = state
if flowResponse != nil {
nextState = flowResponse.StateToken
}
result = &StepResult{
Result: flowResponse,
Error: flowErr,
}
case StepActionOAuthRedirect:
var lastStep *StepResult
if len(prevSteps) != 0 {
lastStep = &prevSteps[len(prevSteps)-1]
}
var parsedTo string
parsedTo, ok = prepareTo(t, cmd, lastStep, step.To)
if !ok {
return nil, state, false
}
finalURL, err := client.OAuthRedirect(parsedTo, step.RedirectURI)
if err != nil {
t.Errorf("failed to follow OAuth redirect in '%s': %v\n", step.Name, err)
return
}
finalURLParsed, err := url.Parse(finalURL)
if err != nil {
t.Errorf("failed to parse final URL in '%s': %v\n", step.Name, err)
return
}
finalURLQueryMap := make(map[string]string)
for key, values := range finalURLParsed.Query() {
if len(values) > 0 {
finalURLQueryMap[key] = values[0]
}
}
nextState = state
result = &StepResult{
Result: finalURLQueryMap,
Error: nil,
}
case StepActionInput:
fallthrough
case "":
if len(prevSteps) == 0 {
t.Errorf("no previous step result in '%s'", step.Name)
return
}
lastStep := prevSteps[len(prevSteps)-1]
input, ok := prepareInput(t, cmd, &lastStep, step.Input)
if !ok {
return nil, state, false
}
flowResponse, flowErr = client.Input(nil, nil, state, input)
if step.Output != nil {
ok := validateOutput(t, step, flowResponse, flowErr)
if !ok {
return nil, state, false
}
}
nextState = state
if flowResponse != nil {
nextState = flowResponse.StateToken
}
result = &StepResult{
Result: flowResponse,
Error: flowErr,
}
default:
t.Errorf("unknown action in '%s': %s", step.Name, step.Action)
return nil, state, false
}
return result, nextState, true
}
func prepareInput(t *testing.T, cmd *End2EndCmd, prev *StepResult, input string) (prepared map[string]interface{}, ok bool) {
parsedInput, err := execTemplate(cmd, prev, input)
if err != nil {
t.Errorf("failed to parse input: %v\n", err)
return nil, false
}
var inputMap map[string]interface{}
err = json.Unmarshal([]byte(parsedInput), &inputMap)
if err != nil {
t.Errorf("failed to parse input: %v\n", err)
return nil, false
}
return inputMap, true
}
func prepareTo(t *testing.T, cmd *End2EndCmd, prev *StepResult, to string) (prepared string, ok bool) {
parsedTo, err := execTemplate(cmd, prev, to)
if err != nil {
t.Errorf("failed to parse to: %v\n", err)
return "", false
}
return parsedTo, true
}
func execTemplate(cmd *End2EndCmd, prev *StepResult, content string) (string, error) {
tmpl := texttemplate.New("")
tmpl.Funcs(makeTemplateFuncMap(cmd))
_, err := tmpl.Parse(content)
if err != nil {
return "", err
}
data := make(map[string]interface{})
// Add prev result to data
data["prev"], err = toMap(prev)
if err != nil {
return "", err
}
var buf strings.Builder
err = tmpl.Execute(&buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}
func makeTemplateFuncMap(cmd *End2EndCmd) texttemplate.FuncMap {
templateFuncMap := sprig.HermeticHtmlFuncMap()
templateFuncMap["linkOTPCode"] = func(claimName string, claimValue string) string {
otpCode, err := cmd.GetLinkOTPCodeByClaim(claimName, claimValue)
if err != nil {
panic(err)
}
return otpCode
}
return templateFuncMap
}
func validateOutput(t *testing.T, step Step, flowResponse *authflowclient.FlowResponse, flowErr error) (ok bool) {
flowResponseJson, _ := json.MarshalIndent(flowResponse, "", " ")
flowErrJson, _ := json.MarshalIndent(flowErr, "", " ")
errorViolations, resultViolations, err := MatchOutput(*step.Output, flowResponse, flowErr)
if err != nil {
t.Errorf("failed to match output in '%s': %v\n", step.Name, err)
t.Errorf(" result: %s\n", flowResponseJson)
t.Errorf(" error: %s\n", flowErrJson)
return false
}
if len(errorViolations) > 0 {
t.Errorf("error output mismatch in '%s':\n", step.Name)
for _, violation := range errorViolations {
t.Errorf(" | %s: %s. Expected %s, got %s", violation.Path, violation.Message, violation.Expected, violation.Actual)
}
t.Errorf(" error: %s\n", flowErrJson)
return false
}
if len(resultViolations) > 0 {
t.Errorf("result output mismatch in '%s':\n", step.Name)
for _, violation := range resultViolations {
t.Errorf(" | %s: %s. Expected %s, got %s", violation.Path, violation.Message, violation.Expected, violation.Actual)
}
t.Errorf(" result: %s\n", flowResponseJson)
return false
}
return true
}
func generateAppID() string {
id := make([]byte, 16)
_, err := rand.Read(id)
if err != nil {
panic(err)
}
return hex.EncodeToString(id)
}
func toMap(data interface{}) (map[string]interface{}, error) {
if data == nil {
return nil, nil
}
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
}
var mapData map[string]interface{}
err = json.Unmarshal(jsonData, &mapData)
if err != nil {
panic(err)
}
return mapData, nil
}