chore: activer ESLint sur le backend
Installe eslint, ajoute le script lint, modernise le parser (retrait de babel-eslint obsolète) et applique l'autofix (points-virgules manquants sur l'ensemble du code, conformément à la règle "semi" déjà présente dans .eslintrc mais jamais appliquée faute d'ESLint installé et d'un script pour l'exécuter).
This commit is contained in:
@@ -1,41 +1,41 @@
|
||||
import {describe, it, expect, vi} from 'vitest'
|
||||
import {describe, it, expect, vi} from 'vitest';
|
||||
|
||||
const {default: createController} = await import('../commentaire.js')
|
||||
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'}
|
||||
const dbUser = {id: 1, username: 'foo', email: 'foo@bar.com'};
|
||||
const dbParole = {id: 7, documentId: 'parole-doc-7'};
|
||||
|
||||
function buildStrapi({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'})),
|
||||
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}`)
|
||||
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}`)
|
||||
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}
|
||||
return {strapi, commentaireDocuments, paroleDocuments, userDbQuery, paroleDbQuery};
|
||||
}
|
||||
|
||||
function buildCtx(data) {
|
||||
@@ -44,7 +44,7 @@ function buildCtx(data) {
|
||||
body: {data},
|
||||
header: {authorization: 'Bearer faketoken'}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function buildData(overrides = {}) {
|
||||
@@ -54,58 +54,58 @@ function buildData(overrides = {}) {
|
||||
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()
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData())
|
||||
const {strapi, commentaireDocuments, paroleDocuments, paroleDbQuery} = buildStrapi();
|
||||
const controller = createController({strapi});
|
||||
const ctx = buildCtx(buildData());
|
||||
|
||||
await controller.create(ctx)
|
||||
await controller.create(ctx);
|
||||
|
||||
expect(paroleDbQuery.findOne).toHaveBeenCalledWith({where: {id: dbParole.id}})
|
||||
expect(commentaireDocuments.create).toHaveBeenCalled()
|
||||
expect(paroleDbQuery.findOne).toHaveBeenCalledWith({where: {id: dbParole.id}});
|
||||
expect(commentaireDocuments.create).toHaveBeenCalled();
|
||||
expect(paroleDocuments.update).toHaveBeenCalledWith({
|
||||
documentId: dbParole.documentId,
|
||||
data: {commentaires: {connect: [99]}}
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it('rejette quand la parole ciblée n\'existe pas', async () => {
|
||||
const {strapi, commentaireDocuments} = buildStrapi({existingParole: null})
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData())
|
||||
const {strapi, commentaireDocuments} = buildStrapi({existingParole: null});
|
||||
const controller = createController({strapi});
|
||||
const ctx = buildCtx(buildData());
|
||||
|
||||
await expect(controller.create(ctx)).rejects.toThrow('Texte introuvable.')
|
||||
expect(commentaireDocuments.create).not.toHaveBeenCalled()
|
||||
})
|
||||
await expect(controller.create(ctx)).rejects.toThrow('Texte introuvable.');
|
||||
expect(commentaireDocuments.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('refuse sans planter quand data.user est absent', async () => {
|
||||
const {strapi, userDbQuery} = buildStrapi()
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData({user: undefined}))
|
||||
const {strapi, userDbQuery} = buildStrapi();
|
||||
const controller = createController({strapi});
|
||||
const ctx = buildCtx(buildData({user: undefined}));
|
||||
|
||||
await expect(controller.create(ctx)).rejects.toThrow('Informations manquantes.')
|
||||
expect(userDbQuery.findOne).not.toHaveBeenCalled()
|
||||
})
|
||||
await expect(controller.create(ctx)).rejects.toThrow('Informations manquantes.');
|
||||
expect(userDbQuery.findOne).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('refuse sans planter quand data.parole est absent', async () => {
|
||||
const {strapi, paroleDbQuery} = buildStrapi()
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData({parole: undefined}))
|
||||
const {strapi, paroleDbQuery} = buildStrapi();
|
||||
const controller = createController({strapi});
|
||||
const ctx = buildCtx(buildData({parole: undefined}));
|
||||
|
||||
await expect(controller.create(ctx)).rejects.toThrow('Informations manquantes.')
|
||||
expect(paroleDbQuery.findOne).not.toHaveBeenCalled()
|
||||
})
|
||||
await expect(controller.create(ctx)).rejects.toThrow('Informations manquantes.');
|
||||
expect(paroleDbQuery.findOne).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
||||
const {strapi, commentaireDocuments} = buildStrapi()
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData({publishedAt: '2020-01-01'}))
|
||||
const {strapi, commentaireDocuments} = buildStrapi();
|
||||
const controller = createController({strapi});
|
||||
const ctx = buildCtx(buildData({publishedAt: '2020-01-01'}));
|
||||
|
||||
await controller.create(ctx)
|
||||
await controller.create(ctx);
|
||||
|
||||
expect(commentaireDocuments.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
@@ -114,6 +114,6 @@ describe('commentaire.create', () => {
|
||||
user: dbUser.id,
|
||||
parole: dbParole.id
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user