fix: corriger les mauvais identifiants utilisés (id vs documentId)
This commit is contained in:
@@ -0,0 +1,93 @@
|
|||||||
|
import {describe, it, expect, vi} from 'vitest'
|
||||||
|
|
||||||
|
const {default: createController} = await import('../commentaire.js')
|
||||||
|
|
||||||
|
const dbUser = {id: 1, username: 'foo', email: 'foo@bar.com'}
|
||||||
|
const dbParole = {id: 7, documentId: 'parole-doc-7'}
|
||||||
|
|
||||||
|
function buildStrapi({jwtUserId, existingParole = dbParole}) {
|
||||||
|
const commentaireDocuments = {
|
||||||
|
create: vi.fn(async ({data}) => ({id: 99, ...data}))
|
||||||
|
}
|
||||||
|
const paroleDocuments = {
|
||||||
|
update: vi.fn(async () => {})
|
||||||
|
}
|
||||||
|
const userDbQuery = {
|
||||||
|
findOne: vi.fn(async ({where}) => (where.id === dbUser.id ? dbUser : null))
|
||||||
|
}
|
||||||
|
const paroleDbQuery = {
|
||||||
|
findOne: vi.fn(async ({where}) => (where.id === existingParole?.id ? existingParole : null))
|
||||||
|
}
|
||||||
|
|
||||||
|
const strapi = {
|
||||||
|
contentType: vi.fn(() => ({uid: 'api::commentaire.commentaire', kind: 'collectionType'})),
|
||||||
|
plugins: {
|
||||||
|
'users-permissions': {
|
||||||
|
services: {
|
||||||
|
jwt: {
|
||||||
|
getToken: vi.fn(async () => ({id: jwtUserId}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
db: {
|
||||||
|
query: vi.fn(uid => {
|
||||||
|
if (uid === 'plugin::users-permissions.user') return userDbQuery
|
||||||
|
if (uid === 'api::parole.parole') return paroleDbQuery
|
||||||
|
throw new Error(`unexpected uid: ${uid}`)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
documents: vi.fn(uid => {
|
||||||
|
if (uid === 'api::commentaire.commentaire') return commentaireDocuments
|
||||||
|
if (uid === 'api::parole.parole') return paroleDocuments
|
||||||
|
throw new Error(`unexpected uid: ${uid}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return {strapi, commentaireDocuments, paroleDocuments, userDbQuery, paroleDbQuery}
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildCtx(data) {
|
||||||
|
return {
|
||||||
|
request: {
|
||||||
|
body: {data},
|
||||||
|
header: {authorization: 'Bearer faketoken'}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildData(overrides = {}) {
|
||||||
|
return {
|
||||||
|
contenu: 'Un commentaire',
|
||||||
|
datePublication: '2026-07-04',
|
||||||
|
parole: dbParole.id,
|
||||||
|
user: {...dbUser},
|
||||||
|
...overrides
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('commentaire.create', () => {
|
||||||
|
it('retrouve la parole par son id (pas par le documentId du user) et l\'associe correctement', async () => {
|
||||||
|
const {strapi, commentaireDocuments, paroleDocuments, paroleDbQuery} = buildStrapi({jwtUserId: dbUser.id})
|
||||||
|
const controller = createController({strapi})
|
||||||
|
const ctx = buildCtx(buildData())
|
||||||
|
|
||||||
|
await controller.create(ctx)
|
||||||
|
|
||||||
|
expect(paroleDbQuery.findOne).toHaveBeenCalledWith({where: {id: dbParole.id}})
|
||||||
|
expect(commentaireDocuments.create).toHaveBeenCalled()
|
||||||
|
expect(paroleDocuments.update).toHaveBeenCalledWith({
|
||||||
|
documentId: dbParole.documentId,
|
||||||
|
data: {commentaires: [99]}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejette quand la parole ciblée n\'existe pas', async () => {
|
||||||
|
const {strapi, commentaireDocuments} = buildStrapi({jwtUserId: dbUser.id, existingParole: null})
|
||||||
|
const controller = createController({strapi})
|
||||||
|
const ctx = buildCtx(buildData())
|
||||||
|
|
||||||
|
await expect(controller.create(ctx)).rejects.toThrow('Texte introuvable.')
|
||||||
|
expect(commentaireDocuments.create).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -21,8 +21,8 @@ module.exports = createCoreController('api::commentaire.commentaire', ({strapi})
|
|||||||
throw new UnauthorizedError(ctx, err, 'Opération non autorisée')
|
throw new UnauthorizedError(ctx, err, 'Opération non autorisée')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const user = await strapi.documents('plugin::users-permissions.user').findOne({
|
const user = await strapi.db.query('plugin::users-permissions.user').findOne({
|
||||||
documentId: data.user.documentId
|
where: {id: data.user.id}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
@@ -35,9 +35,8 @@ module.exports = createCoreController('api::commentaire.commentaire', ({strapi})
|
|||||||
|
|
||||||
data.user = user.id
|
data.user = user.id
|
||||||
|
|
||||||
const parole = await strapi.documents('api::parole.parole').findOne({
|
const parole = await strapi.db.query('api::parole.parole').findOne({
|
||||||
documentId: user.documentId,
|
where: {id: data.parole}
|
||||||
fields: ['id']
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!parole) {
|
if (!parole) {
|
||||||
@@ -51,7 +50,7 @@ module.exports = createCoreController('api::commentaire.commentaire', ({strapi})
|
|||||||
})
|
})
|
||||||
|
|
||||||
await strapi.documents('api::parole.parole').update({
|
await strapi.documents('api::parole.parole').update({
|
||||||
documentId: user.documentId,
|
documentId: parole.documentId,
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
commentaires: [newCommentaire.id]
|
commentaires: [newCommentaire.id]
|
||||||
|
|||||||
@@ -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,7 +53,7 @@ const jwennUserEpiId = async userId => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const user = await strapi.db.query('plugin::users-permissions.user').findOne({
|
const user = await strapi.db.query('plugin::users-permissions.user').findOne({
|
||||||
where: {user: userId}
|
where: {id: userId}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
|||||||
Reference in New Issue
Block a user