From bf2ca751a8f467addfe38c907e6741b503c50f2b Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Mon, 21 Nov 2022 21:06:15 +0100 Subject: [PATCH] add delete note controller --- .../note/note.delete.controller.ts | 4 +- .../note/note.delete.controller.unit.test.ts | 95 +++++++++++++++++++ server/src/db/__mocks__/note.dao.ts | 1 + server/src/logging/__mocks__/EventLogger.ts | 16 +++- 4 files changed, 111 insertions(+), 5 deletions(-) diff --git a/server/src/controllers/note/note.delete.controller.ts b/server/src/controllers/note/note.delete.controller.ts index ce441a3..a10eeec 100644 --- a/server/src/controllers/note/note.delete.controller.ts +++ b/server/src/controllers/note/note.delete.controller.ts @@ -36,7 +36,7 @@ export async function deleteNoteController( console.log("invalid user id"); res.status(400).send("Invalid user id (checksum failed)"); event.error = "Invalid user id (checksum failed)"; - EventLogger.writeEvent(event); + EventLogger.deleteEvent(event); return; } @@ -60,7 +60,7 @@ export async function deleteNoteController( // Delete note try { await deleteNote(note.id); - res.status(200); + res.status(200).send(); event.success = true; event.note_id = note.id; event.size_bytes = getNoteSize(note); diff --git a/server/src/controllers/note/note.delete.controller.unit.test.ts b/server/src/controllers/note/note.delete.controller.unit.test.ts index e69de29..bfac44d 100644 --- a/server/src/controllers/note/note.delete.controller.unit.test.ts +++ b/server/src/controllers/note/note.delete.controller.unit.test.ts @@ -0,0 +1,95 @@ +import { EncryptedNote } from "@prisma/client"; +import express from "express"; +import supertest from "supertest"; +import { vi, describe, it, beforeEach, afterEach, expect } from "vitest"; +import * as noteDao from "../../db/note.dao"; +import EventLogger from "../../logging/EventLogger"; +import { deleteNoteController } from "./note.delete.controller"; + +vi.mock("../../db/note.dao"); +vi.mock("../../logging/EventLogger"); + +const VALID_USER_ID = "f06536e7df6857fc"; + +const MOCK_SECRET_TOKEN = "U0VDUkVUX1RPS0VO"; +const MOCK_NOTE_ID = "NOTE_ID"; + +describe("note.delete.controller", () => { + let mockNoteDao = vi.mocked(noteDao); + let mockEventLogger = vi.mocked(EventLogger); + + const test_app = express() + .use(express.json()) + .delete("/:id", deleteNoteController); + + beforeEach(() => { + mockNoteDao.getNote.mockImplementation(async (noteId) => { + if (noteId === MOCK_NOTE_ID) { + return { + id: MOCK_NOTE_ID, + secret_token: MOCK_SECRET_TOKEN, + } as EncryptedNote; + } else { + return null; + } + }); + + mockNoteDao.deleteNote.mockImplementation(async (id) => { + if (id === MOCK_NOTE_ID) { + return vi.fn() as unknown as EncryptedNote; + } else { + throw new Error("Note not found"); + } + }); + }); + + afterEach(() => { + vi.resetAllMocks(); + }); + + it("Should delete a note with a valid secret token", async () => { + const response = await supertest(test_app) + .delete(`/${MOCK_NOTE_ID}`) + .send({ user_id: VALID_USER_ID, secret_token: MOCK_SECRET_TOKEN }); + + expect(response.status).toBe(200); + expect(mockNoteDao.deleteNote).toBeCalledWith(MOCK_NOTE_ID); + expect(mockEventLogger.deleteEvent).toBeCalledWith( + expect.objectContaining({ + note_id: MOCK_NOTE_ID, + user_id: VALID_USER_ID, + success: true, + }) + ); + }); + + it("Should return 401 for an invalid secret token", async () => { + const response = await supertest(test_app) + .delete(`/${MOCK_NOTE_ID}`) + .send({ user_id: VALID_USER_ID, secret_token: "0000" }); + + expect(response.status).toBe(401); + expect(mockNoteDao.deleteNote).not.toBeCalled(); + expect(mockEventLogger.deleteEvent).toBeCalledWith( + expect.objectContaining({ + user_id: VALID_USER_ID, + success: false, + }) + ); + }); + + it("Should return 404 for a note that does not exist", async () => { + const response = await supertest(test_app) + .delete("/0000") + .send({ user_id: VALID_USER_ID, secret_token: MOCK_SECRET_TOKEN }); + + expect(response.status).toBe(404); + expect(mockNoteDao.deleteNote).not.toBeCalled(); + expect(mockEventLogger.deleteEvent).toBeCalledWith( + expect.objectContaining({ + user_id: VALID_USER_ID, + success: false, + }) + ); + }); +}); diff --git a/server/src/db/__mocks__/note.dao.ts b/server/src/db/__mocks__/note.dao.ts index 17194f5..d49ffab 100644 --- a/server/src/db/__mocks__/note.dao.ts +++ b/server/src/db/__mocks__/note.dao.ts @@ -4,3 +4,4 @@ export const getNote = vi.fn(); export const createNote = vi.fn(); export const getExpiredNotes = vi.fn(); export const deleteNotes = vi.fn(); +export const deleteNote = vi.fn(); diff --git a/server/src/logging/__mocks__/EventLogger.ts b/server/src/logging/__mocks__/EventLogger.ts index 3c2e2b3..b12080b 100644 --- a/server/src/logging/__mocks__/EventLogger.ts +++ b/server/src/logging/__mocks__/EventLogger.ts @@ -1,9 +1,19 @@ import { vi } from "vitest"; +import { Event } from "../EventLogger"; +import logger from "../logger"; + +const logEventToConsole = (event: Event) => { + if (event.error) { + console.error(event.error); + } +}; const mockedEventLogger = { - writeEvent: vi.fn(), - readEvent: vi.fn(), - purgeEvent: vi.fn(), + writeEvent: vi.fn(logEventToConsole), + readEvent: vi.fn(logEventToConsole), + purgeEvent: vi.fn(logEventToConsole), + deleteEvent: vi.fn(logEventToConsole), + updateEvent: vi.fn(logEventToConsole), }; export default mockedEventLogger;