Phase 10, part 2: the first owner, from a one-time setup link
A new server prints a link to create its owner (supermarket setup-link makes another); until it's used, every page says the setup isn't finished. One-time links (schema 33) are kept as hashes and used up in one statement; resets and invitations will use them too. The server edition opens Postgres. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
s, h := newServerEdition(t)
|
||||
|
||||
// Before the owner, every page says so; what's public still works.
|
||||
for _, p := range []string{"/", "/seo/check", "/signin"} {
|
||||
rec := do(h, http.MethodGet, p, nil, nil)
|
||||
if rec.Code != http.StatusServiceUnavailable || !strings.Contains(rec.Body.String(), "isn't set up yet") {
|
||||
t.Errorf("%s: %d", p, rec.Code)
|
||||
}
|
||||
}
|
||||
if rec := do(h, http.MethodGet, "/healthz", nil, nil); rec.Code != http.StatusOK {
|
||||
t.Errorf("healthz: %d", rec.Code)
|
||||
}
|
||||
|
||||
// A made-up link doesn't work.
|
||||
if rec := do(h, http.MethodGet, "/setup/made-up", nil, nil); rec.Code != http.StatusNotFound || !strings.Contains(rec.Body.String(), "doesn't work") {
|
||||
t.Errorf("made-up link: %d", rec.Code)
|
||||
}
|
||||
old, err := users.NewLink(s.DB, users.SetupLink, 0, time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, _ := users.NewLink(s.DB, users.SetupLink, 0, time.Now()) // replaces it
|
||||
if rec := do(h, http.MethodGet, "/setup/"+old, nil, nil); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("replaced link: %d", rec.Code)
|
||||
}
|
||||
rec := do(h, http.MethodGet, "/setup/"+token, nil, nil)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Create the owner") || strings.Contains(rec.Body.String(), `class="sidebar"`) {
|
||||
t.Fatalf("setup page: %d", rec.Code)
|
||||
}
|
||||
|
||||
// Mistakes are shown, and the link still works.
|
||||
form := url.Values{"email": {"Owner@Example.com"}, "name": {"Olu"}, "password": {"short"}, "password2": {"short"}}
|
||||
rec = postForm(h, "/setup/"+token, form)
|
||||
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "at least 10 characters") || !strings.Contains(rec.Body.String(), `value="Owner@Example.com"`) {
|
||||
t.Errorf("short password: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
form.Set("password", "a long enough one")
|
||||
form.Set("password2", "a long enough one!")
|
||||
if rec = postForm(h, "/setup/"+token, form); rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "passwords aren") {
|
||||
t.Errorf("mismatch: %d", rec.Code)
|
||||
}
|
||||
|
||||
form.Set("password2", "a long enough one")
|
||||
rec = postForm(h, "/setup/"+token, form)
|
||||
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/settings/profile?msg=welcome" {
|
||||
t.Fatalf("setup: %d %s %s", rec.Code, rec.Header().Get("Location"), rec.Body.String())
|
||||
}
|
||||
var session *http.Cookie
|
||||
for _, c := range rec.Result().Cookies() {
|
||||
if c.Name == sessionCookie {
|
||||
session = c
|
||||
}
|
||||
}
|
||||
if session == nil {
|
||||
t.Fatal("not signed in")
|
||||
}
|
||||
u, err := users.ByEmail(s.DB, "owner@example.com")
|
||||
if err != nil || u.Status != users.Active || !users.CheckPassword(u, "a long enough one") || u.SignedIn.IsZero() {
|
||||
t.Fatalf("owner: %+v %v", u, err)
|
||||
}
|
||||
if m, _ := users.MembershipOf(s.DB, users.Team, u.ID); m.Role != users.Owner {
|
||||
t.Errorf("role %s", m.Role)
|
||||
}
|
||||
if rec := as(h, session, http.MethodGet, "/settings/profile?msg=welcome", nil); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Start with the company") {
|
||||
t.Errorf("profile after setup: %d", rec.Code)
|
||||
}
|
||||
|
||||
// The link is used up, and setting up again leads to signing in.
|
||||
if rec := postForm(h, "/setup/"+token, form); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/signin" {
|
||||
t.Errorf("second use: %d %s", rec.Code, rec.Header().Get("Location"))
|
||||
}
|
||||
if n, _ := users.Count(s.DB); n != 1 {
|
||||
t.Errorf("%d users", n)
|
||||
}
|
||||
// Not signed in: to the sign-in page, no longer the setup notice.
|
||||
if rec := do(h, http.MethodGet, "/seo/check", nil, nil); rec.Code != http.StatusSeeOther {
|
||||
t.Errorf("after setup, not signed in: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupIsServerOnly(t *testing.T) {
|
||||
s, h := newTestServer(t, nil)
|
||||
token, _ := users.NewLink(s.DB, users.SetupLink, 0, time.Now())
|
||||
if rec := do(h, http.MethodGet, "/setup/"+token, nil, nil); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("local: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user