Fix
This commit is contained in:
1 parent
7ab7acd533
commit
176b1fe47f
18 files changed
+423
-31
No files matched your search
@@ -55,3 +55,44 @@ func TestHandleUpdateAPIKeyRejectsInvalidID(t *testing.T) {
|
||||
t.Fatalf("status = %d body=%s, want 400", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// API-key management is separate from company-admin / team / billing privileges.
|
||||
// Explicit permission checks are also enforced for handler calls that do not
|
||||
// pass through the router's RequireMemberFeature middleware.
|
||||
func TestAllowAPIKeyManagerPrivilegesAndMissingGrant(t *testing.T) {
|
||||
t.Parallel()
|
||||
uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
|
||||
cid := uuid.MustParse("11111111-1111-1111-1111-111111111111")
|
||||
cases := []struct {
|
||||
name string
|
||||
role string
|
||||
platform bool
|
||||
owner bool
|
||||
want int
|
||||
}{
|
||||
{name: "company admin", role: "admin", want: http.StatusOK},
|
||||
{name: "platform admin", role: "member", platform: true, want: http.StatusOK},
|
||||
{name: "company owner", role: "member", owner: true, want: http.StatusOK},
|
||||
{name: "ordinary member without loaded permissions", role: "member", want: http.StatusForbidden},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := &Server{
|
||||
testPlatformAdmin: func(context.Context, uuid.UUID) (bool, error) { return tc.platform, nil },
|
||||
testCompanyOwner: func(context.Context, uuid.UUID) (bool, error) { return tc.owner, nil },
|
||||
}
|
||||
ctx := context.WithValue(context.Background(), ctxUserID, uid)
|
||||
ctx = context.WithValue(ctx, ctxCompanyID, cid)
|
||||
ctx = context.WithValue(ctx, ctxRole, tc.role)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/api-keys", nil).WithContext(ctx)
|
||||
rec := httptest.NewRecorder()
|
||||
allowed := s.allowAPIKeyManager(rec, req)
|
||||
if allowed != (tc.want == http.StatusOK) {
|
||||
t.Fatalf("allowAPIKeyManager allowed=%v status=%d", allowed, rec.Code)
|
||||
}
|
||||
if !allowed && rec.Code != tc.want {
|
||||
t.Fatalf("status=%d body=%s, want %d", rec.Code, rec.Body.String(), tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user