-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for process-session-cookie
- Loading branch information
1 parent
b55903f
commit 3f6ee77
Showing
5 changed files
with
144 additions
and
17 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
134 changes: 126 additions & 8 deletions
134
packages/auth/src/services/process-session-cookie.test.ts
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,17 +1,135 @@ | ||
import { randomUUID } from 'crypto'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { createUser } from '@atj/database'; | ||
import { createSession, createUser } from '@atj/database'; | ||
|
||
import { processProviderCallback } from './process-provider-callback'; | ||
import { createTestAuthContext } from '../context/test'; | ||
import { AuthContext } from '..'; | ||
import { processSessionCookie } from './process-session-cookie'; | ||
|
||
describe('processSessionCookie', () => { | ||
let ctx: AuthContext; | ||
const today = new Date(2020, 1, 1); | ||
const tenYearsAgo = new Date(2010, 1, 1); | ||
|
||
it('works', async () => { | ||
const ctx = await createTestAuthContext(); | ||
//processSessionCookie() | ||
expect(true).toBe(true); | ||
beforeEach(async () => { | ||
vi.setSystemTime(today); | ||
}); | ||
|
||
it('sets null user session with unset session cookie', async () => { | ||
const mocks = { | ||
getCookie: vi.fn(() => undefined), | ||
setUserSession: vi.fn(), | ||
}; | ||
const ctx = await createTestAuthContext(mocks); | ||
|
||
const result = await processSessionCookie( | ||
ctx, | ||
new Request('http://localhost', { | ||
headers: { Origin: 'http://localhost', Host: 'http://www.google.com' }, | ||
}) | ||
); | ||
|
||
expect(result.success).toEqual(true); | ||
expect(mocks.setUserSession).toHaveBeenCalledWith({ | ||
session: null, | ||
user: null, | ||
}); | ||
}); | ||
|
||
it('resets session cookie and sets user session with fresh session cookie', async () => { | ||
const { ctx, mocks, sessionId, user } = await setUpTest(today); | ||
|
||
const result = await processSessionCookie( | ||
ctx, | ||
new Request('http://localhost', { | ||
headers: { Origin: 'http://localhost', Host: 'http://www.google.com' }, | ||
}) | ||
); | ||
|
||
expect(result.success).toEqual(true); | ||
expect(mocks.setCookie).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
attributes: { | ||
httpOnly: true, | ||
maxAge: 2592000, | ||
path: '/', | ||
sameSite: 'lax', | ||
secure: false, | ||
}, | ||
name: 'auth_session', | ||
value: sessionId, | ||
}) | ||
); | ||
expect(mocks.setUserSession).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
session: { | ||
expiresAt: expect.any(Date), | ||
fresh: true, | ||
id: sessionId, | ||
userId: user.id, | ||
}, | ||
user: { | ||
email: '[email protected]', | ||
id: user.id, | ||
}, | ||
}) | ||
); | ||
}); | ||
|
||
it('clears cookies with stale session cookie', async () => { | ||
const { ctx, mocks } = await setUpTest(tenYearsAgo); | ||
|
||
const result = await processSessionCookie( | ||
ctx, | ||
new Request('http://localhost', { | ||
headers: { Origin: 'http://localhost', Host: 'http://www.google.com' }, | ||
}) | ||
); | ||
|
||
expect(result.success).toEqual(true); | ||
expect(mocks.setCookie).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
attributes: { | ||
httpOnly: true, | ||
maxAge: 0, | ||
path: '/', | ||
sameSite: 'lax', | ||
secure: false, | ||
}, | ||
name: 'auth_session', | ||
value: '', | ||
}) | ||
); | ||
expect(mocks.setUserSession).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
session: null, | ||
user: null, | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
const addOneDay = (date: Date): Date => { | ||
const newDate = new Date(date); | ||
newDate.setDate(newDate.getDate() + 1); | ||
return newDate; | ||
}; | ||
|
||
const setUpTest = async (sessionExpirationDate: Date) => { | ||
const mocks = { | ||
getCookie: vi.fn(() => sessionId || ''), | ||
setCookie: vi.fn(), | ||
setUserSession: vi.fn(), | ||
}; | ||
const ctx = await createTestAuthContext(mocks); | ||
const user = await createUser(ctx.database, '[email protected]'); | ||
if (!user) { | ||
expect.fail('error creating test user'); | ||
} | ||
const sessionId = await createSession(ctx.database, { | ||
id: randomUUID(), | ||
expiresAt: addOneDay(sessionExpirationDate), | ||
sessionToken: 'my-token', | ||
userId: user.id, | ||
}); | ||
return { ctx, mocks, sessionId, user }; | ||
}; |
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