From f33d6e20b05da28ee0c27e3a39c2429fabc65722 Mon Sep 17 00:00:00 2001 From: abouchtita <abouchtita@devops-tp.local> Date: Fri, 14 Mar 2025 17:40:59 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20suppression=20d'une=20t=C3=A2?= =?UTF-8?q?che=20(endpoint=20DELETE=20/api/tasks/:id)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 10 ++++++++++ src/taskService.js | 13 ++++++++++++- tests/integration/app.test.js | 25 +++++++++++++++++++++++++ tests/unit/todoService.test.js | 19 ++++++++++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 94fa182..7c815b6 100644 --- a/src/index.js +++ b/src/index.js @@ -45,6 +45,16 @@ app.post('/api/tasks', (req, res) => { } }); +app.delete('/api/tasks/:id', (req, res) => { + const id = req.params.id; + const result = taskService.deleteTask(id); + if (result) { + res.status(204).send(); + } else { + res.status(404).json({ error: 'Tâche non trouvée' }); + } +}); + // Fonction pour démarrer le serveur const startServer = () => { const server = app.listen(port, () => { diff --git a/src/taskService.js b/src/taskService.js index 1ec2dd4..e564d91 100644 --- a/src/taskService.js +++ b/src/taskService.js @@ -28,9 +28,20 @@ function addTask(description) { return newTask; } +function deleteTask(id) { + const taskId = parseInt(id, 10); + const index = tasks.findIndex(task => task.id === taskId); + if (index === -1) { + return false; + } + tasks.splice(index, 1); + return true; +} + //export des fonctions module.exports = { getAllTasks, getTaskById, - addTask + addTask, + deleteTask }; diff --git a/tests/integration/app.test.js b/tests/integration/app.test.js index 79bdebe..c83e546 100644 --- a/tests/integration/app.test.js +++ b/tests/integration/app.test.js @@ -64,4 +64,29 @@ describe('POST /api/tasks', () => { expect(response.body).toHaveProperty('error', 'La description est requise'); }); +}); + + +describe('DELETE /api/tasks/:id', () => { + + test('should delete an existing task and return 204', async () => { + const addResponse = await request(app) + .post('/api/tasks') + .send({ description: "Task to delete via DELETE endpoint" }); + expect(addResponse.statusCode).toBe(201); + const taskId = addResponse.body.id; + + const deleteResponse = await request(app).delete(`/api/tasks/${taskId}`); + expect(deleteResponse.statusCode).toBe(204); + + const getResponse = await request(app).get(`/api/tasks/${taskId}`); + expect(getResponse.statusCode).toBe(404); + }); + + test('should return 404 when trying to delete a non-existent task', async () => { + const response = await request(app).delete('/api/tasks/99999'); + expect(response.statusCode).toBe(404); + expect(response.body).toEqual({ error: 'Tâche non trouvée' }); + }); + }); \ No newline at end of file diff --git a/tests/unit/todoService.test.js b/tests/unit/todoService.test.js index a5409b4..a252acc 100644 --- a/tests/unit/todoService.test.js +++ b/tests/unit/todoService.test.js @@ -1,7 +1,7 @@ //test unitaire des fonctions de service // Import de la fonction à tester -const { getAllTasks, getTaskById, addTask } = require('../../src/taskService'); +const { getAllTasks, getTaskById, addTask, deleteTask } = require('../../src/taskService'); //Test de la fonction de récupération de toutes les tâches. test('should return all tasks', () => { @@ -32,4 +32,21 @@ test('should add a new task with provided description', () => { test('should throw an error if description is missing', () => { expect(() => addTask()).toThrow("La description est requise"); +}); + +test('should delete a task with a valid id', () => { + const initialLength = getAllTasks().length; + + const newTask = addTask("Task to delete"); + expect(getAllTasks().length).toBe(initialLength + 1); + + const result = deleteTask(newTask.id); + expect(result).toBe(true); + + expect(getAllTasks().length).toBe(initialLength); +}); + +test('should return false when deleting a non-existent task', () => { + const result = deleteTask(999); + expect(result).toBe(false); }); \ No newline at end of file -- GitLab