add delete note controller

This commit is contained in:
Maxime Cannoodt 2022-11-21 21:06:15 +01:00
parent e6dc2d849a
commit bf2ca751a8
4 changed files with 111 additions and 5 deletions

View File

@ -36,7 +36,7 @@ export async function deleteNoteController(
console.log("invalid user id"); console.log("invalid user id");
res.status(400).send("Invalid user id (checksum failed)"); res.status(400).send("Invalid user id (checksum failed)");
event.error = "Invalid user id (checksum failed)"; event.error = "Invalid user id (checksum failed)";
EventLogger.writeEvent(event); EventLogger.deleteEvent(event);
return; return;
} }
@ -60,7 +60,7 @@ export async function deleteNoteController(
// Delete note // Delete note
try { try {
await deleteNote(note.id); await deleteNote(note.id);
res.status(200); res.status(200).send();
event.success = true; event.success = true;
event.note_id = note.id; event.note_id = note.id;
event.size_bytes = getNoteSize(note); event.size_bytes = getNoteSize(note);

View File

@ -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,
})
);
});
});

View File

@ -4,3 +4,4 @@ export const getNote = vi.fn();
export const createNote = vi.fn(); export const createNote = vi.fn();
export const getExpiredNotes = vi.fn(); export const getExpiredNotes = vi.fn();
export const deleteNotes = vi.fn(); export const deleteNotes = vi.fn();
export const deleteNote = vi.fn();

View File

@ -1,9 +1,19 @@
import { vi } from "vitest"; 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 = { const mockedEventLogger = {
writeEvent: vi.fn(), writeEvent: vi.fn(logEventToConsole),
readEvent: vi.fn(), readEvent: vi.fn(logEventToConsole),
purgeEvent: vi.fn(), purgeEvent: vi.fn(logEventToConsole),
deleteEvent: vi.fn(logEventToConsole),
updateEvent: vi.fn(logEventToConsole),
}; };
export default mockedEventLogger; export default mockedEventLogger;