test: add tests for POST userID/notif/mark-all-read

This commit is contained in:
zkldi
2022-04-24 23:13:08 +01:00
parent 0b84416c29
commit db990940ac
@@ -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();
});