diff --git a/server/__tests__/notification.test.ts b/server/__tests__/notification.test.ts index ae06e694a..3585cdf25 100644 --- a/server/__tests__/notification.test.ts +++ b/server/__tests__/notification.test.ts @@ -67,7 +67,27 @@ describe('Notification api test', () => { expect(updatedNotifications).toMatchObject(updateData); }); - // TODO: Add a request with a user with commentary to true and expect received a mail + it('should handle error during notification preferences update', async () => { + const app = await getApp(); + + const updateData = { + commentary: true, + reaction: true, + }; + + // Simuler une erreur sur la fonction `EditNotificationPreferences` + jest.spyOn(AppDataSource.getRepository(Notifications), 'save').mockImplementationOnce(() => { + throw new Error('Database error'); + }); + + const response = await supertest(app) + .put('/api/notifications/suscribe/1') + .set('authorization', `Bearer ${accessToken}`) + .send({ data: updateData }) + .expect(500); + + expect(response.body.message).toEqual('erreur de sauvegarde de vos choix, veuillez réessayer ultérieurement'); + }); it("hasSubscribed should send false if the user's commentary is false", async () => { const userRepository = AppDataSource.getRepository(User); diff --git a/server/controllers/notifications.ts b/server/controllers/notifications.ts index 7d6b2fd74..0e48bd1d5 100644 --- a/server/controllers/notifications.ts +++ b/server/controllers/notifications.ts @@ -34,7 +34,7 @@ notificationsController.put({ path: '/suscribe/:userId' }, async (req: Request, try { await EditNotificationPreferences(newNotification); } catch (e) { - res.status(500).json({ message: 'erreur de sauvegarde de vos choix, veuillez réessayer ultérieurement' }); + return res.status(500).json({ message: 'erreur de sauvegarde de vos choix, veuillez réessayer ultérieurement' }); } res.status(200).json({ message: 'Notifications mises à jour' }); });