Skip to content

Commit

Permalink
Assert by casting payment response instead of checking for null
Browse files Browse the repository at this point in the history
Tests no longer fail silently
  • Loading branch information
kwokhe committed Oct 3, 2024
1 parent 994ee24 commit 248de2e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 215 deletions.
3 changes: 2 additions & 1 deletion Adyen.Test/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ protected Client CreateMockTestClientPosLocalApiRequest(string fileName)
{
var config = new Config
{
Environment = Model.Environment.Live,
Environment = Model.Environment.Test,
LocalTerminalApiEndpoint = @"https://_terminal_:8443/nexo/"
};
var mockPath = GetMockFilePath(fileName);
Expand All @@ -377,6 +377,7 @@ protected Client CreateMockTestClientPosLocalApiRequest(string fileName)
ClientInterfaceSubstitute.RequestAsync(Arg.Any<string>(),
Arg.Any<string>(), Arg.Any<RequestOptions>(), Arg.Any<HttpMethod>()).Returns(Task.FromResult(response));


var anyConfig = new Config
{
Environment = Model.Environment.Test
Expand Down
141 changes: 55 additions & 86 deletions Adyen.Test/TerminalApiAsyncServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,107 +13,76 @@ namespace Adyen.Test
public class TerminalApiAsyncServiceTest : BaseTest
{
[TestMethod]
public async Task RequestEncryptedAsync_Success() {
try
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = await asyncService.RequestEncryptedAsync(saleToPoiRequest, encryptionCredentialDetails, new CancellationToken());
Assert.AreEqual(response, "ok");
}
catch (Exception)
public async Task RequestEncryptedAsync_Success()
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
Assert.Fail();
}
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = await asyncService.RequestEncryptedAsync(saleToPoiRequest, encryptionCredentialDetails, new CancellationToken());
Assert.AreEqual(response, "ok");
}

[TestMethod]
public void RequestEncrypted_Success() {
try
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = asyncService.RequestEncrypted(saleToPoiRequest, encryptionCredentialDetails);
Assert.AreEqual(response, "ok");
}
catch (Exception)
public void RequestEncrypted_Success()
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
Assert.Fail();
}
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = asyncService.RequestEncrypted(saleToPoiRequest, encryptionCredentialDetails);
Assert.AreEqual(response, "ok");
}

[TestMethod]
public async Task RequestAsync_Success() {
try
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = await asyncService.RequestAsync(saleToPoiRequest, new CancellationToken());
Assert.AreEqual(response, "ok");
}
catch (Exception)
{
Assert.Fail();
}
public async Task RequestAsync_Success()
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = await asyncService.RequestAsync(saleToPoiRequest, new CancellationToken());
Assert.AreEqual(response, "ok");
}


[TestMethod]
public void Request_Success() {
try
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = asyncService.Request(saleToPoiRequest);
Assert.AreEqual(response, "ok");
}
catch (Exception)
{
Assert.Fail();
}
public void Request_Success()
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-async-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string response = asyncService.Request(saleToPoiRequest);
Assert.AreEqual(response, "ok");
}

