Skip to content

Commit 3005c18

Browse files
committed
Merge branch 'release/1.14.18'
2 parents d592b2c + f6ca8c2 commit 3005c18

File tree

14 files changed

+220
-153
lines changed

14 files changed

+220
-153
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[//]: # (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.)
1212

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

BunqSdk.Tests/BunqSdkTestBase.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class BunqSdkTestBase
1919
/// <summary>
2020
/// Error constants.
2121
/// </summary>
22-
private const string ErrorCouldNotDetermineUserAlias = "Could not determine user alias.";
22+
private const string FIELD_ERROR_COULD_NOT_DETERMINE_USER_ALIAS = "Could not determine user alias.";
2323

2424
/// <summary>
2525
/// Name of the context configuration file.
@@ -93,16 +93,16 @@ protected static ApiContext SetUpApiContext()
9393
}
9494
else
9595
{
96-
var sandboxUser = GenerateNewSandboxUser();
97-
apiContext = ApiContext.Create(ApiEnvironmentType.SANDBOX, sandboxUser.ApiKey, DeviceDescription);
96+
var sandboxUserPerson = GenerateNewSandboxUserPerson();
97+
apiContext = ApiContext.Create(ApiEnvironmentType.SANDBOX, sandboxUserPerson.ApiKey, DeviceDescription);
9898
}
9999

100100
BunqContext.LoadApiContext(apiContext);
101101

102102
return apiContext;
103103
}
104104

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

115-
var requestTask = httpClient.PostAsync(ApiEnvironmentType.SANDBOX.BaseUri + "sandbox-user", null);
115+
var requestTask = httpClient.PostAsync(ApiEnvironmentType.SANDBOX.BaseUri + "sandbox-user-person", null);
116116
requestTask.Wait();
117117

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

121-
return BunqJsonConvert.DeserializeObject<SandboxUser>(responseJson.First.First.First.First.First
122-
.ToString());
121+
return BunqJsonConvert.DeserializeObject<SandboxUserPerson>(
122+
responseJson.First.First.First.First.First.ToString()
123+
);
123124
}
124125

125126
private static MonetaryAccountBank SetUpSecondMonetaryAccount()
@@ -160,14 +161,13 @@ protected static Pointer GetAlias()
160161
{
161162
return userContext.UserPerson.Alias.First();
162163
}
163-
else if (userContext.IsOnlyUserCompanySet())
164+
165+
if (userContext.IsOnlyUserCompanySet())
164166
{
165167
return userContext.UserCompany.Alias.First();
166168
}
167-
else
168-
{
169-
throw new BunqException(ErrorCouldNotDetermineUserAlias);
170-
}
169+
170+
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_USER_ALIAS);
171171
}
172172
}
173173
}

BunqSdk.Tests/Config.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static int GetSecondMonetaryAccountId()
6262
return GetConfig()[FIELD_MONETARY_ACCOUNT_ID2].ToObject<int>();
6363
}
6464

65-
public static int GetMonetarytAccountId()
65+
public static int GetMonetaryAccountId()
6666
{
6767
return GetConfig()[FIELD_MONETARY_ACCOUNT_ID].ToObject<int>();
6868
}
@@ -72,7 +72,7 @@ public static string GetAttachmentPathIn()
7272
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_PATH_IN].ToString();
7373
}
7474

75-
public static string GetAttachmentDescrpition()
75+
public static string GetAttachmentDescription()
7676
{
7777
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_DESCRIPTION].ToString();
7878
}

BunqSdk.Tests/Context/ApiContextTest.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ public class ApiContextTest : BunqSdkTestBase, IClassFixture<ApiContextTest>
1414
/// <summary>
1515
/// Path to a temporary context file.
1616
/// </summary>
17-
private const string ContextFilenameTest = "context-save-restore-test.conf";
17+
private const string FIELD_CONTEXT_FILENAME_TEST = "context-save-restore-test.conf";
1818

