Skip to content

Commit

Permalink
SK-1621: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
“amith-skyflow” committed Nov 21, 2024
1 parent 8f9068a commit 7e8f092
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/utils/jwt-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ function isExpired(token: string) {

function isTokenValid(token: string) {
try {
if (token === "") return false
let isJwtExpired = false;
const decoded: JwtPayload = jwt_decode(token);
const currentTime = (new Date().getTime() / 1000);
const expiryTime = decoded.exp;
if (expiryTime && currentTime > expiryTime) {
isJwtExpired = true;
}
return !isJwtExpired;
if (token === "") return false
let isJwtExpired = false;
const decoded: JwtPayload = jwt_decode(token);
const currentTime = (new Date().getTime() / 1000);
const expiryTime = decoded.exp;
if (expiryTime && currentTime > expiryTime) {
isJwtExpired = true;
}
return !isJwtExpired;
} catch (err) {
return false;
}
Expand Down
18 changes: 17 additions & 1 deletion test/vault/utils/jwt-utils/jwt.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jwt_decode from 'jwt-decode';
import { isTokenValid } from '../../../../src/utils/jwt-utils';
import { isTokenValid, isExpired } from '../../../../src/utils/jwt-utils';

jest.mock('jwt-decode');

Expand All @@ -19,5 +19,21 @@ describe('isTokenValid Tests', () => {
const isValid = isTokenValid("");
expect(isValid).toBe(false);
});

test('should return false in catch', () => {
jwt_decode.mockImplementation(() => {
throw new Error("Invalid Token");
});
const isValid = isTokenValid("TOKEN");
expect(isValid).toBe(false);
});

test('should return false in catch for isExpired', () => {
jwt_decode.mockImplementation(() => {
throw new Error("Invalid Token");
});
const isValid = isExpired("TOKEN");
expect(isValid).toBe(true);
});
});

0 comments on commit 7e8f092

Please sign in to comment.