2026-07-04 20:00:51 +04:00
|
|
|
import {describe, it, expect, vi} from 'vitest';
|
2026-07-04 09:43:28 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const {default: createController} = await import('../commentaire.js');
|
2026-07-04 09:43:28 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const dbUser = {id: 1, username: 'foo', email: 'foo@bar.com'};
|
|
|
|
|
const dbParole = {id: 7, documentId: 'parole-doc-7'};
|
2026-07-04 09:43:28 +04:00
|
|
|
|
2026-07-04 10:04:00 +04:00
|
|
|
function buildStrapi({existingParole = dbParole} = {}) {
|
2026-07-04 09:43:28 +04:00
|
|
|
const commentaireDocuments = {
|
|
|
|
|
create: vi.fn(async ({data}) => ({id: 99, ...data}))
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:43:28 +04:00
|
|
|
const paroleDocuments = {
|
|
|
|
|
update: vi.fn(async () => {})
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:43:28 +04:00
|
|
|
const userDbQuery = {
|
|
|
|
|
findOne: vi.fn(async ({where}) => (where.id === dbUser.id ? dbUser : null))
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:43:28 +04:00
|
|
|
const paroleDbQuery = {
|
|
|
|
|
findOne: vi.fn(async ({where}) => (where.id === existingParole?.id ? existingParole : null))
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:43:28 +04:00
|
|
|
|
|
|
|
|
const strapi = {
|
|
|
|
|
contentType: vi.fn(() => ({uid: 'api::commentaire.commentaire', kind: 'collectionType'})),
|
|
|
|
|
db: {
|
|
|
|
|
query: vi.fn(uid => {
|
2026-07-04 20:00:51 +04:00
|
|
|
if (uid === 'plugin::users-permissions.user') return userDbQuery;
|
|
|
|
|
if (uid === 'api::parole.parole') return paroleDbQuery;
|
|
|
|
|
throw new Error(`unexpected uid: ${uid}`);
|
2026-07-04 09:43:28 +04:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
documents: vi.fn(uid => {
|
2026-07-04 20:00:51 +04:00
|
|
|
if (uid === 'api::commentaire.commentaire') return commentaireDocuments;
|
|
|
|
|
if (uid === 'api::parole.parole') return paroleDocuments;
|
|
|
|
|
throw new Error(`unexpected uid: ${uid}`);
|
2026-07-04 09:43:28 +04:00
|
|
|
})
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:43:28 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
return {strapi, commentaireDocuments, paroleDocuments, userDbQuery, paroleDbQuery};
|
2026-07-04 09:43:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildCtx(data) {
|
|
|
|
|
return {
|
|
|
|
|
request: {
|
|
|
|
|
body: {data},
|
|
|
|
|
header: {authorization: 'Bearer faketoken'}
|
|
|
|
|
}
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:43:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildData(overrides = {}) {
|
|
|
|
|
return {
|
|
|
|
|
contenu: 'Un commentaire',
|
|
|
|
|
datePublication: '2026-07-04',
|
|
|
|
|
parole: dbParole.id,
|
|
|
|
|
user: {...dbUser},
|
|
|
|
|
...overrides
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:43:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('commentaire.create', () => {
|
|
|
|
|
it('retrouve la parole par son id (pas par le documentId du user) et l\'associe correctement', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, commentaireDocuments, paroleDocuments, paroleDbQuery} = buildStrapi();
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData());
|
2026-07-04 09:43:28 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await controller.create(ctx);
|
2026-07-04 09:43:28 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
expect(paroleDbQuery.findOne).toHaveBeenCalledWith({where: {id: dbParole.id}});
|
|
|
|
|
expect(commentaireDocuments.create).toHaveBeenCalled();
|
2026-07-04 09:43:28 +04:00
|
|
|
expect(paroleDocuments.update).toHaveBeenCalledWith({
|
|
|
|
|
documentId: dbParole.documentId,
|
2026-07-04 14:59:58 +04:00
|
|
|
data: {commentaires: {connect: [99]}}
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
|
|
|
|
});
|
2026-07-04 09:43:28 +04:00
|
|
|
|
|
|
|
|
it('rejette quand la parole ciblée n\'existe pas', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, commentaireDocuments} = buildStrapi({existingParole: null});
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData());
|
2026-07-04 09:43:28 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await expect(controller.create(ctx)).rejects.toThrow('Texte introuvable.');
|
|
|
|
|
expect(commentaireDocuments.create).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2026-07-04 11:38:02 +04:00
|
|
|
|
|
|
|
|
it('refuse sans planter quand data.user est absent', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, userDbQuery} = buildStrapi();
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData({user: undefined}));
|
2026-07-04 11:38:02 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await expect(controller.create(ctx)).rejects.toThrow('Informations manquantes.');
|
|
|
|
|
expect(userDbQuery.findOne).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2026-07-04 11:38:02 +04:00
|
|
|
|
|
|
|
|
it('refuse sans planter quand data.parole est absent', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, paroleDbQuery} = buildStrapi();
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData({parole: undefined}));
|
2026-07-04 11:38:02 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await expect(controller.create(ctx)).rejects.toThrow('Informations manquantes.');
|
|
|
|
|
expect(paroleDbQuery.findOne).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2026-07-04 15:08:19 +04:00
|
|
|
|
|
|
|
|
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, commentaireDocuments} = buildStrapi();
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData({publishedAt: '2020-01-01'}));
|
2026-07-04 15:08:19 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await controller.create(ctx);
|
2026-07-04 15:08:19 +04:00
|
|
|
|
|
|
|
|
expect(commentaireDocuments.create).toHaveBeenCalledWith({
|
|
|
|
|
data: {
|
|
|
|
|
contenu: 'Un commentaire',
|
|
|
|
|
datePublication: '2026-07-04',
|
|
|
|
|
user: dbUser.id,
|
|
|
|
|
parole: dbParole.id
|
|
|
|
|
}
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|