ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m59s
ci / test (pull_request) Successful in 12m30s
ci / build (pull_request) Successful in 18m22s
ci / image (pull_request) Successful in 5m54s
The sign-up and consent pages ran the first document's link into the word joining it to the next, as in "Datenschutzerklärungund die AGB". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
197 lines
8.8 KiB
Go
197 lines
8.8 KiB
Go
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)
|
|
}
|
|
if !strings.Contains(form, "</a> and the <a") {
|
|
t.Error("the consent text runs the documents' names into the words between them")
|
|
}
|
|
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, `<li><a href="/legal/privacy"`) {
|
|
t.Error("the consent page should list only the changed terms")
|
|
}
|
|
earlier.acceptConsent()
|
|
admin.acceptConsent()
|
|
|
|
// An account created through a sign-in provider is asked right after
|
|
// its first sign-in.
|
|
idp := ssotest.New(t)
|
|
st, provider := admin.do("POST", "/api/v1/admin/identity-providers", map[string]any{
|
|
"kind": "oidc", "name": "IdP", "issuer": idp.Issuer(), "client_id": idp.ClientID, "client_secret": idp.Secret,
|
|
"allow_signup": true, "enabled": true,
|
|
})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create provider = %d %v", st, provider)
|
|
}
|
|
t.Cleanup(func() {
|
|
_ = e.asSystem(t, func(tx pgx.Tx) error {
|
|
_, err := tx.Exec(ctx, `DELETE FROM platform_identity_providers WHERE id = $1`, provider["id"])
|
|
return err
|
|
})
|
|
})
|
|
idp.SetUser(ssotest.User{Subject: uniq("sub"), Email: uniq("sso") + "@example.test", EmailVerified: true})
|
|
viaSSO := e.browser(t)
|
|
if got := viaSSO.visit("/login/sso/" + provider["id"].(string)); got.Path != "/consent" {
|
|
t.Errorf("SSO sign-up landed on %s, want the consent page", got)
|
|
}
|
|
viaSSO.acceptConsent()
|
|
if got := viaSSO.visit("/"); got.Path != "/orgs/new" {
|
|
t.Errorf("after consenting the new SSO account landed on %s, want /orgs/new", got)
|
|
}
|
|
}
|
|
|
|
func TestSuspendedAccount(t *testing.T) {
|
|
e := newEnv(t)
|
|
ctx := context.Background()
|
|
admin := e.browser(t)
|
|
admin.signup("a@"+uniq("su")+".test", uniq("su-"))
|
|
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)
|
|
}
|
|
email := "s@" + uniq("su") + ".test"
|
|
user := e.browser(t)
|
|
user.signup(email, uniq("su-"))
|
|
userID := user.userIDSelf()
|
|
org := uuid.MustParse(user.orgID())
|
|
user.do("POST", "/api/v1/org/monitors", map[string]any{"name": "web", "type": "heartbeat", "interval_seconds": 3600})
|
|
_, tok := user.do("POST", "/api/v1/org/tokens", map[string]any{"name": "ci", "permissions": []string{"monitors.read"}})
|
|
|
|
if st, body := admin.do("PUT", "/api/v1/admin/users/"+userID.String()+"/suspension", map[string]any{"suspended": true, "reason": "Unpaid invoice"}); st != http.StatusNoContent {
|
|
t.Fatalf("suspend = %d %v", st, body)
|
|
}
|
|
api := &browser{t: t, base: user.base, hc: e.srv.Client(), bearer: tok["token"].(string)}
|
|
if st, _ := api.do("GET", "/api/v1/org/monitors", nil); st != http.StatusUnauthorized {
|
|
t.Errorf("API token of a suspended account = %d, want 401", st)
|
|
}
|
|
|
|
// It signs in again, but only to take its data along or delete itself.
|
|
back := e.browser(t)
|
|
if st, body := back.do("POST", "/api/v1/auth/login", map[string]any{"email": email, "password": password}); st != http.StatusOK {
|
|
t.Fatalf("suspended account signs in = %d %v", st, body)
|
|
}
|
|
if got := back.visit("/"); got.Path != "/account/suspended" {
|
|
t.Errorf("console of a suspended account landed on %s", got)
|
|
}
|
|
_, _, page := back.page("/account/suspended")
|
|
if !strings.Contains(page, "Unpaid invoice") || !strings.Contains(page, "Delete account") || !strings.Contains(page, "You are the last owner of these organizations") {
|
|
t.Error("the suspension page lacks the reason or the way out")
|
|
}
|
|
st, body := back.do("GET", "/api/v1/org/monitors", nil)
|
|
expect(t, "API of a suspended account", st, body, http.StatusForbidden, "account_suspended")
|
|
if st, _, _ := back.page("/api/v1/me/export"); st != http.StatusOK {
|
|
t.Errorf("export of a suspended account = %d", st)
|
|
}
|
|
if st, body := back.do("POST", "/api/v1/me/delete", map[string]any{"password": password}); st != http.StatusOK {
|
|
t.Fatalf("suspended account deletes itself = %d %v", st, body)
|
|
}
|
|
for q, arg := range map[string]any{`SELECT count(*) FROM users WHERE id = $1`: userID, `SELECT count(*) FROM orgs WHERE id = $1`: org, `SELECT count(*) FROM monitors WHERE org_id = $1`: org} {
|
|
if n := e.count(t, q, arg); n != 0 {
|
|
t.Errorf("%s = %d after deletion", q, n)
|
|
}
|
|
}
|
|
}
|