ch logika
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
guwandyk committed 2026-09-18 11:53:37 +05:00
1 parent e0d03730de
commit d3e188affe
28 files changed
+405 -656

No files matched your search

+83 -10
View File
@@ -160,10 +160,10 @@ func TestOperatorCardsAndLoansSahalar(t *testing.T) {
if status := serveGuard(loansOnly, http.MethodGet, "/api/admin/card-balances", "7", nil); status != http.StatusForbidden {
t.Fatalf("loans GET balances=%d", status)
}
if got := operatorPathPermission(http.MethodGet, "/api/admin/card-balances"); got != "cards" {
if got := operatorPathModule(http.MethodGet, "/api/admin/card-balances"); got != "cards" {
t.Fatalf("card-balances path=%q", got)
}
if got := operatorPathPermission(http.MethodPost, "/api/admin/card-orders"); got != "cards" {
if got := operatorPathModule(http.MethodPost, "/api/admin/card-orders"); got != "cards" {
t.Fatalf("card-orders path=%q", got)
}
}
@@ -187,22 +187,22 @@ func TestAllCardAndLoanRoutesMapped(t *testing.T) {
"/api/admin/loans-documents",
}
for _, path := range cardPaths {
if got := operatorPathPermission(http.MethodGet, path); got != "cards" {
t.Errorf("GET %s perm=%q, want cards", path, got)
if got := operatorPathModule(http.MethodGet, path); got != "cards" {
t.Errorf("GET %s module=%q, want cards", path, got)
}
if got := operatorPathPermission(http.MethodPost, path); got != "cards" {
t.Errorf("POST %s perm=%q, want cards", path, got)
if got := operatorPathModule(http.MethodPost, path); got != "cards" {
t.Errorf("POST %s module=%q, want cards", path, got)
}
if !operatorRoleAllows(http.MethodGet, path) || !operatorRoleAllows(http.MethodPost, path) {
t.Errorf("%s not on operator allowlist", path)
}
}
for _, path := range loanPaths {
if got := operatorPathPermission(http.MethodGet, path); got != "loans" {
t.Errorf("GET %s perm=%q, want loans", path, got)
if got := operatorPathModule(http.MethodGet, path); got != "loans" {
t.Errorf("GET %s module=%q, want loans", path, got)
}
if got := operatorPathPermission(http.MethodPost, path); got != "loans" {
t.Errorf("POST %s perm=%q, want loans", path, got)
if got := operatorPathModule(http.MethodPost, path); got != "loans" {
t.Errorf("POST %s module=%q, want loans", path, got)
}
if !operatorRoleAllows(http.MethodGet, path) || !operatorRoleAllows(http.MethodPost, path) {
t.Errorf("%s not on operator allowlist", path)
@@ -378,6 +378,72 @@ func TestGuardMatrix(t *testing.T) {
}
}
func TestAdminPathPermission(t *testing.T) {
tests := []struct {
method, path string
want string
}{
{http.MethodGet, "/api/admin/users", "users.view"},
{http.MethodPost, "/api/admin/users", "users.manage"},
{http.MethodGet, "/api/admin/branch-users", "users.view"},
{http.MethodGet, "/api/admin/roles", "roles.view"},
{http.MethodPost, "/api/admin/roles", "roles.manage"},
{http.MethodGet, "/api/admin/permissions", "roles.view"},
{http.MethodPost, "/api/admin/permission-roles", "roles.manage"},
{http.MethodDelete, "/api/admin/role-users/1", "roles.manage"},
{http.MethodGet, "/api/admin/branches", "branches.view"},
{http.MethodPost, "/api/admin/branches", "branches.manage"},
{http.MethodGet, "/api/admin/provinces", "provinces.view"},
{http.MethodPost, "/api/admin/provinces", "provinces.manage"},
{http.MethodGet, "/api/admin/activity-logs", "activity-logs.view"},
{http.MethodGet, "/api/admin/backups", "backups.view"},
{http.MethodPost, "/api/admin/backups", "backups.manage"},
{http.MethodGet, "/api/admin/currencies", "currencies.view"},
{http.MethodPost, "/api/admin/currencies", "currencies.manage"},
{http.MethodGet, "/api/admin/dashboard", ""},
{http.MethodGet, "/api/admin/profile", ""},
{http.MethodPost, "/api/admin/file", ""},
}
for _, tt := range tests {
if got := adminPathPermission(tt.method, tt.path); got != tt.want {
t.Errorf("adminPathPermission(%s %s)=%q want %q", tt.method, tt.path, got, tt.want)
}
}
}
func TestEnforceAdminSuperadminBypassesEverything(t *testing.T) {
// perms is a non-nil empty map, so every HasPermission/HasModuleAccess
// lookup would deny — superadmin must still pass without ever checking.
auth := &stubAuth{role: roleSuperadmin, perms: map[string]bool{}}
if status := serveGuard(auth, http.MethodGet, "/api/admin/backups", "7", nil); status != http.StatusOK {
t.Fatalf("superadmin GET backups=%d", status)
}
if status := serveGuard(auth, http.MethodDelete, "/api/admin/users/1", "7", nil); status != http.StatusOK {
t.Fatalf("superadmin DELETE users=%d", status)
}
}
func TestEnforceAdminChecksPermission(t *testing.T) {
admin := &stubAuth{role: "admin", perms: map[string]bool{}}
if status := serveGuard(admin, http.MethodGet, "/api/admin/backups", "7", nil); status != http.StatusForbidden {
t.Fatalf("admin without grant GET backups=%d", status)
}
adminWithGrant := &stubAuth{role: "admin", perms: map[string]bool{"backups.view": true}}
if status := serveGuard(adminWithGrant, http.MethodGet, "/api/admin/backups", "7", nil); status != http.StatusOK {
t.Fatalf("admin with grant GET backups=%d", status)
}
manager := &stubAuth{role: "manager", perms: map[string]bool{"cards": true}}
if status := serveGuard(manager, http.MethodGet, "/api/admin/card-orders", "7", nil); status != http.StatusOK {
t.Fatalf("manager GET card-orders=%d", status)
}
if status := serveGuard(manager, http.MethodGet, "/api/admin/currencies", "7", nil); status != http.StatusForbidden {
t.Fatalf("manager without currencies grant GET currencies=%d", status)
}
}
func TestGuardOptionsAndPublicSkipAuthz(t *testing.T) {
gin.SetMode(gin.TestMode)
auth := &stubAuth{role: roleUser}
@@ -655,6 +721,13 @@ func (s *stubAuth) HasPermission(_ context.Context, _ int64, code string) (bool,
return s.perms[code], nil
}
func (s *stubAuth) HasModuleAccess(_ context.Context, _ int64, module string) (bool, error) {
if s.perms == nil {
return true, nil
}
return s.perms[module], nil
}
func (s *stubAuth) UserVisibleInBranches(context.Context, int64, []int64) (bool, error) {
return s.visible, s.visibleErr
}