|
| 1 | +import type { DatabaseTransactionManager, PrismaTransaction } from '../../database'; |
| 2 | +import { WaveAuthenticatorImpl } from './authenticator-implementation'; |
| 3 | +import { mockDeep, type DeepMockProxy } from 'vitest-mock-extended'; |
| 4 | +import type { ApplicationName, AuthenticatorRepository } from './authenticator-repository'; |
| 5 | +import { describe, vi, it, expect, beforeEach } from 'vitest'; |
| 6 | + |
| 7 | +const mockJson = vi.fn(); |
| 8 | +const mockStatus = vi.fn(); |
| 9 | +const mockText = vi.fn(); |
| 10 | +const mockHeaders = vi.fn(); |
| 11 | +global.fetch = vi.fn(() => Promise.resolve({ |
| 12 | + status: mockStatus(), |
| 13 | + headers: { |
| 14 | + get: mockHeaders, |
| 15 | + }, |
| 16 | + json: () => Promise.resolve(mockJson()), |
| 17 | + text: () => Promise.resolve(mockText()), |
| 18 | +}) as unknown as Promise<Response>); |
| 19 | + |
| 20 | +interface MakeSut { |
| 21 | + transactionManager: DeepMockProxy<DatabaseTransactionManager>; |
| 22 | + tokenRepository: DeepMockProxy<AuthenticatorRepository>; |
| 23 | + sut: WaveAuthenticatorImpl; |
| 24 | +} |
| 25 | + |
| 26 | +const makeSut = (): MakeSut => { |
| 27 | + const transactionManager = mockDeep<DatabaseTransactionManager>(); |
| 28 | + const tokenRepository = mockDeep<AuthenticatorRepository>(); |
| 29 | + const sut = new WaveAuthenticatorImpl( |
| 30 | + transactionManager, |
| 31 | + tokenRepository, |
| 32 | + { |
| 33 | + authUrl: 'https://auth.wave.com', |
| 34 | + audience: 'https://api.wave.com', |
| 35 | + clientId: 'clientId', |
| 36 | + clientSecret: 'clientSecret', |
| 37 | + }, |
| 38 | + { |
| 39 | + tokenExpiryInSeconds: 3600, |
| 40 | + }, |
| 41 | + ); |
| 42 | + |
| 43 | + return { transactionManager, tokenRepository, sut }; |
| 44 | +}; |
| 45 | + |
| 46 | +describe('wave authentication impl', () => { |
| 47 | + beforeEach(() => { |
| 48 | + vi.clearAllMocks(); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('authenticate', () => { |
| 52 | + it('should return cached token if not expired', async () => { |
| 53 | + const { sut, tokenRepository } = makeSut(); |
| 54 | + const futureTimestamp = Date.now() + 1000000; |
| 55 | + const mockToken = { token: 'cached-token', expiresAt: futureTimestamp, application: 'WAVE_MVNO_APPLICATION' as ApplicationName }; |
| 56 | + |
| 57 | + tokenRepository.get.mockResolvedValueOnce(mockToken); |
| 58 | + mockStatus.mockReturnValueOnce(200); |
| 59 | + const result = await sut.authenticate('WAVE_MVNO_APPLICATION'); |
| 60 | + |
| 61 | + expect(result).toBe(mockToken.token); |
| 62 | + expect(tokenRepository.get).toHaveBeenCalledWith('WAVE_MVNO_APPLICATION'); |
| 63 | + expect(global.fetch).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should fetch new token if cached token is expired', async () => { |
| 67 | + const { sut, tokenRepository, transactionManager } = makeSut(); |
| 68 | + const pastTimestamp = Date.now() - 1000; |
| 69 | + const mockToken = { token: 'expired-token', expiresAt: pastTimestamp, application: 'WAVE_MVNO_APPLICATION' as ApplicationName }; |
| 70 | + const newToken = { access_token: 'new-token', expires_in: 3600, application: 'WAVE_MVNO_APPLICATION' as ApplicationName }; |
| 71 | + |
| 72 | + tokenRepository.get.mockResolvedValueOnce(mockToken); |
| 73 | + mockJson.mockReturnValueOnce(newToken); |
| 74 | + mockStatus.mockReturnValueOnce(200); |
| 75 | + transactionManager.execute.mockImplementationOnce( |
| 76 | + (callback) => callback({} as PrismaTransaction) |
| 77 | + ); |
| 78 | + |
| 79 | + const result = await sut.authenticate('WAVE_MVNO_APPLICATION'); |
| 80 | + |
| 81 | + expect(result).toBe(newToken.access_token); |
| 82 | + expect(tokenRepository.get).toHaveBeenCalledWith('WAVE_MVNO_APPLICATION'); |
| 83 | + expect(global.fetch).toHaveBeenCalledWith( |
| 84 | + 'https://auth.wave.com/oauth/token', |
| 85 | + expect.objectContaining({ |
| 86 | + method: 'POST', |
| 87 | + headers: { |
| 88 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 89 | + 'Authorization': 'Basic Y2xpZW50SWQ6Y2xpZW50U2VjcmV0', |
| 90 | + }, |
| 91 | + }) |
| 92 | + ); |
| 93 | + expect(tokenRepository.save).toHaveBeenCalledWith( |
| 94 | + expect.objectContaining({ |
| 95 | + application: 'WAVE_MVNO_APPLICATION', |
| 96 | + token: newToken.access_token, |
| 97 | + }), |
| 98 | + expect.anything() |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle non-JSON error responses with status 401', async () => { |
| 103 | + const { sut, tokenRepository, transactionManager } = makeSut(); |
| 104 | + |
| 105 | + tokenRepository.get.mockResolvedValueOnce(undefined); |
| 106 | + mockStatus.mockReturnValueOnce(401); |
| 107 | + mockText.mockReturnValueOnce('Unauthorized'); |
| 108 | + mockHeaders.mockReturnValueOnce('text/plain'); |
| 109 | + transactionManager.execute.mockImplementationOnce( |
| 110 | + (callback) => callback({} as PrismaTransaction) |
| 111 | + ); |
| 112 | + |
| 113 | + await expect(sut.authenticate('WAVE_MVNO_APPLICATION')).rejects.toThrow('Unauthorized'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should handle JSON error responses with status 400', async () => { |
| 117 | + const { sut, tokenRepository, transactionManager } = makeSut(); |
| 118 | + |
| 119 | + tokenRepository.get.mockResolvedValueOnce(undefined); |
| 120 | + mockStatus.mockReturnValueOnce(400); |
| 121 | + mockJson.mockReturnValueOnce({ error: 'invalid_request' }); |
| 122 | + mockHeaders.mockReturnValueOnce('application/json'); |
| 123 | + transactionManager.execute.mockImplementationOnce( |
| 124 | + (callback) => callback({} as PrismaTransaction) |
| 125 | + ); |
| 126 | + |
| 127 | + await expect(sut.authenticate('WAVE_MVNO_APPLICATION')).rejects.toThrow('{"error":"invalid_request"}'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should use default expiry when token response has no expiresIn', async () => { |
| 131 | + const { sut, tokenRepository, transactionManager } = makeSut(); |
| 132 | + const pastTimestamp = Date.now() - 1000; |
| 133 | + const mockToken = { token: 'expired-token', expiresAt: pastTimestamp, application: 'WAVE_MVNO_APPLICATION' as ApplicationName }; |
| 134 | + const newToken = { access_token: 'new-token', expires_in: 3600, application: 'WAVE_MVNO_APPLICATION' as ApplicationName }; // No expiresIn |
| 135 | + |
| 136 | + tokenRepository.get.mockResolvedValueOnce(mockToken); |
| 137 | + mockJson.mockReturnValueOnce(newToken); |
| 138 | + mockStatus.mockReturnValueOnce(200); |
| 139 | + transactionManager.execute.mockImplementationOnce( |
| 140 | + (callback) => callback({} as PrismaTransaction) |
| 141 | + ); |
| 142 | + |
| 143 | + await sut.authenticate('WAVE_MVNO_APPLICATION'); |
| 144 | + |
| 145 | + expect(tokenRepository.save).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ |
| 147 | + application: 'WAVE_MVNO_APPLICATION', |
| 148 | + token: newToken.access_token, |
| 149 | + expiresAt: expect.any(Number), |
| 150 | + }), |
| 151 | + expect.anything() |
| 152 | + ); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments