pcp-server-nodejs-sdk
Version:
[](https://sonarcloud.io/summary/new_code?id=PAYONE-GmbH_PCP-server-nodeJS-SDK) [;
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