|
| 1 | +import { ServerResponse, IncomingMessage } from 'node:http'; |
| 2 | +import { Socket } from 'node:net'; |
| 3 | + |
| 4 | +import * as logtoNode from '@logto/node'; |
| 5 | +import { mockNuxtImport } from '@nuxt/test-utils/runtime'; |
| 6 | +import { createEvent } from 'h3'; |
| 7 | +import { describe, expect, it, vi, type Mock } from 'vitest'; |
| 8 | + |
| 9 | +import handler from '@/src/runtime/server/event-handler'; |
| 10 | + |
| 11 | +mockNuxtImport('useRuntimeConfig', () => |
| 12 | + vi.fn(() => ({ |
| 13 | + logto: { |
| 14 | + cookieEncryptionKey: 'foo', |
| 15 | + pathnames: { |
| 16 | + signIn: '/sign-in', |
| 17 | + signOut: '/sign-out', |
| 18 | + callback: '/callback', |
| 19 | + }, |
| 20 | + }, |
| 21 | + })) |
| 22 | +); |
| 23 | + |
| 24 | +const cookies = new Map(); |
| 25 | +const getRequestURL = vi.fn(() => new URL('http://localhost:3000')); |
| 26 | +const sendRedirect = vi.fn(); |
| 27 | + |
| 28 | +vi.stubGlobal('defineEventHandler', (handler: unknown) => handler); |
| 29 | +vi.stubGlobal('getRequestURL', getRequestURL); |
| 30 | +vi.stubGlobal('getCookie', (_event: unknown, name: string) => cookies.get(name)); |
| 31 | +vi.stubGlobal('setCookie', (_event: unknown, name: string, value: string) => { |
| 32 | + cookies.set(name, value); |
| 33 | +}); |
| 34 | +vi.stubGlobal('sendRedirect', sendRedirect); |
| 35 | + |
| 36 | +const LogtoClient = vi.fn(); |
| 37 | + |
| 38 | +vi.spyOn(logtoNode, 'default', 'get').mockImplementation(() => { |
| 39 | + /* eslint-disable @silverhand/fp/no-mutation */ |
| 40 | + LogtoClient.prototype.signIn = vi.fn(); |
| 41 | + LogtoClient.prototype.isAuthenticated = vi.fn().mockResolvedValue(false); |
| 42 | + LogtoClient.prototype.handleSignInCallback = vi.fn(); |
| 43 | + /* eslint-enable @silverhand/fp/no-mutation */ |
| 44 | + return LogtoClient; |
| 45 | +}); |
| 46 | + |
| 47 | +const createH3Event = () => { |
| 48 | + const incoming = new IncomingMessage(new Socket()); |
| 49 | + const response = new ServerResponse(incoming); |
| 50 | + return createEvent(incoming, response); |
| 51 | +}; |
| 52 | + |
| 53 | +describe('event-handler', async () => { |
| 54 | + it('should inject logto client', async () => { |
| 55 | + const event = createH3Event(); |
| 56 | + await handler(event); |
| 57 | + |
| 58 | + expect(event.context.logtoClient).toBeInstanceOf(LogtoClient); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle sign-in', async () => { |
| 62 | + const event = createH3Event(); |
| 63 | + getRequestURL.mockReturnValueOnce(new URL('http://localhost:3000/sign-in')); |
| 64 | + await handler(event); |
| 65 | + expect(LogtoClient.prototype.signIn).toHaveBeenCalledWith('http://localhost:3000/callback'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should handle callback with custom callback pathname', async () => { |
| 69 | + const event = createH3Event(); |
| 70 | + // @ts-expect-error |
| 71 | + (useRuntimeConfig as Mock<typeof useRuntimeConfig>).mockReturnValueOnce({ |
| 72 | + logto: { |
| 73 | + postCallbackRedirectUri: '/', |
| 74 | + cookieEncryptionKey: 'foo', |
| 75 | + pathnames: { |
| 76 | + signIn: '/sign-in', |
| 77 | + signOut: '/sign-out', |
| 78 | + callback: '/callback-1', |
| 79 | + }, |
| 80 | + }, |
| 81 | + }); |
| 82 | + getRequestURL.mockReturnValueOnce(new URL('http://localhost:3000/callback-1')); |
| 83 | + await handler(event); |
| 84 | + expect(LogtoClient.prototype.handleSignInCallback).toHaveBeenCalledWith( |
| 85 | + 'http://localhost:3000/callback-1' |
| 86 | + ); |
| 87 | + expect(sendRedirect).toHaveBeenCalledWith(event, '/', 302); |
| 88 | + }); |
| 89 | +}); |
0 commit comments