refactor: extraire la vérification JWT/payload en policy partagée

This commit is contained in:
2026-07-04 10:04:00 +04:00
parent 1562ecd706
commit e515944fb9
11 changed files with 122 additions and 107 deletions
@@ -5,7 +5,7 @@ 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}) {
function buildStrapi({existingParole = dbParole} = {}) {
const commentaireDocuments = {
create: vi.fn(async ({data}) => ({id: 99, ...data}))
}
@@ -21,15 +21,6 @@ function buildStrapi({jwtUserId, existingParole = dbParole}) {
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
@@ -68,7 +59,7 @@ function buildData(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 {strapi, commentaireDocuments, paroleDocuments, paroleDbQuery} = buildStrapi()
const controller = createController({strapi})
const ctx = buildCtx(buildData())
@@ -83,7 +74,7 @@ describe('commentaire.create', () => {
})
it('rejette quand la parole ciblée n\'existe pas', async () => {
const {strapi, commentaireDocuments} = buildStrapi({jwtUserId: dbUser.id, existingParole: null})
const {strapi, commentaireDocuments} = buildStrapi({existingParole: null})
const controller = createController({strapi})
const ctx = buildCtx(buildData())