mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): add admin endpoint to issue keys for masters
Add `POST /admin/masters/{id}/keys` allowing admins to issue child keys
on behalf of a master. Introduce an `issued_by` field in the Key model
to audit whether a key was issued by the master or an admin.
Refactor master service to use typed errors for consistent HTTP status
mapping and ensure validation logic (active status, group check) is
shared.
This commit is contained in:
@@ -67,3 +67,31 @@ func TestMasterService_IssueChildKey_RespectsLimit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMasterService_IssueChildKeyAsAdmin_SetsIssuedBy(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
svc := NewMasterService(db)
|
||||
|
||||
m, _, err := svc.CreateMaster("m1", "default", 2, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateMaster: %v", err)
|
||||
}
|
||||
|
||||
key, raw, err := svc.IssueChildKeyAsAdmin(m.ID, "", "chat:write")
|
||||
if err != nil {
|
||||
t.Fatalf("IssueChildKeyAsAdmin: %v", err)
|
||||
}
|
||||
if raw == "" {
|
||||
t.Fatalf("expected raw child key")
|
||||
}
|
||||
if key.IssuedBy != "admin" {
|
||||
t.Fatalf("expected IssuedBy=admin, got %q", key.IssuedBy)
|
||||
}
|
||||
|
||||
var stored model.Key
|
||||
if err := db.First(&stored, key.ID).Error; err != nil {
|
||||
t.Fatalf("load key: %v", err)
|
||||
}
|
||||
if stored.IssuedBy != "admin" {
|
||||
t.Fatalf("expected stored IssuedBy=admin, got %q", stored.IssuedBy)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user