fix: corriger le ReferenceError dans artiste.create
This commit is contained in:
@@ -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()
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -19,10 +19,10 @@ module.exports = createCoreController('api::artiste.artiste', ({strapi}) => ({
|
|||||||
].services.jwt.getToken(ctx)
|
].services.jwt.getToken(ctx)
|
||||||
|
|
||||||
if (id !== data.user.id) {
|
if (id !== data.user.id) {
|
||||||
throw new UnauthorizedError('Opération non autorisée')
|
return ctx.unauthorized('Opération non autorisée')
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new UnauthorizedError(ctx, err, 'Opération non autorisée')
|
return ctx.unauthorized('Opération non autorisée')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,11 +31,11 @@ module.exports = createCoreController('api::artiste.artiste', ({strapi}) => ({
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new NotFoundError('Utilisateur introuvable.')
|
return ctx.notFound('Utilisateur introuvable.')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) {
|
if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) {
|
||||||
ctx.badRequest('Informations non valides.')
|
return ctx.badRequest('Informations non valides.')
|
||||||
}
|
}
|
||||||
|
|
||||||
const artiste = await strapi.db.query('api::artiste.artiste').findOne({
|
const artiste = await strapi.db.query('api::artiste.artiste').findOne({
|
||||||
|
|||||||
Reference in New Issue
Block a user