Skip to content

Commit

Permalink
Merge pull request #18 from Adyen/develop
Browse files Browse the repository at this point in the history
Terminal API
  • Loading branch information
AlexandrosMor authored Sep 21, 2018
2 parents ecda05f + ec140e4 commit 2cf6a4a
Show file tree
Hide file tree
Showing 220 changed files with 6,837 additions and 282 deletions.
6 changes: 3 additions & 3 deletions Adyen.EcommLibrary.Test/Adyen.EcommLibrary.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

<IsPackable>false</IsPackable>

<AssemblyVersion>1.0.5</AssemblyVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>

<FileVersion>1.0.5</FileVersion>
<FileVersion>1.1.0</FileVersion>

<Version>1.0.5</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
11 changes: 11 additions & 0 deletions Adyen.EcommLibrary.Test/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<settings>
<servicePointManager
checkCertificateName="false"
checkCertificateRevocationList="false"
/>
</settings>
</system.net>
</configuration>
117 changes: 95 additions & 22 deletions Adyen.EcommLibrary.Test/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
using Adyen.EcommLibrary.Constants;
using Adyen.EcommLibrary.HttpClient;
using Adyen.EcommLibrary.HttpClient.Interfaces;
using Adyen.EcommLibrary.Model;
using Adyen.EcommLibrary.Model.Modification;
using Adyen.EcommLibrary.Model.Nexo;
using Adyen.EcommLibrary.Service;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Adyen.EcommLibrary.HttpClient.Interfaces;
using Environment = System.Environment;
using Amount = Adyen.EcommLibrary.Model.Amount;
using PaymentResult = Adyen.EcommLibrary.Model.PaymentResult;

