RezinaLMS-BackEnd/internal/services/team/roles_test.go
Yared Yemane df4f4d8ade Limit staff team roles to Super Admin, Admin, and Content Manager.
Add CONTENT_MANAGER RBAC defaults, migration, and Super Admin-only team email invitations; restrict assignable team_member roles without changing platform user roles.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 12:01:02 +03:00

112 lines
3.5 KiB
Go

package team
import (
"Yimaru-Backend/internal/domain"
"context"
"errors"
"strings"
"testing"
"github.com/jackc/pgx/v5"
)
type stubRBACStore struct {
roles map[string]domain.RoleRecord
byID map[int64]domain.RoleRecord
}
func (s *stubRBACStore) CreateRole(context.Context, string, string, bool) (domain.RoleRecord, error) {
panic("not implemented")
}
func (s *stubRBACStore) GetRoleByID(_ context.Context, id int64) (domain.RoleRecord, error) {
if r, ok := s.byID[id]; ok {
return r, nil
}
return domain.RoleRecord{}, pgx.ErrNoRows
}
func (s *stubRBACStore) GetRoleByName(_ context.Context, name string) (domain.RoleRecord, error) {
if r, ok := s.roles[name]; ok {
return r, nil
}
return domain.RoleRecord{}, pgx.ErrNoRows
}
func (s *stubRBACStore) GetRoleByNameIgnoreCase(_ context.Context, name string) (domain.RoleRecord, error) {
upper := strings.ToUpper(strings.TrimSpace(name))
for k, r := range s.roles {
if strings.ToUpper(strings.TrimSpace(k)) == upper {
return r, nil
}
}
return domain.RoleRecord{}, pgx.ErrNoRows
}
func (s *stubRBACStore) ListRoles(context.Context, domain.RoleListFilter) ([]domain.RoleRecord, int64, error) {
panic("not implemented")
}
func (s *stubRBACStore) UpdateRole(context.Context, int64, string, string) error {
panic("not implemented")
}
func (s *stubRBACStore) DeleteRole(context.Context, int64) error { panic("not implemented") }
func (s *stubRBACStore) UpsertPermission(context.Context, domain.PermissionSeed) (domain.Permission, error) {
panic("not implemented")
}
func (s *stubRBACStore) ListPermissions(context.Context) ([]domain.Permission, error) {
panic("not implemented")
}
func (s *stubRBACStore) ListPermissionGroups(context.Context) ([]string, error) {
panic("not implemented")
}
func (s *stubRBACStore) GetPermissionByKey(context.Context, string) (domain.Permission, error) {
panic("not implemented")
}
func (s *stubRBACStore) SetRolePermissions(context.Context, int64, []int64) error {
panic("not implemented")
}
func (s *stubRBACStore) AddRolePermissions(context.Context, int64, []int64) error {
panic("not implemented")
}
func (s *stubRBACStore) GetRolePermissions(context.Context, int64) ([]domain.Permission, error) {
panic("not implemented")
}
func (s *stubRBACStore) GetAllRolesWithPermissions(context.Context) (map[string]map[string]struct{}, error) {
panic("not implemented")
}
func TestResolveAndValidateTeamRole_BuiltinContentManager(t *testing.T) {
svc := &Service{rbacStore: &stubRBACStore{roles: map[string]domain.RoleRecord{}}}
got, err := svc.resolveAndValidateTeamRole(context.Background(), "CONTENT_MANAGER")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != domain.TeamRoleContentManager {
t.Fatalf("got %q", got)
}
}
func TestResolveAndValidateTeamRole_CustomRBACRoleRejected(t *testing.T) {
svc := &Service{
rbacStore: &stubRBACStore{
roles: map[string]domain.RoleRecord{
"CONTENT_MANAGER_NEW": {ID: 9, Name: "CONTENT_MANAGER_NEW"},
},
byID: map[int64]domain.RoleRecord{
9: {ID: 9, Name: "CONTENT_MANAGER_NEW"},
},
},
}
_, err := svc.resolveAndValidateTeamRole(context.Background(), "CONTENT_MANAGER_NEW")
if !errors.Is(err, domain.ErrInvalidTeamRole) {
t.Fatalf("want ErrInvalidTeamRole, got %v", err)
}
}
func TestResolveAndValidateTeamRole_UnknownRole(t *testing.T) {
svc := &Service{rbacStore: &stubRBACStore{roles: map[string]domain.RoleRecord{}}}
_, err := svc.resolveAndValidateTeamRole(context.Background(), "NOT_A_ROLE")
if !errors.Is(err, domain.ErrInvalidTeamRole) {
t.Fatalf("want ErrInvalidTeamRole, got %v", err)
}
}