ch some
This commit is contained in:
@@ -35,14 +35,22 @@ func TestUserRoleAllows(t *testing.T) {
|
||||
{http.MethodPost, "/api/admin/card-orders", true},
|
||||
{http.MethodPut, "/api/admin/loan-orders/2", true},
|
||||
{http.MethodDelete, "/api/admin/loan-orders/2", true},
|
||||
{http.MethodGet, "/api/admin/card-balances", false},
|
||||
{http.MethodGet, "/api/admin/card-balances", true},
|
||||
{http.MethodPost, "/api/admin/card-balances", true},
|
||||
{http.MethodPut, "/api/admin/card-balances/9", true},
|
||||
{http.MethodDelete, "/api/admin/card-balances/9", true},
|
||||
{http.MethodGet, "/api/admin/profile", true},
|
||||
{http.MethodPut, "/api/admin/profile", true},
|
||||
{http.MethodPost, "/api/admin/file", true},
|
||||
{http.MethodDelete, "/api/admin/profile", false},
|
||||
{http.MethodGet, "/api/admin/users", false},
|
||||
{http.MethodGet, "/api/admin/activity-logs", false},
|
||||
{http.MethodGet, "/api/admin/provinces", false},
|
||||
{http.MethodGet, "/api/admin/provinces", true},
|
||||
{http.MethodPost, "/api/admin/provinces", false},
|
||||
{http.MethodGet, "/api/admin/branches", true},
|
||||
{http.MethodPost, "/api/admin/branches", false},
|
||||
{http.MethodGet, "/api/admin/users/clients", true},
|
||||
{http.MethodPost, "/api/admin/users", false},
|
||||
{http.MethodGet, "/api/admin/loan-types", true},
|
||||
{http.MethodPost, "/api/admin/loan-types", false},
|
||||
{http.MethodPost, "/api/admin/loan-orders", true},
|
||||
@@ -75,11 +83,13 @@ func TestOperatorRoleAllows(t *testing.T) {
|
||||
{http.MethodDelete, "/api/admin/card-orders/2", true},
|
||||
{http.MethodGet, "/api/admin/dashboard", true},
|
||||
{http.MethodGet, "/api/admin/users", true},
|
||||
{http.MethodGet, "/api/admin/users/clients", true},
|
||||
{http.MethodPost, "/api/admin/users", true},
|
||||
{http.MethodGet, "/api/admin/activity-logs", false},
|
||||
{http.MethodGet, "/api/admin/provinces", true},
|
||||
{http.MethodPost, "/api/admin/provinces", false},
|
||||
{http.MethodGet, "/api/admin/card-balances", false},
|
||||
{http.MethodGet, "/api/admin/card-balances", true},
|
||||
{http.MethodPost, "/api/admin/card-balances", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := operatorRoleAllows(tt.method, tt.path); got != tt.want {
|
||||
@@ -88,12 +98,124 @@ func TestOperatorRoleAllows(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserPrefixDoesNotBleedIntoOtherModules(t *testing.T) {
|
||||
if userRoleAllows(http.MethodGet, "/api/admin/card-balances") {
|
||||
t.Fatal("/cards must not grant /card-balances")
|
||||
func TestOperatorClientsListNeedsNoPermission(t *testing.T) {
|
||||
if got := operatorPathPermission(http.MethodGet, "/api/admin/users/clients"); got != "" {
|
||||
t.Fatalf("clients GET perm=%q, want none", got)
|
||||
}
|
||||
if operatorRoleAllows(http.MethodGet, "/api/admin/card-balances") {
|
||||
t.Fatal("operator /cards must not grant /card-balances")
|
||||
if got := operatorPathPermission(http.MethodGet, "/api/admin/users"); got != "users.view" {
|
||||
t.Fatalf("users GET perm=%q, want users.view", got)
|
||||
}
|
||||
|
||||
auth := &stubAuth{role: roleOperator, branches: []int64{1}, perms: map[string]bool{}}
|
||||
if status := serveGuard(auth, http.MethodGet, "/api/admin/users/clients", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("clients status=%d", status)
|
||||
}
|
||||
if status := serveGuard(auth, http.MethodGet, "/api/admin/users", "7", nil); status != http.StatusForbidden {
|
||||
t.Fatalf("users list status=%d", status)
|
||||
}
|
||||
noBranch := &stubAuth{role: roleOperator, perms: map[string]bool{}}
|
||||
if status := serveGuard(noBranch, http.MethodGet, "/api/admin/users/clients", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("no-branch clients status=%d", status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperatorCardsAndLoansSahalar(t *testing.T) {
|
||||
cardsOnly := &stubAuth{role: roleOperator, branches: []int64{1}, perms: map[string]bool{"cards": true}}
|
||||
loansOnly := &stubAuth{role: roleOperator, branches: []int64{1}, perms: map[string]bool{"loans": true}}
|
||||
both := &stubAuth{role: roleOperator, branches: []int64{1}, perms: map[string]bool{"cards": true, "loans": true}}
|
||||
|
||||
if status := serveGuard(cardsOnly, http.MethodGet, "/api/admin/card-orders", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("cards GET orders=%d", status)
|
||||
}
|
||||
if status := serveGuard(cardsOnly, http.MethodPost, "/api/admin/card-pin-orders", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("cards POST pin=%d", status)
|
||||
}
|
||||
if status := serveGuard(cardsOnly, http.MethodGet, "/api/admin/visa-master-payment-orders", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("cards GET visa=%d", status)
|
||||
}
|
||||
if status := serveGuard(cardsOnly, http.MethodGet, "/api/admin/loan-orders", "7", nil); status != http.StatusForbidden {
|
||||
t.Fatalf("cards GET loans=%d", status)
|
||||
}
|
||||
|
||||
if status := serveGuard(loansOnly, http.MethodGet, "/api/admin/loan-orders", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("loans GET orders=%d", status)
|
||||
}
|
||||
if status := serveGuard(loansOnly, http.MethodPut, "/api/admin/loan-paid-off-letters/1", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("loans PUT letter=%d", status)
|
||||
}
|
||||
if status := serveGuard(loansOnly, http.MethodGet, "/api/admin/card-orders", "7", nil); status != http.StatusForbidden {
|
||||
t.Fatalf("loans GET cards=%d", status)
|
||||
}
|
||||
|
||||
if status := serveGuard(both, http.MethodGet, "/api/admin/card-orders", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("both GET cards=%d", status)
|
||||
}
|
||||
if status := serveGuard(both, http.MethodGet, "/api/admin/loan-orders", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("both GET loans=%d", status)
|
||||
}
|
||||
|
||||
if status := serveGuard(cardsOnly, http.MethodGet, "/api/admin/card-balances", "7", nil); status != http.StatusOK {
|
||||
t.Fatalf("cards GET balances=%d", status)
|
||||
}
|
||||
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" {
|
||||
t.Fatalf("card-balances path=%q", got)
|
||||
}
|
||||
if got := operatorPathPermission(http.MethodPost, "/api/admin/card-orders"); got != "cards" {
|
||||
t.Fatalf("card-orders path=%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllCardAndLoanRoutesMapped(t *testing.T) {
|
||||
cardPaths := []string{
|
||||
"/api/admin/cards",
|
||||
"/api/admin/card-balances",
|
||||
"/api/admin/card-orders",
|
||||
"/api/admin/card-pin-orders",
|
||||
"/api/admin/card-rekwizit-orders",
|
||||
"/api/admin/visa-master-payment-orders",
|
||||
"/api/admin/payment-cards",
|
||||
"/api/admin/reasons-cards",
|
||||
}
|
||||
loanPaths := []string{
|
||||
"/api/admin/loan-orders",
|
||||
"/api/admin/loans-repayment",
|
||||
"/api/admin/loan-paid-off-letters",
|
||||
"/api/admin/loan-types",
|
||||
"/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 := operatorPathPermission(http.MethodPost, path); got != "cards" {
|
||||
t.Errorf("POST %s perm=%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 := operatorPathPermission(http.MethodPost, path); got != "loans" {
|
||||
t.Errorf("POST %s perm=%q, want loans", path, got)
|
||||
}
|
||||
if !operatorRoleAllows(http.MethodGet, path) || !operatorRoleAllows(http.MethodPost, path) {
|
||||
t.Errorf("%s not on operator allowlist", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserPrefixDoesNotBleedIntoOtherModules(t *testing.T) {
|
||||
if matchesPrefix("/card-balances", []string{"/cards"}) {
|
||||
t.Fatal("/cards prefix must not match /card-balances")
|
||||
}
|
||||
if !matchesPrefix("/card-balances", userOwnedPrefixes) {
|
||||
t.Fatal("/card-balances must be granted via its own entry in userOwnedPrefixes")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,6 +261,7 @@ func TestScopeOwnedListsInjectsUserID(t *testing.T) {
|
||||
"/api/admin/loan-orders",
|
||||
"/api/admin/loans-repayment",
|
||||
"/api/admin/loan-paid-off-letters",
|
||||
"/api/admin/card-balances",
|
||||
}
|
||||
for _, path := range paths {
|
||||
w := httptest.NewRecorder()
|
||||
@@ -151,6 +274,21 @@ func TestScopeOwnedListsInjectsUserID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScopeOwnedListsInjectsCreatedByForCardBalances(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/admin/card-balances?status=pending", nil)
|
||||
|
||||
scopeOwnedLists(c, 42)
|
||||
if got := c.Request.URL.Query().Get("created_by"); got != "42" {
|
||||
t.Fatalf("card-balances created_by=%q, want 42", got)
|
||||
}
|
||||
if got := c.Request.URL.Query().Get("status"); got != "pending" {
|
||||
t.Fatalf("existing query params must be preserved, status=%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScopeOwnedListsSkipsReferenceData(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -182,10 +320,17 @@ func TestGuardMatrix(t *testing.T) {
|
||||
{"user PUT card-orders", roleUser, nil, http.MethodPut, "/api/admin/card-orders/3", http.StatusOK},
|
||||
{"user DELETE loan-orders", roleUser, nil, http.MethodDelete, "/api/admin/loan-orders/3", http.StatusOK},
|
||||
{"user PATCH cards", roleUser, nil, http.MethodPatch, "/api/admin/cards/3", http.StatusOK},
|
||||
{"user GET card-balances", roleUser, nil, http.MethodGet, "/api/admin/card-balances", http.StatusOK},
|
||||
{"user POST card-balances", roleUser, nil, http.MethodPost, "/api/admin/card-balances", http.StatusOK},
|
||||
{"user DELETE card-balances", roleUser, nil, http.MethodDelete, "/api/admin/card-balances/3", http.StatusOK},
|
||||
{"user POST loans-repayment", roleUser, nil, http.MethodPost, "/api/admin/loans-repayment", http.StatusOK},
|
||||
{"user PUT profile", roleUser, nil, http.MethodPut, "/api/admin/profile", http.StatusOK},
|
||||
{"user POST file", roleUser, nil, http.MethodPost, "/api/admin/file", http.StatusOK},
|
||||
{"user GET users", roleUser, nil, http.MethodGet, "/api/admin/users", http.StatusForbidden},
|
||||
{"user GET users/clients", roleUser, nil, http.MethodGet, "/api/admin/users/clients", http.StatusOK},
|
||||
{"user GET branches", roleUser, nil, http.MethodGet, "/api/admin/branches", http.StatusOK},
|
||||
{"user GET provinces", roleUser, nil, http.MethodGet, "/api/admin/provinces", http.StatusOK},
|
||||
{"user POST branches", roleUser, nil, http.MethodPost, "/api/admin/branches", http.StatusForbidden},
|
||||
{"no-role GET currencies", "", nil, http.MethodGet, "/api/admin/currencies", http.StatusOK},
|
||||
{"no-role GET backups", "", nil, http.MethodGet, "/api/admin/backups", http.StatusForbidden},
|
||||
|
||||
@@ -196,6 +341,7 @@ func TestGuardMatrix(t *testing.T) {
|
||||
{"operator POST card-orders", roleOperator, []int64{1}, http.MethodPost, "/api/admin/card-orders", http.StatusOK},
|
||||
{"operator no branch GET currencies", roleOperator, nil, http.MethodGet, "/api/admin/currencies", http.StatusForbidden},
|
||||
{"operator no branch GET profile", roleOperator, nil, http.MethodGet, "/api/admin/profile", http.StatusOK},
|
||||
{"operator no branch GET users/clients", roleOperator, nil, http.MethodGet, "/api/admin/users/clients", http.StatusOK},
|
||||
|
||||
{"admin GET backups", "admin", nil, http.MethodGet, "/api/admin/backups", http.StatusOK},
|
||||
{"admin POST currencies", "admin", nil, http.MethodPost, "/api/admin/currencies", http.StatusOK},
|
||||
@@ -289,9 +435,17 @@ func TestOperatorWriteAllowedUserVisibility(t *testing.T) {
|
||||
t.Fatalf("visible user status=%d", status)
|
||||
}
|
||||
|
||||
// Creating a brand-new order for a customer who has no prior record in
|
||||
// the branch must still succeed — the branchId check alone scopes a
|
||||
// create to the operator's own branch.
|
||||
hidden := &stubAuth{role: roleOperator, branches: []int64{1}, visible: false}
|
||||
if status := serveGuard(hidden, http.MethodPost, "/api/admin/card-orders", "7", []byte(`{"userId":15}`)); status != http.StatusForbidden {
|
||||
t.Fatalf("hidden user status=%d", status)
|
||||
if status := serveGuard(hidden, http.MethodPost, "/api/admin/card-orders", "7", []byte(`{"userId":15}`)); status != http.StatusOK {
|
||||
t.Fatalf("new customer create status=%d", status)
|
||||
}
|
||||
|
||||
// Reassigning an existing order's customer still enforces visibility.
|
||||
if status := serveGuard(hidden, http.MethodPut, "/api/admin/card-orders/1", "7", []byte(`{"userId":15}`)); status != http.StatusForbidden {
|
||||
t.Fatalf("update hidden user status=%d", status)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,6 +503,40 @@ func TestAllowsBranchAndGuardUser(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGuardNewBranchAssignment(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
|
||||
// No branch scope (admin caller): always allowed.
|
||||
if !GuardNewBranchAssignment(c, 42) {
|
||||
t.Fatal("admin caller should be able to assign any user")
|
||||
}
|
||||
|
||||
// Operator scoped to branch 1.
|
||||
c.Set(contextBranchIDs, []int64{1})
|
||||
|
||||
// Target has no existing branch: first-time assignment is allowed.
|
||||
c.Set(contextService, &stubAuth{branches: nil})
|
||||
if !GuardNewBranchAssignment(c, 42) {
|
||||
t.Fatal("unbranched target should be assignable")
|
||||
}
|
||||
|
||||
// Target already belongs to the operator's own branch: no-op re-assignment is allowed.
|
||||
c.Set(contextService, &stubAuth{branches: []int64{1}})
|
||||
if !GuardNewBranchAssignment(c, 42) {
|
||||
t.Fatal("target already in caller's branch should be assignable")
|
||||
}
|
||||
|
||||
// Target belongs to a different branch: reject cross-branch poaching.
|
||||
c.Set(contextService, &stubAuth{branches: []int64{2}})
|
||||
if GuardNewBranchAssignment(c, 42) {
|
||||
t.Fatal("target from a foreign branch must not be assignable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindBranchIDs(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -402,12 +590,13 @@ func TestUserInBranchSQLIncludesNewOrderTables(t *testing.T) {
|
||||
for _, table := range []string{
|
||||
"branch_user", "card_orders", "loan_orders", "card_pin_orders",
|
||||
"card_rekwizit_orders", "visa_master_payment_orders", "loan_paid_off_letters",
|
||||
"card_balances",
|
||||
} {
|
||||
if !strings.Contains(sql, table) {
|
||||
t.Errorf("missing %s", table)
|
||||
}
|
||||
}
|
||||
if strings.Count(sql, "$3") != 7 {
|
||||
if strings.Count(sql, "$3") != 8 {
|
||||
t.Fatalf("$3 count=%d", strings.Count(sql, "$3"))
|
||||
}
|
||||
}
|
||||
@@ -460,6 +649,9 @@ func (s *stubAuth) BranchIDs(context.Context, int64) ([]int64, error) {
|
||||
}
|
||||
|
||||
func (s *stubAuth) HasPermission(_ context.Context, _ int64, code string) (bool, error) {
|
||||
if s.perms == nil {
|
||||
return true, nil
|
||||
}
|
||||
return s.perms[code], nil
|
||||
}
|
||||
|
||||
@@ -486,6 +678,7 @@ func TestEffectiveGuard(t *testing.T) {
|
||||
{"", "", guardWeb},
|
||||
{roleOperator, guardAdmin, guardOperator},
|
||||
{roleOperator, guardWeb, guardOperator},
|
||||
{"operatot", guardWeb, guardOperator},
|
||||
{"superadmin", guardWeb, guardAdmin},
|
||||
{"admin", guardWeb, guardAdmin},
|
||||
{"manager", guardOperator, guardAdmin},
|
||||
@@ -548,6 +741,34 @@ func TestEnforceOwnWritesRewritesUserID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforceOwnWritesRewritesCreatedByForCardBalances(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Set("id", "7")
|
||||
c.Next()
|
||||
})
|
||||
r.Use(Guard(&stubAuth{role: roleUser}))
|
||||
r.POST("/api/admin/card-balances", func(c *gin.Context) {
|
||||
body, _ := io.ReadAll(c.Request.Body)
|
||||
c.String(http.StatusOK, string(body))
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/admin/card-balances", bytes.NewReader([]byte(`{"createdBy":99,"cardNumber":"4111111111111111"}`)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d", w.Code)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), `"createdBy":7`) {
|
||||
t.Fatalf("createdBy not rewritten: %s", w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), `"created_by":7`) {
|
||||
t.Fatalf("created_by not rewritten: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGuardOwnedOwnScope(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
Reference in New Issue
Block a user