namespace Adyen.EcommLibrary.Test
{
public class BaseTest
{

#region Payment request
#region Payment request
/// <summary>
/// Payment with basic authentication
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public PaymentResult CreatePaymentResultFromFile(string fileName)
{
var client = CreateMockTestClientRequest(fileName);
Expand All @@ -25,9 +34,18 @@ public PaymentResult CreatePaymentResultFromFile(string fileName)
var paymentResult = payment.Authorise(paymentRequest);
return GetAdditionaData(paymentResult);
}


public PaymentResult CreatePaymentApiKeyBasedResultFromFile(string fileName)
{
var client = CreateMockTestClientApiKeyBasedRequest(fileName);
var payment = new Payment(client);
var paymentRequest = MockPaymentData.CreateFullPaymentRequest();

var paymentResult = payment.Authorise(paymentRequest);
return GetAdditionaData(paymentResult);
}
#endregion

#region Modification objects

protected CaptureRequest CreateCaptureTestRequest(string pspReference)
Expand Down Expand Up @@ -77,23 +95,14 @@ protected CancelRequest CreateCancelTestRequest(string pspReference)
return cancelRequest;
}

protected static Dictionary<string, string> CreateAdditionalData()
{
return new Dictionary<string, string>
{
{ "liabilityShift", "true"},
{ "fraudResultType", "GREEN"},
{ "authCode", "43733"},
{ "avsResult", "4 AVS not supported for this card type"}
};
}

#endregion

/// <summary>
/// Creates mock test client
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
/// <param name="fileName">The file that is returned</param>
/// <returns>IClient implementation</returns>
protected Client CreateMockTestClientRequest(string fileName)
{
var mockPath = GetMockFilePath(fileName);
Expand All @@ -111,6 +120,50 @@ protected Client CreateMockTestClientRequest(string fileName)
return clientMock;
}

/// <summary>
/// Creates mock test client
/// </summary>
/// <param name="fileName">The file that is returned</param>
/// <returns>IClient implementation</returns>
protected Client CreateMockTestClientApiKeyBasedRequest(string fileName)
{
var mockPath = GetMockFilePath(fileName);
var response = MockFileToString(mockPath);
//Create a mock interface
var clientInterfaceMock = new Mock<IClient>();
var confMock = MockPaymentData.CreateConfingApiKeyBasedMock();
clientInterfaceMock.Setup(x => x.Request(It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<Config>())).Returns(response);
var clientMock = new Client(It.IsAny<Config>())
{
HttpClient = clientInterfaceMock.Object,
Config = confMock
};
return clientMock;
}

/// <summary>
/// Creates mock test client for POS cloud and terminal api. In case of cloud api the xapi should be included
/// </summary>
/// <param name="fileName">The file that is returned</param>
/// <returns>IClient implementation</returns>
protected Client CreateMockTestClientPosApiRequest(string fileName)
{
var config = new Config();
var mockPath = GetMockFilePath(fileName);
var response = MockFileToString(mockPath);
//Create a mock interface
var clientInterfaceMock = new Mock<IClient>();
clientInterfaceMock.Setup(x => x.Request(It.IsAny<string>(),
It.IsAny<string>(), config)).Returns(response);
var clientMock = new Client(It.IsAny<Config>())
{
HttpClient = clientInterfaceMock.Object,
Config = config
};
return clientMock;
}

/// <summary>
/// Creates mock test client
/// </summary>
Expand Down Expand Up @@ -147,7 +200,8 @@ protected Client CreateMockTestClientForErrors(int status, string fileName)
//Create a mock interface
var clientInterfaceMock = new Mock<IClient>();
var confMock = MockPaymentData.CreateConfingMock();
var httpClientException = new HttpClientException(status, "An error occured", new Dictionary<string, List<string>>(), response);
var httpClientException =
new HttpClientException(status, "An error occured", new WebHeaderCollection(), response);

clientInterfaceMock.Setup(x => x.Request(It.IsAny<string>(),
It.IsAny<string>(), confMock)).Throws(httpClientException);
Expand All @@ -169,7 +223,7 @@ protected string MockFileToString(string fileName)
return text;
}
try
{
{
using (var streamReader = new StreamReader(fileName, Encoding.UTF8))
{
text = streamReader.ReadToEnd();
Expand All @@ -179,10 +233,29 @@ protected string MockFileToString(string fileName)
{
throw exception;
}

return text;
}

/// <summary>
/// Create dummy Nexo message header
/// </summary>
/// <returns></returns>
protected MessageHeader MockNexoMessageHeaderRequest()
{
return new MessageHeader
{
MessageType = "Request",
MessageClass = "Service",
MessageCategory = "Payment",
SaleID = "POSSystemID12345",
POIID = "MX915-284251016",
ProtocolVersion = "3.0",
ServiceID = (new Random()).Next(1, 9999).ToString()
};
}


private PaymentResult GetAdditionaData(PaymentResult paymentResult)
{
var paymentResultAdditionalData = paymentResult.AdditionalData;
Expand Down Expand Up @@ -211,13 +284,13 @@ private PaymentResult GetAdditionaData(PaymentResult paymentResult)
return paymentResult;
}

private static string GetMockFilePath(string fileName)
protected static string GetMockFilePath(string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
return "";
}
var projectPath = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
var projectPath = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
var mockPath = Path.Combine(projectPath, fileName);
return mockPath;
}
Expand Down
30 changes: 30 additions & 0 deletions Adyen.EcommLibrary.Test/CloudApiPosRequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Adyen.EcommLibrary.Service;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Adyen.EcommLibrary.Test
{
[TestClass]
public class CloudApiPosRequestTest : BaseTest
{

[TestMethod]
public void TestCloudApiRequest()
{
try
{
//Create a mock pos payment request
var paymentRequest = MockPosApiRequest.CreatePosPaymentRequest("Request");
var client = CreateMockTestClientPosApiRequest("Mocks/pospayment-success.json");
var payment = new PosPayment(client);
var saleToPoiResponse = payment.TerminalApiOverCLoudAsync(paymentRequest);

Assert.IsNotNull(saleToPoiResponse);
}
catch (Exception)
{
Assert.Fail();
}
}
}
}
53 changes: 53 additions & 0 deletions Adyen.EcommLibrary.Test/HeaderRequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Adyen.EcommLibrary.HttpClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Adyen.EcommLibrary.Test
{
[TestClass]
public class HeaderRequestTest
{

private readonly HttpUrlConnectionClient _httpUrlConnectionClient;
private readonly string _endpoint = "https://endpoint:8080";

public HeaderRequestTest()
{
_httpUrlConnectionClient = new HttpUrlConnectionClient();
}
[TestMethod]
public void BasicAuthenticationHeadersTest()
{
var httpWebRequest = _httpUrlConnectionClient.GetHttpWebRequest(_endpoint, MockPaymentData.CreateConfingMock(), false);

Assert.IsNotNull(httpWebRequest.UserAgent);
Assert.AreEqual(httpWebRequest.Address, _endpoint);
Assert.AreEqual(httpWebRequest.Headers["Accept-Charset"], "UTF-8");
Assert.AreEqual(httpWebRequest.Headers["Cache-Control"], "no-cache");
Assert.AreEqual(httpWebRequest.ContentType, "application/json");

Assert.IsNotNull(httpWebRequest.Headers["Authorization"]);
Assert.IsTrue(httpWebRequest.UseDefaultCredentials);

Assert.AreEqual(httpWebRequest.Headers["Cache-Control"], "no-cache");
Assert.IsNull(httpWebRequest.Headers["x-api-key"]);
}

[TestMethod]
public void ApiKeyBasedHeadersTest()
{
var httpWebRequest = _httpUrlConnectionClient.GetHttpWebRequest(_endpoint, MockPaymentData.CreateConfingApiKeyBasedMock(), true);

Assert.IsNotNull(httpWebRequest.UserAgent);
Assert.AreEqual(httpWebRequest.Address, _endpoint);
Assert.AreEqual(httpWebRequest.Headers["Accept-Charset"], "UTF-8");
Assert.AreEqual(httpWebRequest.Headers["Cache-Control"], "no-cache");
Assert.AreEqual(httpWebRequest.ContentType, "application/json");

Assert.IsNull(httpWebRequest.Headers["Authorization"]);
Assert.IsFalse(httpWebRequest.UseDefaultCredentials);

Assert.IsNotNull(httpWebRequest.Headers["x-api-key"]);
Assert.AreEqual(httpWebRequest.Headers["x-api-key"], "AQEyhmfxK....LAG84XwzP5pSpVd");
}
}
}
23 changes: 22 additions & 1 deletion Adyen.EcommLibrary.Test/MockPaymentData.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Adyen.EcommLibrary.Model;
using System;
using System.Collections.Generic;
using Environment = Adyen.EcommLibrary.Model.Enum.Environment;

namespace Adyen.EcommLibrary.Test
{
internal class MockPaymentData:BaseTest
internal class MockPaymentData
{
#region Mock payment data

Expand All @@ -21,6 +22,15 @@ public static Config CreateConfingMock()
};
}

public static Config CreateConfingApiKeyBasedMock()
{
return new Config
{
Environment = Environment.Test,
XApiKey = "AQEyhmfxK....LAG84XwzP5pSpVd"//mock api key
};
}

public static PaymentRequest CreateFullPaymentRequest()
{
var paymentRequest = new PaymentRequest
Expand All @@ -35,6 +45,17 @@ public static PaymentRequest CreateFullPaymentRequest()
return paymentRequest;
}

protected static Dictionary<string, string> CreateAdditionalData()
{
return new Dictionary<string, string>
{
{ "liabilityShift", "true"},
{ "fraudResultType", "GREEN"},
{ "authCode", "43733"},
{ "avsResult", "4 AVS not supported for this card type"}
};
}

public static PaymentRequest3D CreateFullPaymentRequest3D()
{
var paymentRequest = new PaymentRequest3D
Expand Down
Loading

0 comments on commit 2cf6a4a

Please sign in to comment.