1919
/// <summary>
2020
/// Field constants.
2121
/// </summary>
22-
private const string FieldSessionContext = "session_context";
23-
private const string FieldExpiryTime = "expiry_time";
22+
private const string FIELD_FIELD_SESSION_CONTEXT = "session_context";
23+
private const string FIELD_FIELD_EXPIRY_TIME = "expiry_time";
2424

2525
private static ApiContext apiContext;
2626

2727
public ApiContextTest()
2828
{
29-
if (apiContext == null) apiContext = SetUpApiContext();
29+
if (apiContext == null)
30+
{
31+
apiContext = SetUpApiContext();
32+
}
3033
}
3134

3235
/// <summary>
@@ -48,8 +51,8 @@ public void TestApiContextSerializeDeserialize()
4851
public void TestApiContextSaveRestore()
4952
{
5053
var apiContextJson = apiContext.ToJson();
51-
apiContext.Save(ContextFilenameTest);
52-
var apiContextRestored = ApiContext.Restore(ContextFilenameTest);
54+
apiContext.Save(FIELD_CONTEXT_FILENAME_TEST);
55+
var apiContextRestored = ApiContext.Restore(FIELD_CONTEXT_FILENAME_TEST);
5356

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

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

BunqSdk.Tests/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,19 @@ These are the scenarios that are currently being tested:
2424
* Order a card with a second line
2525

2626
Besides these scenarios, some code of ApiContext, ApiClient and the JSON module
27-
are also tested :thumbs_up:.
27+
are also tested.
28+
29+
## Installation
30+
To run the tests, you must first generate a `key.pem` and a `credentials.pfx`.
31+
Navigate to the `/Resources` directory and execute the following the commands:
32+
33+
```
34+
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out chain.cert -days 365 -nodes -subj '/CN=My Test App PISP AISP/C=NL
35+
openssl pkcs12 -inkey key.pem -in chain.cert -export -out credentials.pfx
36+
```
37+
38+
You will be requested to enter a passphrase.
39+
Use the passphrase `secret` (as is defined in [`Psd2ApiContextTest.cs`](./BunqSdkCsharpTest/Context/Psd2ApiContextTest.cs)).
2840

2941
## Configuration
3042
To run the tests you must first setup the test configuration JSON. The example

BunqSdk/BunqSdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageId>Bunq.Sdk</PackageId>
99
</PropertyGroup>
1010
<PropertyGroup>
11-
<VersionPrefix>1.14.1</VersionPrefix>
11+
<VersionPrefix>1.14.18</VersionPrefix>
1212
</PropertyGroup>
1313
<PropertyGroup>
1414
<RootNamespace>Bunq.Sdk</RootNamespace>

BunqSdk/Context/BunqContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public static UserContext UserContext
4949
public static void LoadApiContext(ApiContext apiContextToLoad)
5050
{
5151
ApiContext = apiContextToLoad;
52-
UserContext = new UserContext(apiContextToLoad.SessionContext.UserId);
52+
UserContext = new UserContext(
53+
apiContextToLoad.SessionContext.UserId,
54+
apiContextToLoad.SessionContext.GetUserReference());
5355
UserContext.InitPrimaryMonetaryAccount();
5456
}
5557

BunqSdk/Context/SessionContext.cs

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ namespace Bunq.Sdk.Context
1313
public class SessionContext
1414
{
1515
/// <summary>
16-
/// Error constatns.
16+
/// Error constants.
1717
/// </summary>
18-
private const string ErrorCouldNotDetermineUserId = "Could not determine user id.";
19-
private const string ErrorSessionserverUserapikeyIdNull = "sessionServer.UserApiKey.Id != null";
20-
private const string ErrorSessionserverUserpaymentserviceproviderkeyIdNull =
18+
private const string FIELD_ERROR_COULD_NOT_DETERMINE_USER_ID = "Could not determine user id.";
19+
private const string FIELD_ERROR_SESSION_SERVER_USER_API_KEY_ID_NULL = "sessionServer.UserApiKey.Id != null";
20+
private const string FIELD_ERROR_SESSION_SERVER_USER_PAYMENT_SERVICE_PROVIDER_KEY_ID_NULL =
2121
"sessionServer.UserPaymentServiceProvider.Id != null";
22-
private const string ErrorSessionserverUsercompanyIdNull = "sessionServer.UserCompany.Id != null";
23-
private const string ErrorsessionserverUserpersonIdNull = "sessionServer.UserPerson.Id != null";
24-
private const string ErrorCouldNotDetermineSessionTimeout = "Could not determine session timeout.";
25-
private const string ErrorSessionTimeoutIsNull = "Session timeout is null.";
22+
private const string FIELD_ERROR_SESSION_SERVER_USER_COMPANY_ID_NULL = "sessionServer.UserCompany.Id != null";
23+
private const string FIELD_ERROR_SESSION_SERVER_USER_PERSON_ID_NULL = "sessionServer.UserPerson.Id != null";
24+
private const string FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT = "Could not determine session timeout.";
25+
private const string FIELD_ERROR_SESSION_TIMEOUT_IS_NULL = "Session timeout is null.";
26+
private const string FIELD_ERROR_ALL_FIELD_NULL = "All fields of an extended model or object are null.";
2627

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

43+
[JsonProperty(PropertyName = "user_person")]
44+
public UserPerson UserPerson { get; private set; }
45+
46+
[JsonProperty(PropertyName = "user_company")]
47+
public UserCompany UserCompany { get; private set; }
48+
49+
[JsonProperty(PropertyName = "user_api_key")]
50+
public UserApiKey UserApiKey { get; private set; }
51+
52+
[JsonProperty(PropertyName = "user_payment_service_provider")]
53+
public UserPaymentServiceProvider UserPaymentServiceProvider { get; private set; }
54+
4255
[JsonConstructor]
4356
private SessionContext()
4457
{
@@ -49,41 +62,70 @@ public SessionContext(SessionServer sessionServer)
4962
Token = sessionServer.SessionToken.Token;
5063
ExpiryTime = DateTime.Now.AddSeconds(GetSessionTimeout(sessionServer));
5164
UserId = GetUserId(sessionServer);
65+
SetUser(sessionServer.GetUserReference());
66+
}
67+
68+
private void SetUser(BunqModel user)
69+
{
70+
if (user.GetType() == typeof(UserPerson))
71+
{
72+
UserPerson = (UserPerson) user;
73+
}
74+
else if (user.GetType() == typeof(UserCompany))
75+
{
76+
UserCompany = (UserCompany) user;
77+
}
78+
else if (user.GetType() == typeof(UserApiKey))
79+
{
80+
UserApiKey = (UserApiKey) user;
81+
}
82+
else if (user.GetType() == typeof(UserPaymentServiceProvider))
83+
{
84+
UserPaymentServiceProvider = (UserPaymentServiceProvider) user;
85+
}
86+
else
87+
{
88+
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT);
89+
}
5290
}
5391

5492
private static int GetUserId(SessionServer sessionServer)
5593
{
5694
if (sessionServer.UserCompany != null)
5795
{
58-
Debug.Assert(sessionServer.UserCompany.Id != null, ErrorSessionserverUsercompanyIdNull);
96+
Debug.Assert(sessionServer.UserCompany.Id != null,
97+
FIELD_ERROR_SESSION_SERVER_USER_COMPANY_ID_NULL);
5998

6099
return sessionServer.UserCompany.Id.Value;
61100
}
62-
else if (sessionServer.UserPerson != null)
101+
102+
if (sessionServer.UserPerson != null)
63103
{
64-
Debug.Assert(sessionServer.UserPerson.Id != null, ErrorsessionserverUserpersonIdNull);
104+
Debug.Assert(sessionServer.UserPerson.Id != null,
105+
FIELD_ERROR_SESSION_SERVER_USER_PERSON_ID_NULL);
65106

66107
return sessionServer.UserPerson.Id.Value;
67108
}
68-
else if (sessionServer.UserApiKey != null)
109+
110+
if (sessionServer.UserApiKey != null)
69111
{
70-
Debug.Assert(sessionServer.UserApiKey.Id != null, ErrorSessionserverUserapikeyIdNull);
112+
Debug.Assert(sessionServer.UserApiKey.Id != null,
113+
FIELD_ERROR_SESSION_SERVER_USER_API_KEY_ID_NULL);
71114

72115
return sessionServer.UserApiKey.Id.Value;
73116
}
74-
else if (sessionServer.UserPaymentServiceProvider != null)
117+
118+
if (sessionServer.UserPaymentServiceProvider != null)
75119
{
76120
Debug.Assert(
77121
sessionServer.UserPaymentServiceProvider.Id != null,
78-
ErrorSessionserverUserpaymentserviceproviderkeyIdNull
122+
FIELD_ERROR_SESSION_SERVER_USER_PAYMENT_SERVICE_PROVIDER_KEY_ID_NULL
79123
);
80124

81125
return sessionServer.UserPaymentServiceProvider.Id.Value;
82126
}
83-
else
84-
{
85-
throw new BunqException(ErrorCouldNotDetermineUserId);
86-
}
127+
128+
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_USER_ID);
87129
}
88130

