|
| 1 | +import { Provider, errors } from 'oidc-provider'; |
| 2 | + |
| 3 | +import RequestError from '#src/errors/RequestError/index.js'; |
| 4 | +import { MockTenant } from '#src/test-utils/tenant.js'; |
| 5 | +import { createContextWithRouteParameters } from '#src/utils/test-utils.js'; |
| 6 | + |
| 7 | +import koaConsentGuard from './koa-consent-guard.js'; |
| 8 | + |
| 9 | +const { jest } = import.meta; |
| 10 | + |
| 11 | +describe('koaConsentGuard middleware', () => { |
| 12 | + const provider = new Provider('https://logto.test'); |
| 13 | + const interactionDetails = jest.spyOn(provider, 'interactionDetails'); |
| 14 | + const verifyOneTimeToken = jest.fn(); |
| 15 | + |
| 16 | + const tenant = new MockTenant( |
| 17 | + undefined, |
| 18 | + { |
| 19 | + users: { |
| 20 | + findUserById: jest.fn().mockResolvedValue({ primaryEmail: '[email protected]' }), |
| 21 | + }, |
| 22 | + }, |
| 23 | + undefined, |
| 24 | + { |
| 25 | + oneTimeTokens: { verifyOneTimeToken }, |
| 26 | + } |
| 27 | + ); |
| 28 | + const next = jest.fn(); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw an error if session is not found', async () => { |
| 35 | + // @ts-expect-error |
| 36 | + interactionDetails.mockResolvedValue({ |
| 37 | + params: { token: 'token', login_hint: '[email protected]' }, |
| 38 | + session: undefined, |
| 39 | + }); |
| 40 | + const ctx = createContextWithRouteParameters({ |
| 41 | + url: `/consent`, |
| 42 | + }); |
| 43 | + const guard = koaConsentGuard(provider, tenant.libraries, tenant.queries); |
| 44 | + |
| 45 | + await expect(guard(ctx, next)).rejects.toThrow(errors.SessionNotFound); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should not block if token or login_hint are not provided', async () => { |
| 49 | + interactionDetails.mockResolvedValue({ |
| 50 | + params: { token: '', login_hint: '' }, |
| 51 | + // @ts-expect-error |
| 52 | + session: { accountId: 'foo' }, |
| 53 | + }); |
| 54 | + const ctx = createContextWithRouteParameters({ |
| 55 | + url: `/consent`, |
| 56 | + }); |
| 57 | + const guard = koaConsentGuard(provider, tenant.libraries, tenant.queries); |
| 58 | + |
| 59 | + await guard(ctx, next); |
| 60 | + expect(tenant.queries.users.findUserById).not.toHaveBeenCalled(); |
| 61 | + expect(tenant.libraries.oneTimeTokens.verifyOneTimeToken).not.toHaveBeenCalled(); |
| 62 | + expect(next).toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should redirect to switch account page if email does not match', async () => { |
| 66 | + interactionDetails.mockResolvedValue({ |
| 67 | + params: { token: 'abcdefg', login_hint: '[email protected]' }, |
| 68 | + // @ts-expect-error |
| 69 | + session: { accountId: 'bar' }, |
| 70 | + }); |
| 71 | + const ctx = createContextWithRouteParameters({ |
| 72 | + url: `/consent`, |
| 73 | + }); |
| 74 | + const guard = koaConsentGuard(provider, tenant.libraries, tenant.queries); |
| 75 | + |
| 76 | + await guard(ctx, jest.fn()); |
| 77 | + expect(ctx.redirect).toHaveBeenCalledWith(expect.stringContaining('switch-account')); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should call next middleware if validations pass', async () => { |
| 81 | + const ctx = createContextWithRouteParameters({ |
| 82 | + url: `/consent`, |
| 83 | + }); |
| 84 | + interactionDetails.mockResolvedValue({ |
| 85 | + params: { token: 'token_value', login_hint: '[email protected]' }, |
| 86 | + // @ts-expect-error |
| 87 | + session: { accountId: 'foo' }, |
| 88 | + }); |
| 89 | + verifyOneTimeToken.mockResolvedValueOnce({ token: 'token_value', email: '[email protected]' }); |
| 90 | + const guard = koaConsentGuard(provider, tenant.libraries, tenant.queries); |
| 91 | + |
| 92 | + await guard(ctx, next); |
| 93 | + expect(next).toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should redirect to error page on one-time token verification failure', async () => { |
| 97 | + const ctx = createContextWithRouteParameters({ |
| 98 | + url: `/consent`, |
| 99 | + }); |
| 100 | + interactionDetails.mockResolvedValue({ |
| 101 | + params: { token: 'token', login_hint: '[email protected]' }, |
| 102 | + // @ts-expect-error |
| 103 | + session: { accountId: 'foo' }, |
| 104 | + }); |
| 105 | + verifyOneTimeToken.mockRejectedValue(new RequestError('one_time_token.token_consumed')); |
| 106 | + const guard = koaConsentGuard(provider, tenant.libraries, tenant.queries); |
| 107 | + |
| 108 | + await guard(ctx, next); |
| 109 | + expect(ctx.redirect).toHaveBeenCalledWith( |
| 110 | + expect.stringContaining('code=one_time_token.token_consumed') |
| 111 | + ); |
| 112 | + }); |
| 113 | +}); |
0 commit comments