ch some
This commit is contained in:
97
internal/identity/user/handler_test.go
Normal file
97
internal/identity/user/handler_test.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// stubRoleRepo only implements RoleGuardName meaningfully - the other
|
||||
// methods exist solely to satisfy the Repository interface, since
|
||||
// roleAssignmentAllowed never calls them.
|
||||
type stubRoleRepo struct {
|
||||
guards map[string]string
|
||||
}
|
||||
|
||||
func (s *stubRoleRepo) List(context.Context, ListFilter) (ListUserDTO, error) {
|
||||
return ListUserDTO{}, nil
|
||||
}
|
||||
func (s *stubRoleRepo) ListStaff(context.Context, ListFilter) (ListUserDTO, error) {
|
||||
return ListUserDTO{}, nil
|
||||
}
|
||||
func (s *stubRoleRepo) GetByID(context.Context, int64) (UserDTO, error) { return UserDTO{}, nil }
|
||||
func (s *stubRoleRepo) GetByRole(context.Context, string, ListFilter) (ListUserDTO, error) {
|
||||
return ListUserDTO{}, nil
|
||||
}
|
||||
func (s *stubRoleRepo) Create(context.Context, CreateUserDTO) (UserDTO, error) { return UserDTO{}, nil }
|
||||
func (s *stubRoleRepo) Update(context.Context, int64, UpdateUserDTO) (UserDTO, error) {
|
||||
return UserDTO{}, nil
|
||||
}
|
||||
func (s *stubRoleRepo) Delete(context.Context, int64) error { return nil }
|
||||
func (s *stubRoleRepo) RoleGuardName(_ context.Context, code string) (string, error) {
|
||||
return s.guards[code], nil
|
||||
}
|
||||
|
||||
var _ Repository = (*stubRoleRepo)(nil)
|
||||
|
||||
func TestRoleAssignmentAllowed(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// Mirrors db/seed.sql's roles table, plus a role added after this code
|
||||
// was written - to prove the check follows guard_name, not a hardcoded
|
||||
// list of role names.
|
||||
repo := &stubRoleRepo{guards: map[string]string{
|
||||
"user": "web",
|
||||
"operator": "operator",
|
||||
"admin": "admin",
|
||||
"superadmin": "admin",
|
||||
"manager": "admin",
|
||||
"currency_maintainer": "admin",
|
||||
"custom_branch_ops": "operator",
|
||||
}}
|
||||
h := &handler{repository: repo}
|
||||
|
||||
newContext := func(guard string) *gin.Context {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodPut, "/", nil)
|
||||
if guard != "" {
|
||||
c.Set("guard", guard)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
admin := newContext("admin")
|
||||
for _, role := range []string{"", "user", "operator", "admin", "superadmin", "manager", "currency_maintainer", "unknown-role"} {
|
||||
allowed, err := h.roleAssignmentAllowed(admin, role)
|
||||
if err != nil {
|
||||
t.Fatalf("role %q: unexpected error %v", role, err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Fatalf("admin guard should be able to assign role %q", role)
|
||||
}
|
||||
}
|
||||
|
||||
operator := newContext("operator")
|
||||
for _, role := range []string{"", "user", "operator", "USER", " Operator ", "custom_branch_ops"} {
|
||||
allowed, err := h.roleAssignmentAllowed(operator, role)
|
||||
if err != nil {
|
||||
t.Fatalf("role %q: unexpected error %v", role, err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Fatalf("operator guard should be able to assign role %q", role)
|
||||
}
|
||||
}
|
||||
for _, role := range []string{"admin", "superadmin", "manager", "currency_maintainer", "unknown-role"} {
|
||||
allowed, err := h.roleAssignmentAllowed(operator, role)
|
||||
if err != nil {
|
||||
t.Fatalf("role %q: unexpected error %v", role, err)
|
||||
}
|
||||
if allowed {
|
||||
t.Fatalf("operator guard must not be able to assign role %q", role)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user