2026-07-04 20:00:51 +04:00
|
|
|
import {describe, it, expect, vi} from 'vitest';
|
2026-07-04 09:37:37 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const {default: createController} = await import('../artiste.js');
|
2026-07-04 09:37:37 +04:00
|
|
|
|
2026-07-04 10:04:00 +04:00
|
|
|
function buildStrapi({dbUser, existingArtiste = null}) {
|
2026-07-04 09:37:37 +04:00
|
|
|
const dbQuery = {
|
|
|
|
|
findOne: vi.fn(async () => existingArtiste)
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:37:37 +04:00
|
|
|
const artisteDocuments = {
|
|
|
|
|
create: vi.fn(async ({data}) => ({id: 42, ...data}))
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:37:37 +04:00
|
|
|
const userDocuments = {
|
|
|
|
|
findOne: vi.fn(async () => dbUser)
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:37:37 +04:00
|
|
|
|
|
|
|
|
const strapi = {
|
|
|
|
|
contentType: vi.fn(() => ({uid: 'api::artiste.artiste', kind: 'collectionType'})),
|
|
|
|
|
db: {
|
|
|
|
|
query: vi.fn(() => dbQuery)
|
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
|
throw new Error(`unexpected uid: ${uid}`);
|
2026-07-04 09:37:37 +04:00
|
|
|
})
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:37:37 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
return {strapi, artisteDocuments};
|
2026-07-04 09:37:37 +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:37:37 +04:00
|
|
|
}
|
|
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const dbUser = {id: 1, documentId: 'user-doc-1', username: 'foo', email: 'foo@bar.com'};
|
2026-07-04 09:37:37 +04:00
|
|
|
|
|
|
|
|
function buildData(overrides = {}) {
|
|
|
|
|
return {
|
|
|
|
|
alias: 'Test Artist',
|
|
|
|
|
user: {...dbUser},
|
|
|
|
|
...overrides
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:37:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('artiste.create', () => {
|
2026-07-04 10:04:00 +04:00
|
|
|
it('crée l\'artiste quand le user existe et que l\'alias est nouveau', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {strapi, artisteDocuments} = buildStrapi({dbUser});
|
|
|
|
|
const controller = createController({strapi});
|
|
|
|
|
const ctx = buildCtx(buildData());
|
2026-07-04 09:37:37 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
await controller.create(ctx);
|
2026-07-04 09:37:37 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
expect(artisteDocuments.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} = buildStrapi({dbUser});
|
|
|
|
|
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(strapi.documents).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, artisteDocuments} = buildStrapi({dbUser});
|
|
|
|
|
const controller = createController({strapi});
|
2026-07-04 15:08:19 +04:00
|
|
|
const ctx = buildCtx(buildData({
|
|
|
|
|
isExclusiveArtist: true,
|
|
|
|
|
userAdmin: {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.create(ctx);
|
2026-07-04 15:08:19 +04:00
|
|
|
|
|
|
|
|
expect(artisteDocuments.create).toHaveBeenCalledWith({
|
|
|
|
|
data: {
|
|
|
|
|
alias: 'Test Artist',
|
|
|
|
|
user: dbUser.id
|
|
|
|
|
}
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|