Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
ACCS1-8
  • Loading branch information
Tamara committed Apr 1, 2024
1 parent fa1a4c5 commit 839f824
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 83 deletions.
3 changes: 2 additions & 1 deletion extension/test/integration/make-payment.handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion extension/test/integration/sessions-request.handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
189 changes: 108 additions & 81 deletions extension/test/unit/create-session-request.handler.spec.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(
Expand All @@ -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(() => {
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 839f824

Please sign in to comment.