diff --git a/src/presentation/http/router/noteList.test.ts b/src/presentation/http/router/noteList.test.ts new file mode 100644 index 00000000..79a19afb --- /dev/null +++ b/src/presentation/http/router/noteList.test.ts @@ -0,0 +1,97 @@ +import { describe, test, expect } from 'vitest'; +import userSessions from 'tests/test-data/userSessions.json'; + +const refresh_token = userSessions[0]['refresh_tocken']; +const access_token = await global.api?.fakeRequest({ + method: 'POST', + url: `/auth/?token=${refresh_token}`, +}); + +describe('NoteList API', () => { + describe('GET notes?page', () => { + test('Returns noteList with specified lenght (not for last page)', async () => { + const expectedStatus = 200; + const portionSize = 30; + + const response = await global.api?.fakeRequest({ + method: 'GET', + headers: { + authorization: `Bearer ${access_token}`, + }, + url: '/notes/&page=1', + }); + + expect(response?.statusCode).toBe(expectedStatus); + + const body = response?.body !== undefined ? JSON.parse(response?.body) : {}; + + expect(body).toHaveLength(portionSize); + }); + + test('Returns noteList with specified lenght (for last page)', async () => { + const expectedStatus = 200; + const portionSize = 20; + + const response = await global.api?.fakeRequest({ + method: 'GET', + headers: { + authorization: `Bearer ${access_token}`, + }, + url: '/notes/&page=2', + }); + + expect(response?.statusCode).toBe(expectedStatus); + + const body = response?.body !== undefined ? JSON.parse(response?.body) : {}; + + expect(body).toHaveLength(portionSize); + }); + + test('Returns noteList with no items if page*portionSize > numberOfNotes', async () => { + const expectedStatus = 200; + + const response = await global.api?.fakeRequest({ + method: 'GET', + headers: { + authorization: `Bearer ${access_token}`, + }, + url: '/notes/&page=3', + }); + + expect(response?.statusCode).toBe(expectedStatus); + + const body = response?.body !== undefined ? JSON.parse(response?.body) : {}; + + expect(body).toEqual([]); + expect(body).toHaveLength(0); + }); + + test('Returns 400 when page < 0', async () => { + const expextedStatus = 400; + + const response = await global.api?.fakeRequest({ + method: 'GET', + headers: { + authorization: `Bearer ${access_token}`, + }, + url: '/notes/&page=0', + }); + + expect(response?.statusCode).toBe(expextedStatus); + }); + + test('Returns 400 when page > 0', async () => { + const expextedStatus = 400; + + const response = await global.api?.fakeRequest({ + method: 'GET', + headers: { + authorization: `Bearer ${access_token}`, + }, + url: '/notes/&page=31', + }); + + expect(response?.statusCode).toBe(expextedStatus); + }); + }); +}); \ No newline at end of file diff --git a/src/tests/test-data/notes.json b/src/tests/test-data/notes.json index 4fc09cbf..32a5c1c0 100644 --- a/src/tests/test-data/notes.json +++ b/src/tests/test-data/notes.json @@ -6,5 +6,397 @@ "content": [], "created_at": "2023-10-16T13:49:19.000Z", "updated_at": "2023-10-16T13:49:19.000Z" + }, + { + "id": 2, + "public_id": "99999902", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:2.000Z", + "updated_at": "2023-10-16T13:49:2.000Z" + }, + { + "id": 3, + "public_id": "99999903", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:3.000Z", + "updated_at": "2023-10-16T13:49:3.000Z" + }, + { + "id": 4, + "public_id": "99999904", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:4.000Z", + "updated_at": "2023-10-16T13:49:4.000Z" + }, + { + "id": 5, + "public_id": "99999905", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:5.000Z", + "updated_at": "2023-10-16T13:49:5.000Z" + }, + { + "id": 6, + "public_id": "99999906", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:6.000Z", + "updated_at": "2023-10-16T13:49:6.000Z" + }, + { + "id": 7, + "public_id": "99999907", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:7.000Z", + "updated_at": "2023-10-16T13:49:7.000Z" + }, + { + "id": 8, + "public_id": "99999908", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:8.000Z", + "updated_at": "2023-10-16T13:49:8.000Z" + }, + { + "id": 9, + "public_id": "99999909", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:9.000Z", + "updated_at": "2023-10-16T13:49:9.000Z" + }, + { + "id": 10, + "public_id": "99999910", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:10.000Z", + "updated_at": "2023-10-16T13:49:10.000Z" + }, + { + "id": 11, + "public_id": "99999911", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:11.000Z", + "updated_at": "2023-10-16T13:49:11.000Z" + }, + { + "id": 12, + "public_id": "99999912", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:12.000Z", + "updated_at": "2023-10-16T13:49:12.000Z" + }, + { + "id": 13, + "public_id": "99999913", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:13.000Z", + "updated_at": "2023-10-16T13:49:13.000Z" + }, + { + "id": 14, + "public_id": "99999914", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:14.000Z", + "updated_at": "2023-10-16T13:49:14.000Z" + }, + { + "id": 15, + "public_id": "99999915", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:15.000Z", + "updated_at": "2023-10-16T13:49:15.000Z" + }, + { + "id": 16, + "public_id": "99999916", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:16.000Z", + "updated_at": "2023-10-16T13:49:16.000Z" + }, + { + "id": 17, + "public_id": "99999917", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:17.000Z", + "updated_at": "2023-10-16T13:49:17.000Z" + }, + { + "id": 18, + "public_id": "99999918", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:18.000Z", + "updated_at": "2023-10-16T13:49:18.000Z" + }, + { + "id": 19, + "public_id": "99999919", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:19.000Z", + "updated_at": "2023-10-16T13:49:19.000Z" + }, + { + "id": 20, + "public_id": "99999920", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:20.000Z", + "updated_at": "2023-10-16T13:49:20.000Z" + }, + { + "id": 21, + "public_id": "99999921", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:21.000Z", + "updated_at": "2023-10-16T13:49:21.000Z" + }, + { + "id": 22, + "public_id": "99999922", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:22.000Z", + "updated_at": "2023-10-16T13:49:22.000Z" + }, + { + "id": 23, + "public_id": "99999923", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:23.000Z", + "updated_at": "2023-10-16T13:49:23.000Z" + }, + { + "id": 24, + "public_id": "99999924", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:24.000Z", + "updated_at": "2023-10-16T13:49:24.000Z" + }, + { + "id": 25, + "public_id": "99999925", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:25.000Z", + "updated_at": "2023-10-16T13:49:25.000Z" + }, + { + "id": 26, + "public_id": "99999926", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:26.000Z", + "updated_at": "2023-10-16T13:49:26.000Z" + }, + { + "id": 27, + "public_id": "99999927", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:27.000Z", + "updated_at": "2023-10-16T13:49:27.000Z" + }, + { + "id": 28, + "public_id": "99999928", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:28.000Z", + "updated_at": "2023-10-16T13:49:28.000Z" + }, + { + "id": 29, + "public_id": "99999929", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:29.000Z", + "updated_at": "2023-10-16T13:49:29.000Z" + }, + { + "id": 30, + "public_id": "99999930", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:30.000Z", + "updated_at": "2023-10-16T13:49:30.000Z" + }, + { + "id": 31, + "public_id": "99999931", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:31.000Z", + "updated_at": "2023-10-16T13:49:31.000Z" + }, + { + "id": 32, + "public_id": "99999932", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:32.000Z", + "updated_at": "2023-10-16T13:49:32.000Z" + }, + { + "id": 33, + "public_id": "99999933", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:33.000Z", + "updated_at": "2023-10-16T13:49:33.000Z" + }, + { + "id": 34, + "public_id": "99999934", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:34.000Z", + "updated_at": "2023-10-16T13:49:34.000Z" + }, + { + "id": 35, + "public_id": "99999935", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:35.000Z", + "updated_at": "2023-10-16T13:49:35.000Z" + }, + { + "id": 36, + "public_id": "99999936", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:36.000Z", + "updated_at": "2023-10-16T13:49:36.000Z" + }, + { + "id": 37, + "public_id": "99999937", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:37.000Z", + "updated_at": "2023-10-16T13:49:37.000Z" + }, + { + "id": 38, + "public_id": "99999938", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:38.000Z", + "updated_at": "2023-10-16T13:49:38.000Z" + }, + { + "id": 39, + "public_id": "99999939", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:39.000Z", + "updated_at": "2023-10-16T13:49:39.000Z" + }, + { + "id": 40, + "public_id": "99999940", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:40.000Z", + "updated_at": "2023-10-16T13:49:40.000Z" + }, + { + "id": 41, + "public_id": "99999941", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:41.000Z", + "updated_at": "2023-10-16T13:49:41.000Z" + }, + { + "id": 42, + "public_id": "99999942", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:42.000Z", + "updated_at": "2023-10-16T13:49:42.000Z" + }, + { + "id": 43, + "public_id": "99999943", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:43.000Z", + "updated_at": "2023-10-16T13:49:43.000Z" + }, + { + "id": 44, + "public_id": "99999944", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:44.000Z", + "updated_at": "2023-10-16T13:49:44.000Z" + }, + { + "id": 45, + "public_id": "99999945", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:45.000Z", + "updated_at": "2023-10-16T13:49:45.000Z" + }, + { + "id": 46, + "public_id": "99999946", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:46.000Z", + "updated_at": "2023-10-16T13:49:46.000Z" + }, + { + "id": 47, + "public_id": "99999947", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:47.000Z", + "updated_at": "2023-10-16T13:49:47.000Z" + }, + { + "id": 48, + "public_id": "99999948", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:48.000Z", + "updated_at": "2023-10-16T13:49:48.000Z" + }, + { + "id": 49, + "public_id": "99999949", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:49.000Z", + "updated_at": "2023-10-16T13:49:49.000Z" + }, + { + "id": 50, + "public_id": "99999950", + "creator_id": 1, + "content": [], + "created_at": "2023-10-16T13:49:50.000Z", + "updated_at": "2023-10-16T13:49:50.000Z" } -] +] \ No newline at end of file diff --git a/src/tests/test-data/notes-settings.json b/src/tests/test-data/notesSettings.json similarity index 100% rename from src/tests/test-data/notes-settings.json rename to src/tests/test-data/notesSettings.json diff --git a/src/tests/test-data/userSessions.json b/src/tests/test-data/userSessions.json new file mode 100644 index 00000000..892333ca --- /dev/null +++ b/src/tests/test-data/userSessions.json @@ -0,0 +1,8 @@ +[ + { + "id" : 1, + "user_id" : 4, + "refresh_tocken" : "IqrTkSKmel", + "refresh_tocken_expites_at" : "2023-11-21 19:19:40.911+03" + } +] \ No newline at end of file diff --git a/src/tests/utils/insert-data.ts b/src/tests/utils/insert-data.ts index cbb3c590..27dd56ef 100644 --- a/src/tests/utils/insert-data.ts +++ b/src/tests/utils/insert-data.ts @@ -1,7 +1,8 @@ import type SequelizeOrm from '@repository/storage/postgres/orm/index.js'; import users from '../test-data/users.json'; +import userSessions from '../test-data/userSessions.json'; import notes from '../test-data/notes.json'; -import noteSettings from '../test-data/notes-settings.json'; +import noteSettings from '../test-data/notesSettings.json'; /** * Fills in the database with users data @@ -14,6 +15,16 @@ async function insertUsers(db: SequelizeOrm): Promise { } } +/** + * Fills in the database with user sessions + * + * @param db - SequelizeOrm instance + */ +async function insertUserSessions(db: SequelizeOrm): Promise { + for (const userSession of userSessions) { + await db.connection.query(`INSERT INTO public.userSessions (id, user_id, refresh_token, refresh_token_expires_at) VALUES (${userSession.id}, ${userSession.user_id}, ${userSession.refresh_tocken}, ${userSession.refresh_tocken_expites_at})`); + } +} /** * Fills in the database with notes data * @@ -44,6 +55,7 @@ async function insertNoteSettings(db: SequelizeOrm): Promise { */ export async function insertData(db: SequelizeOrm): Promise { await insertUsers(db); + await insertUserSessions(db); await insertNotes(db); await insertNoteSettings(db); }