Files
api.pawol.nu/src/api/artiste/controllers/__tests__/artiste.test.js
T

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2026-07-04 20:00:51 +04:00
import {describe, it, expect, vi} from 'vitest';
2026-07-04 20:00:51 +04:00
const {default: createController} = await import('../artiste.js');
function buildStrapi({dbUser, existingArtiste = null}) {
const dbQuery = {
findOne: vi.fn(async () => existingArtiste)
2026-07-04 20:00:51 +04:00
};
const artisteDocuments = {
create: vi.fn(async ({data}) => ({id: 42, ...data}))
2026-07-04 20:00:51 +04:00
};
const userDocuments = {
findOne: vi.fn(async () => dbUser)
2026-07-04 20:00:51 +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 20:00:51 +04:00
};
2026-07-04 20:00:51 +04:00
return {strapi, artisteDocuments};
}
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 20:00:51 +04:00
const dbUser = {id: 1, documentId: 'user-doc-1', username: 'foo', email: 'foo@bar.com'};
function buildData(overrides = {}) {
return {
alias: 'Test Artist',
user: {...dbUser},
...overrides
2026-07-04 20:00:51 +04:00
};
}
describe('artiste.create', () => {
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 20:00:51 +04:00
await controller.create(ctx);
2026-07-04 20:00:51 +04:00
expect(artisteDocuments.create).toHaveBeenCalled();
});
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 20:00:51 +04:00
await controller.create(ctx);
2026-07-04 20:00:51 +04:00
expect(ctx.badRequest).toHaveBeenCalled();
expect(strapi.documents).not.toHaveBeenCalled();
});
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});
const ctx = buildCtx(buildData({
isExclusiveArtist: true,
userAdmin: {id: 999}
2026-07-04 20:00:51 +04:00
}));
2026-07-04 20:00:51 +04:00
await controller.create(ctx);
expect(artisteDocuments.create).toHaveBeenCalledWith({
data: {
alias: 'Test Artist',
user: dbUser.id
}
2026-07-04 20:00:51 +04:00
});
});
});