[TestMethod]
public void DecryptNotification_Success()
{
try
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
AdyenCryptoVersion = 1,
KeyIdentifier = "ncrkey",
Password = "ncrpass"
};
string encryptedNotification = @"{""SaleToPOIRequest"":{""SecurityTrailer"":{""AdyenCryptoVersion"":1,""Nonce"":""Be6rAx+vRju2aCHwPh6lrg=="",""KeyIdentifier"":""ncrkey"",""Hmac"":""LG8A9Re1M8xLMr7rDUk0NwsnvAOX+VLjHv9sPHWTl34="",""KeyVersion"":1},""NexoBlob"":""x2DY8J2M9ZCyjOZ8Gt7JdLBA\/6bT\/KXvvAbJf9kzguqO8dWp1I1pPLQpLstpdIiAVqSwG3PR0PrP\/lF82UmhmCnUJGCuEXilqvBNF1tF\/yEgnFOklNc1myR2IPW\/+2oZOWKFXlTo\/gX89EbODXOOGUqaJfSdpDhlqjyMz7mGczobTPvPGqCVx2BDHU8VTxI9nicwQv+QV48GqVZzxnP8ZOdQOQ5cac+bcS0Y3l7SmWpIoQsoicnjahTY9ICosLJmN4DvDHsN4Kh2DAetFO5b9I9Lqgm\/dvnXUVhb9tPbM7Pn+ratjYpaNbonbO5M+Tm8rDEIyKoUUuFXPWISymrCXtCDVKEb2B5S5pilUmokrXVa9Ldtsv3BKG7rbrglYEuql4WVs6kzr6ybgAKh1Q0LsAXEve3pydt72ay4U3FOJSBxJ3gNqmnG8mVW2HCXQVo1RgVaZmP5TBWYuksCKXYypnMulu1PlRI++oeW\/J2qjQU="",""MessageHeader"":{""ProtocolVersion"":""3.0"",""SaleID"":""null"",""MessageClass"":""Event"",""MessageCategory"":""Event"",""POIID"":""P400Plus-275102806"",""MessageType"":""Notification"",""DeviceID"":""5""}}}";
string expectedDecryption = @"{ ""SaleToPOIRequest"": { ""EventNotification"": { ""EventDetails"": ""reference_id=9876"", ""TimeStamp"": ""2020-11-13T09:02:35.697Z"", ""EventToNotify"": ""SaleWakeUp"" }, ""MessageHeader"": { ""ProtocolVersion"": ""3.0"", ""SaleID"": ""null"", ""MessageClass"": ""Event"", ""MessageCategory"": ""Event"", ""POIID"": ""P400Plus-275102806"", ""MessageType"": ""Notification"", ""DeviceID"": ""5"" } } }";
AdyenCryptoVersion = 1,
KeyIdentifier = "ncrkey",
Password = "ncrpass"
};
string encryptedNotification = @"{""SaleToPOIRequest"":{""SecurityTrailer"":{""AdyenCryptoVersion"":1,""Nonce"":""Be6rAx+vRju2aCHwPh6lrg=="",""KeyIdentifier"":""ncrkey"",""Hmac"":""LG8A9Re1M8xLMr7rDUk0NwsnvAOX+VLjHv9sPHWTl34="",""KeyVersion"":1},""NexoBlob"":""x2DY8J2M9ZCyjOZ8Gt7JdLBA\/6bT\/KXvvAbJf9kzguqO8dWp1I1pPLQpLstpdIiAVqSwG3PR0PrP\/lF82UmhmCnUJGCuEXilqvBNF1tF\/yEgnFOklNc1myR2IPW\/+2oZOWKFXlTo\/gX89EbODXOOGUqaJfSdpDhlqjyMz7mGczobTPvPGqCVx2BDHU8VTxI9nicwQv+QV48GqVZzxnP8ZOdQOQ5cac+bcS0Y3l7SmWpIoQsoicnjahTY9ICosLJmN4DvDHsN4Kh2DAetFO5b9I9Lqgm\/dvnXUVhb9tPbM7Pn+ratjYpaNbonbO5M+Tm8rDEIyKoUUuFXPWISymrCXtCDVKEb2B5S5pilUmokrXVa9Ldtsv3BKG7rbrglYEuql4WVs6kzr6ybgAKh1Q0LsAXEve3pydt72ay4U3FOJSBxJ3gNqmnG8mVW2HCXQVo1RgVaZmP5TBWYuksCKXYypnMulu1PlRI++oeW\/J2qjQU="",""MessageHeader"":{""ProtocolVersion"":""3.0"",""SaleID"":""null"",""MessageClass"":""Event"",""MessageCategory"":""Event"",""POIID"":""P400Plus-275102806"",""MessageType"":""Notification"",""DeviceID"":""5""}}}";
string expectedDecryption = @"{ ""SaleToPOIRequest"": { ""EventNotification"": { ""EventDetails"": ""reference_id=9876"", ""TimeStamp"": ""2020-11-13T09:02:35.697Z"", ""EventToNotify"": ""SaleWakeUp"" }, ""MessageHeader"": { ""ProtocolVersion"": ""3.0"", ""SaleID"": ""null"", ""MessageClass"": ""Event"", ""MessageCategory"": ""Event"", ""POIID"": ""P400Plus-275102806"", ""MessageType"": ""Notification"", ""DeviceID"": ""5"" } } }";

