Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulAsjes committed Dec 27, 2024
1 parent 5ba9d4a commit 96adf6c
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions __tests__/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('session.ts', () => {
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Refresh successful. New access token ends in'));
});

it('should delete the cookie and redirect when refreshing fails', async () => {
it('should delete the cookie when refreshing fails', async () => {
jest.spyOn(console, 'log').mockImplementation(() => {});

mockSession.accessToken = await generateTestToken({}, true);
Expand Down Expand Up @@ -277,7 +277,7 @@ describe('session.ts', () => {
[],
);

expect(result.status).toBe(307);
expect(result.status).toBe(200);
expect(nextCookies.get('wos-session')).toBeUndefined();
expect(console.log).toHaveBeenCalledTimes(2);
expect(console.log).toHaveBeenNthCalledWith(
Expand All @@ -286,7 +286,7 @@ describe('session.ts', () => {
);
expect(console.log).toHaveBeenNthCalledWith(
2,
'Failed to refresh. Deleting cookie and redirecting.',
'Failed to refresh. Deleting cookie.',
new Error('Failed to refresh'),
);
});
Expand Down Expand Up @@ -448,6 +448,54 @@ describe('session.ts', () => {
expect(result.status).toBe(307);
});

it('should delete the cookie and redirect when refreshing fails', async () => {
jest.spyOn(console, 'log').mockImplementation(() => {});

mockSession.accessToken = await generateTestToken({}, true);

const nextCookies = await cookies();
nextCookies.set(
'wos-session',
await sealData(mockSession, { password: process.env.WORKOS_COOKIE_PASSWORD as string }),
);

(jwtVerify as jest.Mock).mockImplementation(() => {
throw new Error('Invalid token');
});

jest
.spyOn(workos.userManagement, 'authenticateWithRefreshToken')
.mockRejectedValue(new Error('Failed to refresh'));

const request = new NextRequest(new URL('http://example.com'));

const result = await updateSession(
request,
true,
{
enabled: true,
unauthenticatedPaths: [],
},
process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI as string,
[],
);

expect(result.status).toBe(307);
expect(nextCookies.get('wos-session')).toBeUndefined();
expect(console.log).toHaveBeenCalledTimes(3);
expect(console.log).toHaveBeenNthCalledWith(
1,
`Session invalid. Refreshing access token that ends in ${mockSession.accessToken.slice(-10)}`,
);
expect(console.log).toHaveBeenNthCalledWith(
2,
'Failed to refresh. Deleting cookie.',
new Error('Failed to refresh'),
);

expect(console.log).toHaveBeenNthCalledWith(3, 'Redirecting to AuthKit to log in again.');
});

describe('sign up paths', () => {
it('should redirect to sign up when unauthenticated user is on a sign up path', async () => {
const request = new NextRequest(new URL('http://example.com/signup'));
Expand Down

0 comments on commit 96adf6c

Please sign in to comment.