2026-07-04 20:00:51 +04:00
|
|
|
import {describe, it, expect, vi} from 'vitest';
|
2026-07-04 09:36:17 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const {default: createController} = await import('../parole.js');
|
2026-07-04 09:36:17 +04:00
|
|
|
|
2026-07-04 10:04:00 +04:00
|
|
|
function buildStrapi({dbUser, artiste}) {
|
2026-07-04 09:36:17 +04:00
|
|
|
const paroleDocuments = {
|
|
|
|
|
findMany: vi.fn(async () => []),
|
2026-07-04 15:08:19 +04:00
|
|
|
create: vi.fn(async ({data}) => ({id: 42, ...data})),
|
|
|
|
|
update: vi.fn(async ({data}) => ({id: 42, ...data}))
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:36:17 +04:00
|
|
|
const userDocuments = {
|
|
|
|
|
findOne: vi.fn(async () => dbUser),
|
|
|
|
|
update: vi.fn(async () => {})
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:36:17 +04:00
|
|
|
const artisteDocuments = {
|
|
|
|
|
findOne: vi.fn(async () => artiste)
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:36:17 +04:00
|
|
|
|
|
|
|
|
const strapi = {
|
|
|
|
|
contentType: vi.fn(() => ({uid: 'api::parole.parole', kind: 'collectionType'})),
|
|
|
|
|
service: vi.fn(() => ({
|
|
|
|
|
validateParoles: vi.fn(),
|
|
|
|
|
translateLyrics: vi.fn()
|
|
|
|
|
})),
|
|
|
|
|
documents: vi.fn(uid => {
|
2026-07-04 20:00:51 +04:00
|
|
|
if (uid === 'plugin::users-permissions.user') return userDocuments;
|
|
|
|
|
if (uid === 'api::artiste.artiste') return artisteDocuments;
|
|
|
|
|
if (uid === 'api::parole.parole') return paroleDocuments;
|
|
|
|
|
throw new Error(`unexpected uid: ${uid}`);
|
2026-07-04 09:36:17 +04:00
|
|
|
})
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:36:17 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
return {strapi, paroleDocuments, userDocuments, artisteDocuments};
|
2026-07-04 09:36:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildCtx(data) {
|
|
|
|
|
return {
|
|
|
|
|
request: {
|
|
|
|
|
body: {data},
|
|
|
|
|
header: {authorization: 'Bearer faketoken'}
|
|
|
|
|
},
|
|
|
|
|
badRequest: vi.fn(),
|
|
|
|
|
notFound: vi.fn()
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:36:17 +04:00
|
|
|
}
|
|
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const dbUser = {id: 1, documentId: 'user-doc-1', username: 'foo', email: 'foo@bar.com'};
|
|
|
|
|
const artiste = {id: 9, documentId: 'artiste-doc-1'};
|
2026-07-04 09:36:17 +04:00
|
|
|
|
|
|
|
|
function buildData(overrides = {}) {
|
|
|
|
|
return {
|
|
|
|
|
titre: 'Test',
|
|
|
|
|
transcription: 'Paroles...',
|
|
|
|
|
user: {...dbUser},
|
|
|
|
|
artistes: [{documentId: 'artiste-doc-1'}],
|
|
|
|
|
...overrides
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:36:17 +04:00
|
|
|
}
|
|
|
|
|
|
2026-07-04 14:58:43 +04:00
|
|
|
describe('parole.findOne', () => {
|
|
|
|
|
it('interroge avec le documentId venant de ctx.params.id, pas avec ctx lui-même', async () => {
|
|
|
|
|
const paroleDocuments = {
|
|
|
|
|
findOne: vi.fn(async ({documentId}) => ({id: 1, documentId, titre: 'Test'}))
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 14:58:43 +04:00
|
|
|
const strapi = {
|
|
|
|
|
contentType: vi.fn(() => ({uid: 'api::parole.parole', kind: 'collectionType'})),
|
|
|
|
|
documents: vi.fn(uid => {
|
2026-07-04 20:00:51 +04:00
|
|
|
if (uid === 'api::parole.parole') return paroleDocuments;
|
|
|
|
|
throw new Error(`unexpected uid: ${uid}`);
|
2026-07-04 14:58:43 +04:00
|
|
|
})
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = {params: {id: 'doc-123'}};
|
2026-07-04 14:58:43 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const result = await controller.findOne(ctx);
|
2026-07-04 14:58:43 +04:00
|
|
|
|
|
|
|
|
expect(paroleDocuments.findOne).toHaveBeenCalledWith({
|
|
|
|
|
documentId: 'doc-123',
|
|
|
|
|
populate: ['artistes']
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
|
|
|
|
expect(result).toEqual({id: 1, documentId: 'doc-123', titre: 'Test'});
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-07-04 14:58:43 +04:00
|
|
|
|
2026-07-04 09:36:17 +04:00
|
|
|
describe('parole.create', () => {
|
2026-07-04 10:04:00 +04:00
|
|
|
it('crée la parole quand le user et l\'artiste existent', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, paroleDocuments} = buildStrapi({dbUser, artiste});
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData());
|
2026-07-04 09:36:17 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await controller.create(ctx);
|
2026-07-04 09:36:17 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
expect(paroleDocuments.create).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, userDocuments} = buildStrapi({dbUser, artiste});
|
|
|
|
|
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 controller.create(ctx);
|
2026-07-04 11:38:02 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
expect(ctx.badRequest).toHaveBeenCalled();
|
|
|
|
|
expect(userDocuments.findOne).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2026-07-04 11:38:02 +04:00
|
|
|
|
|
|
|
|
it('refuse sans planter quand data.artistes est vide', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, artisteDocuments} = buildStrapi({dbUser, artiste});
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData({artistes: []}));
|
2026-07-04 11:38:02 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await controller.create(ctx);
|
2026-07-04 11:38:02 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
expect(ctx.badRequest).toHaveBeenCalled();
|
|
|
|
|
expect(artisteDocuments.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, paroleDocuments} = buildStrapi({dbUser, artiste});
|
|
|
|
|
const controller = createController({strapi});
|
2026-07-04 15:08:19 +04:00
|
|
|
const ctx = buildCtx(buildData({
|
|
|
|
|
userAdmin: {id: 999},
|
|
|
|
|
isNewRelease: true,
|
|
|
|
|
difference: [{fake: true}]
|
2026-07-04 20:00:51 +04:00
|
|
|
}));
|
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(paroleDocuments.create).toHaveBeenCalledWith({
|
|
|
|
|
data: {
|
|
|
|
|
titre: 'Test',
|
|
|
|
|
transcription: 'Paroles...',
|
|
|
|
|
traductions: undefined,
|
|
|
|
|
traductionAuto: undefined,
|
|
|
|
|
artistes: [artiste.id],
|
|
|
|
|
user: dbUser.id
|
|
|
|
|
}
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-07-04 15:08:19 +04:00
|
|
|
|
|
|
|
|
describe('parole.update', () => {
|
|
|
|
|
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, paroleDocuments} = buildStrapi({dbUser, artiste});
|
|
|
|
|
const controller = createController({strapi});
|
2026-07-04 15:08:19 +04:00
|
|
|
const ctx = buildCtx({
|
|
|
|
|
documentId: 'doc-1',
|
|
|
|
|
titre: 'Nouveau titre',
|
|
|
|
|
transcription: 'Nouveau texte',
|
|
|
|
|
traductions: {francais: 'salut'},
|
|
|
|
|
traductionAuto: true,
|
|
|
|
|
artistes: [9],
|
|
|
|
|
userAdmin: {id: 999},
|
|
|
|
|
user: {id: 999}
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
2026-07-04 15:08:19 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await controller.update(ctx);
|
2026-07-04 15:08:19 +04:00
|
|
|
|
|
|
|
|
expect(paroleDocuments.update).toHaveBeenCalledWith({
|
|
|
|
|
documentId: 'doc-1',
|
|
|
|
|
data: {
|
|
|
|
|
titre: 'Nouveau titre',
|
|
|
|
|
transcription: 'Nouveau texte',
|
|
|
|
|
traductions: {francais: 'salut'},
|
|
|
|
|
traductionAuto: true,
|
|
|
|
|
artistes: [9]
|
|
|
|
|
}
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|