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