fix: corriger les mauvais identifiants utilisés (id vs documentId)

This commit is contained in:
2026-07-04 09:43:28 +04:00
parent 8b17882d6b
commit ddb6d3f2f0
4 changed files with 141 additions and 8 deletions
@@ -0,0 +1,41 @@
import {describe, it, expect, vi, afterEach} from 'vitest'
async function loadLifecycles(strapiMock) {
vi.resetModules()
global.strapi = strapiMock
const mod = await import('../lifecycles.js')
return mod
}
describe('afterCreate — notification au soumetteur', () => {
afterEach(() => {
delete global.strapi
})
it('recherche le user par id (pas par un champ "user" inexistant) et envoie le mail', async () => {
const userFindOne = vi.fn(async ({where}) => {
if (where.id === 5) return {id: 5, username: 'foo', email: 'foo@bar.com'}
return null
})
const emailSend = vi.fn()
const strapiMock = {
db: {
query: vi.fn(uid => {
if (uid === 'plugin::users-permissions.user') return {findOne: userFindOne}
return {findOne: vi.fn(async () => null)}
})
},
plugins: {
email: {services: {email: {send: emailSend}}}
}
}
const {afterCreate} = await loadLifecycles(strapiMock)
await afterCreate({params: {data: {titre: 'Titre', user: {id: 5}}}})
expect(userFindOne).toHaveBeenCalledWith({where: {id: 5}})
expect(emailSend).toHaveBeenCalled()
})
})
@@ -53,8 +53,8 @@ const jwennUserEpiId = async userId => {
}
const user = await strapi.db.query('plugin::users-permissions.user').findOne({
where: {user: userId}
})
where: {id: userId}
})
if (!user) {
throw new ApplicationError('Utilisateur introuvable.')