Skip to content

Commit

Permalink
add delete note controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mcndt committed Nov 21, 2022
1 parent e6dc2d8 commit bf2ca75
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
4 changes: 2 additions & 2 deletions server/src/controllers/note/note.delete.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
95 changes: 95 additions & 0 deletions server/src/controllers/note/note.delete.controller.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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,
})
);
});
});
1 change: 1 addition & 0 deletions server/src/db/__mocks__/note.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
16 changes: 13 additions & 3 deletions server/src/logging/__mocks__/EventLogger.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit bf2ca75

Please sign in to comment.