Skip to content

Commit

Permalink
Merge pull request #50 from commercetools/kinghin.leung/add-test-cases
Browse files Browse the repository at this point in the history
chore(processor) - add unit test cases for adyen-payment service
  • Loading branch information
leungkinghin-ct authored Mar 21, 2024
2 parents 44339aa + 5318003 commit ee7c450
Show file tree
Hide file tree
Showing 15 changed files with 7,738 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ jobs:
run: npm run build

- name: Execute tests
run: npm run test
run: npm run test -- --coverage
3 changes: 3 additions & 0 deletions processor/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# test converage
coverage/*
2 changes: 1 addition & 1 deletion processor/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module.exports = {
testEnvironment: 'node',
setupFiles: ['./src/jest.setup.ts'],
roots: ['./test'],
};
};
6,909 changes: 6,906 additions & 3 deletions processor/package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"scripts": {
"start": "node dist/main.js",
"start:dev": "node_modules/.bin/nodemon -q dist/main.js",
"lint": "prettier --check \"src/**/*.{ts,js,json}\" && eslint --ext .ts src",
"lint:fix": "prettier --write \"src/**/*.{ts,js,json}\" && eslint --fix --ext .ts src",
"lint": "prettier --check \"**/**/*.{ts,js,json}\" && eslint --ext .ts src",
"lint:fix": "prettier --write \"**/**/*.{ts,js,json}\" && eslint --fix --ext .ts src",
"build": "rm -rf /dist && tsc",
"dev": "ts-node src/main.ts",
"test": "jest --detectOpenHandles",
"test:coverage": "jest --detectOpenHandles --coverage",
"connector:post-deploy": "node src/connectors/post-deploy.ts",
"connector:pre-undeploy": "node src/connectors/pre-undeploy.ts"
},
Expand Down Expand Up @@ -56,4 +57,4 @@
"ts-node": "10.9.2",
"typescript": "5.4.2"
}
}
}
3 changes: 3 additions & 0 deletions processor/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ export const config = {

merchantReturnUrl: process.env.MERCHANT_RETURN_URL || '',
};
export const getConfig = () => {
return config;
};
94 changes: 0 additions & 94 deletions processor/src/libs/fastify/error-handler.spec.ts

This file was deleted.

11 changes: 5 additions & 6 deletions processor/src/services/adyen-payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
RefundPaymentRequest,
StatusResponse,
} from './types/operation.type';
import { config } from '../config/config';
import { getConfig, config } from '../config/config';
import { paymentSDK } from '../payment-sdk';
import { PaymentModificationStatus } from '../dtos/operations/payment-intents.dto';
import { AbstractPaymentService } from './abstract-payment.service';
Expand Down Expand Up @@ -81,8 +81,8 @@ export class AdyenPaymentService extends AbstractPaymentService {
}
async config(): Promise<ConfigResponse> {
return {
clientKey: config.adyenClientKey,
environment: config.adyenEnvironment,
clientKey: getConfig().adyenClientKey,
environment: getConfig().adyenEnvironment,
};
}

Expand All @@ -103,15 +103,15 @@ export class AdyenPaymentService extends AbstractPaymentService {
return {
name: 'Adyen Status check',
status: 'UP',
data: {
details: {
paymentMethods: result.paymentMethods,
},
};
} catch (e) {
return {
name: 'Adyen Status check',
status: 'DOWN',
data: {
details: {
error: e,
},
};
Expand Down Expand Up @@ -240,7 +240,6 @@ export class AdyenPaymentService extends AbstractPaymentService {
paymentId: ctPayment.id,
});
}

const data = this.createPaymentConverter.convertRequest({
data: opts.data,
cart: ctCart,
Expand Down
166 changes: 166 additions & 0 deletions processor/test/libs/fastify/error-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { afterEach, beforeEach, describe, expect, test } from '@jest/globals';
import Fastify, { type FastifyInstance, FastifyError } from 'fastify';
import { errorHandler } from '../../../src/libs/fastify/error-handler';
import { ErrorAuthErrorResponse, Errorx, ErrorxAdditionalOpts } from '@commercetools/connect-payments-sdk';
import { requestContextPlugin } from '../../../src/libs/fastify/context/context';
import { FastifySchemaValidationError } from 'fastify/types/schema';

describe('error-handler', () => {
let fastify: FastifyInstance;
beforeEach(async () => {
fastify = Fastify();
fastify.setErrorHandler(errorHandler);
await fastify.register(requestContextPlugin);
});

afterEach(async () => {
await fastify.close();
});

test('errox error', async () => {
fastify.get('/', () => {
throw new Errorx({
code: 'ErrorCode',
message: 'someMessage',
httpErrorStatus: 404,
});
});

const response = await fastify.inject({
method: 'GET',
url: '/',
});

expect(response.json()).toStrictEqual({
message: 'someMessage',
statusCode: 404,
errors: [
{
code: 'ErrorCode',
message: 'someMessage',
},
],
});
});

test('errox with fields', async () => {
fastify.get('/', () => {
throw new Errorx({
code: 'ErrorCode',
message: 'someMessage',
httpErrorStatus: 404,
fields: {
test: 'field1',
},
});
});

const response = await fastify.inject({
method: 'GET',
url: '/',
});

expect(response.json()).toStrictEqual({
message: 'someMessage',
statusCode: 404,
errors: [
{
code: 'ErrorCode',
message: 'someMessage',
test: 'field1',
},
],
});
});

test('general error', async () => {
fastify.get('/', () => {
throw new Error('some message goes here');
});

const response = await fastify.inject({
method: 'GET',
url: '/',
});

expect(response.json()).toStrictEqual({
message: 'Internal server error.',
statusCode: 500,
errors: [
{
code: 'General',
message: 'Internal server error.',
},
],
});
});

test('Fastify error with missing required field', async () => {
const validationError: FastifySchemaValidationError = {
keyword: 'required',
instancePath: 'instancePath/domain/value',
schemaPath: 'schemaPath/domain/value',
params: {
missingProperty: 'dummy-field',
},
message: 'fastify-error-message',
};
const fastifyError: FastifyError = {
code: 'ErrorCode',
name: 'fastify-error',
message: 'fastify-error-message',
validation: [validationError],
};
fastify.get('/', () => {
throw fastifyError;
});

const response = await fastify.inject({
method: 'GET',
url: '/',
});
expect(response.json()).toStrictEqual({
message: 'A value is required for field dummy-field.',
statusCode: 400,
errors: [
{
code: 'RequiredField',
field: 'dummy-field',
message: 'A value is required for field dummy-field.',
},
],
});
});

test('ErrorAuthErrorResponse', async () => {
const opts: ErrorxAdditionalOpts = {
privateFields: {},
privateMessage: '',
fields: {},
skipLog: true,
cause: undefined,
};
const authErrorResponse: ErrorAuthErrorResponse = new ErrorAuthErrorResponse('someMessage', opts, '401');

fastify.get('/', () => {
throw authErrorResponse;
});

const response = await fastify.inject({
method: 'GET',
url: '/',
});
expect(response.json()).toStrictEqual({
message: 'someMessage',
statusCode: 401,
errors: [
{
code: '401',
message: 'someMessage',
},
],
error: '401',
error_description: 'someMessage',
});
});
});
9 changes: 9 additions & 0 deletions processor/test/sample.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, test, expect } from '@jest/globals';

describe('sample-test-suite', () => {
// Please customize test cases below
test('sample-test-case', async () => {
const result = {};
expect(result).toStrictEqual({});
});
});
Loading

0 comments on commit ee7c450

Please sign in to comment.