forked from presentator/presentator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_report_test.go
100 lines (90 loc) · 2.79 KB
/
api_report_test.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
package presentator
import (
"net/http"
"strings"
"testing"
"github.com/pocketbase/pocketbase/tests"
)
func TestReport(t *testing.T) {
t.Parallel()
validDataFunc := func() *strings.Reader {
return strings.NewReader(`{"message": "test"}`)
}
failAfterTestFunc := func(t *testing.T, app *tests.TestApp, res *http.Response) {
if app.TestMailer.TotalSend != 0 {
t.Fatalf("Expected %d sent emails, got %d", 0, app.TestMailer.TotalSend)
}
}
scenarios := []tests.ApiScenario{
{
Name: "guest and valid data",
Method: http.MethodPost,
Url: "/api/pr/report",
Body: validDataFunc(),
TestAppFactory: setupTestApp,
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "user auth and valid data",
Method: http.MethodPost,
Url: "/api/pr/report",
Body: validDataFunc(),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "link auth and invalid data",
Method: http.MethodPost,
Url: "/api/pr/report",
Body: strings.NewReader(`{"message":"` + strings.Repeat("a", 256) + `"}`),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 400,
ExpectedContent: []string{
`"data":{"message":{"code":"validation_length_too_long"`,
},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "link auth and invalid data",
Method: http.MethodPost,
Url: "/api/pr/report",
Body: validDataFunc(),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 204,
AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) {
if app.TestMailer.TotalSend != 1 {
t.Fatalf("Expected %d sent emails, got %d", 1, app.TestMailer.TotalSend)
}
if len(app.TestMailer.LastMessage.To) != 1 ||
app.TestMailer.LastMessage.To[0].Address != app.Settings().Meta.SenderAddress {
t.Fatalf("Expected exactly 1 recipient with email %q, got %v", app.Settings().Meta.SenderAddress, app.TestMailer.LastMessage.To)
}
htmlExpectations := []string{
"Multiple owners project",
strings.TrimRight(app.Settings().Meta.AppUrl, "/") + "/#/test1",
}
for _, content := range htmlExpectations {
if !strings.Contains(app.TestMailer.LastMessage.HTML, content) {
t.Fatalf("Expected to find \n%q\nin\n%s", content, app.TestMailer.LastMessage.HTML)
}
}
},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}