Skip to content

Commit

Permalink
Merge pull request #107 from bunq/optimise_test_framework_bunq/sdk_cs…
Browse files Browse the repository at this point in the history
…harp#87

Optimise test framework #87
  • Loading branch information
andrederoos authored Jul 20, 2018
2 parents dd7453b + 0ed3dc4 commit 96a8dc2
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 398 deletions.
146 changes: 130 additions & 16 deletions BunqSdk.Tests/BunqSdkTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using Bunq.Sdk.Context;
using Bunq.Sdk.Exception;
using Bunq.Sdk.Json;
using Bunq.Sdk.Model.Generated.Endpoint;
using Bunq.Sdk.Model.Generated.Object;
using Newtonsoft.Json.Linq;

namespace Bunq.Sdk.Tests
{
Expand All @@ -10,50 +16,158 @@ namespace Bunq.Sdk.Tests
/// </summary>
public class BunqSdkTestBase
{
/// <summary>
/// Error constants.
/// </summary>
private const string ErrorCouldNotDetermineUserAlias = "Could not determine user alias.";

/// <summary>
/// Name of the context configuration file.
/// </summary>
private const string FILENAME_CONTEXT_CONF = "../../../bunq-test.conf";
protected const string FilenameContextConf = "../../../bunq-test.conf";

/// <summary>
/// Device description used for tests.
/// Constants for payment creation.
/// </summary>
private const string DEVICE_DESCRIPTION_TEST = "Csharp unit test";
protected const string PaymentAmountEur = "0.01";
protected const string PaymentCurrency = "EUR";
protected const string PaymentDescription = "C# test Payment";

/// <summary>
/// Configuration items.
/// Constants for monetary account.
/// </summary>
protected const string MonetaryAccountDescription = "Test C# monetary account";

/// <summary>
/// Image constants.
/// </summary>
protected const string PathAttachment = "../../../Resources";
protected const string ContentType = "image/png";
protected const string AttachmentDescription = "C# sdk image test.";
protected const string AttachmentPathIn = "/bunq_App_Icon_Square@4x.png";

/// <summary>
/// Device registration constants.
/// </summary>
private const string DeviceDescription = "Csharp test device";

/// <summary>
/// Pointer type constants.
/// </summary>
private const string PointerTypeEmail = "EMAIL";

/// <summary>
/// Email constants.
/// </summary>
private const string EmailBravo = "bravo@bunq.com";
private const string EmailSuggarDaddy = "sugardaddy@bunq.com";

/// <summary>
/// Spending money constants.
/// </summary>
private static readonly string API_KEY = Config.GetApiKey();
private static readonly string[] FIELD_PERMITTED_IPS = Config.GetPermittedIps();
private const string SpendingMoneyAmount = "50.00";
private const string SpendingMoneyRequestDescription = "sdk c# test, thanks daddy.";

protected static MonetaryAccountBank SecondMonetaryAccountBank;

/// <summary>
/// Gets an Api Context, re-creates if needed and returns it.
/// </summary>
protected static void SetUpTestCase()
{
SetUpApiContext();
SecondMonetaryAccountBank = SetUpSecondMonetaryAccount();
RequestSpendingMoney();
System.Threading.Thread.Sleep(500); // ensure requests are auto accepted.
BunqContext.UserContext.RefreshUserContext();
}

protected static ApiContext SetUpApiContext()
{
ApiContext apiContext;

try
if (File.Exists(FilenameContextConf))
{
apiContext = ApiContext.Restore(FILENAME_CONTEXT_CONF);
apiContext = ApiContext.Restore(FilenameContextConf);
apiContext.EnsureSessionActive();
}
catch (BunqException)
else
{
apiContext = CreateApiContext();
var sandboxUser = GenerateNewSandboxUser();
apiContext = ApiContext.Create(ApiEnvironmentType.SANDBOX, sandboxUser.ApiKey, DeviceDescription);
}

apiContext.EnsureSessionActive();
apiContext.Save(FILENAME_CONTEXT_CONF);

BunqContext.LoadApiContext(apiContext);

return apiContext;
}

private static SandboxUser GenerateNewSandboxUser()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-Bunq-Client-Request-Id", "unique");
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no");
httpClient.DefaultRequestHeaders.Add("X-Bunq-Geolocation", "0 0 0 0 NL");
httpClient.DefaultRequestHeaders.Add("X-Bunq-Language", "en_US");
httpClient.DefaultRequestHeaders.Add("X-Bunq-Region", "en_US");
httpClient.DefaultRequestHeaders.Add("User-Agent", "sdk_csharp_test_case");

private static ApiContext CreateApiContext()
var requestTask = httpClient.PostAsync(ApiEnvironmentType.SANDBOX.BaseUri + "sandbox-user", null);
requestTask.Wait();

var responseString = requestTask.Result.Content.ReadAsStringAsync().Result;
var responseJson = BunqJsonConvert.DeserializeObject<JObject>(responseString);

return BunqJsonConvert.DeserializeObject<SandboxUser>(responseJson.First.First.First.First.First
.ToString());
}

private static MonetaryAccountBank SetUpSecondMonetaryAccount()
{
var createdMonetaryAccountId = MonetaryAccountBank.Create(PaymentCurrency, MonetaryAccountDescription);

return MonetaryAccountBank.Get(createdMonetaryAccountId.Value).Value;
}

private static void RequestSpendingMoney()
{
RequestInquiry.Create(
new Amount(SpendingMoneyAmount, PaymentCurrency),
new Pointer(PointerTypeEmail, EmailSuggarDaddy),
SpendingMoneyRequestDescription,
false
);

RequestInquiry.Create(
new Amount(SpendingMoneyAmount, PaymentCurrency),
new Pointer(PointerTypeEmail, EmailSuggarDaddy),
SpendingMoneyRequestDescription,
false,
SecondMonetaryAccountBank.Id
);
}

protected static Pointer GetPointerBravo()
{
return ApiContext.Create(ApiEnvironmentType.SANDBOX, API_KEY, DEVICE_DESCRIPTION_TEST,
new List<string>(FIELD_PERMITTED_IPS));
return new Pointer(PointerTypeEmail, EmailBravo);
}

protected static Pointer GetAlias()
{
var userContex = BunqContext.UserContext;

if (userContex.IsOnlyUserPersonSet())
{
return userContex.UserPerson.Alias.First();
}
else if (userContex.IsOnlyUserCompanySet())
{
return userContex.UserCompany.Alias.First();
}
else
{
throw new BunqException(ErrorCouldNotDetermineUserAlias);
}
}
}
}
6 changes: 3 additions & 3 deletions BunqSdk.Tests/Context/ApiContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ApiContextTest : BunqSdkTestBase, IClassFixture<ApiContextTest>
/// <summary>
/// Path to a temporary context file.
/// </summary>
private const string CONTEXT_FILENAME_TEST = "context-save-restore-test.conf";
private const string ContextFilenameTest = "context-save-restore-test.conf";

