Files
iam/internal/bootstrap/lifecycle_test.go
zeekay e48704c976 users: rule who makes an org admin, and make a revoke take effect now
POST /v1/iam/users and PUT /v1/iam/users/{owner}/{name} answered 200 and
dropped user.isAdmin: the field was read from the calling code only, so a
SuperAdmin could not make hanzo/denis an admin and nothing said so.

One rule, users.Admin, now decides every change to an org-admin role: the
user create and update, SCIM, an admin membership, and an invitation that
carries the role. A SuperAdmin moves it anywhere; an org's admin moves it on
the others of a customer org; only a SuperAdmin makes an admin of a brand
(policy.IsPlatformOrg) or touches the admin org; nobody raises their own; the
last admin stays (409) unless a SuperAdmin takes them; applications, machines
and members are refused (403). The request is read for presence, so leaving
isAdmin out keeps it and a full-row write cannot demote by omission.

A revoke, a removed membership, an upsert lowering and a deleted account call
store.Void: User.Epoch moves to now, the account's token rows and sessions
are deleted, and verifyBearer refuses a token with iat <= epoch.

Invitations carry a role (member or admin), written only by who may grant it
and applied on signup and accept. GET /v1/iam/users takes isAdmin=true. Every
grant, revoke, role invitation, removal and void is an audit row under the
org, with actor, target, from, to, reason and request id.

Claude-Session: https://claude.ai/code/session_016DKtyuzxU43Gsc6Pm9vKTF
2026-11-02 12:07:57 -08:00

46 lines
1.8 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package bootstrap_test
import (
"context"
"strings"
"testing"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/pkg/schema"
)
// The declaration's semantics are unchanged: it grants org-admin only to a row it
// creates and may lower it on any. What the admin lifecycle adds is that both are
// recorded, and that a lowering voids the account's tokens like every revoke.
func TestUpsertUser_grantAndLoweringAreRecorded(t *testing.T) {
app, db := boot(t)
ctx := context.Background()
if st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken,
`{"owner":"acme","name":"ops","password":"s3cret","isAdmin":true}`); st != 200 || m["action"] != "created" {
t.Fatalf("create: status=%d body=%v", st, m)
}
if st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken,
`{"owner":"acme","name":"ops","password":"s3cret","isAdmin":false}`); st != 200 || m["action"] != "updated" {
t.Fatalf("lower: status=%d body=%v", st, m)
}
after := row(t, db, "acme", "ops")
if after.IsAdmin || after.Epoch == 0 {
t.Fatalf("after lowering isAdmin=%v epoch=%d, want false and a void", after.IsAdmin, after.Epoch)
}
rows, err := orm.TypedQuery[schema.AuditLog](db).Filter("Owner=", "acme").Filter("Action=", schema.ActionOrgAdmin).
Order("createdTime").GetAll(ctx)
if err != nil || len(rows) != 2 || rows[0].User != "service" ||
!strings.Contains(rows[0].Object, `"to":true`) || !strings.Contains(rows[1].Object, `"to":false`) {
t.Fatalf("org-admin rows = %+v (%v), want the grant then the lowering, by the service token", rows, err)
}
// Raising it again on the row that now exists is still refused.
if st, _ := post(t, app, "/v1/iam/admin/users/upsert", svcToken,
`{"owner":"acme","name":"ops","isAdmin":true}`); st != 400 {
t.Fatalf("re-raise status = %d, want 400", st)
}
}