Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow .NET Library to send encrypted payloads to the /sync and /async Cloud Terminal API endpoints #1060

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/.DS_Store
.vs/
TestResults/
.vagrant/
Expand Down
5 changes: 0 additions & 5 deletions Adyen.Test/Adyen.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,16 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="MochFiles\**" />
<Compile Remove="Mocks\**" />
<EmbeddedResource Remove="MochFiles\**" />
<EmbeddedResource Remove="Mocks\**" />
<None Remove="MochFiles\**" />
<None Remove="Mocks\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />

<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />

<PackageReference Include="NSubstitute" Version="5.1.0" />
</ItemGroup>

Expand Down
14 changes: 12 additions & 2 deletions Adyen.Test/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Adyen.Constants;
using Adyen.HttpClient.Interfaces;
using Adyen.Model;
Expand Down Expand Up @@ -226,7 +227,7 @@ protected Client CreateMockForAdyenClientTest(Config config)

ClientInterfaceSubstitute.RequestAsync(Arg.Any<string>(),
Arg.Any<string>(), Arg.Any<RequestOptions>(), Arg.Any<HttpMethod>(),
Arg.Any<CancellationToken>()).Returns("");
Arg.Any<CancellationToken>()).Returns(Task.FromResult(""));

var clientMock = new Client(config)
{
Expand All @@ -251,6 +252,7 @@ protected Client CreateMockTestClientRequest(string fileName)
ClientInterfaceSubstitute.RequestAsync(Arg.Any<string>(),
Arg.Any<string>(), Arg.Any<RequestOptions>(), Arg.Any<HttpMethod>(),
Arg.Any<CancellationToken>()).Returns(response);

var config = new Config
{
Environment = Model.Environment.Test
Expand Down Expand Up @@ -333,9 +335,13 @@ protected Client CreateMockTestClientPosCloudApiRequest(string fileName)

//Create a mock interface
ClientInterfaceSubstitute = Substitute.For<IClient>();

ClientInterfaceSubstitute.Request(Arg.Any<string>(),
Arg.Any<string>(), Arg.Any<RequestOptions>(), Arg.Any<HttpMethod>()).Returns(response);

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 Expand Up @@ -367,6 +373,10 @@ protected Client CreateMockTestClientPosLocalApiRequest(string fileName)

ClientInterfaceSubstitute.Request(Arg.Any<string>(),
Arg.Any<string>(), Arg.Any<RequestOptions>(), Arg.Any<HttpMethod>()).Returns(response);

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
119 changes: 119 additions & 0 deletions Adyen.Test/TerminalApiAsyncServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using Adyen.Model.TerminalApi.Message;
using Adyen.Security;
using Adyen.Service;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;
using System.Threading.Tasks;
using Adyen.ApiSerialization;

namespace Adyen.Test
{
[TestClass]
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)
{
Assert.Fail();
}
}

[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)
{
Assert.Fail();
}
}

[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();
}
}


[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();
}
}

[TestMethod]
public void DecryptNotification_Success()
{
try
{
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"" } } }";

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();
}
}
}
}
2 changes: 1 addition & 1 deletion Adyen.Test/TerminalApiCloudRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,4 @@ public void TestTerminalCloudEndpointLiveSetCustomValue()
"https://custom-value-endpoint/async", Arg.Any<String>(), Arg.Any<RequestOptions>(), null);
}
}
}
}
98 changes: 98 additions & 0 deletions Adyen.Test/TerminalApiLocalServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Adyen.Model.TerminalApi.Message;
using Adyen.Security;
using Adyen.Service;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;
using System.Threading.Tasks;
using Adyen.ApiSerialization;
using Adyen.Model.TerminalApi;

namespace Adyen.Test
{
[TestClass]
public class TerminalApiLocalServiceTest : 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-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();
}
}

[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)
{
Assert.Fail();
}
}

[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();
}
}

[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();
}
}
}
}
2 changes: 1 addition & 1 deletion Adyen.Test/TerminalApiPosRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ public void TestTerminalApiRequestEmptySecurityTrailer()
}
}
}
}
}
Loading
Loading