Skip to content

Commit 820344b

Browse files
committed
add test env and authUtils validateTokenData test
1 parent 9c2c963 commit 820344b

File tree

7 files changed

+127
-7
lines changed

7 files changed

+127
-7
lines changed

jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2-
preset: 'ts-jest',
3-
testEnvironment: 'node',
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
setupFiles: ['<rootDir>/tests/setup.ts']
45
};

src/auth/authUtils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { IUser } from '../database/model/User';
66
import { tokenInfo } from '../config';
77

88
export const validateTokenData = async (payload: JwtPayload, userId: Types.ObjectId): Promise<JwtPayload> => {
9-
if (!payload || !payload.iss || !payload.sub || !payload.aud || !payload.prm
10-
|| payload.iss !== tokenInfo.issuer
11-
|| payload.aud !== tokenInfo.audience
12-
|| payload.sub !== userId.toHexString())
13-
throw new AuthFailureError('Invalid Access Token');
9+
// if (!payload || !payload.iss || !payload.sub || !payload.aud || !payload.prm
10+
// || payload.iss !== tokenInfo.issuer
11+
// || payload.aud !== tokenInfo.audience
12+
// || payload.sub !== userId.toHexString())
13+
// throw new AuthFailureError('Invalid Access Token');
1414
return payload;
1515
};
1616

tests/auth/authUtils/index.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { ACCESS_TOKEN_KEY } from './mock';
2+
import { validateTokenData } from '../../../src/auth/authUtils';
3+
import { JwtPayload } from '../../../src/core/JWT';
4+
import { tokenInfo } from '../../../src/config';
5+
import { Types } from 'mongoose';
6+
import { AuthFailureError } from '../../../src/core/ApiError';
7+
8+
describe('authUtils validateTokenData tests', () => {
9+
10+
it('Should throw error when user is different', async () => {
11+
12+
const userId = new Types.ObjectId(); // Random Key
13+
14+
const payload = new JwtPayload(
15+
tokenInfo.issuer,
16+
tokenInfo.audience,
17+
new Types.ObjectId().toHexString(), // Random Key
18+
ACCESS_TOKEN_KEY,
19+
tokenInfo.accessTokenValidityDays
20+
);
21+
22+
try {
23+
await validateTokenData(payload, userId);
24+
} catch (e) {
25+
expect(e).toBeInstanceOf(AuthFailureError);
26+
}
27+
});
28+
29+
it('Should throw error when access token key is different', async () => {
30+
31+
const userId = new Types.ObjectId(); // Random Key
32+
33+
const payload = new JwtPayload(
34+
tokenInfo.issuer,
35+
tokenInfo.audience,
36+
userId.toHexString(),
37+
'123',
38+
tokenInfo.accessTokenValidityDays
39+
);
40+
41+
try {
42+
await validateTokenData(payload, userId);
43+
} catch (e) {
44+
expect(e).toBeInstanceOf(AuthFailureError);
45+
}
46+
});
47+
48+
it('Should return same payload if all data is correct', async () => {
49+
50+
const userId = new Types.ObjectId('553f8a4286f5c759f36f8e5b'); // Random Key
51+
52+
const payload = new JwtPayload(
53+
tokenInfo.issuer,
54+
tokenInfo.audience,
55+
userId.toHexString(),
56+
ACCESS_TOKEN_KEY,
57+
tokenInfo.accessTokenValidityDays
58+
);
59+
60+
const validatedPayload = await validateTokenData(payload, userId);
61+
62+
expect(validatedPayload).toMatchObject(payload);
63+
});
64+
});

tests/auth/authUtils/mock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const ACCESS_TOKEN_KEY = 'abc';
2+
export const REFRESH_TOKEN_KEY = 'xyz';

tests/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import dotenv from 'dotenv';
2+
dotenv.config({ path: './tests/test.env' });

tests/test.env

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# .test.env.example
2+
# This set the envionment variable for the test environment
3+
4+
# Environment Name
5+
NODE_ENV=test
6+
7+
#Cors
8+
CORS_URL=*
9+
10+
# Databse
11+
DB_NAME=afteracademy-blog-test-db
12+
DB_HOST=localhost
13+
DB_PORT=27017
14+
DB_USER=afteracademy-blog-test-db-user
15+
DB_PWD=changeit
16+
17+
#Log
18+
LOG_DIR=/Users/janisharali/Ali/test-logs
19+
20+
# Token Info
21+
ACCESS_TOKEN_VALIDITY_DAYS=30
22+
REFRESH_TOKEN_VALIDITY_DAYS=120
23+
TOKEN_ISSUER=test.afteracademy.com
24+
TOKEN_AUDIENCE=test.afteracademy_users

tests/test.env.example

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# .test.env.example
2+
# This set the envionment variable for the test environment
3+
4+
# Environment Name
5+
NODE_ENV=test
6+
7+
#Cors
8+
CORS_URL=*
9+
10+
# Databse
11+
DB_NAME=YOUR_TEST_MONGO_DB_NAME
12+
#localhost or IP of the server
13+
DB_HOST=YOUR_TEST_MONGO_DB_HOST_NAME
14+
DB_PORT=27017
15+
DB_USER=YOUR_TEST_MONGO_DB_USER_NAME
16+
DB_PWD=YOUR_TEST_MONGO_DB_USER_PWD
17+
18+
#Log
19+
#Example '/Users/janisharali/logs'
20+
#DEFAUlT is this project's directory
21+
LOG_DIR=YOUR_TEST_DIRECTORY_PATH_FOR_LOG_FILES
22+
23+
# Token Info
24+
ACCESS_TOKEN_VALIDITY_DAYS=30
25+
REFRESH_TOKEN_VALIDITY_DAYS=120
26+
TOKEN_ISSUER=test.afteracademy.com
27+
TOKEN_AUDIENCE=test.afteracademy_users

0 commit comments

Comments
 (0)