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
71 changes: 70 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,70 @@ describe('Note API', () => {
expect(body).toStrictEqual({ message: 'Note not found' });
});
});
});

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

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/Pq1T9vc23Q',
});

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

const body = response?.body !== undefined ? JSON.parse(response?.body) : {};

expect(body).toStrictEqual(expectedNote);
TatianaFomina marked this conversation as resolved.
Show resolved Hide resolved
});

test('Returns 403 when public access is disabled in the note settings', async () => {
const expectedStatus = 403;

const notPublicNote = notes.find(note => {
const settings = noteSettings.find(ns => ns.note_id === note.id)
if(settings != undefined)
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
return settings.is_public === false
})

if(notPublicNote != undefined) {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
const response = await global.api?.fakeRequest({
method: 'GET',
url: `/note/${notPublicNote.public_id}`,
});

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

const body = response?.body !== undefined ? JSON.parse(response?.body) : {};

expect(body).toStrictEqual({ message: 'Permission denied' });
}

});

test('Returns 406 when the id contains incorrect characters', async () => {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
const expectedStatus = 406;

const response = await global.api?.fakeRequest({
method: 'GET',
url: '/note/PR0B_bmdSy',
});
neSpecc marked this conversation as resolved.
Show resolved Hide resolved

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

const body = response?.body !== undefined ? JSON.parse(response?.body) : {};

expect(body).toStrictEqual({ message: 'Note not found' });
});

});
});
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/'
},
},
});