mismatched test

This commit is contained in:
2026-02-19 18:37:22 +01:00
parent c6c3a42ffd
commit 3b95e756a1
+25 -25
View File
@@ -3,7 +3,7 @@ import type { SignOptions } from "jsonwebtoken";
describe('JWT', () => { describe('JWT', () => {
const jwt = new JwtAuth('secret'); const jwt = new JwtAuth('secret');
const user: {username: string, password: string} = { const user: { username: string, password: string } = {
username: 'test', username: 'test',
password: 'test' password: 'test'
}; };
@@ -11,13 +11,13 @@ describe('JWT', () => {
const settings: SignOptions = { const settings: SignOptions = {
expiresIn: '1h' expiresIn: '1h'
}; };
it('should generate a JWT', () => { it('should generate a JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
expect(token).toBeDefined(); expect(token).toBeDefined();
}); });
test('should return token object when valid token is provided', () => { test('should return token object when valid token is provided', () => {
// Arrange // Arrange
const validToken = jwt.generateJWT(user); const validToken = jwt.generateJWT(user);
@@ -25,7 +25,7 @@ describe('JWT', () => {
const result = jwt.verifyJWT(validToken); const result = jwt.verifyJWT(validToken);
// Assert // Assert
if(result instanceof Object) { if (result instanceof Object) {
expect(result.username).toEqual(user.username); expect(result.username).toEqual(user.username);
} else { } else {
fail('Token verification failed'); fail('Token verification failed');
@@ -33,7 +33,7 @@ describe('JWT', () => {
}); });
it('should decode a JWT', () => { it('should decode a JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
const decoded = jwt.decodeJWT(token); const decoded = jwt.decodeJWT(token);
expect(decoded).toBeDefined(); expect(decoded).toBeDefined();
if (typeof decoded !== 'string' && decoded !== null) { if (typeof decoded !== 'string' && decoded !== null) {
@@ -46,78 +46,78 @@ describe('JWT', () => {
}); });
it('should get the expiration date of a JWT', () => { it('should get the expiration date of a JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }, settings); const token = jwt.generateJWT({ username: user.username, password: user.password }, settings);
const expirationDate = jwt.getJWTExpirationDate(token); const expirationDate = jwt.getJWTExpirationDate(token);
expect(expirationDate).toBeDefined(); expect(expirationDate).toBeDefined();
}); });
it('should check if a JWT is expired', () => { it('should check if a JWT is expired', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }, settings); const token = jwt.generateJWT({ username: user.username, password: user.password }, settings);
const isExpired = jwt.isJWTExpired(token); const isExpired = jwt.isJWTExpired(token);
expect(isExpired).toBeDefined(); expect(isExpired).toBeDefined();
}); });
it('should refresh a JWT', () => { it('should refresh a JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
const newToken = jwt.refreshJWT(token); const newToken = jwt.refreshJWT(token);
expect(newToken).toBeDefined(); expect(newToken).toBeDefined();
}); });
it('should not verify a JWT with invalid secret key', () => { it('should not verify a JWT with invalid secret key', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
const verified = jwt.verifyJWT(token, 'invalid'); const verified = jwt.verifyJWT(token, 'invalid');
expect(verified).toEqual({ valid: false, message: "Token is invalid."}); expect(verified).toEqual({ valid: false, message: "Token is invalid." });
}); });
it('should not refresh a JWT with invalid secret key', () => { it('should not refresh a JWT with invalid secret key', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }, settings, 'test'); const token = jwt.generateJWT({ username: user.username, password: user.password }, settings, 'test');
const newToken = jwt.refreshJWT(token, {}, 'invalid'); const newToken = jwt.refreshJWT(token, {}, 'invalid');
expect(newToken).toEqual({ valid: false, message: "Token is invalid."}); expect(newToken).toEqual({ valid: false, message: "Token is invalid." });
}); });
it('should blacklist a JWT', () => { it('should blacklist a JWT', () => {
const token = jwt.generateJWT({ username: 'test12', password: user.password }); const token = jwt.generateJWT({ username: 'test12', password: user.password });
const blacklisted = jwt.BlackListJWT(token); const blacklisted = jwt.BlackListJWT(token);
expect(blacklisted).toBeDefined(); expect(blacklisted).toBeDefined();
}); });
it('should not verify a blacklisted JWT', () => { it('should not verify a blacklisted JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
jwt.BlackListJWT(token); jwt.BlackListJWT(token);
const verified = jwt.verifyJWT(token); const verified = jwt.verifyJWT(token);
expect(verified).toBeDefined(); expect(verified).toBeDefined();
}); });
it('should not decode a blacklisted JWT', () => { it('should not decode a blacklisted JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
jwt.BlackListJWT(token); jwt.BlackListJWT(token);
const decoded = jwt.decodeJWT(token); const decoded = jwt.decodeJWT(token);
expect(decoded).toBeDefined(); expect(decoded).toBeDefined();
}); });
it('should not get the expiration date of a blacklisted JWT', () => { it('should not get the expiration date of a blacklisted JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
jwt.BlackListJWT(token); jwt.BlackListJWT(token);
const expirationDate = jwt.getJWTExpirationDate(token); const expirationDate = jwt.getJWTExpirationDate(token);
expect(expirationDate).toBeDefined(); expect(expirationDate).toBeDefined();
}); });
it('should not check if a blacklisted JWT is expired', () => { it('should not check if a blacklisted JWT is expired', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
jwt.BlackListJWT(token); jwt.BlackListJWT(token);
const isExpired = jwt.isJWTExpired(token); const isExpired = jwt.isJWTExpired(token);
expect(isExpired).toBeDefined(); expect(isExpired).toBeDefined();
}); });
it('should not refresh a blacklisted JWT', () => { it('should not refresh a blacklisted JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
jwt.BlackListJWT(token); jwt.BlackListJWT(token);
const newToken = jwt.refreshJWT(token); const newToken = jwt.refreshJWT(token);
expect(newToken).toBeDefined(); expect(newToken).toBeDefined();
}); });
it('should not blacklist a blacklisted JWT', () => { it('should not blacklist a blacklisted JWT', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
jwt.BlackListJWT(token); jwt.BlackListJWT(token);
const blacklisted = jwt.BlackListJWT(token); const blacklisted = jwt.BlackListJWT(token);
expect(blacklisted).toBeDefined(); expect(blacklisted).toBeDefined();
@@ -128,12 +128,12 @@ describe('JWT', () => {
const verify = jwt.verifyJWT(token, 'invalid'); const verify = jwt.verifyJWT(token, 'invalid');
const blacklisted = jwt.IsBlackListed(token); const blacklisted = jwt.IsBlackListed(token);
expect(blacklisted).toEqual({ valid: true, message: "Token is blacklisted." }) expect(blacklisted).toEqual({ valid: true, message: "Token is blacklisted." })
}); });
it('should remove a JWT from the blacklist', () => { it('should remove a JWT from the blacklist', () => {
const token = jwt.generateJWT({ username: user.username, password: user.password }); const token = jwt.generateJWT({ username: user.username, password: user.password });
jwt.BlackListJWT(token); jwt.BlackListJWT(token);
const removed = jwt.RemoveFromBlackList(token); const removed = jwt.RemoveFromBlackList(token);
expect(removed).toEqual({ valid: true, message: "Token successfully removed from blacklist." }); expect(removed).toEqual({ valid: true, message: "Token successfully removed from blacklist." });
}); });
}); });