diff --git a/src/presentation/http/router/auth.ts b/src/presentation/http/router/auth.ts index 6addd2b1..d77bc231 100644 --- a/src/presentation/http/router/auth.ts +++ b/src/presentation/http/router/auth.ts @@ -41,11 +41,6 @@ const AuthRouter: FastifyPluginCallback = (fastify, opts, don Reply: AuthSession | ErrorResponse; }>('/', async (request, reply) => { const { token } = request.body; - - console.log('token======================================'); - console.log(token); - console.log('======================================'); - const userSession = await opts.authService.verifyRefreshToken(token); /** diff --git a/src/presentation/http/router/noteList.test.ts b/src/presentation/http/router/noteList.test.ts index 96678c23..91cac554 100644 --- a/src/presentation/http/router/noteList.test.ts +++ b/src/presentation/http/router/noteList.test.ts @@ -1,28 +1,51 @@ +import type AuthSession from '@domain/entities/authSession'; import userSessions from '@tests/test-data/userSessions.json'; -import { describe, test, expect } from 'vitest'; - -const refresh_token = userSessions[0]['refresh_token']; - - -console.log('ref============================================'); -console.log(refresh_token); -console.log('============================================'); - -const access_token = await global.api?.fakeRequest({ - method: 'POST', - url: '/auth', - body : JSON.stringify({ token : refresh_token }), - // body : refresh_token, -}); +import { describe, test, expect, beforeAll } from 'vitest'; + + +/** + * Access token that will be used for Auhorization header + */ +let accessToken = ''; + +/** + * Util for authorization + * + * @param refreshToken - refresh token. There should be a user session with this refresh token in database + * @todo Move this function to tests/utils + */ +async function authorize(refreshToken: string): Promise { + const response = await global.api?.fakeRequest({ + method: 'POST', + url: '/auth', + headers: { + // eslint-disable-next-line @typescript-eslint/naming-convention + 'Content-Type': 'application/json', + }, + body : JSON.stringify({ token : refreshToken }), + }); + const body: AuthSession = response?.body !== undefined ? JSON.parse(response?.body) : {}; -console.log('acc============================================'); -console.log(access_token); -console.log('============================================'); + return body.accessToken; +} describe('NoteList API', () => { + beforeAll(async () => { + /** + * Authorize using refresh token and POST /auth + */ + const refreshToken = userSessions[0]['refresh_token']; + + try { + accessToken = await authorize(refreshToken); + } catch (error) { + console.log('Test Authorization failed', error); + } + }); + describe('GET /notes?page', () => { - test('Returns noteList with specified lenght (not for last page)', async () => { + test('Returns noteList with specified length (not for last page)', async () => { const expectedStatus = 200; const portionSize = 30; const pageNumber = 1; @@ -30,9 +53,9 @@ describe('NoteList API', () => { const response = await global.api?.fakeRequest({ method: 'GET', headers: { - authorization: `Bearer ${access_token}`, + authorization: `Bearer ${accessToken}`, }, - url: `/notes&page=${pageNumber}`, + url: `/notes?page=${pageNumber}`, }); expect(response?.statusCode).toBe(expectedStatus); @@ -50,9 +73,9 @@ describe('NoteList API', () => { const response = await global.api?.fakeRequest({ method: 'GET', headers: { - authorization: `Bearer ${access_token}`, + authorization: `Bearer ${accessToken}`, }, - url: `/notes&page=${pageNumber}`, + url: `/notes?page=${pageNumber}`, }); expect(response?.statusCode).toBe(expectedStatus); @@ -66,12 +89,15 @@ describe('NoteList API', () => { const expectedStatus = 200; const pageNumber = 3; + console.log('accessToken', accessToken); + + const response = await global.api?.fakeRequest({ method: 'GET', headers: { - authorization: `Bearer ${access_token}`, + authorization: `Bearer ${accessToken}`, }, - url: `/notes&page=${pageNumber}`, + url: `/notes?page=${pageNumber}`, }); expect(response?.statusCode).toBe(expectedStatus); @@ -90,9 +116,9 @@ describe('NoteList API', () => { const response = await global.api?.fakeRequest({ method: 'GET', headers: { - authorization: `Bearer ${access_token}`, + authorization: `Bearer ${accessToken}`, }, - url: `/notes&page=${pageNumber}`, + url: `/notes?page=${pageNumber}`, }); expect(response?.statusCode).toBe(expextedStatus); @@ -105,9 +131,9 @@ describe('NoteList API', () => { const response = await global.api?.fakeRequest({ method: 'GET', headers: { - authorization: `Bearer ${access_token}`, + authorization: `Bearer ${accessToken}`, }, - url: `/notes&page=${pageNumber}`, + url: `/notes?page=${pageNumber}`, }); expect(response?.statusCode).toBe(expextedStatus); diff --git a/src/tests/test-data/userSessions.json b/src/tests/test-data/userSessions.json index c35b9cba..a9d26410 100644 --- a/src/tests/test-data/userSessions.json +++ b/src/tests/test-data/userSessions.json @@ -3,6 +3,6 @@ "id" : 1, "user_id" : 4, "refresh_token" : "IqrTkSKmel", - "refresh_token_expites_at" : "2025-11-21 19:19:40.911+03" + "refresh_token_expires_at" : "2025-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 d9f134e8..76140a7c 100644 --- a/src/tests/utils/insert-data.ts +++ b/src/tests/utils/insert-data.ts @@ -22,7 +22,7 @@ async function insertUsers(db: SequelizeOrm): Promise { */ async function insertUserSessions(db: SequelizeOrm): Promise { for (const userSession of userSessions) { - await db.connection.query(`INSERT INTO public.user_sessions (id, "user_id", "refresh_token", "refresh_token_expires_at") VALUES (${userSession.id}, ${userSession.user_id}, '${userSession.refresh_token}', '${userSession.refresh_token_expites_at}')`); + await db.connection.query(`INSERT INTO public.user_sessions (id, "user_id", "refresh_token", "refresh_token_expires_at") VALUES (${userSession.id}, ${userSession.user_id}, '${userSession.refresh_token}', '${userSession.refresh_token_expires_at}')`); } } /** diff --git a/vitest.config.js b/vitest.config.js index 65930db4..6539b273 100644 --- a/vitest.config.js +++ b/vitest.config.js @@ -17,4 +17,5 @@ export default defineConfig({ '@tests/': '/src/tests/', }, }, + console: 'integratedTerminal', });