From 839f824954303663f4c0c862d9c14e60c8a741b2 Mon Sep 17 00:00:00 2001 From: Tamara Date: Mon, 1 Apr 2024 13:34:27 +0200 Subject: [PATCH] Add unit tests ACCS1-8 --- .../integration/make-payment.handler.spec.js | 3 +- .../sessions-request.handler.spec.js | 3 +- .../create-session-request.handler.spec.js | 189 +++++++----- .../test/unit/make-payment.handler.spec.js | 289 ++++++++++++++++++ 4 files changed, 401 insertions(+), 83 deletions(-) diff --git a/extension/test/integration/make-payment.handler.spec.js b/extension/test/integration/make-payment.handler.spec.js index d4bc656dc..2563f148f 100644 --- a/extension/test/integration/make-payment.handler.spec.js +++ b/extension/test/integration/make-payment.handler.spec.js @@ -46,7 +46,8 @@ describe('::make-payment with multiple adyen accounts use case::', () => { 'when makePaymentRequest contains some additional fields, ' + 'and cart contains fields billingAddress, shippingAddress, lineItems, customLineItems, ' + 'then fields from makePaymentRequest should remain unchanged,' + - 'and other fields from cart should be mapped to makePaymentRequest', + 'and other fields from cart should be mapped to makePaymentRequest' + + 'and a transaction is successfully authorized', async () => { const payment = await initPaymentWithCart({ ctpClient, diff --git a/extension/test/integration/sessions-request.handler.spec.js b/extension/test/integration/sessions-request.handler.spec.js index 8be4827fc..0699eb884 100644 --- a/extension/test/integration/sessions-request.handler.spec.js +++ b/extension/test/integration/sessions-request.handler.spec.js @@ -121,7 +121,8 @@ describe('::create-session-request::', () => { 'when createSessionRequest contains some additional fields, ' + 'and cart contains fields billingAddress, shippingAddress, lineItems, customLineItems, ' + 'then fields from createSessionRequest should remain unchanged,' + - 'and other fields from cart should be mapped to createSessionRequest', + 'and other fields from cart should be mapped to createSessionRequest' + + 'and a transaction is successfully authorized', async () => { const payment = await initPaymentWithCart({ ctpClient, diff --git a/extension/test/unit/create-session-request.handler.spec.js b/extension/test/unit/create-session-request.handler.spec.js index ce340cb01..a0f8e5cfe 100644 --- a/extension/test/unit/create-session-request.handler.spec.js +++ b/extension/test/unit/create-session-request.handler.spec.js @@ -1,5 +1,6 @@ import { expect } from 'chai' import nock from 'nock' +import _ from 'lodash' import c from '../../src/config/constants.js' import config from '../../src/config/config.js' import utils from '../../src/utils.js' @@ -15,10 +16,80 @@ describe('create-session-request::execute::', () => { let ctpCustomer let ctpCartWithNoData let scope - let getSessionRequest - let paymentObject - let getSessionRequestWithAdditionalFields - let paymentObjectWithAdditionalFields + + const getSessionRequest = { + countryCode: 'DE', + reference: 'UNIQUE_PAYMENT_REFERENCE', + amount: { + currency: 'EUR', + value: 1000, + }, + } + const paymentObject = { + amountPlanned: { + currencyCode: 'EUR', + centAmount: 1000, + }, + paymentMethodInfo: { + paymentInterface: c.CTP_ADYEN_INTEGRATION, + }, + interfaceInteractions: [], + custom: { + type: { + typeId: 'type', + key: c.CTP_PAYMENT_CUSTOM_TYPE_KEY, + }, + fields: { + commercetoolsProjectKey, + createSessionRequest: JSON.stringify(getSessionRequest), + adyenMerchantAccount, + }, + }, + } + + const getSessionRequestWithAdditionalFields = { + countryCode: 'DE', + reference: 'UNIQUE_PAYMENT_REFERENCE', + amount: { + currency: 'EUR', + value: 1000, + }, + dateOfBirth: '2000-08-08', + additionalData: { + enhancedSchemeData: { + destinationCountryCode: 'FR', + destinationPostalCode: '75001', + }, + }, + shopperName: { + firstName: 'Test', + lastName: 'Customer', + }, + } + + const paymentObjectWithAdditionalFields = { + amountPlanned: { + currencyCode: 'EUR', + centAmount: 1000, + }, + paymentMethodInfo: { + paymentInterface: c.CTP_ADYEN_INTEGRATION, + }, + interfaceInteractions: [], + custom: { + type: { + typeId: 'type', + key: c.CTP_PAYMENT_CUSTOM_TYPE_KEY, + }, + fields: { + commercetoolsProjectKey, + createSessionRequest: JSON.stringify( + getSessionRequestWithAdditionalFields, + ), + adyenMerchantAccount, + }, + }, + } before(async () => { ctpCart = await utils.readAndParseJsonFile( @@ -39,78 +110,6 @@ describe('create-session-request::execute::', () => { beforeEach(() => { const adyenConfig = config.getAdyenConfig(adyenMerchantAccount) scope = nock(`${adyenConfig.apiBaseUrl}`) - - getSessionRequest = { - countryCode: 'DE', - reference: 'UNIQUE_PAYMENT_REFERENCE', - amount: { - currency: 'EUR', - value: 1000, - }, - } - paymentObject = { - amountPlanned: { - currencyCode: 'EUR', - centAmount: 1000, - }, - paymentMethodInfo: { - paymentInterface: c.CTP_ADYEN_INTEGRATION, - }, - interfaceInteractions: [], - custom: { - type: { - typeId: 'type', - key: c.CTP_PAYMENT_CUSTOM_TYPE_KEY, - }, - fields: { - commercetoolsProjectKey, - createSessionRequest: JSON.stringify(getSessionRequest), - adyenMerchantAccount, - }, - }, - } - getSessionRequestWithAdditionalFields = { - countryCode: 'DE', - reference: 'UNIQUE_PAYMENT_REFERENCE', - amount: { - currency: 'EUR', - value: 1000, - }, - dateOfBirth: '2000-08-08', - additionalData: { - enhancedSchemeData: { - destinationCountryCode: 'FR', - destinationPostalCode: '75001', - }, - }, - shopperName: { - firstName: 'Test', - lastName: 'Customer', - }, - } - paymentObjectWithAdditionalFields = { - amountPlanned: { - currencyCode: 'EUR', - centAmount: 1000, - }, - paymentMethodInfo: { - paymentInterface: c.CTP_ADYEN_INTEGRATION, - }, - interfaceInteractions: [], - custom: { - type: { - typeId: 'type', - key: c.CTP_PAYMENT_CUSTOM_TYPE_KEY, - }, - fields: { - commercetoolsProjectKey, - createSessionRequest: JSON.stringify( - getSessionRequestWithAdditionalFields, - ), - adyenMerchantAccount, - }, - }, - } }) afterEach(() => { @@ -133,8 +132,13 @@ describe('create-session-request::execute::', () => { mockCtpEnpoints._mockCtpCartsEndpoint(ctpCart, commercetoolsProjectKey) scope.post('/sessions').query(true).reply(200, adyenGetSessionResponse) + const getSessionRequestClone = _.cloneDeep(getSessionRequest) + const paymentObjectClone = _.cloneDeep(paymentObject) + paymentObjectClone.custom.fields.makePaymentRequest = JSON.stringify( + getSessionRequestClone, + ) const result = - await createSessionRequestPaymentHandler.execute(paymentObject) + await createSessionRequestPaymentHandler.execute(paymentObjectClone) expect(result.actions.length).to.equal(3) expect(result.actions[0].action).to.equal('addInterfaceInteraction') @@ -164,8 +168,13 @@ describe('create-session-request::execute::', () => { mockCtpEnpoints._mockCtpCartsEndpoint(ctpCart, commercetoolsProjectKey) scope.post('/sessions').query(true).replyWithError(errorMsg) + const getSessionRequestClone = _.cloneDeep(getSessionRequest) + const paymentObjectClone = _.cloneDeep(paymentObject) + paymentObjectClone.custom.fields.makePaymentRequest = JSON.stringify( + getSessionRequestClone, + ) const result = - await createSessionRequestPaymentHandler.execute(paymentObject) + await createSessionRequestPaymentHandler.execute(paymentObjectClone) expect(result.actions.length).to.equal(3) expect(result.actions[0].action).to.equal('addInterfaceInteraction') @@ -198,9 +207,17 @@ describe('create-session-request::execute::', () => { ) scope.post('/sessions').reply(200, createSessionSuccessResponse) - const response = await createSessionRequestPaymentHandler.execute( + const getSessionRequestWithAdditionalFieldsClone = _.cloneDeep( + getSessionRequestWithAdditionalFields, + ) + const paymentObjectWithAdditionalFieldsClone = _.cloneDeep( paymentObjectWithAdditionalFields, ) + paymentObjectWithAdditionalFieldsClone.custom.fields.makePaymentRequest = + JSON.stringify(getSessionRequestWithAdditionalFieldsClone) + const response = await createSessionRequestPaymentHandler.execute( + paymentObjectWithAdditionalFieldsClone, + ) expect(response.actions).to.have.lengthOf(3) const createSessionRequestInteraction = JSON.parse( @@ -287,8 +304,13 @@ describe('create-session-request::execute::', () => { ) scope.post('/sessions').reply(200, createSessionSuccessResponse) + const getSessionRequestClone = _.cloneDeep(getSessionRequest) + const paymentObjectClone = _.cloneDeep(paymentObject) + paymentObjectClone.custom.fields.makePaymentRequest = JSON.stringify( + getSessionRequestClone, + ) const response = - await createSessionRequestPaymentHandler.execute(paymentObject) + await createSessionRequestPaymentHandler.execute(paymentObjectClone) expect(response.actions).to.have.lengthOf(3) const createSessionRequestInteraction = JSON.parse( @@ -353,8 +375,13 @@ describe('create-session-request::execute::', () => { ) scope.post('/sessions').reply(200, createSessionSuccessResponse) + const getSessionRequestClone = _.cloneDeep(getSessionRequest) + const paymentObjectClone = _.cloneDeep(paymentObject) + paymentObjectClone.custom.fields.makePaymentRequest = JSON.stringify( + getSessionRequestClone, + ) const response = - await createSessionRequestPaymentHandler.execute(paymentObject) + await createSessionRequestPaymentHandler.execute(paymentObjectClone) expect(response.actions).to.have.lengthOf(3) const createSessionRequestInteraction = JSON.parse( diff --git a/extension/test/unit/make-payment.handler.spec.js b/extension/test/unit/make-payment.handler.spec.js index 4cb70b189..f4c2cba9e 100644 --- a/extension/test/unit/make-payment.handler.spec.js +++ b/extension/test/unit/make-payment.handler.spec.js @@ -17,6 +17,9 @@ describe('make-payment::execute', () => { let scope let ctpCart let ctpPayment + let ctpCartWithCustomer + let ctpCustomer + let ctpCartWithNoData /* eslint-disable max-len */ const makePaymentRequest = { @@ -52,6 +55,51 @@ describe('make-payment::execute', () => { value: 1000, }, } + const makePaymentRequestWithAdditionalFields = { + reference: 'YOUR_REFERENCE', + riskData: { + clientData: + 'eyJ2ZXJzaW9uIjoiMS4wLjAiLCJkZXZpY2VGaW5nZXJwcmludCI6ImRmLXRpbWVkT3V0In0=', + }, + paymentMethod: { + type: 'scheme', + encryptedCardNumber: + 'adyenjs_0_1_25$DUEzRRqi0giNphYds8B1/AhDcp1Mx9fM6uFW/fxx7HcO2vd8Lt/tT0IgODmu16duCco+vnB+HFJVV5t3m6yD93AZxA/ugFU7uzggh7UNAKPd3khkpReBRoHSzLwyj9dRnOxTDYWRX+K/3ozT+9RXvfHoPL1nWhU6A0DIKAdjiurDRHZA657XNZKd2M090R1yhqmIGH7rkHNJ3yV7/Ox/qTi5KLB4TmiEpGKD/sbuy18hG69om+66+BttPglSwPFZIy8zNXuqetYQaLY+cVlYfdKcRgEvoKJay85AtZgbxGvpp6pB+AXzIR55HM+KYykxHcHc/7O6KRMWjoxOkd9vpA==$FkH2iICmazYvxCLQ9Cu8zmFHfoXwcCZ/tMGuPyulvuyRar9Z5SMAx9GyTQvSHzTYNhglA4wQJNoEP9Vt9hnOUWdonWLMbLBDt4QhuB6c2kGcGRU7As6cBkDqllTNj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y5zcIcLDVw4L73CZeRjBZyyGGTeOFV4zAr0UAYKhjQbWHQ7d/SyZuRSZGk2NL6u/eY8bf5ogSHnkYSRtVJYd95WCBhpiZR1AWrjpZBJtDqh2q53zHZuWtwmIJiGO7lZZdDdc/TpbOsLpJWcOXq6UYPiwyfIuE8U6xKyLTumkHNOBv8vDHT2PRlyvufUcrp7XthYBpHJqADjD0jvb8Kn80tL7/RDmbCtTUzHq1GFlzB25nF4JErDPgUZ+BiSI7SlMGmeJdoy9RKP2DIKZEEiw1/w3dA/4ea5D7hZ3D53LKBJTEO7uyqTJI+/M1NuAKSeeFLZE9X95+AFpx3RDGKCG380PAH', + }, + browserInfo: { + acceptHeader: '*/*', + colorDepth: 24, + language: 'en-US', + javaEnabled: false, + screenHeight: 1050, + screenWidth: 1680, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36', + timeZoneOffset: -120, + }, + amount: { + currency: 'EUR', + value: 1000, + }, + countryCode: 'DE', + dateOfBirth: '2000-08-08', + additionalData: { + enhancedSchemeData: { + destinationCountryCode: 'FR', + destinationPostalCode: '75001', + }, + }, + shopperName: { + firstName: 'Test', + lastName: 'Customer', + }, + } const adyenMerchantAccount = config.getAllAdyenMerchantAccounts()[0] const commercetoolsProjectKey = config.getAllCtpProjectKeys()[0] @@ -62,6 +110,15 @@ describe('make-payment::execute', () => { ctpCart = await utils.readAndParseJsonFile( 'test/unit/fixtures/ctp-cart.json', ) + ctpCartWithCustomer = await utils.readAndParseJsonFile( + 'test/unit/fixtures/ctp-cart-with-customer.json', + ) + ctpCustomer = await utils.readAndParseJsonFile( + 'test/unit/fixtures/ctp-customer.json', + ) + ctpCartWithNoData = await utils.readAndParseJsonFile( + 'test/unit/fixtures/ctp-cart-no-data.json', + ) }) beforeEach(() => { @@ -442,4 +499,236 @@ describe('make-payment::execute', () => { expect(setMethodInfoMethod).to.be.undefined }, ) + + it( + 'when makePaymentRequest contains some additional fields, ' + + 'and cart contains fields like billingAddress, shippingAddress, lineItems, customLineItems, ' + + 'then fields from makePaymentRequest should remain unchanged,' + + 'and other fields from cart should be mapped to makePaymentRequest', + async () => { + mockCtpEnpoints._mockCtpCustomerEndpoint( + ctpCustomer, + commercetoolsProjectKey, + ) + mockCtpEnpoints._mockCtpCartsEndpoint( + ctpCartWithCustomer, + commercetoolsProjectKey, + ) + scope.post('/payments').reply(200, paymentSuccessResponse) + + const makePaymentRequestClone = _.cloneDeep( + makePaymentRequestWithAdditionalFields, + ) + const ctpPaymentClone = _.cloneDeep(ctpPayment) + ctpPaymentClone.custom.fields.makePaymentRequest = JSON.stringify( + makePaymentRequestClone, + ) + ctpPaymentClone.custom.fields.adyenMerchantAccount = adyenMerchantAccount + ctpPaymentClone.custom.fields.commercetoolsProjectKey = + commercetoolsProjectKey + + const response = await makePaymentHandler.execute(ctpPaymentClone) + + expect(response.actions).to.have.lengthOf(6) + const makePaymentRequestInteraction = JSON.parse( + response.actions.find((a) => a.action === 'addInterfaceInteraction') + .fields.request, + ) + const makePaymentRequestJson = JSON.parse( + makePaymentRequestInteraction.body, + ) + + expect(makePaymentRequestJson.countryCode).to.equal( + makePaymentRequestClone.countryCode, + ) + expect(makePaymentRequestJson.dateOfBirth).to.equal( + makePaymentRequestClone.dateOfBirth, + ) + expect( + makePaymentRequestJson.additionalData.enhancedSchemeData + .destinationCountryCode, + ).to.equal( + makePaymentRequestClone.additionalData.enhancedSchemeData + .destinationCountryCode, + ) + expect( + makePaymentRequestJson.additionalData.enhancedSchemeData + .destinationPostalCode, + ).to.equal( + makePaymentRequestClone.additionalData.enhancedSchemeData + .destinationPostalCode, + ) + expect(makePaymentRequestJson.shopperName.firstName).to.equal( + makePaymentRequestClone.shopperName.firstName, + ) + expect(makePaymentRequestJson.shopperName.lastName).to.equal( + makePaymentRequestClone.shopperName.lastName, + ) + + expect(makePaymentRequestJson.billingAddress.street).to.equal( + ctpCartWithCustomer.billingAddress.streetName, + ) + expect(makePaymentRequestJson.billingAddress.houseNumberOrName).to.equal( + ctpCartWithCustomer.billingAddress.streetNumber, + ) + expect(makePaymentRequestJson.billingAddress.city).to.equal( + ctpCartWithCustomer.billingAddress.city, + ) + expect(makePaymentRequestJson.billingAddress.postalCode).to.equal( + ctpCartWithCustomer.billingAddress.postalCode, + ) + expect(makePaymentRequestJson.billingAddress.country).to.equal( + ctpCartWithCustomer.billingAddress.country, + ) + expect(makePaymentRequestJson.shopperEmail).to.equal( + ctpCartWithCustomer.customerEmail, + ) + + expect(makePaymentRequestJson.accountInfo.accountCreationDate).to.equal( + ctpCustomer.createdAt, + ) + expect(makePaymentRequestJson.accountInfo.accountChangeDate).to.equal( + ctpCustomer.lastModifiedAt, + ) + expect(makePaymentRequestJson.accountInfo.accountAgeIndicator).to.equal( + 'moreThan60Days', + ) + expect( + makePaymentRequestJson.accountInfo.accountChangeIndicator, + ).to.equal('moreThan60Days') + }, + ) + + it( + "when makePaymentRequest doesn't contain any additional field, " + + 'and cart contains fields like billingAddress, shippingAddress, lineItems, customLineItems, ' + + 'then fields from cart should be mapped to makePaymentRequest', + async () => { + mockCtpEnpoints._mockCtpCustomerEndpoint( + ctpCustomer, + commercetoolsProjectKey, + ) + mockCtpEnpoints._mockCtpCartsEndpoint( + ctpCartWithCustomer, + commercetoolsProjectKey, + ) + scope.post('/payments').reply(200, paymentSuccessResponse) + + const makePaymentRequestClone = _.cloneDeep(makePaymentRequest) + const ctpPaymentClone = _.cloneDeep(ctpPayment) + ctpPaymentClone.custom.fields.makePaymentRequest = JSON.stringify( + makePaymentRequestClone, + ) + ctpPaymentClone.custom.fields.adyenMerchantAccount = adyenMerchantAccount + ctpPaymentClone.custom.fields.commercetoolsProjectKey = + commercetoolsProjectKey + + const response = await makePaymentHandler.execute(ctpPaymentClone) + + expect(response.actions).to.have.lengthOf(6) + const makePaymentRequestInteraction = JSON.parse( + response.actions.find((a) => a.action === 'addInterfaceInteraction') + .fields.request, + ) + const makePaymentRequestJson = JSON.parse( + makePaymentRequestInteraction.body, + ) + + expect(makePaymentRequestJson.billingAddress.street).to.equal( + ctpCartWithCustomer.billingAddress.streetName, + ) + expect(makePaymentRequestJson.billingAddress.houseNumberOrName).to.equal( + ctpCartWithCustomer.billingAddress.streetNumber, + ) + expect(makePaymentRequestJson.billingAddress.city).to.equal( + ctpCartWithCustomer.billingAddress.city, + ) + expect(makePaymentRequestJson.billingAddress.postalCode).to.equal( + ctpCartWithCustomer.billingAddress.postalCode, + ) + expect(makePaymentRequestJson.billingAddress.country).to.equal( + ctpCartWithCustomer.billingAddress.country, + ) + expect(makePaymentRequestJson.shopperEmail).to.equal( + ctpCartWithCustomer.customerEmail, + ) + expect( + makePaymentRequestJson.additionalData.enhancedSchemeData + .destinationCountryCode, + ).to.equal(ctpCartWithCustomer.shippingAddress.country) + expect( + makePaymentRequestJson.additionalData.enhancedSchemeData + .destinationPostalCode, + ).to.equal(ctpCartWithCustomer.shippingAddress.postalCode) + + expect(makePaymentRequestJson.dateOfBirth).to.equal( + ctpCustomer.dateOfBirth, + ) + expect(makePaymentRequestJson.accountInfo.accountCreationDate).to.equal( + ctpCustomer.createdAt, + ) + expect(makePaymentRequestJson.accountInfo.accountChangeDate).to.equal( + ctpCustomer.lastModifiedAt, + ) + expect(makePaymentRequestJson.accountInfo.accountAgeIndicator).to.equal( + 'moreThan60Days', + ) + expect( + makePaymentRequestJson.accountInfo.accountChangeIndicator, + ).to.equal('moreThan60Days') + expect(makePaymentRequestJson.shopperName.firstName).to.equal( + ctpCustomer.firstName, + ) + expect(makePaymentRequestJson.shopperName.lastName).to.equal( + ctpCustomer.lastName, + ) + }, + ) + + it( + "when makePaymentRequest doesn't contain any additional fields, " + + "and cart also doesn't contain any of fields like billingAddress, shippingAddress, lineItems, customLineItems, " + + 'then makePaymentRequest will not have any additional fields', + async () => { + mockCtpEnpoints._mockCtpCartsEndpoint( + ctpCartWithNoData, + commercetoolsProjectKey, + ) + scope.post('/payments').reply(200, paymentSuccessResponse) + + const makePaymentRequestClone = _.cloneDeep(makePaymentRequest) + const ctpPaymentClone = _.cloneDeep(ctpPayment) + ctpPaymentClone.custom.fields.makePaymentRequest = JSON.stringify( + makePaymentRequestClone, + ) + ctpPaymentClone.custom.fields.adyenMerchantAccount = adyenMerchantAccount + ctpPaymentClone.custom.fields.commercetoolsProjectKey = + commercetoolsProjectKey + + const response = await makePaymentHandler.execute(ctpPaymentClone) + + expect(response.actions).to.have.lengthOf(6) + const makePaymentRequestInteraction = JSON.parse( + response.actions.find((a) => a.action === 'addInterfaceInteraction') + .fields.request, + ) + const makePaymentRequestJson = JSON.parse( + makePaymentRequestInteraction.body, + ) + + expect(makePaymentRequestJson.lineItems).to.have.lengthOf(0) + expect(makePaymentRequestJson.billingAddress).to.be.empty + expect(makePaymentRequestJson).to.not.have.own.property('dateOfBirth') + expect(makePaymentRequestJson).to.not.have.own.property('shopperEmail') + expect(makePaymentRequestJson).to.not.have.own.property('accountInfo') + expect(makePaymentRequestJson).to.not.have.own.property('shopperName') + expect(makePaymentRequestJson).to.not.have.own.property('shopperLocale') + expect( + makePaymentRequestJson.additionalData.enhancedSchemeData, + ).to.not.have.own.property('destinationCountryCode') + expect( + makePaymentRequestJson.additionalData.enhancedSchemeData, + ).to.not.have.own.property('postalCode') + }, + ) })