@@ -2,13 +2,14 @@ import nock from "nock";
22import { createClient } from "../../__mocks__/base" ;
33import { createCloudDeviceAPIRefundRequest , createCloudDeviceApiRequest } from "../../__mocks__/cloudDeviceApi/baseCloudDeviceApi" ;
44import { asyncResponseError } from "../../__mocks__/cloudDeviceApi/async" ;
5- import { syncResponse , syncResponseEventNotificationWithAdditionalAttributes , syncResponseEventNotificationWithUnknownEnum , syncResponseRefund } from "../../__mocks__/cloudDeviceApi/sync" ;
5+ import { syncEncryptedResponse , syncResponse , syncResponseEventNotificationWithAdditionalAttributes , syncResponseEventNotificationWithUnknownEnum , syncResponseRefund } from "../../__mocks__/cloudDeviceApi/sync" ;
66import { connectedDevicesResponse , deviceStatusResponse } from "../../__mocks__/cloudDeviceApi/devices" ;
77
88import Client from "../../client" ;
9- import CloudDeviceAPI , { CLOUD_DEVICE_API_VERSION } from "../../services/cloudDevice/cloudDeviceApi" ;
9+ import CloudDeviceAPI , { CLOUD_DEVICE_API_VERSION , CloudDeviceApiError } from "../../services/cloudDevice/cloudDeviceApi" ;
1010import { cloudDevice } from "../../typings" ;
1111import { EncryptionCredentialDetails } from "../../security/encryptionCredentialDetails" ;
12+ import { MessageType } from "../../typings/terminal/messageType" ;
1213
1314
1415let client : Client ;
@@ -37,9 +38,9 @@ describe("Cloud device API", (): void => {
3738
3839 const merchantAccount = "TestMerchantAccount" ;
3940 const deviceId = "P400Plus-123456789" ;
40- const response = await cloudDeviceAPI . async ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
41+ const response = await cloudDeviceAPI . sendAsync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
4142
42- // verify deviceId is set
43+ // verify deviceId is set on request
4344 expect ( cloudDeviceApiRequest . SaleToPOIRequest . MessageHeader . POIID ) . toBe ( deviceId ) ;
4445 // verify response
4546 expect ( typeof response ) . toBe ( "string" ) ;
@@ -54,9 +55,9 @@ describe("Cloud device API", (): void => {
5455
5556 const merchantAccount = "TestMerchantAccount" ;
5657 const deviceId = "P400Plus-123456789" ;
57- const response = await cloudDeviceAPI . async ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
58+ const response = await cloudDeviceAPI . sendAsync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
5859
59- // verify deviceId is set
60+ // verify deviceId is set on request
6061 expect ( cloudDeviceApiRequest . SaleToPOIRequest . MessageHeader . POIID ) . toBe ( deviceId ) ;
6162 // verify response
6263 if ( typeof response === "object" ) {
@@ -74,9 +75,9 @@ describe("Cloud device API", (): void => {
7475
7576 const merchantAccount = "TestMerchantAccount" ;
7677 const deviceId = "P400Plus-123456789" ;
77- const response = await cloudDeviceAPI . sync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
78+ const response = await cloudDeviceAPI . sendSync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
7879
79- // verify deviceId is set
80+ // verify deviceId is set on request
8081 expect ( cloudDeviceApiRequest . SaleToPOIRequest . MessageHeader . POIID ) . toBe ( deviceId ) ;
8182
8283 expect ( response . SaleToPOIResponse ) . toBeDefined ( ) ;
@@ -93,9 +94,9 @@ describe("Cloud device API", (): void => {
9394 await expect ( async ( ) => {
9495 const merchantAccount = "TestMerchantAccount" ;
9596 const deviceId = "P400Plus-123456789" ;
96- const response = await cloudDeviceAPI . sync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
97+ const response = await cloudDeviceAPI . sendSync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
9798
98- // verify deviceId is set
99+ // verify deviceId is set on request
99100 expect ( cloudDeviceApiRequest . SaleToPOIRequest . MessageHeader . POIID ) . toBe ( deviceId ) ;
100101
101102 expect ( response . SaleToPOIRequest ?. EventNotification ) . toBeDefined ( ) ;
@@ -112,9 +113,9 @@ describe("Cloud device API", (): void => {
112113 await expect ( async ( ) => {
113114 const merchantAccount = "TestMerchantAccount" ;
114115 const deviceId = "P400Plus-123456789" ;
115- const response = await cloudDeviceAPI . sync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
116+ const response = await cloudDeviceAPI . sendSync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
116117
117- // verify deviceId is set
118+ // verify deviceId is set on request
118119 expect ( cloudDeviceApiRequest . SaleToPOIRequest . MessageHeader . POIID ) . toBe ( deviceId ) ;
119120
120121 expect ( response . SaleToPOIRequest ?. EventNotification ) . toBeDefined ( ) ;
@@ -142,9 +143,9 @@ describe("Cloud device API", (): void => {
142143
143144 const merchantAccount = "TestMerchantAccount" ;
144145 const deviceId = "P400Plus-123456789" ;
145- const response = await cloudDeviceAPI . sync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
146+ const response = await cloudDeviceAPI . sendSync ( merchantAccount , deviceId , cloudDeviceApiRequest ) ;
146147
147- // verify deviceId is set
148+ // verify deviceId is set on request
148149 expect ( cloudDeviceApiRequest . SaleToPOIRequest . MessageHeader . POIID ) . toBe ( deviceId ) ;
149150
150151 expect ( response . SaleToPOIResponse ?. ReversalResponse ?. Response . Result ) . toBe ( "Success" ) ;
@@ -184,9 +185,29 @@ describe("Cloud device API", (): void => {
184185 } ) ;
185186
186187 test ( "should make an encrypted async request" , async ( ) : Promise < void > => {
187- scope . post ( "/" ) . reply ( 200 , "ok" ) ;
188+ scope . post ( "/merchants/TestMerchantAccount/devices/P400Plus-123456789/async " ) . reply ( 200 , "ok" ) ;
188189 const merchantAccount = "TestMerchantAccount" ;
189- const deviceId = "AMS1-000168242800763" ;
190+ const deviceId = "P400Plus-123456789" ;
191+
192+ const cloudDeviceApiSecuredRequest = createCloudDeviceApiRequest ( ) ;
193+
194+ const encryptionCredentialDetails : EncryptionCredentialDetails = {
195+ AdyenCryptoVersion : 0 ,
196+ KeyIdentifier : "CryptoKeyIdentifier12345" ,
197+ KeyVersion : 0 ,
198+ Passphrase : "p@ssw0rd123456" ,
199+ } ;
200+
201+ const response = await cloudDeviceAPI . sendEncryptedAsync ( merchantAccount , deviceId , cloudDeviceApiSecuredRequest , encryptionCredentialDetails ) ;
202+
203+ expect ( response ) . toBeDefined ( ) ;
204+ expect ( response ) . toEqual ( "ok" ) ;
205+ } ) ;
206+
207+ test ( "should make an encrypted sync request" , async ( ) : Promise < void > => {
208+ scope . post ( "/merchants/TestMerchantAccount/devices/MX915-284251016/sync" ) . reply ( 200 , syncEncryptedResponse ) ;
209+ const merchantAccount = "TestMerchantAccount" ;
210+ const deviceId = "MX915-284251016" ;
190211
191212 const cloudDeviceApiSecuredRequest = createCloudDeviceApiRequest ( ) ;
192213
@@ -197,11 +218,56 @@ describe("Cloud device API", (): void => {
197218 Passphrase : "p@ssw0rd123456" ,
198219 } ;
199220
200- const response = await cloudDeviceAPI . asyncEncrypted ( merchantAccount , deviceId , cloudDeviceApiSecuredRequest , encryptionCredentialDetails ) ;
221+ const response = await cloudDeviceAPI . sendEncryptedSync ( merchantAccount , deviceId , cloudDeviceApiSecuredRequest , encryptionCredentialDetails ) ;
201222
202223 expect ( response ) . toBeDefined ( ) ;
203- // expect(response.SaleToPOIResponse?.PaymentResponse).toBeDefined();
204- // expect(response.SaleToPOIResponse?.MessageHeader).toBeDefined();
224+ expect ( response . SaleToPOIResponse ?. MessageHeader ) . toBeDefined ( ) ;
225+ expect ( response . SaleToPOIResponse ?. MessageHeader . MessageType ) . toBe ( MessageType . Response ) ;
226+ expect ( response . SaleToPOIResponse ?. PaymentResponse ) . toBeDefined ( ) ;
227+ expect ( response . SaleToPOIResponse ?. PaymentResponse ?. Response . Result ) . toBe ( "Success" ) ;
228+ // verify deviceId is set on request
229+ expect ( cloudDeviceApiSecuredRequest . SaleToPOIRequest . MessageHeader . POIID ) . toBe ( deviceId ) ;
230+
205231 } ) ;
206232
207- } ) ;
233+ test ( "should throw CloudDeviceApiError when request fails" , async ( ) : Promise < void > => {
234+ scope . post ( "/merchants/TestMerchantAccount/devices/P400Plus-123456789/sync" ) . replyWithError ( "timeout" ) ;
235+
236+ const merchantAccount = "TestMerchantAccount" ;
237+ const deviceId = "P400Plus-123456789" ;
238+
239+ const cloudDeviceApiSecuredRequest = createCloudDeviceApiRequest ( ) ;
240+
241+ const encryptionCredentialDetails : EncryptionCredentialDetails = {
242+ AdyenCryptoVersion : 0 ,
243+ KeyIdentifier : "CryptoKeyIdentifier12345" ,
244+ KeyVersion : 0 ,
245+ Passphrase : "invalid passphrase" ,
246+ } ;
247+
248+ await expect (
249+ cloudDeviceAPI . sendEncryptedAsync (
250+ merchantAccount ,
251+ deviceId ,
252+ cloudDeviceApiSecuredRequest ,
253+ encryptionCredentialDetails
254+ )
255+ ) . rejects . toThrow ( CloudDeviceApiError ) ;
256+ } ) ;
257+
258+ } ) ;
259+
260+ describe ( "should extract PaymentRequest with extractPayloadObject" , ( ) => {
261+
262+ it ( "should return the correct payload object when one is set" , ( ) => {
263+ const cloudDeviceApiRequest = createCloudDeviceApiRequest ( ) ;
264+
265+ const payload = cloudDeviceAPI . extractPayloadObject ( cloudDeviceApiRequest . SaleToPOIRequest ) ;
266+
267+ expect ( payload ) . not . toBeNull ( ) ;
268+ expect ( payload ) . toHaveProperty ( "PaymentRequest" ) ;
269+ expect ( payload ) . not . toHaveProperty ( "ReversalRequest" ) ;
270+ expect ( payload ) . not . toHaveProperty ( "MessageHeader" ) ;
271+ } ) ;
272+
273+ } ) ;
0 commit comments