From 99f16247436ea2102b86775d12661ef0d51dfbfe Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 3 Dec 2024 22:37:45 +0100 Subject: [PATCH] Create AuthHelper test --- test/auth/auth-helper.server.test.ts | 101 +++++++++++++++++++++++++++ test/routes/login.test.tsx | 15 ---- 2 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 test/auth/auth-helper.server.test.ts diff --git a/test/auth/auth-helper.server.test.ts b/test/auth/auth-helper.server.test.ts new file mode 100644 index 0000000..6cf4042 --- /dev/null +++ b/test/auth/auth-helper.server.test.ts @@ -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 = fromPartial({ authenticate }); + + const defaultSessionData = fromPartial({ 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({ 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(); + }); + }); +}); diff --git a/test/routes/login.test.tsx b/test/routes/login.test.tsx index 05fd20d..94e665d 100644 --- a/test/routes/login.test.tsx +++ b/test/routes/login.test.tsx @@ -11,21 +11,6 @@ describe('login', () => { const authHelper = fromPartial({ 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({ url }); - // action(fromPartial({ request }), authenticator); - // - // expect(authenticate).toHaveBeenCalledWith(CREDENTIALS_STRATEGY, request, { - // successRedirect: expectedSuccessRedirect, - // failureRedirect: url, - // }); - // }); - it('authenticates user', () => { const request = fromPartial({}); action(fromPartial({ request }), authHelper);