-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added tests and made a start to auth.ts * Add tests for cookie and callback route * Tests for session and actions * Add jsdom tests for tsx files * Add new workflow * Clean up jest config file * Didn't mean to add this * Add jest config and setup scripts to ts exclude * Impersonation shouldn't be a client component for now * 100% test coverage * Add debug flag * Add another test and change coverage engine to have local and github show the same results * Should actually add the test * Restructured components, added provider hooks * Update readme and make sure refreshAuth works as expected * Tests * Fix coverage * Remove oauthTokens from provider * Exclude all test files from build
- Loading branch information
Paul Asjes
authored
Jan 13, 2025
1 parent
e695170
commit 3714fb8
Showing
22 changed files
with
687 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,12 @@ | ||
import { describe, it, expect, beforeEach, jest } from '@jest/globals'; | ||
|
||
import { getSignInUrl, getSignUpUrl, signOut } from '../src/auth.js'; | ||
import { workos } from '../src/workos.js'; | ||
|
||
// These are mocked in jest.setup.ts | ||
import { cookies, headers } from 'next/headers'; | ||
import { redirect } from 'next/navigation'; | ||
import { sealData } from 'iron-session'; | ||
import { generateTestToken } from './test-helpers.js'; | ||
import { User } from '@workos-inc/node'; | ||
|
||
// jest.mock('../src/workos', () => ({ | ||
// workos: { | ||
// userManagement: { | ||
// getLogoutUrl: jest.fn().mockReturnValue('https://example.com/logout'), | ||
// getJwksUrl: jest.fn().mockReturnValue('https://api.workos.com/sso/jwks/client_1234567890'), | ||
// }, | ||
// }, | ||
// })); | ||
|
||
describe('auth.ts', () => { | ||
const mockSession = { | ||
accessToken: 'access-token', | ||
oauthTokens: undefined, | ||
sessionId: 'session_123', | ||
organizationId: 'org_123', | ||
role: 'member', | ||
permissions: ['posts:create', 'posts:delete'], | ||
entitlements: ['audit-logs'], | ||
impersonator: undefined, | ||
user: { | ||
object: 'user', | ||
id: 'user_123', | ||
email: '[email protected]', | ||
emailVerified: true, | ||
profilePictureUrl: null, | ||
firstName: null, | ||
lastName: null, | ||
createdAt: '2024-01-01', | ||
updatedAt: '2024-01-01', | ||
} as User, | ||
}; | ||
|
||
beforeEach(async () => { | ||
// Clear all mocks between tests | ||
jest.clearAllMocks(); | ||
|
@@ -80,32 +45,7 @@ describe('auth.ts', () => { | |
}); | ||
|
||
describe('signOut', () => { | ||
it('should delete the cookie and redirect to the logout url if there is a session', async () => { | ||
const nextCookies = await cookies(); | ||
const nextHeaders = await headers(); | ||
|
||
mockSession.accessToken = await generateTestToken(); | ||
|
||
nextHeaders.set('x-workos-middleware', 'true'); | ||
nextHeaders.set( | ||
'x-workos-session', | ||
await sealData(mockSession, { password: process.env.WORKOS_COOKIE_PASSWORD as string }), | ||
); | ||
|
||
nextCookies.set('wos-session', 'foo'); | ||
|
||
jest.spyOn(workos.userManagement, 'getLogoutUrl').mockReturnValue('https://example.com/logout'); | ||
|
||
await signOut(); | ||
|
||
const sessionCookie = nextCookies.get('wos-session'); | ||
|
||
expect(sessionCookie).toBeUndefined(); | ||
expect(redirect).toHaveBeenCalledTimes(1); | ||
expect(redirect).toHaveBeenCalledWith('https://example.com/logout'); | ||
}); | ||
|
||
it('should delete the cookie and redirect to the root path if there is no session', async () => { | ||
it('should delete the cookie and redirect', async () => { | ||
const nextCookies = await cookies(); | ||
const nextHeaders = await headers(); | ||
|
||
|
Oops, something went wrong.