private static ApiContext apiContext;

Expand Down Expand Up @@ -40,8 +40,8 @@ public void TestApiContextSerializeDeserialize()
public void TestApiContextSaveRestore()
{
var apiContextJson = apiContext.ToJson();
apiContext.Save(CONTEXT_FILENAME_TEST);
var apiContextRestored = ApiContext.Restore(CONTEXT_FILENAME_TEST);
apiContext.Save(ContextFilenameTest);
var apiContextRestored = ApiContext.Restore(ContextFilenameTest);

Assert.Equal(apiContextJson, apiContextRestored.ToJson());
}
Expand Down
37 changes: 10 additions & 27 deletions BunqSdk.Tests/Http/PaginationScenarioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,23 @@ namespace Bunq.Sdk.Tests.Http
/// </summary>
public class PaginationScenarioTest : BunqSdkTestBase
{
/// <summary>
/// Config values.
/// </summary>
private static readonly int USER_ID = Config.GetUserId();
private static readonly int MONETARY_ACCOUNT_ID = Config.GetMonetarytAccountId();
private static readonly Pointer COUNTER_PARTY_OTHER = Config.GetCounterPartyAliasOther();

/// <summary>
/// Constants for scenario testing.
/// </summary>
private const int PAYMENT_LISTING_PAGE_SIZE = 2;
private const int PAYMENT_REQUIRED_COUNT_MINIMUM = PAYMENT_LISTING_PAGE_SIZE * 2;
private const int NUMBER_ZERO = 0;

/// <summary>
/// Constants for payment creation.
/// </summary>
private const string PAYMENT_AMOUNT_EUR = "0.01";
private const string PAYMENT_CURRENCY = "EUR";
private const string PAYMENT_DESCRIPTION = "C# test Payment";

/// <summary>
/// API context to use for the test API calls.
/// </summary>
private static readonly ApiContext API_CONTEXT = SetUpApiContext();
private const int PaymentListingPageSize = 2;
private const int PaymentRequiredCountMinimum = PaymentListingPageSize * 2;
private const int NumberZero = 0;

[Fact]
public void TestApiScenarioPaymentListingWithPagination()
{
SetUpTestCase();

EnsureEnoughPayments();
var paymentsExpected = new List<Payment>(GetPaymentsRequired());
var paginationCountOnly = new Pagination
{
Count = PAYMENT_LISTING_PAGE_SIZE
Count = PaymentListingPageSize
};

var responseLatest = ListPayments(paginationCountOnly.UrlParamsCountOnly);
Expand All @@ -67,22 +50,22 @@ public void TestApiScenarioPaymentListingWithPagination()

private static void EnsureEnoughPayments()
{
for (var i = NUMBER_ZERO; i < GetPaymentsMissingCount(); ++i)
for (var i = NumberZero; i < GetPaymentsMissingCount(); ++i)
{
CreatePayment();
}
}

private static int GetPaymentsMissingCount()
{
return PAYMENT_REQUIRED_COUNT_MINIMUM - GetPaymentsRequired().Count;
return PaymentRequiredCountMinimum - GetPaymentsRequired().Count;
}

private static IList<Payment> GetPaymentsRequired()
{
var pagination = new Pagination
{
Count = PAYMENT_REQUIRED_COUNT_MINIMUM
Count = PaymentRequiredCountMinimum
};

return ListPayments(pagination.UrlParamsCountOnly).Value;
Expand All @@ -95,7 +78,7 @@ private static BunqResponse<List<Payment>> ListPayments(IDictionary<string, stri

private static void CreatePayment()
{
Payment.Create(new Amount(PAYMENT_AMOUNT_EUR, PAYMENT_CURRENCY), COUNTER_PARTY_OTHER, PAYMENT_DESCRIPTION);
Payment.Create(new Amount(PaymentAmountEur, PaymentCurrency), GetPointerBravo(), PaymentDescription);
}
}
}
Loading

0 comments on commit 96a8dc2

Please sign in to comment.