Skip to content

Commit

Permalink
changes generateJWT params
Browse files Browse the repository at this point in the history
  • Loading branch information
Dopeamin committed Oct 1, 2024
1 parent b8519b1 commit e3ccd16
Showing 1 changed file with 22 additions and 66 deletions.
88 changes: 22 additions & 66 deletions tests/unit/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ async function startJWKSserver() {
async function generateJWT(
issuer: string,
expiresIn: number,
payload: Record<string, any>,
userId: string,
fullName: string,
privateKey: KeyLike,
): Promise<string> {
return await new SignJWT({
...payload,
sub: userId,
name: fullName,
iss: issuer,
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + expiresIn,
Expand Down Expand Up @@ -117,15 +119,7 @@ describe('Session Service Unit Tests', () => {
});

test('should throw ValidationError using invalid private key', async () => {
const jwt = await generateJWT(
TEST_ISSUER,
600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
invalidPrivateKey,
);
const jwt = await generateJWT(TEST_ISSUER, 600, TEST_USER_ID, TEST_FULL_NAME, invalidPrivateKey);

await expect(sessionService.validateToken(jwt)).rejects.toThrow(ValidationError);
await expect(sessionService.validateToken(jwt)).rejects.toThrow(
Expand Down Expand Up @@ -154,15 +148,7 @@ describe('Session Service Unit Tests', () => {
});

test('should throw ValidationError using an expired JWT', async () => {
const jwt = await generateJWT(
TEST_ISSUER,
-600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
validPrivateKey,
);
const jwt = await generateJWT(TEST_ISSUER, -600, TEST_USER_ID, TEST_FULL_NAME, validPrivateKey);

await expect(sessionService.validateToken(jwt)).rejects.toThrow(ValidationError);
await expect(sessionService.validateToken(jwt)).rejects.toThrow(
Expand All @@ -181,10 +167,8 @@ describe('Session Service Unit Tests', () => {
const jwt = await generateJWT(
'https://pro-1.frontendapi.corbado.io',
600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
TEST_USER_ID,
TEST_FULL_NAME,
validPrivateKey,
);

Expand All @@ -199,10 +183,8 @@ describe('Session Service Unit Tests', () => {
const jwt = await generateJWT(
'https://pro-1.frontendapi.cloud.corbado.io',
600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
TEST_USER_ID,
TEST_FULL_NAME,
validPrivateKey,
);

Expand All @@ -214,15 +196,7 @@ describe('Session Service Unit Tests', () => {
});

test('should throw ValidationError if issuer is undefined', async () => {
const jwt = await generateJWT(
'',
600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
validPrivateKey,
);
const jwt = await generateJWT('', 600, TEST_USER_ID, TEST_FULL_NAME, validPrivateKey);

await expect(sessionService.validateToken(jwt)).rejects.toThrow(ValidationError);
await expect(sessionService.validateToken(jwt)).rejects.toThrow(
Expand All @@ -234,18 +208,14 @@ describe('Session Service Unit Tests', () => {
const jwt = await generateJWT(
'https://pro-2.frontendapi.cloud.corbado.io',
600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
TEST_USER_ID,
TEST_FULL_NAME,
validPrivateKey,
);

const user = await sessionService.validateToken(jwt);
expect(user).toEqual({
userId: TEST_USER_ID,
fullName: TEST_FULL_NAME,
});
expect(user.userId).toBe(TEST_USER_ID);
expect(user.fullName).toBe(TEST_FULL_NAME);
});

test('should return user using old Frontend API URL as config issuer', async () => {
Expand All @@ -259,36 +229,22 @@ describe('Session Service Unit Tests', () => {
const jwt = await generateJWT(
'https://pro-2.frontendapi.corbado.io',
600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
TEST_USER_ID,
TEST_FULL_NAME,
validPrivateKey,
);

const user = await sessionService.validateToken(jwt);
expect(user).toEqual({
userId: TEST_USER_ID,
fullName: TEST_FULL_NAME,
});
expect(user.userId).toBe(TEST_USER_ID);
expect(user.fullName).toBe(TEST_FULL_NAME);
});

test('should return user data using CNAME', async () => {
sessionService = new SessionService(SHORT_SESSION_COOKIE_NAME, 'https://auth.acme.com', JWKS_URI, 10, PROJECT_ID);
const jwt = await generateJWT(
'https://auth.acme.com',
600,
{
sub: TEST_USER_ID,
name: TEST_FULL_NAME,
},
validPrivateKey,
);
const jwt = await generateJWT('https://auth.acme.com', 600, TEST_USER_ID, TEST_FULL_NAME, validPrivateKey);

const user = await sessionService.validateToken(jwt);
expect(user).toEqual({
userId: TEST_USER_ID,
fullName: TEST_FULL_NAME,
});
expect(user.userId).toBe(TEST_USER_ID);
expect(user.fullName).toBe(TEST_FULL_NAME);
});
});

0 comments on commit e3ccd16

Please sign in to comment.