fix: corriger le ReferenceError dans artiste.create

This commit is contained in:
2026-07-04 09:37:37 +04:00
parent f592d4ded7
commit 8b17882d6b
2 changed files with 88 additions and 4 deletions
@@ -0,0 +1,84 @@
import {describe, it, expect, vi} from 'vitest'
const {default: createController} = await import('../artiste.js')
function buildStrapi({jwtUserId, dbUser, existingArtiste = null}) {
const dbQuery = {
findOne: vi.fn(async () => existingArtiste)
}
const artisteDocuments = {
create: vi.fn(async ({data}) => ({id: 42, ...data}))
}
const userDocuments = {
findOne: vi.fn(async () => dbUser)
}
const strapi = {
contentType: vi.fn(() => ({uid: 'api::artiste.artiste', kind: 'collectionType'})),
plugins: {
'users-permissions': {
services: {
jwt: {
getToken: vi.fn(async () => ({id: jwtUserId}))
}
}
}
},
db: {
query: vi.fn(() => dbQuery)
},
documents: vi.fn(uid => {
if (uid === 'plugin::users-permissions.user') return userDocuments
if (uid === 'api::artiste.artiste') return artisteDocuments
throw new Error(`unexpected uid: ${uid}`)
})
}
return {strapi, artisteDocuments}
}
function buildCtx(data) {
return {
request: {
body: {data},
header: {authorization: 'Bearer faketoken'}
},
unauthorized: vi.fn(),
badRequest: vi.fn(),
notFound: vi.fn()
}
}
const dbUser = {id: 1, documentId: 'user-doc-1', username: 'foo', email: 'foo@bar.com'}
function buildData(overrides = {}) {
return {
alias: 'Test Artist',
user: {...dbUser},
...overrides
}
}
describe('artiste.create', () => {
it('refuse et ne crée rien quand le user du JWT ne correspond pas au user du payload, sans planter', async () => {
const {strapi, artisteDocuments} = buildStrapi({jwtUserId: 999, dbUser})
const controller = createController({strapi})
const ctx = buildCtx(buildData())
await controller.create(ctx)
expect(ctx.unauthorized).toHaveBeenCalled()
expect(artisteDocuments.create).not.toHaveBeenCalled()
})
it('crée l\'artiste quand le user du JWT correspond au user du payload', async () => {
const {strapi, artisteDocuments} = buildStrapi({jwtUserId: 1, dbUser})
const controller = createController({strapi})
const ctx = buildCtx(buildData())
await controller.create(ctx)
expect(ctx.unauthorized).not.toHaveBeenCalled()
expect(artisteDocuments.create).toHaveBeenCalled()
})
})