Fix scouthub-home-be
This commit is contained in:
@@ -14,6 +14,7 @@ const createOrganization = jest.fn();
|
||||
const createOrganizationGroup = jest.fn();
|
||||
const addMemberToOrganization = jest.fn();
|
||||
const gruppoScoutCreate = jest.fn();
|
||||
const gruppoScoutFindMany = jest.fn();
|
||||
|
||||
jest.mock('../../src/keycloak-admin', () => ({
|
||||
createOrganization: (...args: unknown[]) => createOrganization(...args),
|
||||
@@ -25,6 +26,7 @@ jest.mock('../../src/db/prisma', () => ({
|
||||
prisma: {
|
||||
gruppoScout: {
|
||||
create: (...args: unknown[]) => gruppoScoutCreate(...args),
|
||||
findMany: (...args: unknown[]) => gruppoScoutFindMany(...args),
|
||||
},
|
||||
},
|
||||
}));
|
||||
@@ -47,7 +49,7 @@ function signToken(roles: string[]): string {
|
||||
);
|
||||
}
|
||||
|
||||
const adminToken = () => signToken(['admin-centrale']);
|
||||
const adminToken = () => signToken(['admin']);
|
||||
const nonAdminToken = () => signToken(['capo-gruppo']);
|
||||
|
||||
function conflictError(): Error {
|
||||
@@ -166,7 +168,7 @@ describe('POST /gruppi', () => {
|
||||
consoleErrorSpy.mockRestore();
|
||||
});
|
||||
|
||||
test('risponde 403 se il ruolo non è admin-centrale', async () => {
|
||||
test('risponde 403 se il ruolo non è admin', async () => {
|
||||
const response = await request(app)
|
||||
.post('/gruppi')
|
||||
.set('Authorization', `Bearer ${nonAdminToken()}`)
|
||||
@@ -193,3 +195,68 @@ describe('POST /gruppi', () => {
|
||||
expect(createOrganization).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /gruppi', () => {
|
||||
test('restituisce 200 con la lista dei gruppi scout per admin', async () => {
|
||||
gruppoScoutFindMany.mockResolvedValueOnce([
|
||||
{ orgId: 'org-1', nome: 'Gruppo Alfa', regione: 'Lombardia' },
|
||||
{ orgId: 'org-2', nome: 'Gruppo Beta', regione: null },
|
||||
]);
|
||||
|
||||
const response = await request(app).get('/gruppi').set('Authorization', `Bearer ${adminToken()}`);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual([
|
||||
{ orgId: 'org-1', nome: 'Gruppo Alfa', regione: 'Lombardia' },
|
||||
{ orgId: 'org-2', nome: 'Gruppo Beta', regione: null },
|
||||
]);
|
||||
expect(gruppoScoutFindMany).toHaveBeenCalledWith({
|
||||
select: { orgId: true, nome: true, regione: true },
|
||||
orderBy: { nome: 'asc' },
|
||||
});
|
||||
});
|
||||
|
||||
test('risponde 403 se il ruolo non è admin', async () => {
|
||||
const response = await request(app).get('/gruppi').set('Authorization', `Bearer ${nonAdminToken()}`);
|
||||
|
||||
expect(response.status).toBe(403);
|
||||
expect(gruppoScoutFindMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('risponde 401 senza token', async () => {
|
||||
const response = await request(app).get('/gruppi');
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(gruppoScoutFindMany).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /gruppi/elenco-pubblico', () => {
|
||||
test('restituisce 200 con solo orgId/nome, accessibile a un utente non admin', async () => {
|
||||
gruppoScoutFindMany.mockResolvedValueOnce([
|
||||
{ orgId: 'org-1', nome: 'Gruppo Alfa' },
|
||||
{ orgId: 'org-2', nome: 'Gruppo Beta' },
|
||||
]);
|
||||
|
||||
const response = await request(app)
|
||||
.get('/gruppi/elenco-pubblico')
|
||||
.set('Authorization', `Bearer ${nonAdminToken()}`);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual([
|
||||
{ orgId: 'org-1', nome: 'Gruppo Alfa' },
|
||||
{ orgId: 'org-2', nome: 'Gruppo Beta' },
|
||||
]);
|
||||
expect(gruppoScoutFindMany).toHaveBeenCalledWith({
|
||||
select: { orgId: true, nome: true },
|
||||
orderBy: { nome: 'asc' },
|
||||
});
|
||||
});
|
||||
|
||||
test('risponde 401 senza token', async () => {
|
||||
const response = await request(app).get('/gruppi/elenco-pubblico');
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(gruppoScoutFindMany).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user