From e5427056b206382d1c49a43b4d864dd8164bcaae Mon Sep 17 00:00:00 2001 From: Danya Date: Thu, 9 Nov 2023 20:32:16 +0300 Subject: [PATCH] fix almost all comments --- package.json | 2 +- src/presentation/http/router/auth.test.ts | 27 +++++++++++++++-------- src/tests/test-data/notes.json | 2 +- src/tests/utils/insert-data.ts | 4 ++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 338fb77f..0c15b22d 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@fastify/swagger-ui": "^1.9.3", "@testcontainers/postgresql": "^10.2.1", "arg": "^5.0.2", - "axios": "^1.6.0", + "fastify": "^4.17.0", "http-status-codes": "^2.2.0", "jsonwebtoken": "^9.0.0", diff --git a/src/presentation/http/router/auth.test.ts b/src/presentation/http/router/auth.test.ts index eee080e6..6c359b38 100644 --- a/src/presentation/http/router/auth.test.ts +++ b/src/presentation/http/router/auth.test.ts @@ -13,18 +13,19 @@ describe('Auth API', () => { expect(response?.statusCode).toBe(expectedStatus); - const body = response?.body !== undefined ? JSON.parse(response?.body) : {}; + const body = await response?.json(); expect(body).toStrictEqual({ message: 'Session is not valid' }); }); - test('Returns 200 when session was authorized', async () => { + test('Returns 200 when refreshToken is in database', async () => { const expectedStatus = 200; /** Define the token to include in the request body */ const refreshToken = 'pv-jIqfPj1'; /** Define regular expression to be sure that string is not empty */ - const notEmptyString = /^.$/; + const notEmptyString = /^.+$/; + const expectedAuthReply = { refreshToken: notEmptyString, @@ -40,26 +41,34 @@ describe('Auth API', () => { expect(response?.statusCode).toBe(expectedStatus); - const body = response?.body !== undefined ? JSON.parse(response?.body) : {}; + const body = await response?.json(); + + expect(body.refreshToken).toBeDefined(); + expect(body.refreshToken).not.toBeNull(); + expect(body.refreshToken).not.toBe(''); + + expect(body.accessToken).toBeDefined(); + expect(body.accessToken).not.toBeNull(); + expect(body.accessToken).not.toBe(''); + expect(body).toMatchObject(expectedAuthReply); }); test('Returns 401 when expiration day has passed', async () => { const expectedStatus=401; - const refreshToken = 'pv-jIqfPj1'; - /** Set the expiration day to a date in the past */ - const pastExpirationDay = new Date('Sat, 03 Nov 2022 16:53:36 GMT').toUTCString(); + const refreshToken = 'F5tTF24K9Q'; + const response=await global.api?.fakeRequest({ method: 'POST', url: '/auth', - headers:{ date:pastExpirationDay }, + body:{ token: refreshToken }, }); expect (response?.statusCode).toBe(expectedStatus); - const body = response?.body !== undefined ? JSON.parse(response?.body):{}; + const body = await response?.json(); expect(body).toStrictEqual({ message:'Session is not valid' }); }); diff --git a/src/tests/test-data/notes.json b/src/tests/test-data/notes.json index b5747fed..4fc09cbf 100644 --- a/src/tests/test-data/notes.json +++ b/src/tests/test-data/notes.json @@ -1,4 +1,4 @@ - [ +[ { "id": 1, "public_id": "note_1", diff --git a/src/tests/utils/insert-data.ts b/src/tests/utils/insert-data.ts index 21c26503..58bfda22 100644 --- a/src/tests/utils/insert-data.ts +++ b/src/tests/utils/insert-data.ts @@ -2,7 +2,7 @@ import type SequelizeOrm from '@repository/storage/postgres/orm/index.js'; import users from '../test-data/users.json'; import notes from '../test-data/notes.json'; import noteSettings from '../test-data/notes-settings.json'; -import tokens from '../test-data/userSessions.json'; +import userSessions from '../test-data/userSessions.json'; /** * Fills in the database with users data @@ -43,7 +43,7 @@ async function insertNoteSettings(db: SequelizeOrm): Promise { * @param db - SequelizeOrm instance */ async function insertUserSessions(db:SequelizeOrm): Promise { - for (const token of tokens) { + for (const token of userSessions) { await db.connection.query(`INSERT INTO public.user_sessions("id","user_id","refresh_token","refresh_token_expires_at") VALUES (${token.Id}, ${token.user_Id}, '${token.refreshToken}', '${token.refresh_token_expires_at}')`); } }