Sistemato magazzino
This commit is contained in:
@@ -10,6 +10,7 @@ process.env.KEYCLOAK_MAGAZZINO_CLIENT_SECRET = 'test-secret';
|
||||
process.env.DATABASE_URL = 'postgresql://user:pass@localhost:5432/scouthub_magazzino_test';
|
||||
|
||||
const tipoEventoFindMany = jest.fn();
|
||||
const tipoEventoFindUnique = jest.fn();
|
||||
const tipoEventoCreate = jest.fn();
|
||||
const tipoEventoUpdate = jest.fn();
|
||||
const tipoEventoDelete = jest.fn();
|
||||
@@ -18,6 +19,7 @@ jest.mock('../../src/db/prisma', () => ({
|
||||
prisma: {
|
||||
tipoEvento: {
|
||||
findMany: (...args: unknown[]) => tipoEventoFindMany(...args),
|
||||
findUnique: (...args: unknown[]) => tipoEventoFindUnique(...args),
|
||||
create: (...args: unknown[]) => tipoEventoCreate(...args),
|
||||
update: (...args: unknown[]) => tipoEventoUpdate(...args),
|
||||
delete: (...args: unknown[]) => tipoEventoDelete(...args),
|
||||
@@ -39,11 +41,11 @@ function signToken(payload: object): string {
|
||||
return jwt.sign(payload, privateKeyPem, { algorithm: 'RS256', keyid: KID, expiresIn: '5m' });
|
||||
}
|
||||
|
||||
function utenteToken(): string {
|
||||
function utenteToken(orgId = 'org-1'): string {
|
||||
return signToken({
|
||||
sub: 'user-1',
|
||||
realm_access: { roles: ['censito'] },
|
||||
organization: { 'gruppo-alfa': { id: 'org-1', roles: [] } },
|
||||
organization: { 'gruppo-alfa': { id: orgId, roles: [] } },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,13 +72,49 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
describe('GET /tipi-evento', () => {
|
||||
test('è pubblico e restituisce la lista', async () => {
|
||||
tipoEventoFindMany.mockResolvedValueOnce([{ id: 't-1', nome: 'Campo estivo' }]);
|
||||
test('è pubblico e restituisce solo i tipi evento confermati', async () => {
|
||||
tipoEventoFindMany.mockResolvedValueOnce([{ id: 't-1', nome: 'Campo estivo', stato: 'confermata' }]);
|
||||
|
||||
const response = await request(app).get('/tipi-evento');
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual([{ id: 't-1', nome: 'Campo estivo' }]);
|
||||
expect(response.body).toEqual([{ id: 't-1', nome: 'Campo estivo', stato: 'confermata' }]);
|
||||
expect(tipoEventoFindMany).toHaveBeenCalledWith({
|
||||
where: { stato: 'confermata' },
|
||||
orderBy: { nome: 'asc' },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /tipi-evento/moderazione', () => {
|
||||
test('un utente normale non può accedere', async () => {
|
||||
const response = await request(app)
|
||||
.get('/tipi-evento/moderazione')
|
||||
.set('Authorization', `Bearer ${utenteToken()}`);
|
||||
|
||||
expect(response.status).toBe(403);
|
||||
expect(tipoEventoFindMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('un moderatore vede tutti i tipi evento, incluse le proposte', async () => {
|
||||
tipoEventoFindMany.mockResolvedValueOnce([
|
||||
{ id: 't-1', nome: 'Campo estivo', stato: 'confermata' },
|
||||
{ id: 't-2', nome: 'Bivacco', stato: 'da_approvare' },
|
||||
]);
|
||||
|
||||
const response = await request(app)
|
||||
.get('/tipi-evento/moderazione')
|
||||
.set('Authorization', `Bearer ${adminCatalogoToken()}`);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toHaveLength(2);
|
||||
});
|
||||
|
||||
test('risponde 401 senza token', async () => {
|
||||
const response = await request(app).get('/tipi-evento/moderazione');
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(tipoEventoFindMany).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -91,8 +129,8 @@ describe('POST /tipi-evento', () => {
|
||||
expect(tipoEventoCreate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('un moderatore può creare un tipo evento', async () => {
|
||||
tipoEventoCreate.mockResolvedValueOnce({ id: 't-2', nome: 'Bivacco' });
|
||||
test('un moderatore crea un tipo evento già confermato', async () => {
|
||||
tipoEventoCreate.mockResolvedValueOnce({ id: 't-2', nome: 'Bivacco', stato: 'confermata' });
|
||||
|
||||
const response = await request(app)
|
||||
.post('/tipi-evento')
|
||||
@@ -100,7 +138,7 @@ describe('POST /tipi-evento', () => {
|
||||
.send({ nome: 'Bivacco' });
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(tipoEventoCreate).toHaveBeenCalledWith({ data: { nome: 'Bivacco' } });
|
||||
expect(tipoEventoCreate).toHaveBeenCalledWith({ data: { nome: 'Bivacco', stato: 'confermata' } });
|
||||
});
|
||||
|
||||
test('risponde 401 senza token', async () => {
|
||||
@@ -111,6 +149,35 @@ describe('POST /tipi-evento', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /tipi-evento/proposte', () => {
|
||||
test("un utente autenticato con un'org propone un tipo evento, che nasce 'da_approvare'", async () => {
|
||||
tipoEventoCreate.mockResolvedValueOnce({
|
||||
id: 't-3',
|
||||
nome: 'Uscita notturna',
|
||||
stato: 'da_approvare',
|
||||
creatoDaOrgId: 'org-1',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.post('/tipi-evento/proposte')
|
||||
.set('Authorization', `Bearer ${utenteToken('org-1')}`)
|
||||
.send({ nome: 'Uscita notturna' });
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body).toMatchObject({ stato: 'da_approvare', creatoDaOrgId: 'org-1' });
|
||||
expect(tipoEventoCreate).toHaveBeenCalledWith({
|
||||
data: { nome: 'Uscita notturna', stato: 'da_approvare', creatoDaOrgId: 'org-1' },
|
||||
});
|
||||
});
|
||||
|
||||
test('risponde 401 senza token', async () => {
|
||||
const response = await request(app).post('/tipi-evento/proposte').send({ nome: 'Uscita notturna' });
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(tipoEventoCreate).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /tipi-evento/:id', () => {
|
||||
test('un utente normale non può modificare un tipo evento', async () => {
|
||||
const response = await request(app)
|
||||
@@ -159,3 +226,65 @@ describe('DELETE /tipi-evento/:id', () => {
|
||||
expect(tipoEventoDelete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /tipi-evento/:id/approva', () => {
|
||||
test('un moderatore può approvare un tipo evento proposto', async () => {
|
||||
tipoEventoFindUnique.mockResolvedValueOnce({ id: 't-3', nome: 'Uscita notturna', stato: 'da_approvare' });
|
||||
tipoEventoUpdate.mockResolvedValueOnce({ id: 't-3', nome: 'Uscita notturna', stato: 'confermata' });
|
||||
|
||||
const response = await request(app)
|
||||
.post('/tipi-evento/t-3/approva')
|
||||
.set('Authorization', `Bearer ${adminCatalogoToken()}`);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.stato).toBe('confermata');
|
||||
expect(tipoEventoUpdate).toHaveBeenCalledWith({ where: { id: 't-3' }, data: { stato: 'confermata' } });
|
||||
});
|
||||
|
||||
test('risponde 409 se il tipo evento è già confermato', async () => {
|
||||
tipoEventoFindUnique.mockResolvedValueOnce({ id: 't-1', nome: 'Campo estivo', stato: 'confermata' });
|
||||
|
||||
const response = await request(app)
|
||||
.post('/tipi-evento/t-1/approva')
|
||||
.set('Authorization', `Bearer ${adminCatalogoToken()}`);
|
||||
|
||||
expect(response.status).toBe(409);
|
||||
expect(tipoEventoUpdate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('risponde 404 se il tipo evento non esiste', async () => {
|
||||
tipoEventoFindUnique.mockResolvedValueOnce(null);
|
||||
|
||||
const response = await request(app)
|
||||
.post('/tipi-evento/inesistente/approva')
|
||||
.set('Authorization', `Bearer ${adminCatalogoToken()}`);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(tipoEventoUpdate).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /tipi-evento/:id/rifiuta', () => {
|
||||
test('un moderatore può rifiutare un tipo evento proposto, che viene eliminato', async () => {
|
||||
tipoEventoFindUnique.mockResolvedValueOnce({ id: 't-3', nome: 'Uscita notturna', stato: 'da_approvare' });
|
||||
tipoEventoDelete.mockResolvedValueOnce({ id: 't-3' });
|
||||
|
||||
const response = await request(app)
|
||||
.post('/tipi-evento/t-3/rifiuta')
|
||||
.set('Authorization', `Bearer ${adminCatalogoToken()}`);
|
||||
|
||||
expect(response.status).toBe(204);
|
||||
expect(tipoEventoDelete).toHaveBeenCalledWith({ where: { id: 't-3' } });
|
||||
});
|
||||
|
||||
test('risponde 409 se il tipo evento è già confermato', async () => {
|
||||
tipoEventoFindUnique.mockResolvedValueOnce({ id: 't-1', nome: 'Campo estivo', stato: 'confermata' });
|
||||
|
||||
const response = await request(app)
|
||||
.post('/tipi-evento/t-1/rifiuta')
|
||||
.set('Authorization', `Bearer ${adminCatalogoToken()}`);
|
||||
|
||||
expect(response.status).toBe(409);
|
||||
expect(tipoEventoDelete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user