|
1 | 1 | import request from "supertest"; |
2 | 2 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 3 |
|
| 4 | +const ROLE_LEVEL = { |
| 5 | + user: 0, |
| 6 | + support: 1, |
| 7 | + admin: 2, |
| 8 | + super_admin: 3, |
| 9 | +} as const; |
| 10 | + |
| 11 | +const PERMISSION_MIN_ROLE = { |
| 12 | + "dashboard.read": "support", |
| 13 | + "scrapers.read": "support", |
| 14 | + "observability.health": "support", |
| 15 | + "users.read": "admin", |
| 16 | + "users.block": "admin", |
| 17 | + "users.reset_password": "admin", |
| 18 | + "scrapers.trigger": "admin", |
| 19 | + "observability.metrics": "admin", |
| 20 | + "audit.read": "admin", |
| 21 | + "users.change_role": "super_admin", |
| 22 | + "users.delete": "super_admin", |
| 23 | +} as const; |
| 24 | + |
4 | 25 | const mocks = vi.hoisted(() => ({ |
5 | 26 | dashboard: vi.fn((_req, res) => res.json({ ok: true, scope: "dashboard" })), |
6 | 27 | usersList: vi.fn((_req, res) => res.json({ data: [], total: 0 })), |
@@ -63,6 +84,17 @@ vi.mock("iron-session", () => ({ |
63 | 84 | getIronSession: vi.fn(), |
64 | 85 | })); |
65 | 86 |
|
| 87 | +vi.mock("../../../src/modules/admin/permissions/permissions.service", () => ({ |
| 88 | + permissionsService: { |
| 89 | + can: vi.fn(async (role: keyof typeof ROLE_LEVEL, resource: string, action: string) => { |
| 90 | + const key = `${resource}.${action}` as keyof typeof PERMISSION_MIN_ROLE; |
| 91 | + const minRole = PERMISSION_MIN_ROLE[key]; |
| 92 | + if (!minRole) return false; |
| 93 | + return ROLE_LEVEL[role] >= ROLE_LEVEL[minRole]; |
| 94 | + }), |
| 95 | + }, |
| 96 | +})); |
| 97 | + |
66 | 98 | import { getIronSession } from "iron-session"; |
67 | 99 | import { createJobsApiApp } from "../../../src/app"; |
68 | 100 |
|
|
0 commit comments