Files
beardrive/internal/webapp/gating_test.go
T
Snow LeeandClaude Fable 5 baad9e6dc2 feat(hub): signup gating, admin lifecycle, and onboarding UX
Makes a self-hosted hub safe to expose on a public URL and operable
without hand-editing JSON — addressing the blocker/major findings from
the persona usability evaluations.

Signup gating (config auth block, all optional):
- allowed_domains: signup email must match (e.g. only @runbear.io)
- require_verification: email-link activation before sign-in (reuses mailer)
- require_approval: hub admins approve new accounts (admins list)
- brand shown on the sign-in page; allow_signup:false already hid Sign up
Accounts carry a Status (active/unverified/pending); non-active accounts
cannot authenticate.

Admin lifecycle (endpoints + web UI):
- org: rename, member role change, member remove (last-owner guarded),
  invite list + revoke
- project: create (web), rename, delete (from the org panel)
- hub admins: approve/deny pending signups (sidebar bell + panel)
- org-wide public-share audit with revoke

UX: onboarding empty-state (explains invites, paste-invite + create-project)
instead of a blank sidebar; visible "Search ⌘K" button; toasts replace
blocking alert(); responsive layout with an off-canvas sidebar; joining
via #join now survives a logged-out click (token carried through login).
Web uploads are attributed to the signed-in account, not the server.
Login/signup are rate-limited per IP.

Tests: domain/verification/approval gates, auth rate limit, org+project
lifecycle, owner-only guards, invite→join→role→remove over HTTP.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R7Q9ZKSZRTdvrSJkYLUmYs
2026-07-08 22:23:27 -07:00

127 lines
3.9 KiB
Go

package webapp
import (
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
)
// gatedAuth builds a BuiltinAuth with the given gating knobs.
func gatedAuth(t *testing.T, tune func(*BuiltinAuth)) *BuiltinAuth {
t.Helper()
a, err := OpenBuiltinAuth(filepath.Join(t.TempDir(), "auth.json"), true, nil)
if err != nil {
t.Fatal(err)
}
if tune != nil {
tune(a)
}
return a
}
func TestSignupDomainAllowlist(t *testing.T) {
a := gatedAuth(t, func(a *BuiltinAuth) { a.AllowedDomains = []string{"runbear.io"} })
if _, err := a.signup("mallory@evil.example", "M", "password1"); err == nil {
t.Fatal("outside-domain signup should be rejected")
}
if _, err := a.signup("dev@runbear.io", "D", "password1"); err != nil {
t.Fatalf("allowed-domain signup rejected: %v", err)
}
// case-insensitive, tolerant of a leading @ in config
a2 := gatedAuth(t, func(a *BuiltinAuth) { a.AllowedDomains = []string{"@Runbear.IO"} })
if _, err := a2.signup("x@RUNBEAR.io", "X", "password1"); err != nil {
t.Fatalf("case-insensitive domain match failed: %v", err)
}
}
func TestSignupVerificationGate(t *testing.T) {
a := gatedAuth(t, func(a *BuiltinAuth) { a.RequireVerification = true })
u, err := a.signup("dev@x.io", "D", "password1")
if err != nil {
t.Fatal(err)
}
if u.Status != statusUnverified {
t.Fatalf("status = %q, want unverified", u.Status)
}
// an unverified account cannot authenticate even with a token
tok, _ := a.issueToken(u.ID, "cli")
if _, ok := a.userForToken(tok); ok {
t.Fatal("unverified account authenticated")
}
// verifying activates it
grant := a.newGrant("verify", u.ID, "", true, 0)
_ = grant
a.mu.Lock()
a.users[u.ID].Status = statusActive
a.mu.Unlock()
if _, ok := a.userForToken(tok); !ok {
t.Fatal("activated account still cannot authenticate")
}
}
func TestSignupApprovalGate(t *testing.T) {
a := gatedAuth(t, func(a *BuiltinAuth) { a.RequireApproval = true })
u, _ := a.signup("dev@x.io", "D", "password1")
if u.Status != statusPending {
t.Fatalf("status = %q, want pending", u.Status)
}
if len(a.PendingUsers()) != 1 {
t.Fatal("pending user not listed")
}
tok, _ := a.issueToken(u.ID, "cli")
if _, ok := a.userForToken(tok); ok {
t.Fatal("pending account authenticated before approval")
}
if err := a.Approve(u.ID); err != nil {
t.Fatal(err)
}
if _, ok := a.userForToken(tok); !ok {
t.Fatal("approved account cannot authenticate")
}
if len(a.PendingUsers()) != 0 {
t.Fatal("approved user still pending")
}
}
// The signup page reflects the gate: verification shows "verify your email"
// and does NOT set a session cookie.
func TestSignupPageVerificationFlow(t *testing.T) {
srv, _, _ := newHub(t, true, nil)
srv.Auth = gatedAuth(t, func(a *BuiltinAuth) { a.RequireVerification = true })
h := srv.Handler()
form := url.Values{"email": {"a@x.io"}, "name": {"A"}, "password": {"password1"}}
req := httptest.NewRequest("POST", "/auth/signup", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Verify your email") {
t.Fatalf("expected verify page: %d %s", rec.Code, rec.Body)
}
for _, c := range rec.Result().Cookies() {
if c.Name == sessionCookie && c.Value != "" {
t.Fatal("verification-gated signup set a live session")
}
}
}
func TestAuthRateLimit(t *testing.T) {
srv, _, _ := newHub(t, true, nil)
srv.Auth = gatedAuth(t, nil)
h := srv.Handler()
last := 0
for i := 0; i < 12; i++ {
req := httptest.NewRequest("POST", "/auth/login", strings.NewReader("email=x@x.io&password=nope1234"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.RemoteAddr = "9.9.9.9:1"
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
last = rec.Code
}
if last != http.StatusTooManyRequests {
t.Fatalf("12th login attempt from one IP: %d, want 429", last)
}
}