diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 889b85c8..9eb082ae 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -10,6 +10,12 @@ module.exports = { 'tsconfigRootDir': __dirname, 'sourceType': 'module', }, + 'overrides': [{ + 'files': ['*.test.ts'], + 'rules': { + '@typescript-eslint/no-magic-numbers': 'off' + } + }], 'rules': { '@typescript-eslint/naming-convention': [ 'error', diff --git a/.github/workflows/run-build-check.yml b/.github/workflows/run-build-check.yml index bc7f98ae..09cb8332 100644 --- a/.github/workflows/run-build-check.yml +++ b/.github/workflows/run-build-check.yml @@ -29,5 +29,4 @@ jobs: - name: Build run: yarn build - - + diff --git a/.gitignore b/.gitignore index f563cbda..dcd4666b 100644 --- a/.gitignore +++ b/.gitignore @@ -139,3 +139,5 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +*.code-workspace \ No newline at end of file diff --git a/src/presentation/http/router/auth.test.ts b/src/presentation/http/router/auth.test.ts new file mode 100644 index 00000000..cb362465 --- /dev/null +++ b/src/presentation/http/router/auth.test.ts @@ -0,0 +1,66 @@ +import { describe, test, expect } from 'vitest'; + +describe('Auth API', () => { + describe('POST /auth', () => { + test('Returns 401 when refresh token in not valid', async () => { + const refreshToken = 'EF1JX65xSZ'; + + const response = await global.api?.fakeRequest({ + method: 'POST', + url: '/auth', + body: { token: refreshToken }, + }); + + expect(response?.statusCode).toBe(401); + + const body = await response?.json(); + + expect(body).toStrictEqual({ message: 'Session is not valid' }); + }); + + test('Returns 200 when refresh token is valid and expiration date has not passed', async () => { + const refreshToken = 'pv-jIqfPj2'; + + /** + * Insert session data to the DB with tomorrow expiration date + */ + await global.db.query(`INSERT INTO public.user_sessions (id, "user_id", "refresh_token", "refresh_token_expires_at") VALUES (9999, 1, '${refreshToken}', CURRENT_DATE + INTERVAL '1 day')`); + + const response = await global.api?.fakeRequest({ + method: 'POST', + url: '/auth', + body: { token: refreshToken }, + }); + + expect(response?.statusCode).toBe(200); + + const body = await response?.json(); + + expect(typeof body.refreshToken).toBe('string'); + expect(body.refreshToken).toBeDefined(); + expect(body.refreshToken).not.toBeNull(); + expect(body.refreshToken).not.toBe(''); + + expect(typeof body.accessToken).toBe('string'); + expect(body.accessToken).toBeDefined(); + expect(body.accessToken).not.toBeNull(); + expect(body.accessToken).not.toBe(''); + }); + + test('Returns 401 when expiration day has passed', async () => { + const outdatedToken = 'F5tTF24K9Q'; + + const response = await global.api?.fakeRequest({ + method: 'POST', + url: '/auth', + body:{ token: outdatedToken }, + }); + + expect (response?.statusCode).toBe(401); + + const body = await response?.json(); + + expect(body).toStrictEqual({ message:'Session is not valid' }); + }); + }); +}); diff --git a/src/tests/test-data/user-sessions.json b/src/tests/test-data/user-sessions.json index a9d26410..6eac4761 100644 --- a/src/tests/test-data/user-sessions.json +++ b/src/tests/test-data/user-sessions.json @@ -1,8 +1,14 @@ [ { - "id" : 1, - "user_id" : 4, - "refresh_token" : "IqrTkSKmel", - "refresh_token_expires_at" : "2025-11-21 19:19:40.911+03" + "id": 2, + "user_id": 4, + "refresh_token": "F5tTF24K9Q", + "refresh_token_expires_at": "2023-11-09 11:23:54+02" + }, + { + "id": 3, + "user_id": 1, + "refresh_token": "IqrTkSKmel", + "refresh_token_expires_at": "2025-11-21 19:19:40.911+03" } ] diff --git a/src/tests/utils/insert-data.ts b/src/tests/utils/insert-data.ts index a322a00b..149991a4 100644 --- a/src/tests/utils/insert-data.ts +++ b/src/tests/utils/insert-data.ts @@ -4,6 +4,7 @@ import userSessions from '../test-data/user-sessions.json'; import notes from '../test-data/notes.json'; import noteSettings from '../test-data/notes-settings.json'; + /** * Fills in the database with users data * @@ -26,7 +27,7 @@ async function insertUserSessions(db: SequelizeOrm): Promise { } } /** - * Fills in the database with notes datas + * Fills in the database with notes data * * @param db - SequelizeOrm instance */ diff --git a/src/tests/utils/setup.ts b/src/tests/utils/setup.ts index 0e5644b1..66226af3 100644 --- a/src/tests/utils/setup.ts +++ b/src/tests/utils/setup.ts @@ -33,7 +33,7 @@ declare global { * @param userId - id of the user that will be considered the author of the request * @returns accessToken for authorization */ - function auth(userId: number) : string; + function auth(userId: number): string; /* eslint-disable-next-line no-var */ var db: { @@ -42,7 +42,6 @@ declare global { * Might be used in tests to perform some specific database operations * * @param sql - string containing sql to executein test DB - * @returns */ query: (sql: string) => Promise; };