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

Tests for get note by id #99

Merged
merged 12 commits into from
Nov 3, 2023
90 changes: 89 additions & 1 deletion src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import { describe, test, expect } from 'vitest';

import notes from '@tests/test-data/notes.json';
import noteSettings from '@tests/test-data/notes-settings.json';

describe('Note API', () => {
describe('GET note/resolve-hostname/:hostname ', () => {
test('Returns note with specified hostname', async () => {
Expand Down Expand Up @@ -49,4 +52,89 @@ describe('Note API', () => {
expect(body).toStrictEqual({ message: 'Note not found' });
});
});
});

describe('GET note/:notePublicId ', () => {
test('Returns note by public id with 200 status when note is publicly available', async () => {
const expectedStatus = 200;
const correctID = 'Pq1T9vc23Q';

const expectedNote = {
'id': 2,
'publicId': 'Pq1T9vc23Q',
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
'creatorId': 1,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
};

const response = await global.api?.fakeRequest({
method: 'GET',
url: `/note/${correctID}`,
});

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

expect(response?.json()).toStrictEqual(expectedNote);
});

test('Returns 403 when public access is disabled, user is not creator of the note', async () => {
const expectedStatus = 403;

const notPublicNote = notes.find(newNote => {
const settings = noteSettings.find(ns => ns.note_id === newNote.id);

return settings!.is_public === false;
});

const response = await global.api?.fakeRequest({
method: 'GET',
url: `/note/${notPublicNote!.public_id}`,
});

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

expect(response?.json()).toStrictEqual({ message: 'Permission denied' });
});

test('Returns 406 when the id does not exist', async () => {
const expectedStatus = 406;
const nonexistentId = 'ishvm5qH84';

const response = await global.api?.fakeRequest({
method: 'GET',
url: `/note/${nonexistentId}`,
});

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

expect(response?.json()).toStrictEqual({ message: 'Note not found' });
});

test.each([
{ id: 'mVz3iHuez',
expectedMessage: 'params/notePublicId must NOT have fewer than 10 characters' },

{ id: 'cR8eqF1mFf0',
expectedMessage: 'params/notePublicId must NOT have more than 10 characters' },

{ id: '+=*&*5%&&^&-',
expectedMessage: '\'/note/+=*&*5%&&^&-\' is not a valid url component' },
])
('Returns 400 when id has incorrect characters and length', async ({ id, expectedMessage }) => {
const expectedStatus = 400;

const response = await global.api?.fakeRequest({
method: 'GET',
url: `/note/${id}`,
});

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

expect(response?.json().message).toStrictEqual(expectedMessage);
});

test.todo('Returns note by public id with 200 status when access is disabled, but user is creator');

test.todo('API should not return internal id and "publicId". It should return only "id" which is public id.');
});
});
14 changes: 14 additions & 0 deletions src/tests/test-data/notes-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,19 @@
"note_id": 1,
"custom_hostname": "codex.so",
"is_public": true
},

{
"id": 2,
"note_id": 2,
"custom_hostname": "codex.so",
"is_public": true
},

{
"id": 3,
"note_id": 3,
"custom_hostname": "codex.so",
"is_public": false
}
]
18 changes: 18 additions & 0 deletions src/tests/test-data/notes.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,23 @@
"content": [],
"created_at": "2023-10-16T13:49:19.000Z",
"updated_at": "2023-10-16T13:49:19.000Z"
},

{
"id": 2,
"public_id": "Pq1T9vc23Q",
"creator_id": 1,
"content": [],
"created_at": "2023-10-16T13:49:19.000Z",
"updated_at": "2023-10-16T13:49:19.000Z"
},

{
"id": 3,
"public_id": "73NdxFZ4k7",
"creator_id": 1,
"content": [],
"created_at": "2023-10-16T13:49:19.000Z",
"updated_at": "2023-10-16T13:49:19.000Z"
}
]
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"@presentation/*": ["presentation/*"],
"@lib/*": ["lib/*"],
"@domain/*": ["domain/*"],
"@repository/*": ["repository/*"]
"@repository/*": ["repository/*"],
"@tests/*": ["tests/*"]
},
},
"include": [
Expand Down
1 change: 1 addition & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineConfig({
'@lib/': '/src/lib/',
'@domain/': '/src/domain/',
'@repository/': '/src/repository/',
'@tests/': '/src/tests/',
},
},
});