-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4dde199
commit 2908019
Showing
1 changed file
with
95 additions
and
8 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 |
---|---|---|
|
@@ -3,8 +3,9 @@ import { HandlerEvent, HandlerContext } from '@netlify/functions' | |
import * as emails from '../emails'; | ||
import { getCollection, seedFixture } from './utils'; | ||
import config from '../config'; | ||
import { ObjectId } from 'bson'; | ||
|
||
function mockAuthEvent(operation: string, email: string, callbackUrl: string): Partial<HandlerEvent> { | ||
function mockAuthEmailEvent(operation: string, email: string, callbackUrl: string,): Partial<HandlerEvent> { | ||
|
||
const encodedEmail = encodeURIComponent(email); | ||
const encodedCallbackUrl = encodeURIComponent(callbackUrl); | ||
|
@@ -55,6 +56,32 @@ function mockMagicLinkEvent(email: string, token: string, callbackUrl: string): | |
} | ||
} | ||
|
||
function mockAuthSessionEvent(sessionToken: string): Partial<HandlerEvent> { | ||
|
||
const encodedCallbackUrl = encodeURIComponent('/'); | ||
|
||
return { | ||
path: "/api/auth/session", | ||
httpMethod: "GET", | ||
queryStringParameters: {}, | ||
headers: { | ||
cookie: `next-auth.session-token=${sessionToken}; next-auth.callback-url=${encodedCallbackUrl}; next-auth.csrf-token=3fc5b5ba3bb4457090ea32b335e69294637dca5a9473dcc669a4ed00cdadf199%7C1ecd6e1064b23beb6a1e215165a586be0a08bed2d588cc0a440ab3e43c4d2e87`, | ||
connection: "close", | ||
origin: "http://localhost:8000", | ||
"content-length": "181", | ||
"content-type": "application/x-www-form-urlencoded", | ||
referer: "http://localhost:8000/signup/", | ||
"accept-encoding": "gzip, deflate, br, zstd", | ||
"accept-language": "en-US,en;q=0.5", | ||
accept: "*/*", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:134.0) Gecko/20100101 Firefox/134.0", | ||
host: "localhost:8000", | ||
}, | ||
body: ``, | ||
} | ||
} | ||
|
||
|
||
|
||
describe('Auth', () => { | ||
|
||
|
@@ -71,7 +98,7 @@ describe('Auth', () => { | |
|
||
const sendEmailMock = jest.spyOn(emails, 'sendEmail').mockResolvedValue(); | ||
|
||
const event = mockAuthEvent('login', '[email protected]', '/'); | ||
const event = mockAuthEmailEvent('login', '[email protected]', '/'); | ||
|
||
const response = await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
|
@@ -103,7 +130,7 @@ describe('Auth', () => { | |
}); | ||
|
||
const sendEmailMock = jest.spyOn(emails, 'sendEmail').mockResolvedValue(); | ||
const event = mockAuthEvent('login', email, '/'); | ||
const event = mockAuthEmailEvent('login', email, '/'); | ||
|
||
const response = await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
|
@@ -154,7 +181,7 @@ describe('Auth', () => { | |
}); | ||
|
||
const sendEmailMock = jest.spyOn(emails, 'sendEmail').mockResolvedValue(); | ||
const event = mockAuthEvent('login', email, callbackUrl); | ||
const event = mockAuthEmailEvent('login', email, callbackUrl); | ||
|
||
await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
|
@@ -188,7 +215,7 @@ describe('Auth', () => { | |
}); | ||
|
||
const sendEmailMock = jest.spyOn(emails, 'sendEmail').mockResolvedValue(); | ||
const event = mockAuthEvent('signup', email, '/'); | ||
const event = mockAuthEmailEvent('signup', email, '/'); | ||
|
||
const response = await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
|
@@ -235,7 +262,7 @@ describe('Auth', () => { | |
}); | ||
|
||
const sendEmailMock = jest.spyOn(emails, 'sendEmail').mockResolvedValue(); | ||
const event = mockAuthEvent('signup', email, '/'); | ||
const event = mockAuthEmailEvent('signup', email, '/'); | ||
|
||
const response = await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
|
@@ -278,7 +305,7 @@ describe('Auth', () => { | |
}); | ||
|
||
const sendEmailMock = jest.spyOn(emails, 'sendEmail').mockResolvedValue(); | ||
const event = mockAuthEvent('signup', email, callbackUrl); | ||
const event = mockAuthEmailEvent('signup', email, callbackUrl); | ||
|
||
await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
|
@@ -298,7 +325,7 @@ describe('Auth', () => { | |
|
||
}); | ||
|
||
describe('Callback', () => { | ||
describe('Callbacks', () => { | ||
|
||
test('Should verify the email and create a user with the appropriate userId', async () => { | ||
|
||
|
@@ -343,5 +370,65 @@ describe('Auth', () => { | |
roles: ['subscriber'], | ||
}]); | ||
}); | ||
|
||
test('Should return enriched session data when hitting session endpoint', async () => { | ||
const userId = new ObjectId('5f9f1b3b1c9d440000000000'); | ||
const email = '[email protected]'; | ||
const sessionToken = 'f59bedfb-f318-4b55-ad29-9887abba8f06' | ||
const expires = new Date(Date.now() + 24 * 60 * 60 * 1000); | ||
|
||
await seedFixture({ | ||
auth: { | ||
users: [{ | ||
_id: userId, | ||
email, | ||
emailVerified: new Date(), | ||
}], | ||
sessions: [{ | ||
userId, | ||
sessionToken, | ||
expires, | ||
}] | ||
}, | ||
customData: { | ||
users: [{ | ||
userId: userId.toHexString(), | ||
roles: ['subscriber', 'editor'], | ||
first_name: 'Test', | ||
last_name: 'User' | ||
}] | ||
} | ||
}); | ||
|
||
const event = mockAuthSessionEvent(sessionToken); | ||
|
||
const response = await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
expect(response).toMatchObject({ | ||
body: JSON.stringify({ | ||
user: { | ||
email, | ||
id: userId, | ||
roles: ['subscriber', 'editor'], | ||
first_name: 'Test', | ||
last_name: 'User' | ||
}, | ||
expires: expires.toISOString(), | ||
}), | ||
}); | ||
}); | ||
|
||
test('Should return null session when no valid session exists', async () => { | ||
|
||
const event = mockAuthSessionEvent('fake-session-token'); | ||
|
||
const response = await handler(event as HandlerEvent, {} as HandlerContext); | ||
|
||
expect(response).toMatchObject({ | ||
body: `{}`, | ||
statusCode: 200, | ||
}); | ||
}); | ||
}); | ||
|
||
}); |