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
84 changes: 83 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,83 @@ 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';

/**
* @todo API should not return internal id and "publicId".
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
* It should return only "id" which is public id. Not implemented yet.
*/

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('Returns 406 when the id contains incorrect characters', async () => {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
const expectedStatus = 406;
const incorrectCharId = 'PR^B{@,&S!';
neSpecc marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

test.todo('Returns note by public id with 200 status when access is disabled, but user is creator');
});
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/',
},
},
});