From c183b18074a499711a0565cbe6e83721d54040b7 Mon Sep 17 00:00:00 2001 From: Remi R Date: Fri, 8 Dec 2023 10:18:26 +0100 Subject: [PATCH] tests: add test for recipes controller --- .../src/controllers/recipes.controller.ts | 21 ++-- API-Project/tests/app.spec.ts | 2 +- API-Project/tests/ingredients.spec.ts | 2 +- API-Project/tests/recipes.spec.ts | 102 ++++++++++++++++++ 4 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 API-Project/tests/recipes.spec.ts diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index b703305..9ce99fe 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -22,7 +22,8 @@ RecipesController.get('/:id', async (req, res) => { const id = Number(req.params.id); if (!Number.isInteger(id)) { - throw new Exceptions.BadRequestException('id invalid !'); + res.status(400).send('invalid parameter or no parameter') + return } try { @@ -46,7 +47,8 @@ RecipesController.get('/withingr/:ids', async (req, res) => { for (let key in raw_ids) { const test = Number(raw_ids[key]) 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)) } @@ -55,7 +57,7 @@ RecipesController.get('/withingr/:ids', async (req, res) => { const recipes = await recipes_gw.getIdsRecipesThatContainsIngredients(ids) if (recipes.length == 0) { - res.status(404).json('no data found') + res.status(404).send('no data found') } else { res.status(200).json(recipes) @@ -69,14 +71,14 @@ RecipesController.get('/withingr/:ids', async (req, res) => { RecipesController.get('/getcommentsdictionary/:id', async (req, res) => { const id: number = Number(req.params.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 { const dictionary_comments = await recipes_gw.getCommentsDictionary(id) - - if (dictionary_comments.length == 0) { - res.status(404).json('no data found') + if (Object.keys(dictionary_comments).length == 0) { + res.status(404).send('no data found') } else { res.status(200).json(dictionary_comments) @@ -91,14 +93,15 @@ RecipesController.get('/getcommentsdictionary/:id', async (req, res) => { RecipesController.get('/getratinglist/:id', async (req, res) => { const id: number = Number(req.params.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 { const rating_list = await recipes_gw.getRatingList(id) if (rating_list.length == 0) { - res.status(404).json('no data found') + res.status(404).send('no data found') } else { res.status(200).json(rating_list) diff --git a/API-Project/tests/app.spec.ts b/API-Project/tests/app.spec.ts index ac77852..9074323 100644 --- a/API-Project/tests/app.spec.ts +++ b/API-Project/tests/app.spec.ts @@ -7,7 +7,7 @@ describe('GET /api/endpoint', () => { const port = 3000 beforeAll(() => { - server = startServer(3000); + server = startServer(port); }); afterAll((done) => { diff --git a/API-Project/tests/ingredients.spec.ts b/API-Project/tests/ingredients.spec.ts index 0c46bc3..02b2625 100644 --- a/API-Project/tests/ingredients.spec.ts +++ b/API-Project/tests/ingredients.spec.ts @@ -8,7 +8,7 @@ describe('GET /ingredients', () => { const port = 3001 beforeAll(() => { - server = startServer(3001); + server = startServer(port); }); afterAll((done) => { diff --git a/API-Project/tests/recipes.spec.ts b/API-Project/tests/recipes.spec.ts new file mode 100644 index 0000000..11218cc --- /dev/null +++ b/API-Project/tests/recipes.spec.ts @@ -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; + 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); + }); +}); \ No newline at end of file