forked from longtao.wu/droboticflow
126 lines
6.2 KiB
Go
126 lines
6.2 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestAccountManagementPermissionMatrix(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ds := store.NewMemoryStore()
|
|
h := NewHandler(ds, nil)
|
|
root := bootstrapSuperAdminSession(t, h)
|
|
org := adminCreateOrganization(t, h, root, "Account permissions", "account-permissions")
|
|
admin := adminCreateUser(t, h, root, org.ID, "Admin", "account-admin@example.com", domain.UserRoleAdmin, "test-password")
|
|
supplier, _ := ds.EnsureDefaultSupplier(t.Context(), org.ID)
|
|
manager := adminCreateUser(t, h, root, org.ID, "Manager", "account-manager@example.com", domain.UserRoleSupplierManager, "test-password", supplier.ID)
|
|
peer := adminCreateUser(t, h, root, org.ID, "Peer", "account-peer@example.com", domain.UserRoleSupplierManager, "test-password", supplier.ID)
|
|
worker := adminCreateUser(t, h, root, org.ID, "Worker", "account-worker@example.com", domain.UserRoleAnnotator, "test-password", supplier.ID)
|
|
external := domain.SupplierRecord{ID: "account-other", OrganizationID: org.ID, Status: "active"}
|
|
_ = ds.UpsertSupplier(t.Context(), external)
|
|
outsider := adminCreateUser(t, h, root, org.ID, "Outside", "account-outside@example.com", domain.UserRoleAnnotator, "test-password", external.ID)
|
|
mc := loginSession(t, h, manager.Phone, "test-password")
|
|
ac := loginSession(t, h, admin.Phone, "test-password")
|
|
for _, target := range []domain.UserRecord{admin, manager, peer, outsider} {
|
|
for _, op := range []struct{ method, body string }{{"PUT", `{"name":"changed"}`}, {"PUT", `{"password":"new-password"}`}, {"PUT", `{"role":"annotator"}`}, {"DELETE", ``}} {
|
|
r := serveJSONWithCookie(h, op.method, "/api/users/"+target.ID, []byte(op.body), mc)
|
|
if r.Code != 403 {
|
|
t.Fatalf("manager %s target %s: %d %s", op.method, target.Role, r.Code, r.Body.String())
|
|
}
|
|
}
|
|
}
|
|
r := serveJSONWithCookie(h, "GET", "/api/users?includeDisabled=true", nil, mc)
|
|
var list struct {
|
|
Items []domain.UserRecord `json:"items"`
|
|
}
|
|
_ = json.Unmarshal(r.Body.Bytes(), &list)
|
|
if len(list.Items) != 1 || list.Items[0].ID != worker.ID {
|
|
t.Fatalf("manager directory leaked accounts: %s", r.Body.String())
|
|
}
|
|
for _, role := range []string{"admin", "supplier_manager"} {
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/"+worker.ID, []byte(`{"role":"`+role+`"}`), mc)
|
|
if r.Code != 400 {
|
|
t.Fatalf("promotion allowed: %d", r.Code)
|
|
}
|
|
}
|
|
if _, err := ds.SetSupplierAcceptanceAuthorization(t.Context(), org.ID, supplier.ID, true, 0, admin.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, role := range []string{"quality_reviewer", "acceptance_reviewer", "annotator"} {
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/"+worker.ID, []byte(`{"role":"`+role+`"}`), mc)
|
|
if r.Code != 200 {
|
|
t.Fatalf("employee role update: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
// Ownership in create requests is forced to the manager's supplier.
|
|
r = serveJSONWithCookie(h, "POST", "/api/users", []byte(`{"name":"Created","phone":"13800002001","role":"annotator","supplierId":"account-other","password":"test-password"}`), mc)
|
|
var created domain.UserRecord
|
|
_ = json.Unmarshal(r.Body.Bytes(), &created)
|
|
if r.Code != 201 || created.SupplierID != supplier.ID {
|
|
t.Fatalf("create scope: %d %s", r.Code, r.Body.String())
|
|
}
|
|
// An administrator can manage supplier-manager accounts.
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/"+peer.ID, []byte(`{"name":"Updated manager"}`), ac)
|
|
if r.Code != 200 {
|
|
t.Fatalf("admin update manager: %d", r.Code)
|
|
}
|
|
for _, op := range []struct{ method, path, body string }{{"PUT", "/api/users/", `{"role":"annotator"}`}, {"PUT", "/api/users/", `{"status":"disabled"}`}, {"DELETE", "/api/users/", ``}, {"DELETE", "/api/admin/users/", ``}} {
|
|
cookie := ac
|
|
if op.path == "/api/admin/users/" {
|
|
cookie = root
|
|
}
|
|
r = serveJSONWithCookie(h, op.method, op.path+admin.ID, []byte(op.body), cookie)
|
|
if r.Code != 409 {
|
|
t.Fatalf("last admin protection: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
// Disable releases pending work, preserves completed work, and invalidates sessions.
|
|
wc := loginSession(t, h, worker.Phone, "test-password")
|
|
for _, status := range []domain.HumanItemStatus{domain.HumanItemInProgress, domain.HumanItemAccepted} {
|
|
_ = ds.UpsertHumanItem(t.Context(), domain.HumanItemRecord{ID: string(status), OrganizationID: org.ID, AssignedUserID: worker.ID, Status: status, Version: 1})
|
|
}
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/"+worker.ID, []byte(`{"status":"disabled"}`), mc)
|
|
if r.Code != 200 {
|
|
t.Fatalf("disable: %d %s", r.Code, r.Body.String())
|
|
}
|
|
pending, _ := ds.GetHumanItem(t.Context(), string(domain.HumanItemInProgress))
|
|
done, _ := ds.GetHumanItem(t.Context(), string(domain.HumanItemAccepted))
|
|
if pending.AssignedUserID != "" || pending.Status != domain.HumanItemReady || done.AssignedUserID != worker.ID {
|
|
t.Fatal("incorrect task release")
|
|
}
|
|
if r = serveJSONWithCookie(h, "GET", "/api/auth/me", nil, wc); r.Code != 401 {
|
|
t.Fatal("disabled session valid")
|
|
}
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/"+worker.ID, []byte(`{"status":"active"}`), mc)
|
|
if r.Code != 200 {
|
|
t.Fatal("reactivation failed")
|
|
}
|
|
if r = serveJSONWithCookie(h, "GET", "/api/auth/me", nil, wc); r.Code != 401 {
|
|
t.Fatal("old session resurrected")
|
|
}
|
|
wc = loginSession(t, h, worker.Phone, "test-password")
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/"+worker.ID, []byte(`{"password":"reset-password"}`), mc)
|
|
if r.Code != 200 {
|
|
t.Fatal("password reset failed")
|
|
}
|
|
if r = serveJSONWithCookie(h, "GET", "/api/auth/me", nil, wc); r.Code != 401 {
|
|
t.Fatal("reset did not revoke sessions")
|
|
}
|
|
wc = loginSession(t, h, worker.Phone, "reset-password")
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/me", []byte(`{"currentPassword":"wrong","newPassword":"new-password"}`), wc)
|
|
if r.Code != http.StatusForbidden {
|
|
t.Fatal("password change did not require current password")
|
|
}
|
|
r = serveJSONWithCookie(h, "PUT", "/api/users/me", []byte(`{"name":"New name","currentPassword":"reset-password","newPassword":"new-password"}`), wc)
|
|
if r.Code != 200 {
|
|
t.Fatalf("self password: %d %s", r.Code, r.Body.String())
|
|
}
|
|
if r = serveJSONWithCookie(h, "GET", "/api/auth/me", nil, wc); r.Code != 401 {
|
|
t.Fatal("self change did not revoke session")
|
|
}
|
|
loginSession(t, h, worker.Phone, "new-password")
|
|
}
|