89131
private static double GetSessionTimeout(SessionServer sessionServer)
@@ -92,22 +134,23 @@ private static double GetSessionTimeout(SessionServer sessionServer)
92134
{
93135
return GetSessionTimeOutForUser(sessionServer.UserApiKey.RequestedByUser.GetReferencedObject());
94136
}
95-
else if (sessionServer.UserCompany != null)
137+
138+
if (sessionServer.UserCompany != null)
96139
{
97140
return GetSessionTimeOutForUser(sessionServer.UserCompany);
98141
}
99-
else if (sessionServer.UserPerson != null)
142+
143+
if (sessionServer.UserPerson != null)
100144
{
101145
return GetSessionTimeOutForUser(sessionServer.UserPerson);
102146
}
103-
else if (sessionServer.UserPaymentServiceProvider != null)
147+
148+
if (sessionServer.UserPaymentServiceProvider != null)
104149
{
105150
return GetSessionTimeOutForUser(sessionServer.UserPaymentServiceProvider);
106151
}
107-
else
108-
{
109-
throw new BunqException(ErrorCouldNotDetermineSessionTimeout);
110-
}
152+
153+
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT);
111154
}
112155

113156
private static double GetSessionTimeOutForUser(BunqModel user)
@@ -128,7 +171,7 @@ private static double GetSessionTimeOutForUser(BunqModel user)
128171
}
129172
else
130173
{
131-
throw new BunqException(ErrorCouldNotDetermineSessionTimeout);
174+
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT);
132175
}
133176

