Skip to content

Commit

Permalink
Merge branch 'release/1.14.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
angelomelonas committed Sep 17, 2020
2 parents d592b2c + f6ca8c2 commit 3005c18
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[//]: # (If there is a traceback please share it in a quote! You can do this by pasting the traceback text, highlighting it and pressing the quote button.)

## SDK version and environment
- Tested on [1.14.0](https://github.com/bunq/sdk_csharp/releases/tag/1.14.0)
- Tested on [1.14.1](https://github.com/bunq/sdk_csharp/releases/tag/1.14.1)
- [ ] Sandbox
- [ ] Production

Expand Down
24 changes: 12 additions & 12 deletions BunqSdk.Tests/BunqSdkTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BunqSdkTestBase
/// <summary>
/// Error constants.
/// </summary>
private const string ErrorCouldNotDetermineUserAlias = "Could not determine user alias.";
private const string FIELD_ERROR_COULD_NOT_DETERMINE_USER_ALIAS = "Could not determine user alias.";

/// <summary>
/// Name of the context configuration file.
Expand Down Expand Up @@ -93,16 +93,16 @@ protected static ApiContext SetUpApiContext()
}
else
{
var sandboxUser = GenerateNewSandboxUser();
apiContext = ApiContext.Create(ApiEnvironmentType.SANDBOX, sandboxUser.ApiKey, DeviceDescription);
var sandboxUserPerson = GenerateNewSandboxUserPerson();
apiContext = ApiContext.Create(ApiEnvironmentType.SANDBOX, sandboxUserPerson.ApiKey, DeviceDescription);
}

BunqContext.LoadApiContext(apiContext);

return apiContext;
}

private static SandboxUser GenerateNewSandboxUser()
private static SandboxUserPerson GenerateNewSandboxUserPerson()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-Bunq-Client-Request-Id", "unique");
Expand All @@ -112,14 +112,15 @@ private static SandboxUser GenerateNewSandboxUser()
httpClient.DefaultRequestHeaders.Add("X-Bunq-Region", "en_US");
httpClient.DefaultRequestHeaders.Add("User-Agent", "sdk_csharp_test_case");

var requestTask = httpClient.PostAsync(ApiEnvironmentType.SANDBOX.BaseUri + "sandbox-user", null);
var requestTask = httpClient.PostAsync(ApiEnvironmentType.SANDBOX.BaseUri + "sandbox-user-person", 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());
return BunqJsonConvert.DeserializeObject<SandboxUserPerson>(
responseJson.First.First.First.First.First.ToString()
);
}

private static MonetaryAccountBank SetUpSecondMonetaryAccount()
Expand Down Expand Up @@ -160,14 +161,13 @@ protected static Pointer GetAlias()
{
return userContext.UserPerson.Alias.First();
}
else if (userContext.IsOnlyUserCompanySet())

if (userContext.IsOnlyUserCompanySet())
{
return userContext.UserCompany.Alias.First();
}
else
{
throw new BunqException(ErrorCouldNotDetermineUserAlias);
}

throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_USER_ALIAS);
}
}
}
4 changes: 2 additions & 2 deletions BunqSdk.Tests/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static int GetSecondMonetaryAccountId()
return GetConfig()[FIELD_MONETARY_ACCOUNT_ID2].ToObject<int>();
}

public static int GetMonetarytAccountId()
public static int GetMonetaryAccountId()
{
return GetConfig()[FIELD_MONETARY_ACCOUNT_ID].ToObject<int>();
}
Expand All @@ -72,7 +72,7 @@ public static string GetAttachmentPathIn()
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_PATH_IN].ToString();
}

public static string GetAttachmentDescrpition()
public static string GetAttachmentDescription()
{
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_DESCRIPTION].ToString();
}
Expand Down
17 changes: 10 additions & 7 deletions BunqSdk.Tests/Context/ApiContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ public class ApiContextTest : BunqSdkTestBase, IClassFixture<ApiContextTest>
/// <summary>
/// Path to a temporary context file.
/// </summary>
private const string ContextFilenameTest = "context-save-restore-test.conf";
private const string FIELD_CONTEXT_FILENAME_TEST = "context-save-restore-test.conf";

