tests: add test for recipes controller

WORK-RRE
Rémi REGNAULT 1 year ago
parent e3e444acda
commit c183b18074

@ -22,7 +22,8 @@ RecipesController.get('/:id', async (req, res) => {
const id = Number(req.params.id); const id = Number(req.params.id);
if (!Number.isInteger(id)) { if (!Number.isInteger(id)) {
throw new Exceptions.BadRequestException('id invalid !'); res.status(400).send('invalid parameter or no parameter')
return
} }
try { try {
@ -46,7 +47,8 @@ RecipesController.get('/withingr/:ids', async (req, res) => {
for (let key in raw_ids) { for (let key in raw_ids) {
const test = Number(raw_ids[key]) const test = Number(raw_ids[key])
if (Number.isNaN(test) || !Number.isInteger(test)) { if (Number.isNaN(test) || !Number.isInteger(test)) {
res.status(400).json('A parameter is not an integer') res.status(400).send('A parameter is not an integer')
return
} }
ids.push(Number(test)) ids.push(Number(test))
} }
@ -55,7 +57,7 @@ RecipesController.get('/withingr/:ids', async (req, res) => {
const recipes = await recipes_gw.getIdsRecipesThatContainsIngredients(ids) const recipes = await recipes_gw.getIdsRecipesThatContainsIngredients(ids)
if (recipes.length == 0) { if (recipes.length == 0) {
res.status(404).json('no data found') res.status(404).send('no data found')
} }
else { else {
res.status(200).json(recipes) res.status(200).json(recipes)
@ -69,14 +71,14 @@ RecipesController.get('/withingr/:ids', async (req, res) => {
RecipesController.get('/getcommentsdictionary/:id', async (req, res) => { RecipesController.get('/getcommentsdictionary/:id', async (req, res) => {
const id: number = Number(req.params.id) const id: number = Number(req.params.id)
if (Number.isNaN(id) || !Number.isInteger(id)) { if (Number.isNaN(id) || !Number.isInteger(id)) {
res.status(400).json('The parameter is not an integer') res.status(400).send('The parameter is not an integer')
return
} }
try { try {
const dictionary_comments = await recipes_gw.getCommentsDictionary(id) const dictionary_comments = await recipes_gw.getCommentsDictionary(id)
if (Object.keys(dictionary_comments).length == 0) {
if (dictionary_comments.length == 0) { res.status(404).send('no data found')
res.status(404).json('no data found')
} }
else { else {
res.status(200).json(dictionary_comments) res.status(200).json(dictionary_comments)
@ -91,14 +93,15 @@ RecipesController.get('/getcommentsdictionary/:id', async (req, res) => {
RecipesController.get('/getratinglist/:id', async (req, res) => { RecipesController.get('/getratinglist/:id', async (req, res) => {
const id: number = Number(req.params.id) const id: number = Number(req.params.id)
if (Number.isNaN(id) || !Number.isInteger(id)) { if (Number.isNaN(id) || !Number.isInteger(id)) {
res.status(400).json('The parameter is not an integer') res.status(400).send('The parameter is not an integer')
return
} }
try { try {
const rating_list = await recipes_gw.getRatingList(id) const rating_list = await recipes_gw.getRatingList(id)
if (rating_list.length == 0) { if (rating_list.length == 0) {
res.status(404).json('no data found') res.status(404).send('no data found')
} }
else { else {
res.status(200).json(rating_list) res.status(200).json(rating_list)

@ -7,7 +7,7 @@ describe('GET /api/endpoint', () => {
const port = 3000 const port = 3000
beforeAll(() => { beforeAll(() => {
server = startServer(3000); server = startServer(port);
}); });
afterAll((done) => { afterAll((done) => {

@ -8,7 +8,7 @@ describe('GET /ingredients', () => {
const port = 3001 const port = 3001
beforeAll(() => { beforeAll(() => {
server = startServer(3001); server = startServer(port);
}); });
afterAll((done) => { afterAll((done) => {

@ -0,0 +1,102 @@
import request from 'supertest';
import app, { startServer } from '../src/server';
import { Server, IncomingMessage, ServerResponse } from 'http';
describe('GET /recipes', () => {
let server: Server<typeof IncomingMessage, typeof ServerResponse>;
const port = 3002
beforeAll(() => {
server = startServer(port);
});
afterAll((done) => {
server.close(done);
});
// /recipes/
it('should return a 200 status code', async () => {
const response = await request(app).get('/recipes/');
expect(response.status).toBe(200);
}, 120000);
// /recipes/:id
it('should return a 200 status code', async () => {
const response = await request(app).get('/recipes/4444');
expect(response.status).toBe(200);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/recipes/oui');
expect(response.status).toBe(400);
});
it('should return a 404 status code', async () => {
const response = await request(app).get('/recipes/1');
expect(response.status).toBe(404);
});
// /recipes/withingr/:ids
it('should return a 200 status code', async () => {
const response = await request(app).get('/recipes/withingr/1928:2148:2809:2853:3723:6261:6335:7076');
expect(response.status).toBe(200);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/recipes/withingr/oui');
expect(response.status).toBe(400);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/recipes/withingr/2:oui:3');
expect(response.status).toBe(400);
});
it('should return a 404 status code', async () => {
const response = await request(app).get('/recipes/withingr/1');
expect(response.status).toBe(404);
});
// /recipes/getcommentsdictionary/:id
it('should return a 200 status code', async () => {
const response = await request(app).get('/recipes/getcommentsdictionary/4444');
expect(response.status).toBe(200);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/recipes/getcommentsdictionary/oui');
expect(response.status).toBe(400);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/recipes/getcommentsdictionary/');
expect(response.status).toBe(400);
});
it('should return a 404 status code', async () => {
const response = await request(app).get('/recipes/getcommentsdictionary/1');
expect(response.status).toBe(404);
});
// /recipes/getratinglist/:id
it('should return a 200 status code', async () => {
const response = await request(app).get('/recipes/getratinglist/4444');
expect(response.status).toBe(200);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/recipes/getratinglist/oui');
expect(response.status).toBe(400);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/recipes/getratinglist/');
expect(response.status).toBe(400);
});
it('should return a 404 status code', async () => {
const response = await request(app).get('/recipes/getratinglist/1');
expect(response.status).toBe(404);
});
});
Loading…
Cancel
Save