From db990940acdeaf6c8e55e508368dd106db7b1f9a Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Sun, 24 Apr 2022 23:13:08 +0100 Subject: [PATCH] test: add tests for POST userID/notif/mark-all-read --- .../_userID/notifications/router.test.ts | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/server/src/server/router/api/v1/users/_userID/notifications/router.test.ts b/server/src/server/router/api/v1/users/_userID/notifications/router.test.ts index 9058faa82..f89d629ee 100644 --- a/server/src/server/router/api/v1/users/_userID/notifications/router.test.ts +++ b/server/src/server/router/api/v1/users/_userID/notifications/router.test.ts @@ -63,3 +63,69 @@ t.test("GET /api/v1/users/:userID/notifications", async (t) => { t.end(); }); + +t.test("POST /api/v1/users/:userID/notifications/mark-all-read", async (t) => { + t.beforeEach(ResetDBState); + t.beforeEach(() => + Promise.all([ + db.notifications.insert([ + mkFakeNotification({ notifID: "read", read: true, sentAt: 2 }), + mkFakeNotification({ notifID: "unread", read: false, sentAt: 3 }), + mkFakeNotification({ notifID: "unread_2", read: false, sentAt: 4 }), + mkFakeNotification({ notifID: "not_ours", sentTo: 2, sentAt: 5 }), + ]), + db.users.insert(mkFakeUser(2)), + ]) + ); + + const cookie = await CreateFakeAuthCookie(mockApi); + + t.test("Should mark all of a user's notifications as read.", async (t) => { + const res = await mockApi + .post("/api/v1/users/1/notifications/mark-all-read") + .set("Cookie", cookie); + + t.equal(res.statusCode, 200, "Should return 200."); + + t.strictSame(res.body.body, {}, "Should have no response body."); + + const dbRes = await db.notifications.findOne({ + sentTo: 1, + read: false, + }); + + t.equal(dbRes, null, "Should not find any unread messages in the users inbox."); + + t.end(); + }); + + t.test("Should return 401 if not authenticated.", async (t) => { + const res = await mockApi.post("/api/v1/users/1/notifications/mark-all-read"); + + t.equal(res.statusCode, 401); + + t.end(); + }); + + t.test("Should return 403 if authed as someone else.", async (t) => { + const res = await mockApi + .post("/api/v1/users/2/notifications/mark-all-read") + .set("Cookie", cookie); + + t.equal(res.statusCode, 403); + + t.end(); + }); + + t.test("Should return 403 if authed as right user without self-key", async (t) => { + const res = await mockApi + .post("/api/v1/users/1/notifications/mark-all-read") + .set("Authorization", "Bearer fake_api_token"); + + t.equal(res.statusCode, 403); + + t.end(); + }); + + t.end(); +});