From fda79bf5441bef22ab3d5902241cbe2475edec64 Mon Sep 17 00:00:00 2001 From: aelassas Date: Wed, 21 Feb 2024 08:49:54 +0100 Subject: [PATCH] Update notification.test.ts --- api/coverage/cobertura-coverage.xml | 268 +++++++++--------- api/src/controllers/notificationController.ts | 24 +- api/tests/notification.test.ts | 50 +++- api/tests/property.test.ts | 2 +- 4 files changed, 181 insertions(+), 163 deletions(-) diff --git a/api/coverage/cobertura-coverage.xml b/api/coverage/cobertura-coverage.xml index 216bc095..33f87a46 100644 --- a/api/coverage/cobertura-coverage.xml +++ b/api/coverage/cobertura-coverage.xml @@ -1,6 +1,6 @@ - + C:\dev\movinin\src\api @@ -96,9 +96,9 @@ - + - + @@ -116,14 +116,14 @@ - + - + - + - + @@ -141,10 +141,10 @@ - - - - + + + + @@ -152,8 +152,8 @@ - - + + @@ -289,7 +289,7 @@ - + @@ -318,9 +318,9 @@ - + - + @@ -328,9 +328,9 @@ - + - + @@ -406,8 +406,8 @@ - - + + @@ -415,8 +415,8 @@ - - + + @@ -866,11 +866,11 @@ - + - + - + @@ -878,9 +878,9 @@ - + - + @@ -888,36 +888,36 @@ - + - + - + - + - + - + - + - - - - + + + + - - - + + + @@ -929,66 +929,60 @@ - - - + + + - - - - - - - - - - - - - + + + + + + + + + - + - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1895,44 +1889,44 @@ - + - + - + - + - + - + - + - + - + - + - - - - + + + + - + @@ -2238,14 +2232,14 @@ - + - + - + - + @@ -2258,9 +2252,9 @@ - + - + @@ -2293,14 +2287,14 @@ - + - + - + - + @@ -2330,9 +2324,9 @@ - - - + + + @@ -2352,7 +2346,7 @@ - + @@ -2377,17 +2371,17 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/api/src/controllers/notificationController.ts b/api/src/controllers/notificationController.ts index 1bb95578..d2b4f505 100644 --- a/api/src/controllers/notificationController.ts +++ b/api/src/controllers/notificationController.ts @@ -94,12 +94,14 @@ export async function markAsRead(req: Request, res: Response) { const { length } = notifications bulk.find({ _id: { $in: ids }, isRead: false }).update({ $set: { isRead: true } }) - const result = await bulk.execute() + await bulk.execute() + // const result = await bulk.execute() + + // if (result.modifiedCount !== length) { + // console.error(`[notification.markAsRead] ${strings.DB_ERROR}`) + // return res.status(400).send(strings.DB_ERROR) + // } - if (result.modifiedCount !== length) { - console.error(`[notification.markAsRead] ${strings.DB_ERROR}`) - return res.status(400).send(strings.DB_ERROR) - } const counter = await NotificationCounter.findOne({ user: userId }) if (!counter || typeof counter.count === 'undefined') { return res.sendStatus(204) @@ -139,12 +141,14 @@ export async function markAsUnRead(req: Request, res: Response) { const { length } = notifications bulk.find({ _id: { $in: ids }, isRead: true }).update({ $set: { isRead: false } }) - const result = await bulk.execute() + await bulk.execute() + // const result = await bulk.execute() + + // if (result.modifiedCount !== length) { + // console.error(`[notification.markAsUnRead] ${strings.DB_ERROR}`) + // return res.status(400).send(strings.DB_ERROR) + // } - if (result.modifiedCount !== length) { - console.error(`[notification.markAsUnRead] ${strings.DB_ERROR}`) - return res.status(400).send(strings.DB_ERROR) - } const counter = await NotificationCounter.findOne({ user: userId }) if (!counter || typeof counter.count === 'undefined') { return res.sendStatus(204) diff --git a/api/tests/notification.test.ts b/api/tests/notification.test.ts index 833afe17..6ef17d3c 100644 --- a/api/tests/notification.test.ts +++ b/api/tests/notification.test.ts @@ -8,6 +8,7 @@ import app from '../src/app' import * as env from '../src/config/env.config' let ADMIN_USER_ID: string +let AGENCY_ID: string let NOTIFICATION1_ID: string let NOTIFICATION2_ID: string @@ -18,6 +19,8 @@ beforeAll(async () => { if (await DatabaseHelper.Connect(false)) { await TestHelper.initializeDatabase() ADMIN_USER_ID = TestHelper.getAdminUserId() + const agencyName = TestHelper.getAgencyName() + AGENCY_ID = await TestHelper.createAgency(`${agencyName}@test.bookcars.ma`, agencyName) // create admin user notifications and notification counter let notification = new Notification({ user: ADMIN_USER_ID, message: 'Message 1' }) @@ -37,6 +40,8 @@ beforeAll(async () => { afterAll(async () => { await TestHelper.clearDatabase() + await TestHelper.deleteAgency(AGENCY_ID) + // clear admin user notifications and notification counter await Notification.deleteMany({ user: ADMIN_USER_ID }) await NotificationCounter.deleteOne({ user: ADMIN_USER_ID }) @@ -52,13 +57,18 @@ describe('GET /api/notification-counter/:userId', () => { it('should get notification counter', async () => { const token = await TestHelper.signinAsAdmin() - const res = await request(app) + let res = await request(app) .get(`/api/notification-counter/${ADMIN_USER_ID}`) .set(env.X_ACCESS_TOKEN, token) - expect(res.statusCode).toBe(200) expect(res.body.count).toBe(2) + res = await request(app) + .get(`/api/notification-counter/${AGENCY_ID}`) + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(200) + expect(res.body.count).toBe(0) + await TestHelper.signout(token) }) }) @@ -83,17 +93,21 @@ describe('POST /api/mark-notifications-as-read/:userId', () => { const token = await TestHelper.signinAsAdmin() const payload = { ids: [NOTIFICATION1_ID, NOTIFICATION2_ID] } - - const res = await request(app) + let res = await request(app) .post(`/api/mark-notifications-as-read/${ADMIN_USER_ID}`) .set(env.X_ACCESS_TOKEN, token) .send(payload) - expect(res.statusCode).toBe(200) - const counter = await NotificationCounter.findOne({ user: ADMIN_USER_ID }) expect(counter?.count).toBe(0) + payload.ids = [] + res = await request(app) + .post(`/api/mark-notifications-as-read/${TestHelper.getUserId()}`) + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + expect(res.statusCode).toBe(204) + await TestHelper.signout(token) }) }) @@ -103,17 +117,21 @@ describe('POST /api/mark-notifications-as-unread/:userId', () => { const token = await TestHelper.signinAsAdmin() const payload = { ids: [NOTIFICATION1_ID, NOTIFICATION2_ID] } - - const res = await request(app) + let res = await request(app) .post(`/api/mark-notifications-as-unread/${ADMIN_USER_ID}`) .set(env.X_ACCESS_TOKEN, token) .send(payload) - expect(res.statusCode).toBe(200) - const counter = await NotificationCounter.findOne({ user: ADMIN_USER_ID }) expect(counter?.count).toBe(2) + payload.ids = [] + res = await request(app) + .post(`/api/mark-notifications-as-unread/${TestHelper.getUserId()}`) + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + expect(res.statusCode).toBe(204) + await TestHelper.signout(token) }) }) @@ -126,20 +144,22 @@ describe('POST /api/delete-notifications/:userId', () => { expect(notifications.length).toBe(2) const payload = { ids: [NOTIFICATION1_ID, NOTIFICATION2_ID] } - - const res = await request(app) + let res = await request(app) .post(`/api/delete-notifications/${ADMIN_USER_ID}`) .set(env.X_ACCESS_TOKEN, token) .send(payload) - expect(res.statusCode).toBe(200) - notifications = await Notification.find({ user: ADMIN_USER_ID }) expect(notifications.length).toBe(0) - const counter = await NotificationCounter.findOne({ user: ADMIN_USER_ID }) expect(counter?.count).toBe(0) + res = await request(app) + .post(`/api/delete-notifications/${TestHelper.getUserId()}`) + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + expect(res.statusCode).toBe(204) + await TestHelper.signout(token) }) }) diff --git a/api/tests/property.test.ts b/api/tests/property.test.ts index ac6eb8eb..ce6b9d90 100644 --- a/api/tests/property.test.ts +++ b/api/tests/property.test.ts @@ -43,8 +43,8 @@ beforeAll(async () => { // create two agencies const agencyName1 = TestHelper.getAgencyName() - const agencyName2 = TestHelper.getAgencyName() AGENCY1_ID = await TestHelper.createAgency(`${agencyName1}@test.movinin.ma`, agencyName1) + const agencyName2 = TestHelper.getAgencyName() AGENCY2_ID = await TestHelper.createAgency(`${agencyName2}@test.movinin.ma`, agencyName2) // create two locations