smartjwt/test/test.ts

52 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2023-08-21 06:53:14 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-10-24 05:14:17 +00:00
import * as smartjwt from '../ts/index.js';
2019-02-22 23:48:01 +00:00
2019-10-01 16:04:43 +00:00
let smartjwtInstance: smartjwt.SmartJwt;
2019-10-01 17:07:59 +00:00
let testJwt: string;
2019-10-01 16:04:43 +00:00
tap.test('should create a valid instance', async () => {
smartjwtInstance = new smartjwt.SmartJwt();
await smartjwtInstance.createNewKeyPair();
console.log(smartjwtInstance);
});
tap.test('should create a valid jwt', async () => {
2019-10-01 17:07:59 +00:00
await smartjwtInstance.createNewKeyPair();
});
tap.test('should create a new jwt', async () => {
2019-10-01 17:22:02 +00:00
testJwt = await smartjwtInstance.createJWT({ hi: 'there' });
2019-10-01 17:07:59 +00:00
console.log(testJwt);
2019-02-22 23:50:21 +00:00
});
2019-02-22 23:48:01 +00:00
2019-10-01 17:07:59 +00:00
tap.test('should verify a jwt', async () => {
const data = await smartjwtInstance.verifyJWTAndGetData(testJwt);
2021-02-09 10:30:00 +00:00
// tslint:disable-next-line: no-unused-expression
2022-10-24 05:14:17 +00:00
expect(data).not.toBeNull();
2019-10-01 17:07:59 +00:00
console.log(data);
2021-02-09 10:26:42 +00:00
console.log(smartjwtInstance.publicKey.toPemString());
2019-10-01 17:07:59 +00:00
});
2019-10-01 17:22:02 +00:00
tap.test('should not verify a wrong jwt', async () => {
const jwt2 = await smartjwtInstance.createJWT({ wow: 'soclear' });
const jwt2Array = jwt2.split('.');
const testJwtArray = testJwt.split('.');
const newJwt = `${testJwtArray[0]}.${jwt2Array[1]}.${testJwtArray[2]}`;
let error: Error;
try {
await smartjwtInstance.verifyJWTAndGetData(newJwt);
2021-09-21 23:29:47 +00:00
} catch (e: any) {
2019-10-01 17:22:02 +00:00
error = e;
}
2022-10-24 05:14:17 +00:00
expect(error).toBeInstanceOf(Error);
2019-10-01 17:22:02 +00:00
});
2019-10-01 17:07:59 +00:00
2021-02-09 10:26:42 +00:00
tap.test('should verify a jwt on another instance', async () => {
const secondSmartJwtInstance = new smartjwt.SmartJwt();
secondSmartJwtInstance.setPublicPemKeyForVerification(smartjwtInstance.publicKey.toPemString());
const result = secondSmartJwtInstance.verifyJWTAndGetData(testJwt);
console.log(result);
2021-02-09 10:30:00 +00:00
});
2021-02-09 10:26:42 +00:00
2019-02-22 23:50:21 +00:00
tap.start();