| 1 | package webtest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/base/internal/store" |
| 10 | ) |
| 11 | |
| 12 | func TestOwnerCanSignIn(t *testing.T) { |
| 13 | a := newApp(t) |
| 14 | res := a.signIn(ownerAddress) |
| 15 | if res.Code != 303 || res.Location() != "/" { |
| 16 | t.Fatalf("verify: %d to %q", res.Code, res.Location()) |
| 17 | } |
| 18 | if home := a.get("/"); home.Code != 200 { |
| 19 | t.Fatalf("GET /: %d", home.Code) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestStrangerRefused(t *testing.T) { |
| 24 | a := newApp(t) |
| 25 | res := a.signIn("stranger@elsewhere.test") |
| 26 | if res.Code != 303 || !strings.HasPrefix(res.Location(), "/signin") { |
| 27 | t.Fatalf("verify: %d to %q", res.Code, res.Location()) |
| 28 | } |
| 29 | page := a.get(res.Location()) |
| 30 | if page.Code != 200 || !strings.Contains(page.Body, "Sign-in isn") { |
| 31 | t.Fatalf("refusal page: %d\n%s", page.Code, page.Body) |
| 32 | } |
| 33 | if home := a.get("/"); home.Code != 303 || !strings.HasPrefix(home.Location(), "/signin") { |
| 34 | t.Fatalf("stranger reached /: %d to %q", home.Code, home.Location()) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestInvitedMemberJoinsOnFirstSignIn(t *testing.T) { |
| 39 | a := newApp(t) |
| 40 | ctx := context.Background() |
| 41 | if _, _, err := store.Invite(ctx, a.DB, "Ana@Acme.test"); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if m, _, _ := store.MemberByEmail(ctx, a.DB, "ana@acme.test"); m.JoinedAt != nil { |
| 45 | t.Fatal("joined before signing in") |
| 46 | } |
| 47 | if res := a.signIn("ana@acme.test"); res.Code != 303 || res.Location() != "/" { |
| 48 | t.Fatalf("verify: %d to %q", res.Code, res.Location()) |
| 49 | } |
| 50 | m, _, err := store.MemberByEmail(ctx, a.DB, "ana@acme.test") |
| 51 | if err != nil || m.JoinedAt == nil { |
| 52 | t.Fatalf("JoinedAt not set: %+v, %v", m, err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestSignOut(t *testing.T) { |
| 57 | a := newApp(t) |
| 58 | a.signIn(ownerAddress) |
| 59 | if a.get("/").Code != 200 { |
| 60 | t.Fatal("not signed in") |
| 61 | } |
| 62 | if res := a.post("/auth/signout", nil); res.Code != 303 { |
| 63 | t.Fatalf("signout: %d", res.Code) |
| 64 | } |
| 65 | if home := a.get("/"); home.Code != 303 || !strings.HasPrefix(home.Location(), "/signin") { |
| 66 | t.Fatalf("GET / after signout: %d to %q", home.Code, home.Location()) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestSigninPageServesItsStylesheet(t *testing.T) { |
| 71 | a := newApp(t) |
| 72 | page := a.get("/signin") |
| 73 | if page.Code != 200 || !strings.Contains(page.Body, "Sign in to Acme") { |
| 74 | t.Fatalf("signin page: %d\n%s", page.Code, page.Body) |
| 75 | } |
| 76 | m := regexp.MustCompile(`href="(/static/app[^"]*\.css)"`).FindStringSubmatch(page.Body) |
| 77 | if m == nil { |
| 78 | t.Fatalf("no stylesheet link in:\n%s", page.Body) |
| 79 | } |
| 80 | if css := a.get(m[1]); css.Code != 200 || !strings.Contains(css.Body, ".signin") { |
| 81 | t.Fatalf("stylesheet: %d", css.Code) |
| 82 | } |
| 83 | } |
| 84 | |