-
Notifications
You must be signed in to change notification settings - Fork 25
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 * Address feedback * Also run prettier on test files * Throw if no cookie password or if it's too short * Add onSuccess test * Address feedback
- Loading branch information
Showing
3 changed files
with
94 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,28 @@ jest.mock('../src/workos', () => ({ | |
})); | ||
|
||
describe('authkit-callback-route', () => { | ||
const mockAuthResponse = { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
user: { | ||
id: 'user_123', | ||
email: '[email protected]', | ||
emailVerified: true, | ||
profilePictureUrl: 'https://example.com/photo.jpg', | ||
firstName: 'Test', | ||
lastName: 'User', | ||
object: 'user' as const, | ||
createdAt: '2024-01-01T00:00:00Z', | ||
updatedAt: '2024-01-01T00:00:00Z', | ||
}, | ||
oauthTokens: { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
expiresAt: 1719811200, | ||
scopes: ['foo', 'bar'], | ||
}, | ||
}; | ||
|
||
describe('handleAuth', () => { | ||
let request: NextRequest; | ||
|
||
|
@@ -42,14 +64,7 @@ describe('authkit-callback-route', () => { | |
}); | ||
|
||
it('should handle successful authentication', async () => { | ||
// Mock successful authentication response | ||
const mockAuthResponse = { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
user: { id: 'user_123' }, | ||
}; | ||
|
||
(workos.userManagement.authenticateWithCode as jest.Mock).mockResolvedValue(mockAuthResponse); | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); | ||
|
||
// Set up request with code | ||
request.nextUrl.searchParams.set('code', 'test-code'); | ||
|
@@ -80,7 +95,7 @@ describe('authkit-callback-route', () => { | |
|
||
it('should handle authentication failure if a non-Error object is thrown', async () => { | ||
// Mock authentication failure | ||
(workos.userManagement.authenticateWithCode as jest.Mock).mockRejectedValue('Auth failed'); | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockRejectedValue('Auth failed'); | ||
|
||
request.nextUrl.searchParams.set('code', 'invalid-code'); | ||
|
||
|
@@ -102,13 +117,7 @@ describe('authkit-callback-route', () => { | |
}); | ||
|
||
it('should respect custom returnPathname', async () => { | ||
const mockAuthResponse = { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
user: { id: 'user1' }, | ||
}; | ||
|
||
(workos.userManagement.authenticateWithCode as jest.Mock).mockResolvedValue(mockAuthResponse); | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); | ||
|
||
request.nextUrl.searchParams.set('code', 'test-code'); | ||
|
||
|
@@ -119,13 +128,7 @@ describe('authkit-callback-route', () => { | |
}); | ||
|
||
it('should handle state parameter with returnPathname', async () => { | ||
const mockAuthResponse = { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
user: { id: 'user1' }, | ||
}; | ||
|
||
(workos.userManagement.authenticateWithCode as jest.Mock).mockResolvedValue(mockAuthResponse); | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); | ||
|
||
const state = btoa(JSON.stringify({ returnPathname: '/custom-path' })); | ||
request.nextUrl.searchParams.set('code', 'test-code'); | ||
|
@@ -138,13 +141,7 @@ describe('authkit-callback-route', () => { | |
}); | ||
|
||
it('should extract custom search params from returnPathname', async () => { | ||
const mockAuthResponse = { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
user: { id: 'user1' }, | ||
}; | ||
|
||
(workos.userManagement.authenticateWithCode as jest.Mock).mockResolvedValue(mockAuthResponse); | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); | ||
|
||
const state = btoa(JSON.stringify({ returnPathname: '/custom-path?foo=bar&baz=qux' })); | ||
request.nextUrl.searchParams.set('code', 'test-code'); | ||
|
@@ -160,14 +157,7 @@ describe('authkit-callback-route', () => { | |
const originalRedirect = NextResponse.redirect; | ||
(NextResponse as Partial<typeof NextResponse>).redirect = undefined; | ||
|
||
// Mock successful authentication response | ||
const mockAuthResponse = { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
user: { id: 'user_123' }, | ||
}; | ||
|
||
(workos.userManagement.authenticateWithCode as jest.Mock).mockResolvedValue(mockAuthResponse); | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); | ||
|
||
// Set up request with code | ||
request.nextUrl.searchParams.set('code', 'test-code'); | ||
|
@@ -199,14 +189,7 @@ describe('authkit-callback-route', () => { | |
}); | ||
|
||
it('should use baseURL if provided', async () => { | ||
// Mock successful authentication response | ||
const mockAuthResponse = { | ||
accessToken: 'access123', | ||
refreshToken: 'refresh123', | ||
user: { id: 'user_123' }, | ||
}; | ||
|
||
(workos.userManagement.authenticateWithCode as jest.Mock).mockResolvedValue(mockAuthResponse); | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); | ||
|
||
// Set up request with code | ||
request.nextUrl.searchParams.set('code', 'test-code'); | ||
|
@@ -232,5 +215,18 @@ describe('authkit-callback-route', () => { | |
|
||
expect(response.status).toBe(500); | ||
}); | ||
|
||
it('should call onSuccess if provided', async () => { | ||
jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); | ||
|
||
// Set up request with code | ||
request.nextUrl.searchParams.set('code', 'test-code'); | ||
|
||
const onSuccess = jest.fn(); | ||
const handler = handleAuth({ onSuccess: onSuccess }); | ||
await handler(request); | ||
|
||
expect(onSuccess).toHaveBeenCalledWith(mockAuthResponse); | ||
}); | ||
}); | ||
}); |
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