note dao unit test

This commit is contained in:
Maxime Cannoodt 2022-08-30 10:05:41 +02:00
parent 433394a3c2
commit 86d8771303
3 changed files with 65 additions and 2 deletions

2
plugin

@ -1 +1 @@
Subproject commit 834850afae81abce873da8be8792614ab8cc0b76
Subproject commit 8ccf08c4d2d1fb99f38488085c3f40c22393c9c0

View File

@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import type { EncryptedEmbed, EncryptedNote } from "@prisma/client";
import type { EncryptedNote } from "@prisma/client";
import { getEmbed, createEmbed } from "./embed.dao";
import { createNote } from "./note.dao";

View File

@ -0,0 +1,63 @@
import { EncryptedNote } from "@prisma/client";
import { describe, it, expect } from "vitest";
import { createNote, deleteNotes, getExpiredNotes, getNote } from "./note.dao";
const VALID_CIPHERTEXT = Buffer.from("sample_ciphertext").toString("base64");
const VALID_HMAC = Buffer.from("sample_hmac").toString("base64");
const VALID_NOTE = {
ciphertext: VALID_CIPHERTEXT,
hmac: VALID_HMAC,
crypto_version: "v2",
expire_time: new Date(),
} as EncryptedNote;
describe("Writes and reads", () => {
it("should write a new note", async () => {
const res = await createNote(VALID_NOTE);
expect(res.id).not.toBeNull();
expect(res.id.length).toBeGreaterThan(0);
expect(res.ciphertext).toStrictEqual(VALID_NOTE.ciphertext);
expect(res.hmac).toStrictEqual(VALID_NOTE.hmac);
expect(res.crypto_version).toStrictEqual(VALID_NOTE.crypto_version);
expect(res.expire_time).toStrictEqual(VALID_NOTE.expire_time);
expect(res.insert_time).not.toBeNull();
expect(res.insert_time.getTime()).toBeLessThanOrEqual(new Date().getTime());
});
it("should find an existing note by id", async () => {
const note = await createNote(VALID_NOTE);
const res = await getNote(note.id);
expect(res).not.toBeNull();
expect(res).toMatchObject(note);
});
it("should not find a non-existing note by id", async () => {
const res = await getNote("non-existing-id");
expect(res).toBeNull();
});
it("should properly delete notes", async () => {
const note = await createNote(VALID_NOTE);
const res = await getNote(note.id);
expect(res).not.toBeNull();
const res2 = await deleteNotes([note.id]);
expect(res2).toBe(1);
const res3 = await getNote(note.id);
expect(res3).toBeNull();
});
it("should return expired notes", async () => {
const expiredNote = await createNote({
...VALID_NOTE,
expire_time: new Date(0),
});
const freshNote = await createNote({
...VALID_NOTE,
expire_time: new Date(Date.now() + 1000),
});
const res = await getExpiredNotes();
expect(res).toContainEqual(expiredNote);
expect(res).not.toContainEqual(freshNote);
});
});