package api_test import ( "net/http" "strings" "testing" "github.com/google/uuid" ) func TestOwners(t *testing.T) { e := newEnv(t) first := e.browser(t) first.signup("f@"+uniq("ow")+".test", uniq("ow-")) org := uuid.MustParse(first.orgID()) firstID := first.userIDSelf() second := e.joinVia(t, first.invite("s@"+uniq("ow")+".test", first.roleID("viewer"))) secondID := second.userIDSelf() owner := func(b *browser, user uuid.UUID, on bool) (int, map[string]any) { return b.do("PUT", "/api/v1/org/members/"+user.String()+"/owner", map[string]any{"owner": on}) } isOwner := func(user uuid.UUID) bool { return e.count(t, `SELECT count(*) FROM memberships WHERE org_id = $1 AND user_id = $2 AND is_owner`, org, user) == 1 } // Only owners decide. st, body := owner(second, secondID, true) expect(t, "non-owner makes itself owner", st, body, http.StatusForbidden, "owner_only") st, body = owner(first, firstID, false) expect(t, "last owner steps down", st, body, http.StatusConflict, "last_owner") if _, _, html := first.page("/settings/members"); !strings.Contains(html, "Make owner") || strings.Contains(html, "Step down as owner") { t.Error("the members page offers the wrong owner actions to the only owner") } // Handing over: make someone else owner, then step down. if st, body := owner(first, secondID, true); st != http.StatusNoContent || !isOwner(secondID) { t.Fatalf("make owner = %d %v", st, body) } if st, _ := second.do("PUT", "/api/v1/org/legal/imprint", map[string]any{"markdown": "# Impressum"}); st != http.StatusNoContent { t.Errorf("new owner manages the organization = %d", st) } if _, _, html := first.page("/settings/members"); !strings.Contains(html, "Step down as owner") || !strings.Contains(html, "Remove as owner") { t.Error("with two owners the page offers neither stepping down nor removing") } st, body = first.do("DELETE", "/api/v1/org/members/"+secondID.String(), nil) expect(t, "remove an owner", st, body, http.StatusForbidden, "owner_cannot_be_removed") if st, body := owner(first, firstID, false); st != http.StatusNoContent || isOwner(firstID) { t.Fatalf("step down = %d %v", st, body) } // The former owner keeps working as Admin. if n := e.count(t, `SELECT count(*) FROM role_assignments WHERE org_id = $1 AND user_id = $2 AND role_id = '00000000-0000-0000-0000-00000000a001'`, org, firstID); n != 1 { t.Errorf("former owner has %d admin assignments, want 1", n) } if st, _ := first.do("GET", "/api/v1/org/members", nil); st != http.StatusOK { t.Errorf("former owner lists members = %d", st) } st, body = owner(first, firstID, true) expect(t, "former owner takes ownership back", st, body, http.StatusForbidden, "owner_only") st, body = owner(second, secondID, false) expect(t, "the new last owner steps down", st, body, http.StatusConflict, "last_owner") // Deleting an account leaves organizations with further owners and // takes along those it is the last owner of. if st, body := owner(second, firstID, true); st != http.StatusNoContent { t.Fatalf("make owner again = %d %v", st, body) } if _, _, html := first.page("/settings/security"); strings.Contains(html, "last owner of these organizations") { t.Error("an organization with another owner is announced as deleted") } if st, body := first.do("POST", "/api/v1/me/delete", map[string]any{"password": password}); st != http.StatusOK { t.Fatalf("co-owner deletes account = %d %v", st, body) } if e.count(t, `SELECT count(*) FROM orgs WHERE id = $1`, org) != 1 || !isOwner(secondID) { t.Fatal("the organization did not stay with its other owner") } if _, _, html := second.page("/settings/security"); !strings.Contains(html, "last owner of these organizations") { t.Error("the last owner is not warned") } if st, body := second.do("POST", "/api/v1/me/delete", map[string]any{"password": password}); st != http.StatusOK { t.Fatalf("last owner deletes account = %d %v", st, body) } if e.count(t, `SELECT count(*) FROM orgs WHERE id = $1`, org) != 0 { t.Error("the organization survived its last owner") } }