134177
return GetDoubleFromSessionTimeout(sessionTimeout);
@@ -138,12 +181,38 @@ private static double GetDoubleFromSessionTimeout(int? sessionTimeout)
138181
{
139182
if (sessionTimeout == null)
140183
{
141-
throw new BunqException(ErrorSessionTimeoutIsNull);
184+
throw new BunqException(FIELD_ERROR_SESSION_TIMEOUT_IS_NULL);
142185
}
143-
else
186+
187+
return (double) sessionTimeout;
188+
}
189+
190+
public BunqModel GetUserReference()
191+
{
192+
if (UserCompany == null && UserApiKey == null && UserPerson != null && UserPaymentServiceProvider == null)
144193
{
145-
return (double) sessionTimeout;
194+
return UserPerson;
146195
}
196+
197+
if (UserPerson == null && UserApiKey == null && UserCompany != null &&
198+
UserPaymentServiceProvider == null)
199+
{
200+
return UserCompany;
201+
}
202+
203+
if (UserPerson == null && UserCompany == null && UserApiKey != null &&
204+
UserPaymentServiceProvider == null)
205+
{
206+
return UserApiKey;
207+
}
208+
209+
if (UserPerson == null && UserCompany == null && UserApiKey == null &&
210+
UserPaymentServiceProvider != null)
211+
{
212+
return UserPaymentServiceProvider;
213+
}
214+
215+
throw new BunqException(FIELD_ERROR_ALL_FIELD_NULL);
147216
}
148217
}
149218
}

0 commit comments

Comments
 (0)