RezinaLMS-BackEnd/internal/services/team/roles_test.go
Yared Yemane dbdea622d4 feat: allow custom RBAC roles for team member invites
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-03 04:52:45 -07:00

113 lines
3.4 KiB
Go

package team
import (
"Yimaru-Backend/internal/domain"
"context"
"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) 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_CustomRBACRole(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"},
},
},
}
got, err := svc.resolveAndValidateTeamRole(context.Background(), "CONTENT_MANAGER_NEW")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != domain.TeamRole("CONTENT_MANAGER_NEW") {
t.Fatalf("got %q", got)
}
}
func TestResolveAndValidateTeamRole_ByRBACID(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"},
},
},
}
got, err := svc.resolveAndValidateTeamRole(context.Background(), "9")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != domain.TeamRole("CONTENT_MANAGER_NEW") {
t.Fatalf("got %q", got)
}
}
func TestResolveAndValidateTeamRole_UnknownRole(t *testing.T) {
svc := &Service{rbacStore: &stubRBACStore{roles: map[string]domain.RoleRecord{}}}
_, err := svc.resolveAndValidateTeamRole(context.Background(), "NOT_A_ROLE")
if err != domain.ErrInvalidTeamRole {
t.Fatalf("want ErrInvalidTeamRole, got %v", err)
}
}