-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBunqSdkTestBase.cs
173 lines (145 loc) · 5.98 KB
/
BunqSdkTestBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
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
{
/// <summary>
/// Base class for all the Bunq SDK tests.
/// </summary>
public class BunqSdkTestBase
{
/// <summary>
/// Error constants.
/// </summary>
private const string FIELD_ERROR_COULD_NOT_DETERMINE_USER_ALIAS = "Could not determine user alias.";
/// <summary>
/// Name of the context configuration file.
/// </summary>
protected const string FilenameContextConf = "../../../bunq-test.conf";
/// <summary>
/// Constants for payment creation.
/// </summary>
protected const string PaymentAmountEur = "0.01";
protected const string PaymentCurrency = "EUR";
protected const string PaymentDescription = "C# test Payment";
/// <summary>
/// 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 = "/vader.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 = "[email protected]";
private const string EmailSuggarDaddy = "[email protected]";
/// <summary>
/// Spending money constants.
/// </summary>
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();
Thread.Sleep(500); // ensure requests are auto accepted.
BunqContext.UserContext.RefreshUserContext();
}
protected static ApiContext SetUpApiContext()
{
ApiContext apiContext;
if (File.Exists(FilenameContextConf))
{
apiContext = ApiContext.Restore(FilenameContextConf);
apiContext.EnsureSessionActive();
}
else
{
var sandboxUserPerson = GenerateNewSandboxUserPerson();
apiContext = ApiContext.Create(ApiEnvironmentType.SANDBOX, sandboxUserPerson.ApiKey, DeviceDescription);
}
BunqContext.LoadApiContext(apiContext);
return apiContext;
}
private static SandboxUserPerson GenerateNewSandboxUserPerson()
{
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");
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<SandboxUserPerson>(
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 new Pointer(PointerTypeEmail, EmailBravo);
}
protected static Pointer GetAlias()
{
var userContext = BunqContext.UserContext;
if (userContext.IsOnlyUserPersonSet())
{
return userContext.UserPerson.Alias.First();
}
if (userContext.IsOnlyUserCompanySet())
{
return userContext.UserCompany.Alias.First();
}
throw new BunqException(FIELD_ERROR_COULD_NOT_DETERMINE_USER_ALIAS);
}
}
}