package webtest import ( "context" "regexp" "strings" "testing" "amadan.net/oficina/base/internal/store" ) func TestOwnerCanSignIn(t *testing.T) { a := newApp(t) res := a.signIn(ownerAddress) if res.Code != 303 || res.Location() != "/" { t.Fatalf("verify: %d to %q", res.Code, res.Location()) } if home := a.get("/"); home.Code != 200 { t.Fatalf("GET /: %d", home.Code) } } func TestStrangerRefused(t *testing.T) { a := newApp(t) res := a.signIn("stranger@elsewhere.test") if res.Code != 303 || !strings.HasPrefix(res.Location(), "/signin") { t.Fatalf("verify: %d to %q", res.Code, res.Location()) } page := a.get(res.Location()) if page.Code != 200 || !strings.Contains(page.Body, "Sign-in isn") { t.Fatalf("refusal page: %d\n%s", page.Code, page.Body) } if home := a.get("/"); home.Code != 303 || !strings.HasPrefix(home.Location(), "/signin") { t.Fatalf("stranger reached /: %d to %q", home.Code, home.Location()) } } func TestInvitedMemberJoinsOnFirstSignIn(t *testing.T) { a := newApp(t) ctx := context.Background() if _, _, err := store.Invite(ctx, a.DB, "Ana@Acme.test"); err != nil { t.Fatal(err) } if m, _, _ := store.MemberByEmail(ctx, a.DB, "ana@acme.test"); m.JoinedAt != nil { t.Fatal("joined before signing in") } if res := a.signIn("ana@acme.test"); res.Code != 303 || res.Location() != "/" { t.Fatalf("verify: %d to %q", res.Code, res.Location()) } m, _, err := store.MemberByEmail(ctx, a.DB, "ana@acme.test") if err != nil || m.JoinedAt == nil { t.Fatalf("JoinedAt not set: %+v, %v", m, err) } } func TestSignOut(t *testing.T) { a := newApp(t) a.signIn(ownerAddress) if a.get("/").Code != 200 { t.Fatal("not signed in") } if res := a.post("/auth/signout", nil); res.Code != 303 { t.Fatalf("signout: %d", res.Code) } if home := a.get("/"); home.Code != 303 || !strings.HasPrefix(home.Location(), "/signin") { t.Fatalf("GET / after signout: %d to %q", home.Code, home.Location()) } } func TestSigninPageServesItsStylesheet(t *testing.T) { a := newApp(t) page := a.get("/signin") if page.Code != 200 || !strings.Contains(page.Body, "Sign in to Acme") { t.Fatalf("signin page: %d\n%s", page.Code, page.Body) } m := regexp.MustCompile(`href="(/static/app[^"]*\.css)"`).FindStringSubmatch(page.Body) if m == nil { t.Fatalf("no stylesheet link in:\n%s", page.Body) } if css := a.get(m[1]); css.Code != 200 || !strings.Contains(css.Body, ".signin") { t.Fatalf("stylesheet: %d", css.Code) } }