Skip to content

Commit

Permalink
Fix some tests for POST /note
Browse files Browse the repository at this point in the history
  • Loading branch information
elizachi committed Dec 10, 2023
1 parent b27c565 commit 49392ab
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
49 changes: 46 additions & 3 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Note API', () => {
const expectedStatus = 200;
const expectedResponse = {
'note': {
'id': 1,
'id': 60,
'publicId': 'note_1',
'creatorId': 1,
'content': null,
Expand All @@ -19,8 +19,8 @@ describe('Note API', () => {
'noteSettings': {
'customHostname': 'codex.so',
'isPublic': true,
'id': 1,
'noteId': 1,
'id': 60,
'noteId': 60,
'invitationHash': 'Hzh2hy4igf',
},
},
Expand Down Expand Up @@ -254,6 +254,29 @@ describe('Note API', () => {
});

describe('PATCH note/:notePublicId ', () => {
test('Update note by public id with 200 status, user is creator of the note', async () => {
const expectedStatus = 200;
const userId = 2;
const accessToken = global.auth(userId);

const userNote = notes.find(newNote => {
return newNote.creator_id === userId;
});

const response = await global.api?.fakeRequest({
method: 'PATCH',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: `/note/${userNote!.public_id}`,
body: {
'content': { new: 'content added' },
},
});

expect(response?.statusCode).toBe(expectedStatus);
});

test('Returns status 401 when the user is not authorized', async () => {
const expectedStatus = 401;
const correctID = 'Pq1T9vc23Q';
Expand Down Expand Up @@ -310,6 +333,26 @@ describe('Note API', () => {
test.todo('Update note by public id with 200 status, user is creator of the note');
});

describe('POST /note', () => {
test('Post a new note', async () => {
const expectedStatus = 200;
const userId = 2;
const accessToken = global.auth(userId);

const response = await global.api?.fakeRequest({
method: 'POST',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: `/note`,
body: {
},
});

expect(response?.statusCode).toBe(expectedStatus);
});
});

test.todo('Create note with parentId field');

test.todo('API should not return internal id and "publicId". It should return only "id" which is public id.');
Expand Down
7 changes: 0 additions & 7 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,10 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
* TODO: Validate request query
*/
const content = request.body.content as JSON;
const parentPublicId = request.params.parentId;
const { userId } = request;

const addedNote = await noteService.addNote(content, userId as number); // "authRequired" policy ensures that userId is not null

if (parentPublicId !== undefined) {
const parentId = (await noteService.getNoteByPublicId(parentPublicId)).id;

await noteRelationshipService.addNoteRelation(addedNote.id, parentId);
}

/**
* @todo use event bus: emit 'note-added' event and subscribe to it in other modules like 'note-settings'
*/
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test-data/notes-settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"id": 1,
"note_id": 1,
"id": 60,
"note_id": 60,
"custom_hostname": "codex.so",
"is_public": true,
"invitation_hash": "Hzh2hy4igf"
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test-data/notes.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"id": 1,
"id": 60,
"public_id": "note_1",
"creator_id": 1,
"content": [],
Expand Down

0 comments on commit 49392ab

Please sign in to comment.