Skip to content

Commit

Permalink
fix almost all comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Danspb77 committed Nov 9, 2023
1 parent dacac93 commit e542705
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 18 additions & 9 deletions src/presentation/http/router/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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' });
});
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test-data/notes.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
[
{
"id": 1,
"public_id": "note_1",
Expand Down
4 changes: 2 additions & 2 deletions src/tests/utils/insert-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +43,7 @@ async function insertNoteSettings(db: SequelizeOrm): Promise<void> {
* @param db - SequelizeOrm instance
*/
async function insertUserSessions(db:SequelizeOrm): Promise<void> {
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}')`);
}
}
Expand Down

0 comments on commit e542705

Please sign in to comment.