Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(notes): added e2e api test #14

Merged
merged 4 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/app-client/src/modules/notes/notes.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function createNote({ content, isPasswordProtected, ttlInSeconds, deleteAf

async function fetchNoteById({ noteId }: { noteId: string }) {
const { note } = await apiClient<{ note: { content: string; isPasswordProtected: boolean } }>({
path: `/api/note/${noteId}`,
path: `/api/notes/${noteId}`,
method: 'GET',
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { env as getEnv } from 'hono/adapter';
import { createMiddleware } from 'hono/factory';
import _ from 'lodash';
import { getConfig } from '../config/config';
import type { Config } from '../config/config.types';

Expand All @@ -13,10 +12,7 @@ export function createConfigMiddleware({ config: initialConfig }: { config?: Con
}

const env = getEnv(c);
const baseConfig = getConfig({ env });
const overrideConfig = baseConfig.env === 'test' ? c.env.CONFIG_OVERRIDE : {};

const config = _.merge({}, baseConfig, overrideConfig);
const config = getConfig({ env });

c.set('config', config);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, test } from 'vitest';
import memoryDriver from 'unstorage/drivers/memory';
import { createServer } from '../../app/server';

describe('e2e', () => {
describe('create and view note', () => {
test('a note can be created and viewed', async () => {
const { app } = createServer({
storageDriver: memoryDriver(),
});

const note = {
content: '<encrypted-content>',
isPasswordProtected: false,
deleteAfterReading: false,
ttlInSeconds: 600,
};

const createNoteResponse = await app.request(
'/api/notes',
{
method: 'POST',
body: JSON.stringify(note),
headers: new Headers({ 'Content-Type': 'application/json' }),
},
);

expect(createNoteResponse.status).to.eql(200);

const { noteId } = await createNoteResponse.json<any>();

expect(noteId).toBeDefined();
expect(noteId).to.be.a('string');

const viewNoteResponse = await app.request(`/api/notes/${noteId}`);

expect(viewNoteResponse.status).to.eql(200);

const { note: retrievedNote } = await viewNoteResponse.json<any>();

expect(retrievedNote).to.eql({
content: '<encrypted-content>',
isPasswordProtected: false,
deleteAfterReading: false,
});
});
});
});
76 changes: 76 additions & 0 deletions packages/app-server/src/modules/notes/notes.repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { createStorage } from 'unstorage';
import { describe, expect, test } from 'vitest';
import memoryDriver from 'unstorage/drivers/memory';
import { createNoteRepository } from './notes.repository';
import { createNoteNotFoundError } from './notes.errors';

describe('notes repository', () => {
describe('getNoteById', () => {
test('a note identifier is returned when a note is saved, and the note can be retrieved by its identifier', async () => {
const { getNoteById, saveNote } = createNoteRepository({ storage: createStorage({ driver: memoryDriver() }) });

const { noteId } = await saveNote({
content: '<encrypted-content>',
ttlInSeconds: 60,
isPasswordProtected: false,
deleteAfterReading: false,
now: new Date('2024-01-01T00:00:00Z'),
});

const { note } = await getNoteById({
noteId,
now: new Date('2024-01-01T00:00:30Z'),
});

expect(note).to.eql({
content: '<encrypted-content>',
isPasswordProtected: false,
deleteAfterReading: false,

});
});

test('a note is deleted after reading if the deleteAfterReading flag is set', async () => {
const { getNoteById, saveNote } = createNoteRepository({ storage: createStorage({ driver: memoryDriver() }) });

const { noteId } = await saveNote({
content: '<encrypted-content>',
ttlInSeconds: 60,
isPasswordProtected: false,
deleteAfterReading: true,
now: new Date('2024-01-01T00:00:00Z'),
});

await getNoteById({
noteId,
now: new Date('2024-01-01T00:00:30Z'),
});

expect(
getNoteById({
noteId,
now: new Date('2024-01-01T00:01:00Z'),
}),
).rejects.toThrow(createNoteNotFoundError());
});

test('a note that has expired is deleted and cannot be retrieved', async () => {
const { getNoteById, saveNote } = createNoteRepository({ storage: createStorage({ driver: memoryDriver() }) });

const { noteId } = await saveNote({
content: '<encrypted-content>',
ttlInSeconds: 60,
isPasswordProtected: false,
deleteAfterReading: false,
now: new Date('2024-01-01T00:00:00Z'),
});

expect(
getNoteById({
noteId,
now: new Date('2024-01-01T00:01:01Z'),
}),
).rejects.toThrow(createNoteNotFoundError());
});
});
});
5 changes: 4 additions & 1 deletion packages/app-server/src/modules/notes/notes.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Storage } from 'unstorage';
import { addSeconds, isBefore } from 'date-fns';
import _ from 'lodash';
import { generateNoteId } from './notes.models';
import { createNoteNotFoundError } from './notes.errors';

Expand Down Expand Up @@ -49,7 +50,9 @@ function createNoteRepository({ storage }: { storage: Storage }) {
await storage.removeItem(noteId);
}

return { note };
return {
note: _.omit(note, 'expirationDate'),
};
},
};
}
2 changes: 1 addition & 1 deletion packages/app-server/src/modules/notes/notes.routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function registerNotesRoutes({ app }: { app: ServerInstance }) {
}

function setupGetNoteRoute({ app }: { app: ServerInstance }) {
app.get('/api/note/:noteId', async (context) => {
app.get('/api/notes/:noteId', async (context) => {
const { noteId } = context.req.param();

const storage = context.get('storage');
Expand Down
Loading