|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { AuthOrchestrator } from './auth-orchestrator.service'; |
| 3 | +import { IdempotentUserService } from '../users/idempotent-user.service'; |
| 4 | +import { WalletCreationOrchestrator } from '../wallets/wallet-creation-orchestrator.service'; |
| 5 | +import { IdempotencyService } from '../common/idempotency/idempotency.service'; |
| 6 | +import { WebhookEventEmitterService } from '../webhooks/webhook-event-emitter.service'; |
| 7 | +import { WalletNetwork, WalletStatus } from '../wallets/domain/wallet.model'; |
| 8 | + |
| 9 | +const NOW = new Date('2026-01-01T00:00:00.000Z'); |
| 10 | + |
| 11 | +const makeUser = (overrides: Record<string, any> = {}) => ({ |
| 12 | + id: 'user-abc', |
| 13 | + authId: 'auth-abc', |
| 14 | + email: 'user@example.com', |
| 15 | + displayName: 'Test User', |
| 16 | + status: 'ACTIVE', |
| 17 | + authProvider: 'GOOGLE', |
| 18 | + lastLoginAt: NOW, |
| 19 | + createdAt: NOW, |
| 20 | + updatedAt: NOW, |
| 21 | + ...overrides, |
| 22 | +}); |
| 23 | + |
| 24 | +const makeWallet = (overrides: Record<string, any> = {}) => ({ |
| 25 | + id: 'wallet-abc', |
| 26 | + userId: 'user-abc', |
| 27 | + publicKey: 'GABC1234567890', |
| 28 | + encryptedSecret: 'enc', |
| 29 | + encryptionVersion: 1, |
| 30 | + secretVersion: 1, |
| 31 | + network: WalletNetwork.TESTNET, |
| 32 | + status: WalletStatus.ACTIVE, |
| 33 | + statusChangedAt: NOW, |
| 34 | + createdAt: NOW, |
| 35 | + updatedAt: NOW, |
| 36 | + rotatedFromId: null, |
| 37 | + statusReason: null, |
| 38 | + ...overrides, |
| 39 | +}); |
| 40 | + |
| 41 | +describe('AuthOrchestrator — domain event emission', () => { |
| 42 | + let orchestrator: AuthOrchestrator; |
| 43 | + let webhookEventEmitter: jest.Mocked<WebhookEventEmitterService>; |
| 44 | + |
| 45 | + const mockUserService = { |
| 46 | + findOrCreateUser: jest.fn(), |
| 47 | + findUserByAuthId: jest.fn(), |
| 48 | + listSessions: jest.fn(), |
| 49 | + }; |
| 50 | + const mockWalletOrchestrator = { |
| 51 | + getWalletByUser: jest.fn(), |
| 52 | + createWallet: jest.fn(), |
| 53 | + }; |
| 54 | + const mockIdempotencyService = { |
| 55 | + getCachedResponse: jest.fn().mockResolvedValue(null), |
| 56 | + cacheResponse: jest.fn().mockResolvedValue(undefined), |
| 57 | + }; |
| 58 | + const mockWebhookEventEmitter = { |
| 59 | + emitUserAuthenticated: jest.fn().mockResolvedValue(undefined), |
| 60 | + emitNewUserRegistered: jest.fn().mockResolvedValue(undefined), |
| 61 | + emitAuthenticationFailed: jest.fn().mockResolvedValue(undefined), |
| 62 | + }; |
| 63 | + |
| 64 | + beforeEach(async () => { |
| 65 | + const module: TestingModule = await Test.createTestingModule({ |
| 66 | + providers: [ |
| 67 | + AuthOrchestrator, |
| 68 | + { provide: IdempotentUserService, useValue: mockUserService }, |
| 69 | + { provide: WalletCreationOrchestrator, useValue: mockWalletOrchestrator }, |
| 70 | + { provide: IdempotencyService, useValue: mockIdempotencyService }, |
| 71 | + { provide: WebhookEventEmitterService, useValue: mockWebhookEventEmitter }, |
| 72 | + ], |
| 73 | + }).compile(); |
| 74 | + |
| 75 | + orchestrator = module.get<AuthOrchestrator>(AuthOrchestrator); |
| 76 | + webhookEventEmitter = module.get(WebhookEventEmitterService); |
| 77 | + jest.clearAllMocks(); |
| 78 | + mockIdempotencyService.getCachedResponse.mockResolvedValue(null); |
| 79 | + mockIdempotencyService.cacheResponse.mockResolvedValue(undefined); |
| 80 | + mockWebhookEventEmitter.emitUserAuthenticated.mockResolvedValue(undefined); |
| 81 | + mockWebhookEventEmitter.emitNewUserRegistered.mockResolvedValue(undefined); |
| 82 | + mockWebhookEventEmitter.emitAuthenticationFailed.mockResolvedValue(undefined); |
| 83 | + }); |
| 84 | + |
| 85 | + it('emits auth.new_user_registered for first-time users', async () => { |
| 86 | + const user = makeUser(); |
| 87 | + const wallet = makeWallet(); |
| 88 | + |
| 89 | + mockUserService.findOrCreateUser.mockResolvedValue({ user, isNewUser: true }); |
| 90 | + mockWalletOrchestrator.getWalletByUser.mockResolvedValue(null); |
| 91 | + mockWalletOrchestrator.createWallet.mockResolvedValue({ |
| 92 | + wallet, |
| 93 | + privateKey: 'secret', |
| 94 | + isNewWallet: true, |
| 95 | + }); |
| 96 | + |
| 97 | + await orchestrator.handleAuthentication({ authId: 'auth-abc' }); |
| 98 | + |
| 99 | + // Allow best-effort async emission to complete |
| 100 | + await new Promise((r) => setTimeout(r, 10)); |
| 101 | + |
| 102 | + expect(mockWebhookEventEmitter.emitNewUserRegistered).toHaveBeenCalledWith( |
| 103 | + expect.objectContaining({ |
| 104 | + userId: user.id, |
| 105 | + authId: user.authId, |
| 106 | + authProvider: user.authProvider, |
| 107 | + walletId: wallet.id, |
| 108 | + walletNetwork: WalletNetwork.TESTNET, |
| 109 | + }), |
| 110 | + ); |
| 111 | + expect(mockWebhookEventEmitter.emitUserAuthenticated).not.toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('emits auth.user_authenticated for returning users', async () => { |
| 115 | + const user = makeUser(); |
| 116 | + const wallet = makeWallet(); |
| 117 | + |
| 118 | + mockUserService.findOrCreateUser.mockResolvedValue({ user, isNewUser: false }); |
| 119 | + mockWalletOrchestrator.getWalletByUser.mockResolvedValue(wallet); |
| 120 | + |
| 121 | + await orchestrator.handleAuthentication({ authId: 'auth-abc' }); |
| 122 | + |
| 123 | + await new Promise((r) => setTimeout(r, 10)); |
| 124 | + |
| 125 | + expect(mockWebhookEventEmitter.emitUserAuthenticated).toHaveBeenCalledWith( |
| 126 | + expect.objectContaining({ |
| 127 | + userId: user.id, |
| 128 | + authId: user.authId, |
| 129 | + authProvider: user.authProvider, |
| 130 | + isNewWallet: false, |
| 131 | + }), |
| 132 | + ); |
| 133 | + expect(mockWebhookEventEmitter.emitNewUserRegistered).not.toHaveBeenCalled(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('emits auth.authentication_failed on error', async () => { |
| 137 | + mockUserService.findOrCreateUser.mockRejectedValue(new Error('DB down')); |
| 138 | + |
| 139 | + await expect( |
| 140 | + orchestrator.handleAuthentication({ authId: 'auth-abc' }), |
| 141 | + ).rejects.toThrow('Authentication failed'); |
| 142 | + |
| 143 | + await new Promise((r) => setTimeout(r, 10)); |
| 144 | + |
| 145 | + expect(mockWebhookEventEmitter.emitAuthenticationFailed).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ authId: 'auth-abc', reason: 'DB down' }), |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('does not throw if event emission fails (best-effort)', async () => { |
| 151 | + const user = makeUser(); |
| 152 | + const wallet = makeWallet(); |
| 153 | + |
| 154 | + mockUserService.findOrCreateUser.mockResolvedValue({ user, isNewUser: false }); |
| 155 | + mockWalletOrchestrator.getWalletByUser.mockResolvedValue(wallet); |
| 156 | + mockWebhookEventEmitter.emitUserAuthenticated.mockRejectedValue( |
| 157 | + new Error('webhook down'), |
| 158 | + ); |
| 159 | + |
| 160 | + await expect( |
| 161 | + orchestrator.handleAuthentication({ authId: 'auth-abc' }), |
| 162 | + ).resolves.toBeDefined(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments