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
78 changes: 77 additions & 1 deletion src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

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

import notes from '@tests/test-data/notes.json';
import noteSettings from '@tests/test-data/notes-settings.json';
// import users from '@tests/test-data/notes.json';
neSpecc marked this conversation as resolved.
Show resolved Hide resolved

describe('Note API', () => {
describe('GET note/resolve-hostname/:hostname ', () => {
test('Returns note with specified hostname', async () => {
Expand Down Expand Up @@ -49,4 +53,76 @@ 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".
// It should return only "id" which is public id. Not implemented yet.
neSpecc marked this conversation as resolved.
Show resolved Hide resolved

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);

const body = response?.json();

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

// TODO add authorization or something
// else so that the user can be recognized as the author of the note

test('Returns 403 when public access is disabled in the note settings, ' +
'user is not creator of the note', async () => {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
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);

const body = response?.json();

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 nonexistentId = 'PR0B_bmdSy';

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

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

const body = response?.json();

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