package webtest import ( "context" "net/url" "strings" "testing" "amadan.net/oficina/base/internal/store" ) func (a *testApp) signedInOwner() { a.t.Helper() if r := a.signIn(ownerAddress); r.Code != 303 { a.t.Fatalf("owner sign-in: %d", r.Code) } } func invite(email string) url.Values { return url.Values{"email": {email}} } func TestOwnerInvites(t *testing.T) { a := newApp(t) a.signedInOwner() before := len(a.mailbox()) if r := a.post("/people", invite("maria@acme.co")); r.Code != 303 || r.Location() != "/people" { t.Fatalf("invite: %d %q", r.Code, r.Location()) } page := a.get("/people") if page.Code != 200 { t.Fatalf("people: %d", page.Code) } for _, want := range []string{"maria@acme.co", "Invited. Hasn't signed in yet."} { if !strings.Contains(page.Body, want) && !strings.Contains(page.Body, strings.ReplaceAll(want, "'", "'")) { t.Errorf("page lacks %q:\n%s", want, page.Body) } } box := a.mailbox() if len(box) != before+1 { t.Fatalf("want one invitation, mailbox grew by %d", len(box)-before) } m := box[len(box)-1] if m.To != "maria@acme.co" || !strings.Contains(m.Body, testOrigin+"/signin") { t.Errorf("invitation to %q lacks %s/signin:\n%s", m.To, testOrigin, m.Body) } } func TestInviteIsCaseInsensitive(t *testing.T) { a := newApp(t) a.signedInOwner() a.post("/people", invite("maria@acme.co")) n := len(a.mailbox()) if r := a.post("/people", invite(" Maria@Acme.co ")); r.Code != 303 { t.Fatalf("second invite: %d", r.Code) } if got := len(a.mailbox()); got != n { t.Errorf("second invitation was sent (%d -> %d)", n, got) } ms, err := store.Members(context.Background(), a.DB) if err != nil { t.Fatal(err) } if len(ms) != 2 { // owner + maria t.Errorf("want 2 members, got %d", len(ms)) } } func TestMemberCannotInvite(t *testing.T) { a := newApp(t) a.signedInOwner() a.post("/people", invite("maria@acme.co")) if r := a.signIn("maria@acme.co"); r.Code != 303 { t.Fatalf("maria sign-in: %d", r.Code) } n := len(a.mailbox()) if r := a.post("/people", invite("bob@acme.co")); r.Code != 403 { t.Errorf("member invite: want 403, got %d", r.Code) } if len(a.mailbox()) != n { t.Error("an invitation was sent") } if r := a.get("/people"); r.Code != 200 { t.Errorf("members may still see People: got %d", r.Code) } } func TestPeopleWorksWithoutScripts(t *testing.T) { a := newApp(t) a.signedInOwner() page := a.get("/people") f := formFields(t, page.Body, "/people") f.Set("email", "maria@acme.co") if r := a.post("/people", f); r.Code != 303 { t.Errorf("want 303, got %d", r.Code) } if strings.Contains(page.Body, "