/// <summary>
/// Field constants.
/// </summary>
private const string FieldSessionContext = "session_context";
private const string FieldExpiryTime = "expiry_time";
private const string FIELD_FIELD_SESSION_CONTEXT = "session_context";
private const string FIELD_FIELD_EXPIRY_TIME = "expiry_time";

private static ApiContext apiContext;

public ApiContextTest()
{
if (apiContext == null) apiContext = SetUpApiContext();
if (apiContext == null)
{
apiContext = SetUpApiContext();
}
}

/// <summary>
Expand All @@ -48,8 +51,8 @@ public void TestApiContextSerializeDeserialize()
public void TestApiContextSaveRestore()
{
var apiContextJson = apiContext.ToJson();
apiContext.Save(ContextFilenameTest);
var apiContextRestored = ApiContext.Restore(ContextFilenameTest);
apiContext.Save(FIELD_CONTEXT_FILENAME_TEST);
var apiContextRestored = ApiContext.Restore(FIELD_CONTEXT_FILENAME_TEST);

Assert.Equal(apiContextJson, apiContextRestored.ToJson());
}
Expand All @@ -59,7 +62,7 @@ public void TestAutoApiContextReLoad()
{
var contextJson = JObject.Parse(apiContext.ToJson());
var expiredTime = DateTime.Now.Subtract(TimeSpan.FromDays(20));
contextJson.SelectToken(FieldSessionContext)[FieldExpiryTime] = expiredTime.ToString();
contextJson.SelectToken(FIELD_FIELD_SESSION_CONTEXT)[FIELD_FIELD_EXPIRY_TIME] = expiredTime.ToString();

var expiredApiContext = ApiContext.FromJson(contextJson.ToString());

Expand Down
14 changes: 13 additions & 1 deletion BunqSdk.Tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ These are the scenarios that are currently being tested:
* Order a card with a second line

Besides these scenarios, some code of ApiContext, ApiClient and the JSON module
are also tested :thumbs_up:.
are also tested.

## Installation
To run the tests, you must first generate a `key.pem` and a `credentials.pfx`.
Navigate to the `/Resources` directory and execute the following the commands:

```
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out chain.cert -days 365 -nodes -subj '/CN=My Test App PISP AISP/C=NL
openssl pkcs12 -inkey key.pem -in chain.cert -export -out credentials.pfx
```

You will be requested to enter a passphrase.
Use the passphrase `secret` (as is defined in [`Psd2ApiContextTest.cs`](./BunqSdkCsharpTest/Context/Psd2ApiContextTest.cs)).

## Configuration
To run the tests you must first setup the test configuration JSON. The example
Expand Down
2 changes: 1 addition & 1 deletion BunqSdk/BunqSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageId>Bunq.Sdk</PackageId>
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.14.1</VersionPrefix>
<VersionPrefix>1.14.18</VersionPrefix>
</PropertyGroup>
<PropertyGroup>
<RootNamespace>Bunq.Sdk</RootNamespace>
Expand Down
4 changes: 3 additions & 1 deletion BunqSdk/Context/BunqContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public static UserContext UserContext
public static void LoadApiContext(ApiContext apiContextToLoad)
{
ApiContext = apiContextToLoad;
UserContext = new UserContext(apiContextToLoad.SessionContext.UserId);
UserContext = new UserContext(
apiContextToLoad.SessionContext.UserId,
apiContextToLoad.SessionContext.GetUserReference());
UserContext.InitPrimaryMonetaryAccount();
}

Expand Down
129 changes: 99 additions & 30 deletions BunqSdk/Context/SessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ namespace Bunq.Sdk.Context
public class SessionContext
{
/// <summary>
/// Error constatns.
/// Error constants.
/// </summary>
private const string ErrorCouldNotDetermineUserId = "Could not determine user id.";
private const string ErrorSessionserverUserapikeyIdNull = "sessionServer.UserApiKey.Id != null";
private const string ErrorSessionserverUserpaymentserviceproviderkeyIdNull =
private const string FIELD_ERROR_COULD_NOT_DETERMINE_USER_ID = "Could not determine user id.";
private const string FIELD_ERROR_SESSION_SERVER_USER_API_KEY_ID_NULL = "sessionServer.UserApiKey.Id != null";
private const string FIELD_ERROR_SESSION_SERVER_USER_PAYMENT_SERVICE_PROVIDER_KEY_ID_NULL =
"sessionServer.UserPaymentServiceProvider.Id != null";
private const string ErrorSessionserverUsercompanyIdNull = "sessionServer.UserCompany.Id != null";
private const string ErrorsessionserverUserpersonIdNull = "sessionServer.UserPerson.Id != null";
private const string ErrorCouldNotDetermineSessionTimeout = "Could not determine session timeout.";
private const string ErrorSessionTimeoutIsNull = "Session timeout is null.";
private const string FIELD_ERROR_SESSION_SERVER_USER_COMPANY_ID_NULL = "sessionServer.UserCompany.Id != null";
private const string FIELD_ERROR_SESSION_SERVER_USER_PERSON_ID_NULL = "sessionServer.UserPerson.Id != null";
private const string FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT = "Could not determine session timeout.";
private const string FIELD_ERROR_SESSION_TIMEOUT_IS_NULL = "Session timeout is null.";
private const string FIELD_ERROR_ALL_FIELD_NULL = "All fields of an extended model or object are null.";

/// <summary>
/// Session token returned as a response to POST /session-server.
Expand All @@ -39,6 +40,18 @@ public class SessionContext
[JsonProperty(PropertyName = "user_id")]
public int UserId { get; private set; }

[JsonProperty(PropertyName = "user_person")]
public UserPerson UserPerson { get; private set; }

[JsonProperty(PropertyName = "user_company")]
public UserCompany UserCompany { get; private set; }

[JsonProperty(PropertyName = "user_api_key")]
public UserApiKey UserApiKey { get; private set; }

[JsonProperty(PropertyName = "user_payment_service_provider")]
public UserPaymentServiceProvider UserPaymentServiceProvider { get; private set; }

[JsonConstructor]
private SessionContext()
{
Expand All @@ -49,41 +62,70 @@ public SessionContext(SessionServer sessionServer)
Token = sessionServer.SessionToken.Token;
ExpiryTime = DateTime.Now.AddSeconds(GetSessionTimeout(sessionServer));
UserId = GetUserId(sessionServer);
SetUser(sessionServer.GetUserReference());
}

private void SetUser(BunqModel user)
{
if (user.GetType() == typeof(UserPerson))
{
UserPerson = (UserPerson) user;
}
else if (user.GetType() == typeof(UserCompany))
{
UserCompany = (UserCompany) user;
}
else if (user.GetType() == typeof(UserApiKey))
{
UserApiKey = (UserApiKey) user;
}
else if (user.GetType() == typeof(UserPaymentServiceProvider))
{
UserPaymentServiceProvider = (UserPaymentServiceProvider) user;
}
else
{
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT);
}
}

private static int GetUserId(SessionServer sessionServer)
{
if (sessionServer.UserCompany != null)
{
Debug.Assert(sessionServer.UserCompany.Id != null, ErrorSessionserverUsercompanyIdNull);
Debug.Assert(sessionServer.UserCompany.Id != null,
FIELD_ERROR_SESSION_SERVER_USER_COMPANY_ID_NULL);

return sessionServer.UserCompany.Id.Value;
}
else if (sessionServer.UserPerson != null)

if (sessionServer.UserPerson != null)
{
Debug.Assert(sessionServer.UserPerson.Id != null, ErrorsessionserverUserpersonIdNull);
Debug.Assert(sessionServer.UserPerson.Id != null,
FIELD_ERROR_SESSION_SERVER_USER_PERSON_ID_NULL);

return sessionServer.UserPerson.Id.Value;
}
else if (sessionServer.UserApiKey != null)

if (sessionServer.UserApiKey != null)
{
Debug.Assert(sessionServer.UserApiKey.Id != null, ErrorSessionserverUserapikeyIdNull);
Debug.Assert(sessionServer.UserApiKey.Id != null,
FIELD_ERROR_SESSION_SERVER_USER_API_KEY_ID_NULL);

return sessionServer.UserApiKey.Id.Value;
}
else if (sessionServer.UserPaymentServiceProvider != null)

if (sessionServer.UserPaymentServiceProvider != null)
{
Debug.Assert(
sessionServer.UserPaymentServiceProvider.Id != null,
ErrorSessionserverUserpaymentserviceproviderkeyIdNull
FIELD_ERROR_SESSION_SERVER_USER_PAYMENT_SERVICE_PROVIDER_KEY_ID_NULL
);

return sessionServer.UserPaymentServiceProvider.Id.Value;
}
else
{
throw new BunqException(ErrorCouldNotDetermineUserId);
}

throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_USER_ID);
}

