Skip to content

Commit

Permalink
Create AuthHelper test
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Dec 3, 2024
1 parent fdf86a0 commit 99f1624
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 15 deletions.
101 changes: 101 additions & 0 deletions test/auth/auth-helper.server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type { SessionStorage } from '@remix-run/node';
import { fromPartial } from '@total-typescript/shoehorn';
import type { Authenticator } from 'remix-auth';
import { AuthHelper } from '../../app/auth/auth-helper.server';
import type { SessionData } from '../../app/auth/session-context';

describe('AuthHelper', () => {
const authenticate = vi.fn();
const authenticator: Authenticator<SessionData> = fromPartial({ authenticate });

const defaultSessionData = fromPartial<SessionData>({ displayName: 'foo' });
const getSessionData = vi.fn().mockReturnValue(defaultSessionData);
const getSession = vi.fn().mockResolvedValue({ get: getSessionData, set: vi.fn() });
const commitSession = vi.fn();
const destroySession = vi.fn();
const sessionStorage: SessionStorage = fromPartial({ getSession, commitSession, destroySession });

const setUp = () => new AuthHelper(authenticator, sessionStorage);
const buildRequest = (url?: string) => fromPartial<Request>({ url, headers: new Headers() });

describe('login', () => {
it.each([
['http://example.com', '/'],
[`http://example.com?redirect-to=${encodeURIComponent('/foo/bar')}`, '/foo/bar'],
[`http://example.com?redirect-to=${encodeURIComponent('https://example.com')}`, '/'],
])('authenticates user and redirects to expected location', async (url, expectedRedirect) => {
const authHelper = setUp();
const request = buildRequest(url);

const response = await authHelper.login(request);

expect(response.headers.get('Location')).toEqual(expectedRedirect);
expect(authenticate).toHaveBeenCalled();
expect(getSession).toHaveBeenCalled();
expect(commitSession).toHaveBeenCalled();
expect(destroySession).not.toHaveBeenCalled();
});
});

describe('logout', () => {
it('destroys session and redirects to login page', async () => {
const authHelper = setUp();
const request = buildRequest();

const response = await authHelper.logout(request);

expect(response.headers.get('Location')).toEqual('/login');
expect(getSession).toHaveBeenCalled();
expect(destroySession).toHaveBeenCalled();
expect(commitSession).not.toHaveBeenCalled();
expect(authenticate).not.toHaveBeenCalled();
});
});

describe('getSession', () => {
it.each([
[defaultSessionData],
[undefined],
])('returns session data when no redirect is provided', async (returnedSessionData) => {
const authHelper = setUp();
const request = buildRequest();

getSessionData.mockReturnValue(returnedSessionData);
const sessionData = await authHelper.getSession(request);

expect(sessionData).toEqual(returnedSessionData);
});

it('throws redirect to provided URL if session is not found', async () => {
const authHelper = setUp();
const request = buildRequest();

getSessionData.mockReturnValue(undefined);

await expect(() => authHelper.getSession(request, '/redirect-here')).rejects.toThrow();
expect(getSession).toHaveBeenCalled();
expect(destroySession).not.toHaveBeenCalled();
expect(commitSession).not.toHaveBeenCalled();
expect(authenticate).not.toHaveBeenCalled();
});
});

describe('isAuthenticated', () => {
it.each([
[defaultSessionData],
[undefined],
])('checks if a session exists', async (returnedSessionData) => {
const authHelper = setUp();
const request = buildRequest();

getSessionData.mockReturnValue(returnedSessionData);
const result = await authHelper.isAuthenticated(request);

expect(result).toEqual(!!returnedSessionData);
expect(getSession).toHaveBeenCalled();
expect(destroySession).not.toHaveBeenCalled();
expect(commitSession).not.toHaveBeenCalled();
expect(authenticate).not.toHaveBeenCalled();
});
});
});
15 changes: 0 additions & 15 deletions test/routes/login.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ describe('login', () => {
const authHelper = fromPartial<AuthHelper>({ login, isAuthenticated });

describe('action', () => {
// it.each([
// ['http://example.com', '/'],
// [`http://example.com?redirect-to=${encodeURIComponent('/foo/bar')}`, '/foo/bar'],
// [`http://example.com?redirect-to=${encodeURIComponent('https://example.com')}`, '/'],
// [`http://example.com?redirect-to=${encodeURIComponent('HTTPS://example.com')}`, '/'],
// ])('authenticates user and redirects to expected location', (url, expectedSuccessRedirect) => {
// const request = fromPartial<Request>({ url });
// action(fromPartial({ request }), authenticator);
//
// expect(authenticate).toHaveBeenCalledWith(CREDENTIALS_STRATEGY, request, {
// successRedirect: expectedSuccessRedirect,
// failureRedirect: url,
// });
// });

it('authenticates user', () => {
const request = fromPartial<Request>({});
action(fromPartial({ request }), authHelper);
Expand Down

0 comments on commit 99f1624

Please sign in to comment.