Skip to content

Commit

Permalink
Add test to session callback
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarvarela committed Dec 24, 2024
1 parent 4dde199 commit 2908019
Showing 1 changed file with 95 additions and 8 deletions.
103 changes: 95 additions & 8 deletions site/gatsby-site/server/tests/next-auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -298,7 +325,7 @@ describe('Auth', () => {

});

describe('Callback', () => {
describe('Callbacks', () => {

test('Should verify the email and create a user with the appropriate userId', async () => {

Expand Down Expand Up @@ -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,
});
});
});

});

0 comments on commit 2908019

Please sign in to comment.