private static double GetSessionTimeout(SessionServer sessionServer)
Expand All @@ -92,22 +134,23 @@ private static double GetSessionTimeout(SessionServer sessionServer)
{
return GetSessionTimeOutForUser(sessionServer.UserApiKey.RequestedByUser.GetReferencedObject());
}
else if (sessionServer.UserCompany != null)

if (sessionServer.UserCompany != null)
{
return GetSessionTimeOutForUser(sessionServer.UserCompany);
}
else if (sessionServer.UserPerson != null)

if (sessionServer.UserPerson != null)
{
return GetSessionTimeOutForUser(sessionServer.UserPerson);
}
else if (sessionServer.UserPaymentServiceProvider != null)

if (sessionServer.UserPaymentServiceProvider != null)
{
return GetSessionTimeOutForUser(sessionServer.UserPaymentServiceProvider);
}
else
{
throw new BunqException(ErrorCouldNotDetermineSessionTimeout);
}

throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT);
}

private static double GetSessionTimeOutForUser(BunqModel user)
Expand All @@ -128,7 +171,7 @@ private static double GetSessionTimeOutForUser(BunqModel user)
}
else
{
throw new BunqException(ErrorCouldNotDetermineSessionTimeout);
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT);
}

return GetDoubleFromSessionTimeout(sessionTimeout);
Expand All @@ -138,12 +181,38 @@ private static double GetDoubleFromSessionTimeout(int? sessionTimeout)
{
if (sessionTimeout == null)
{
throw new BunqException(ErrorSessionTimeoutIsNull);
throw new BunqException(FIELD_ERROR_SESSION_TIMEOUT_IS_NULL);
}
else

return (double) sessionTimeout;
}

public BunqModel GetUserReference()
{
if (UserCompany == null && UserApiKey == null && UserPerson != null && UserPaymentServiceProvider == null)
{
return (double) sessionTimeout;
return UserPerson;
}

if (UserPerson == null && UserApiKey == null && UserCompany != null &&
UserPaymentServiceProvider == null)
{
return UserCompany;
}

if (UserPerson == null && UserCompany == null && UserApiKey != null &&
UserPaymentServiceProvider == null)
{
return UserApiKey;
}

if (UserPerson == null && UserCompany == null && UserApiKey == null &&
UserPaymentServiceProvider != null)
{
return UserPaymentServiceProvider;
}

throw new BunqException(FIELD_ERROR_ALL_FIELD_NULL);
}
}
}
Loading

0 comments on commit 3005c18

Please sign in to comment.