package api_test import ( "context" "net/http" "strings" "testing" "github.com/google/uuid" "github.com/jackc/pgx/v5" "git.zb-server.de/ZB-Server/uptimesebi/internal/sso/ssotest" "git.zb-server.de/ZB-Server/uptimesebi/internal/store" "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" ) func TestConsent(t *testing.T) { e := newEnv(t) ctx := context.Background() admin := e.browser(t) admin.signup("a@"+uniq("cs")+".test", uniq("cs-")) if err := tenant.New(e.dbPool).Tx(ctx, tenant.Global(), func(q *store.Queries) error { return q.MakeAdmin(ctx, admin.userIDSelf()) }); err != nil { t.Fatal(err) } earlier := e.browser(t) earlier.signup("e@"+uniq("cs")+".test", uniq("cs-")) t.Cleanup(func() { clearPlatformLegal(admin) }) for kind, text := range map[string]string{"privacy": "# Datenschutz", "terms": "# AGB"} { if st, body := admin.do("PUT", "/api/v1/admin/legal/"+kind, map[string]any{"markdown": text}); st != http.StatusNoContent { t.Fatalf("set %s = %d %v", kind, st, body) } } admin.acceptConsent() if _, _, html := e.browser(t).page("/login"); !strings.Contains(html, `href="/legal/terms"`) { t.Error("the footer lacks the terms") } if st, body := admin.do("PUT", "/api/v1/org/legal/terms", map[string]any{"markdown": "x"}); st != http.StatusNotFound { t.Errorf("organization sets terms = %d %v, want 404", st, body) } // Signing up needs the box ticked for the versions shown. visitor := e.browser(t) _, _, form := visitor.page("/signup") versions := consentShown(form) if !strings.Contains(versions, "privacy:") || !strings.Contains(versions, "terms:") || !strings.Contains(form, `name="consent" required`) { t.Fatalf("sign-up form lacks the consent box: versions %q", versions) } signup := func(consent bool, versions string) (int, map[string]any) { return e.browser(t).do("POST", "/api/v1/auth/signup", map[string]any{ "email": uniq("n") + "@example.test", "password": password, "consent": consent, "consent_versions": versions, }) } st, body := signup(false, versions) expect(t, "sign up without consent", st, body, http.StatusUnprocessableEntity, "consent_required") st, body = signup(true, "privacy:1,terms:1") expect(t, "sign up with outdated versions", st, body, http.StatusConflict, "consent_outdated") email := uniq("n") + "@example.test" fresh := e.browser(t) if st, body := fresh.do("POST", "/api/v1/auth/signup", map[string]any{ "email": email, "password": password, "consent": true, "consent_versions": versions, }); st != http.StatusOK { t.Fatalf("sign up with consent = %d %v", st, body) } freshID := fresh.userIDSelf() if n := e.count(t, `SELECT count(*) FROM legal_acceptances WHERE user_id = $1`, freshID); n != 2 { t.Errorf("recorded %d acceptances, want 2", n) } if got := fresh.visit("/"); got.Path == "/consent" { t.Error("an account that accepted on sign-up is asked again") } // Accounts from before have to accept first; until then they can read // the documents, take their data along and sign out. if got := earlier.visit("/"); got.Path != "/consent" || got.Query().Get("next") != "/" { t.Errorf("console before consent landed on %s", got) } st, body = earlier.do("GET", "/api/v1/org/monitors", nil) expect(t, "API before consent", st, body, http.StatusForbidden, "consent_required") for _, path := range []string{"/legal/terms", "/api/v1/me/export"} { if st, _, _ := earlier.page(path); st != http.StatusOK { t.Errorf("GET %s before consent = %d", path, st) } } _, _, page := earlier.page("/consent") if !strings.Contains(page, "Delete account") || !strings.Contains(page, `href="/legal/terms"`) { t.Error("the consent page offers no way out or lacks the documents") } st, body = earlier.do("POST", "/api/v1/consent", map[string]any{"versions": "terms:1", "next": "/"}) expect(t, "accept outdated versions", st, body, http.StatusConflict, "consent_outdated") if st, body := earlier.do("POST", "/api/v1/consent", map[string]any{"versions": consentShown(page), "next": "//evil.example"}); st != http.StatusOK || body["redirect"] != "/" { t.Errorf("accept = %d %v, want a local redirect", st, body) } if st, _ := earlier.do("GET", "/api/v1/org/monitors", nil); st != http.StatusOK { t.Errorf("API after consent = %d", st) } // A correction asks nobody again; a new version asks everyone. admin.do("PUT", "/api/v1/admin/legal/terms", map[string]any{"markdown": "# AGB\n\nTippfehler behoben"}) if got := earlier.visit("/"); got.Path == "/consent" { t.Error("a correction asked for consent again") } admin.do("PUT", "/api/v1/admin/legal/terms", map[string]any{"markdown": "# AGB v2", "new_version": true}) if got := earlier.visit("/"); got.Path != "/consent" { t.Errorf("a new version of the terms was not put before the account: %s", got) } if _, _, page := earlier.page("/consent"); !strings.Contains(page, `href="/legal/terms"`) || strings.Contains(page, `