Client client = CreateMockTestClientPosLocalApiRequest("mocks/terminalapi/pospayment-encrypted-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string decryptedNotification = asyncService.DecryptNotification(encryptedNotification, encryptionCredentialDetails);
Assert.AreEqual(decryptedNotification, expectedDecryption);
}
catch (Exception)
{
Assert.Fail();
}
Client client = CreateMockTestClientPosLocalApiRequest("mocks/terminalapi/pospayment-encrypted-success.json");
ITerminalApiAsyncService asyncService = new TerminalApiAsyncService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
string decryptedNotification = asyncService.DecryptNotification(encryptedNotification, encryptionCredentialDetails);
Assert.AreEqual(decryptedNotification, expectedDecryption);
}
}
}
96 changes: 36 additions & 60 deletions Adyen.Test/TerminalApiLocalServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,59 @@ public class TerminalApiLocalServiceTest : BaseTest
[TestMethod]
public async Task RequestEncryptedAsync_Success()
{
try
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-encrypted-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = await localService.RequestEncryptedAsync(saleToPoiRequest, encryptionCredentialDetails, new CancellationToken());
Assert.IsNotNull(response);
}
catch (Exception)
{
Assert.Fail();
}
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosLocalApiRequest("mocks/terminalapi/pospayment-encrypted-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = await localService.RequestEncryptedAsync(saleToPoiRequest, encryptionCredentialDetails, new CancellationToken());
PaymentResponse paymentResponse = response.MessagePayload as PaymentResponse;
Assert.AreEqual(paymentResponse?.Response.Result, ResultType.Success);
}

[TestMethod]
public void RequestEncrypted_Success()
{
try
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-encrypted-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = localService.RequestEncrypted(saleToPoiRequest, encryptionCredentialDetails);
Assert.IsNotNull(response);
}
catch (Exception)
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
EncryptionCredentialDetails encryptionCredentialDetails = new EncryptionCredentialDetails
{
Assert.Fail();
}
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Client client = CreateMockTestClientPosLocalApiRequest("mocks/terminalapi/pospayment-encrypted-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = localService.RequestEncrypted(saleToPoiRequest, encryptionCredentialDetails);
PaymentResponse paymentResponse = response.MessagePayload as PaymentResponse;
Assert.AreEqual(paymentResponse?.Response.Result, ResultType.Success);
}

[TestMethod]
public async Task RequestAsync_Success()
{
try
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = await localService.RequestAsync(saleToPoiRequest, new CancellationToken());
Assert.IsNotNull(response);
}
catch (Exception)
{
Assert.Fail();
}
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosLocalApiRequest("mocks/terminalapi/pospayment-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = await localService.RequestAsync(saleToPoiRequest, new CancellationToken());
PaymentResponse paymentResponse = response.MessagePayload as PaymentResponse;
Assert.AreEqual(paymentResponse?.Response.Result, ResultType.Success);
}

[TestMethod]
public void Request_Success()
{
try
{
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosCloudApiRequest("mocks/terminalapi/pospayment-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = localService.Request(saleToPoiRequest);
Assert.IsNotNull(response);
}
catch (Exception)
{
Assert.Fail();
}
SaleToPOIRequest saleToPoiRequest = MockPosApiRequest.CreatePosPaymentRequest();
Client client = CreateMockTestClientPosLocalApiRequest("mocks/terminalapi/pospayment-success.json");
ITerminalApiLocalService localService = new TerminalApiLocalService(client, new SaleToPoiMessageSerializer(), new SaleToPoiMessageSecuredEncryptor(), new SaleToPoiMessageSecuredSerializer());
SaleToPOIResponse response = localService.Request(saleToPoiRequest);
PaymentResponse paymentResponse = response.MessagePayload as PaymentResponse;
Assert.AreEqual(paymentResponse?.Response.Result, ResultType.Success);
}
}
}
Loading

0 comments on commit 248de2e

Please sign in to comment.