Skip to content

Commit

Permalink
feat(payments): added sample test for routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dasanorct committed Mar 6, 2024
1 parent 754089a commit 1a2cdc7
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 16 deletions.
2 changes: 1 addition & 1 deletion processor/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ['./src/jest.setup.ts'],
roots: ['./src'],
roots: ['./test'],
};
10 changes: 5 additions & 5 deletions processor/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const config = {
loggerLevel: process.env.LOGGER_LEVEL || 'info',

// Payment Providers config
adyenEnvironment: process.env.ADYEN_ENVIRONMENT || '',
adyenClientKey: process.env.ADYEN_CLIENT_KEY || '',
adyenApiKey: process.env.ADYEN_API_KEY || '',
adyenHMACKey: process.env.ADYEN_NOTIFICATION_HMAC_KEY || '',
adyenEnvironment: process.env.ADYEN_ENVIRONMENT || 'TEST',
adyenClientKey: process.env.ADYEN_CLIENT_KEY || 'adyenClientKey',
adyenApiKey: process.env.ADYEN_API_KEY || 'adyenApiKey',
adyenHMACKey: process.env.ADYEN_NOTIFICATION_HMAC_KEY || 'adyenHMACKey',
adyenLiveUrlPrefix: process.env.ADYEN_LIVE_URL_PREFIX || '',
adyenMerchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT || '',
adyenMerchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT || 'adyenMerchantAccount',

merchantReturnUrl: process.env.MERCHANT_RETURN_URL || '',
};
168 changes: 168 additions & 0 deletions processor/test/routes.test/operations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import fastify from 'fastify';
import { describe, beforeAll, afterAll, test, expect } from '@jest/globals';
import {
AuthorityAuthorizationHook,
AuthorityAuthorizationManager,
CommercetoolsCartService,
CommercetoolsPaymentService,
ContextProvider,
JWTAuthenticationHook,
JWTAuthenticationManager,
Oauth2AuthenticationHook,
Oauth2AuthenticationManager,
RequestContextData,
SessionAuthenticationHook,
SessionAuthenticationManager,
} from '@commercetools/connect-payments-sdk';
import { IncomingHttpHeaders } from 'node:http';
import { operationsRoute } from '../../src/routes/operation.route';
import { AdyenPaymentService } from '../../src/services/adyen-payment.service';

describe('/operations APIs', () => {
const app = fastify({ logger: false });
const token = 'token';
const jwtToken = 'jwtToken';
const sessionId = 'session-id';

const spyAuthenticateJWT = jest
.spyOn(JWTAuthenticationHook.prototype, 'authenticate')
.mockImplementationOnce(() => async (request: { headers: IncomingHttpHeaders }) => {
expect(request.headers['authorization']).toContain(`Bearer ${jwtToken}`);
});

const spyAuthenticateOauth2 = jest
.spyOn(Oauth2AuthenticationHook.prototype, 'authenticate')
.mockImplementationOnce(() => async (request: { headers: IncomingHttpHeaders }) => {
expect(request.headers['authorization']).toContain(`Bearer ${token}`);
});

const spyAuthenticateSession = jest
.spyOn(SessionAuthenticationHook.prototype, 'authenticate')
.mockImplementationOnce(() => async (request: { headers: IncomingHttpHeaders }) => {
expect(request.headers['x-session-id']).toContain('session-id');
});

const spiedJwtAuthenticationHook = new JWTAuthenticationHook({
authenticationManager: jest.fn() as unknown as JWTAuthenticationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
});

const spiedOauth2AuthenticationHook = new Oauth2AuthenticationHook({
authenticationManager: jest.fn() as unknown as Oauth2AuthenticationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
});

const spiedSessionAuthenticationHook = new SessionAuthenticationHook({
authenticationManager: jest.fn() as unknown as SessionAuthenticationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
});

const spiedAuthorityAuthorizationHook = new AuthorityAuthorizationHook({
authorizationManager: jest.fn() as unknown as AuthorityAuthorizationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
});

const spiedPaymentService = new AdyenPaymentService({
ctCartService: jest.fn() as unknown as CommercetoolsCartService,
ctPaymentService: jest.fn() as unknown as CommercetoolsPaymentService,
});

beforeAll(async () => {
await app.register(operationsRoute, {
prefix: '/operations',
oauth2AuthHook: spiedOauth2AuthenticationHook,
jwtAuthHook: spiedJwtAuthenticationHook,
sessionAuthHook: spiedSessionAuthenticationHook,
authorizationHook: spiedAuthorityAuthorizationHook,
paymentService: spiedPaymentService,
});
});

afterEach(async () => {
spyAuthenticateJWT.mockClear();
spyAuthenticateOauth2.mockClear();
spyAuthenticateSession.mockClear();
await app.ready();
});

afterAll(async () => {
await app.close();
});

describe('GET /operations/config', () => {
test('it should return the Adyen client config', async () => {
//When
const responseGetConfig = await app.inject({
method: 'GET',
url: `/operations/config`,
headers: {
'x-session-id': sessionId,
'content-type': 'application/json',
},
});

//Then
expect(responseGetConfig.statusCode).toEqual(200);
expect(responseGetConfig.json()).toEqual({
clientKey: 'adyenClientKey',
environment: 'TEST',
});
});

describe('GET /operations/status', () => {
test('it should return the status of the connector', async () => {
//Given
jest.spyOn(spiedPaymentService, 'status').mockResolvedValue({
metadata: {
name: 'payment-integration-adyen',
description: 'Payment integration with Adyen',
},
version: '1.0.0',
timestamp: '2024-01-01T00:00:00Z',
status: 'UP',
checks: [
{
name: 'CoCo Permissions',
status: 'UP',
},
{
name: 'Adyen Status check',
status: 'UP',
},
],
});

//When
const responseGetStatus = await app.inject({
method: 'GET',
url: `/operations/status`,
headers: {
authorization: `Bearer ${jwtToken}`,
'content-type': 'application/json',
},
});

//Then
expect(responseGetStatus.statusCode).toEqual(200);
expect(responseGetStatus.json()).toEqual(
expect.objectContaining({
metadata: expect.any(Object),
status: 'UP',
timestamp: expect.any(String),
version: '1.0.0',
checks: expect.arrayContaining([
expect.objectContaining({
name: 'CoCo Permissions',
status: 'UP',
}),
expect.objectContaining({
name: 'Adyen Status check',
status: 'UP',
}),
]),
}),
);
});
});
});
});
10 changes: 0 additions & 10 deletions processor/test/sample.spec.ts

This file was deleted.

0 comments on commit 1a2cdc7

Please sign in to comment.