forked from presentator/presentator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_share_test.go
127 lines (118 loc) · 3.67 KB
/
api_share_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
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
package presentator
import (
"net/http"
"strings"
"testing"
"github.com/pocketbase/pocketbase/tests"
)
func TestLinkShare(t *testing.T) {
t.Parallel()
validDataFunc := func() *strings.Reader {
return strings.NewReader(`{
"message": "test",
"emails": ["[email protected]", "[email protected]"]
}`)
}
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 with existing link and valid data",
Method: http.MethodPost,
Url: "/api/pr/share/5kgzv7br5487j0d",
Body: validDataFunc(),
TestAppFactory: setupTestApp,
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "link auth with existing link and valid data",
Method: http.MethodPost,
Url: "/api/pr/share/5kgzv7br5487j0d",
Body: validDataFunc(),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "non-owner user auth with existing link and valid data",
Method: http.MethodPost,
Url: "/api/pr/share/5kgzv7br5487j0d",
Body: validDataFunc(),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test4"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "owner user auth with existing link and empty data",
Method: http.MethodPost,
Url: "/api/pr/share/5kgzv7br5487j0d",
Body: strings.NewReader("{}"),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test2"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 400,
ExpectedContent: []string{
`"emails":{"code":"validation_required"`,
},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "owner user auth with existing link and invalid data",
Method: http.MethodPost,
Url: "/api/pr/share/5kgzv7br5487j0d",
Body: strings.NewReader(`{"message": "` + strings.Repeat("a", 256) + `", "emails":["[email protected]","invalid"]}`),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test2"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 400,
ExpectedContent: []string{
`"emails":{"1":{"code":"validation_is_email"`,
`"message":{"code":"validation_length_too_long"`,
},
AfterTestFunc: failAfterTestFunc,
},
{
Name: "owner user auth with existing link and valid data",
Method: http.MethodPost,
Url: "/api/pr/share/5kgzv7br5487j0d",
Body: validDataFunc(),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test2"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 204,
AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) {
if app.TestMailer.TotalSend != 2 {
t.Fatalf("Expected %d sent emails, got %d", 2, app.TestMailer.TotalSend)
}
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)
}
}