feat: several owners per organization, and handing ownership over
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m19s
ci / test (pull_request) Successful in 9m25s
ci / build (pull_request) Successful in 15m40s
ci / image (pull_request) Successful in 4m26s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m19s
ci / test (pull_request) Successful in 9m25s
ci / build (pull_request) Successful in 15m40s
ci / image (pull_request) Successful in 4m26s
An organization may now have several owners; the unique index allowing only one is gone. Owners make members owners or take it back, their own included, through PUT /api/v1/org/members/:user/owner; handing an organization over is making someone else owner and stepping down. The last owner cannot step down, and changes serialize on a per-organization lock so two owners stepping down at once cannot leave none. A former owner without roles keeps working as Admin. Owners still cannot be removed while they are owners. Deleting an account now takes along only the organizations it is the last owner of, counted under the same lock; it just leaves those with further owners. The members page offers "Make owner", "Remove as owner" and "Step down as owner" to owners. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
8d7c1f856c
commit
db3ff19983
23 files changed
+558
-115
No files matched your search
@@ -0,0 +1,85 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user