UNPKG

pcp-server-nodejs-sdk

Version:

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=PAYONE-GmbH_PCP-server-nodeJS-SDK&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=PAYONE-GmbH_PCP-server-nodeJS-SDK) [![Coverage](https://sonarcloud.io/api/pr

58 lines 2.61 kB
import fetch, { Headers } from 'node-fetch'; import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import { CommunicatorConfiguration } from '../CommunicatorConfiguration.js'; import { createResponseMock } from '../testutils/mock-response.js'; import { AuthenticationApiClient } from './AuthenticationApiClient.js'; const mockedFetch = vi.mocked(fetch, true); vi.mock('node-fetch', async (importOriginal) => { return { ...(await importOriginal()), default: vi.fn(), }; }); describe('AuthenticationApiClient', () => { let authenticationApiClient; beforeEach(() => { authenticationApiClient = new AuthenticationApiClient(new CommunicatorConfiguration('dummy-key', 'dummy-secret', 'https://api.testhost.com')); }); afterEach(() => { mockedFetch.mockReset(); }); test('should throw if merchantId is missing', async () => { await expect(authenticationApiClient.getAuthenticationTokens('')).rejects.toThrow('Merchant ID is required'); }); test('should return token for valid merchantId', async () => { const mockToken = { token: 'jwt-token', id: 'uuid-123', creationDate: new Date().toISOString(), expirationDate: new Date(Date.now() + 600000).toISOString(), }; mockedFetch.mockResolvedValueOnce(createResponseMock(200, mockToken)); const result = await authenticationApiClient.getAuthenticationTokens('merchant-123'); expect(result.token).toBe('jwt-token'); expect(result.id).toBe('uuid-123'); expect(result.creationDate).toBeDefined(); expect(result.expirationDate).toBeDefined(); }); test('should pass X-Request-ID header if provided', async () => { const mockToken = { token: 'jwt-token', id: 'uuid-123', creationDate: new Date().toISOString(), expirationDate: new Date(Date.now() + 600000).toISOString(), }; let receivedRequest; mockedFetch.mockImplementationOnce((url, req) => { receivedRequest = req; return Promise.resolve(createResponseMock(200, mockToken)); }); const requestId = 'request-id-123'; await authenticationApiClient.getAuthenticationTokens('merchant-123', requestId); // Check the Headers object for the header expect(receivedRequest?.headers instanceof Headers ? receivedRequest.headers.get('X-Request-ID') : undefined).toBe(requestId); }); }); //# sourceMappingURL=AuthenticationApiClient.test.js.map