Skip to content

Commit

Permalink
fix: test auth
Browse files Browse the repository at this point in the history
  • Loading branch information
neSpecc committed Oct 27, 2023
1 parent b4704d8 commit 29c81a5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
5 changes: 0 additions & 5 deletions src/presentation/http/router/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ const AuthRouter: FastifyPluginCallback<AuthRouterOptions> = (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);

/**
Expand Down
84 changes: 55 additions & 29 deletions src/presentation/http/router/noteList.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,61 @@
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<string> {
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;

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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test-data/userSessions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
]
2 changes: 1 addition & 1 deletion src/tests/utils/insert-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function insertUsers(db: SequelizeOrm): Promise<void> {
*/
async function insertUserSessions(db: SequelizeOrm): Promise<void> {
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}')`);
}
}
/**
Expand Down
1 change: 1 addition & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export default defineConfig({
'@tests/': '/src/tests/',
},
},
console: 'integratedTerminal',
});

0 comments on commit 29c81a5

Please sign in to comment.