fix: whitelister les champs autorisés sur create/update (mass assignment)
This commit is contained in:
@@ -5,7 +5,8 @@ const {default: createController} = await import('../parole.js')
|
||||
function buildStrapi({dbUser, artiste}) {
|
||||
const paroleDocuments = {
|
||||
findMany: vi.fn(async () => []),
|
||||
create: vi.fn(async ({data}) => ({id: 42, ...data}))
|
||||
create: vi.fn(async ({data}) => ({id: 42, ...data})),
|
||||
update: vi.fn(async ({data}) => ({id: 42, ...data}))
|
||||
}
|
||||
const userDocuments = {
|
||||
findOne: vi.fn(async () => dbUser),
|
||||
@@ -44,7 +45,7 @@ function buildCtx(data) {
|
||||
}
|
||||
|
||||
const dbUser = {id: 1, documentId: 'user-doc-1', username: 'foo', email: 'foo@bar.com'}
|
||||
const artiste = {documentId: 'artiste-doc-1'}
|
||||
const artiste = {id: 9, documentId: 'artiste-doc-1'}
|
||||
|
||||
function buildData(overrides = {}) {
|
||||
return {
|
||||
@@ -113,4 +114,57 @@ describe('parole.create', () => {
|
||||
expect(ctx.badRequest).toHaveBeenCalled()
|
||||
expect(artisteDocuments.findOne).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
||||
const {strapi, paroleDocuments} = buildStrapi({dbUser, artiste})
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData({
|
||||
userAdmin: {id: 999},
|
||||
isNewRelease: true,
|
||||
difference: [{fake: true}]
|
||||
}))
|
||||
|
||||
await controller.create(ctx)
|
||||
|
||||
expect(paroleDocuments.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
titre: 'Test',
|
||||
transcription: 'Paroles...',
|
||||
traductions: undefined,
|
||||
traductionAuto: undefined,
|
||||
artistes: [artiste.id],
|
||||
user: dbUser.id
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('parole.update', () => {
|
||||
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
||||
const {strapi, paroleDocuments} = buildStrapi({dbUser, artiste})
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx({
|
||||
documentId: 'doc-1',
|
||||
titre: 'Nouveau titre',
|
||||
transcription: 'Nouveau texte',
|
||||
traductions: {francais: 'salut'},
|
||||
traductionAuto: true,
|
||||
artistes: [9],
|
||||
userAdmin: {id: 999},
|
||||
user: {id: 999}
|
||||
})
|
||||
|
||||
await controller.update(ctx)
|
||||
|
||||
expect(paroleDocuments.update).toHaveBeenCalledWith({
|
||||
documentId: 'doc-1',
|
||||
data: {
|
||||
titre: 'Nouveau titre',
|
||||
transcription: 'Nouveau texte',
|
||||
traductions: {francais: 'salut'},
|
||||
traductionAuto: true,
|
||||
artistes: [9]
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -61,7 +61,11 @@ module.exports = createCoreController('api::parole.parole', ({strapi}) => ({
|
||||
documentId: data.documentId,
|
||||
|
||||
data: {
|
||||
...data
|
||||
titre: data.titre,
|
||||
transcription: data.transcription,
|
||||
traductions: data.traductions,
|
||||
traductionAuto: data.traductionAuto,
|
||||
artistes: data.artistes
|
||||
}
|
||||
})
|
||||
|
||||
@@ -119,7 +123,12 @@ module.exports = createCoreController('api::parole.parole', ({strapi}) => ({
|
||||
|
||||
const newParole = await strapi.documents('api::parole.parole').create({
|
||||
data: {
|
||||
...data
|
||||
titre: data.titre,
|
||||
transcription: data.transcription,
|
||||
traductions: data.traductions,
|
||||
traductionAuto: data.traductionAuto,
|
||||
artistes: [artiste.id],
|
||||
user: user.id
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user