Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: auth test #100

Merged
merged 38 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8cab05d
create sample of test, without test data
Danspb77 Oct 29, 2023
84cb253
Update src/presentation/http/router/auth.test.ts
Danspb77 Oct 30, 2023
5574e34
insert not real test-data into database
Danspb77 Oct 30, 2023
10a3ff6
add real test-data, but with wrong urls in auth.test.ts
Danspb77 Oct 31, 2023
0bb2c74
Merge remote-tracking branch 'origin/post-access-refresh-token-tests'…
Danspb77 Oct 31, 2023
8814436
add real data in expectedAuthReply
Danspb77 Nov 1, 2023
462886f
add body to POST method
Danspb77 Nov 2, 2023
e5e4f22
fix insert data for auth tests
Danspb77 Nov 2, 2023
e389e69
fix data for auth tests
Danspb77 Nov 2, 2023
b31848a
fix data for auth tests in the second time
Danspb77 Nov 2, 2023
624748f
fix test with not valid token
Danspb77 Nov 2, 2023
c04144e
change format of checking in test with status 200
Danspb77 Nov 3, 2023
148f0a7
change script for insert data for auth test in ESLint format
Danspb77 Nov 3, 2023
d41f8dd
add test-case for situation when expiration day has passed
Danspb77 Nov 6, 2023
89c326b
add test-case for situation when expiration day has passed v 2.0
Danspb77 Nov 6, 2023
9d3ec54
draft commit
Danspb77 Nov 6, 2023
71b01db
200 test doesn't work , but others work
Danspb77 Nov 7, 2023
dacac93
fix test case for situation when expiration dau has passed
Danspb77 Nov 8, 2023
e542705
fix almost all comments
Danspb77 Nov 9, 2023
10513f8
fix almost all comments v2.0
Danspb77 Nov 9, 2023
8c2fd57
fix almost all comments v2.1
Danspb77 Nov 9, 2023
a65eeeb
fix almost all comments v2.2
Danspb77 Nov 9, 2023
f45371f
fix almost all comments v2.3
Danspb77 Nov 9, 2023
477bb34
add script to dynamicly set tomorrow expiration day for token
Danspb77 Nov 10, 2023
7b44943
Merge branch 'main' into post-access-refresh-token-tests
Danspb77 Nov 10, 2023
7020794
looks like that all right
Danspb77 Nov 12, 2023
d1f42f9
add script to dynamically update expiration date
Danspb77 Nov 12, 2023
813eb4b
resolve conflicts with main branch
Danspb77 Nov 12, 2023
adbc3cb
fix review's comments
Danspb77 Nov 13, 2023
d41a1f7
fix others review's comments
Danspb77 Nov 13, 2023
94ee690
fix review's comments except dynamically updating date
Danspb77 Nov 14, 2023
33d0167
rewrite insertUserSessions to fill dynamically updated expiration date
Danspb77 Nov 14, 2023
1e9bd9e
move function to insert dynamic updated date in auth.test.ts
Danspb77 Nov 16, 2023
281460f
Remove useless changes
TatianaFomina Nov 25, 2023
efce67b
Merge branch 'main' into post-access-refresh-token-tests
TatianaFomina Nov 25, 2023
9d9da5a
Various fixes
TatianaFomina Nov 25, 2023
1781784
Remove changes
TatianaFomina Nov 25, 2023
f87287c
Remove get-tommorrow-date-formatted
TatianaFomina Nov 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/run-build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ jobs:

- name: Build
run: yarn build



TatianaFomina marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

*.code-workspace
67 changes: 67 additions & 0 deletions src/presentation/http/router/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { getTomorrowDateFormatted } from '@tests/utils/get-tomorrow-date-formatted';
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);

e11sy marked this conversation as resolved.
Show resolved Hide resolved
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}', '${getTomorrowDateFormatted()}')`);
TatianaFomina marked this conversation as resolved.
Show resolved Hide resolved

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' });
});
});
});
14 changes: 10 additions & 4 deletions src/tests/test-data/user-sessions.json
Original file line number Diff line number Diff line change
@@ -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,
TatianaFomina marked this conversation as resolved.
Show resolved Hide resolved
"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"
}
]
15 changes: 15 additions & 0 deletions src/tests/utils/get-tomorrow-date-formatted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Returns tomorrow date in DB compatible format
*/
export const getTomorrowDateFormatted = (): string => {
const tomorrow = new Date();
const border = 19;

tomorrow.setDate(tomorrow.getDate() + 1);

const newExpirationDay: string = tomorrow.toISOString().slice(0, border)
.replace('T', ' ') + '+02';

return newExpirationDay;
};

3 changes: 2 additions & 1 deletion src/tests/utils/insert-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -26,7 +27,7 @@ async function insertUserSessions(db: SequelizeOrm): Promise<void> {
}
}
/**
* Fills in the database with notes datas
* Fills in the database with notes data
*
* @param db - SequelizeOrm instance
*/
Expand Down
3 changes: 1 addition & 2 deletions src/tests/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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<unknown>;
};
Expand Down
Loading