oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1package webtest
2
3import (
4 "context"
5 "net/url"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/base/internal/store"
10)
11
12func (a *testApp) signedInOwner() {
13 a.t.Helper()
14 if r := a.signIn(ownerAddress); r.Code != 303 {
15 a.t.Fatalf("owner sign-in: %d", r.Code)
16 }
17}
18
19func invite(email string) url.Values { return url.Values{"email": {email}} }
20
21func TestOwnerInvites(t *testing.T) {
22 a := newApp(t)
23 a.signedInOwner()
24 before := len(a.mailbox())
25 if r := a.post("/people", invite("maria@acme.co")); r.Code != 303 || r.Location() != "/people" {
26 t.Fatalf("invite: %d %q", r.Code, r.Location())
27 }
28 page := a.get("/people")
29 if page.Code != 200 {
30 t.Fatalf("people: %d", page.Code)
31 }
32 for _, want := range []string{"maria@acme.co", "Invited. Hasn't signed in yet."} {
33 if !strings.Contains(page.Body, want) && !strings.Contains(page.Body, strings.ReplaceAll(want, "'", "'")) {
34 t.Errorf("page lacks %q:\n%s", want, page.Body)
35 }
36 }
37 box := a.mailbox()
38 if len(box) != before+1 {
39 t.Fatalf("want one invitation, mailbox grew by %d", len(box)-before)
40 }
41 m := box[len(box)-1]
42 if m.To != "maria@acme.co" || !strings.Contains(m.Body, testOrigin+"/signin") {
43 t.Errorf("invitation to %q lacks %s/signin:\n%s", m.To, testOrigin, m.Body)
44 }
45}
46
47func TestInviteIsCaseInsensitive(t *testing.T) {
48 a := newApp(t)
49 a.signedInOwner()
50 a.post("/people", invite("maria@acme.co"))
51 n := len(a.mailbox())
52 if r := a.post("/people", invite(" Maria@Acme.co ")); r.Code != 303 {
53 t.Fatalf("second invite: %d", r.Code)
54 }
55 if got := len(a.mailbox()); got != n {
56 t.Errorf("second invitation was sent (%d -> %d)", n, got)
57 }
58 ms, err := store.Members(context.Background(), a.DB)
59 if err != nil {
60 t.Fatal(err)
61 }
62 if len(ms) != 2 { // owner + maria
63 t.Errorf("want 2 members, got %d", len(ms))
64 }
65}
66
67func TestMemberCannotInvite(t *testing.T) {
68 a := newApp(t)
69 a.signedInOwner()
70 a.post("/people", invite("maria@acme.co"))
71 if r := a.signIn("maria@acme.co"); r.Code != 303 {
72 t.Fatalf("maria sign-in: %d", r.Code)
73 }
74 n := len(a.mailbox())
75 if r := a.post("/people", invite("bob@acme.co")); r.Code != 403 {
76 t.Errorf("member invite: want 403, got %d", r.Code)
77 }
78 if len(a.mailbox()) != n {
79 t.Error("an invitation was sent")
80 }
81 if r := a.get("/people"); r.Code != 200 {
82 t.Errorf("members may still see People: got %d", r.Code)
83 }
84}
85
86func TestPeopleWorksWithoutScripts(t *testing.T) {
87 a := newApp(t)
88 a.signedInOwner()
89 page := a.get("/people")
90 f := formFields(t, page.Body, "/people")
91 f.Set("email", "maria@acme.co")
92 if r := a.post("/people", f); r.Code != 303 {
93 t.Errorf("want 303, got %d", r.Code)
94 }
95 if strings.Contains(page.Body, "<script") {
96 t.Error("page needs no scripts")
97 }
98}
99
100func TestFailedInvitationCanBeRetried(t *testing.T) {
101 a := newApp(t)
102 a.signedInOwner()
103 n := len(a.mailbox())
104
105 a.mail.mu.Lock()
106 a.mail.fail = true
107 a.mail.mu.Unlock()
108 r := a.post("/people", invite("maria@acme.co"))
109 if r.Code != 303 || r.Location() != "/people?problem=mail" {
110 t.Fatalf("failed send: %d %q", r.Code, r.Location())
111 }
112 if page := a.get("/people?problem=mail"); !strings.Contains(page.Body, "The invitation wasn") {
113 t.Errorf("owner is not told the invitation failed:\n%s", page.Body)
114 }
115 m, ok, err := store.MemberByEmail(context.Background(), a.DB, "maria@acme.co")
116 if err != nil || !ok || m.InviteSentAt != nil {
117 t.Fatalf("want member with no sent invite, got %+v ok=%v err=%v", m, ok, err)
118 }
119
120 a.mail.mu.Lock()
121 a.mail.fail = false
122 a.mail.mu.Unlock()
123 if r := a.post("/people", invite("maria@acme.co")); r.Location() != "/people" {
124 t.Errorf("retry: %d %q", r.Code, r.Location())
125 }
126 if got := len(a.mailbox()); got != n+1 {
127 t.Fatalf("retry should send exactly one invitation, mailbox grew by %d", got-n)
128 }
129 if m, _, _ := store.MemberByEmail(context.Background(), a.DB, "maria@acme.co"); m.InviteSentAt == nil {
130 t.Error("delivery was not recorded")
131 }
132 a.post("/people", invite("maria@acme.co"))
133 if got := len(a.mailbox()); got != n+1 {
134 t.Errorf("third invite sent another email (grew by %d)", got-n)
135 }
136}
137
138func TestInvalidAddressIsRefused(t *testing.T) {
139 a := newApp(t)
140 a.signedInOwner()
141 n := len(a.mailbox())
142 r := a.post("/people", invite("not an email"))
143 if r.Code != 303 || r.Location() != "/people?problem=email" {
144 t.Fatalf("invalid: %d %q", r.Code, r.Location())
145 }
146 if page := a.get(r.Location()); !strings.Contains(page.Body, "look like an email address") {
147 t.Errorf("no visible error:\n%s", page.Body)
148 }
149 if ms, _ := store.Members(context.Background(), a.DB); len(ms) != 1 {
150 t.Errorf("want only the owner, got %d members", len(ms))
151 }
152 if len(a.mailbox()) != n {
153 t.Error("an email was sent")
154 }
155}
156