Skip to content

Commit 8fc0969

Browse files
authored
Merge pull request #1 from bunq/feature/unit-tests
Add first wave unit-tests
2 parents af22267 + 583eedb commit 8fc0969

17 files changed

+844
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,5 @@ pip-log.txt
263263
# bunq specific
264264
bunq.conf
265265
**/Tmp/
266+
bunq-test.conf
267+
config.json
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Bunq.Sdk.Context;
5+
using Bunq.Sdk.Exception;
6+
using Bunq.Sdk.Model.Generated;
7+
8+
namespace Bunq.Sdk.Tests
9+
{
10+
/// <summary>
11+
/// Will check fi the current session token is still valid.
12+
/// </summary>
13+
public class ApiContextHandler
14+
{
15+
private const string FILENAME_CONTEXT_CONF = "../../../bunq-test.conf";
16+
private const string DEVICE_DESCRIPTION = "Csharp unit test";
17+
private static readonly string API_KEY = Config.GetApiKey();
18+
private static readonly string FIELD_PERMITTED_IP = Config.GetPermittedIp();
19+
20+
/// <summary>
21+
/// Calls IsSessionActive to check if the session token is still active and returns the ApiContext.
22+
/// </summary>
23+
public static ApiContext GetApiContext()
24+
{
25+
if (IsSessionActive()) return ApiContext.Restore(FILENAME_CONTEXT_CONF);
26+
27+
var permittedIp = new List<string> {FIELD_PERMITTED_IP};
28+
var apiContext = new ApiContext(ApiEnvironmentType.SANDBOX, API_KEY);
29+
apiContext.Initialize(DEVICE_DESCRIPTION, permittedIp);
30+
apiContext.Save(FILENAME_CONTEXT_CONF);
31+
32+
return apiContext;
33+
}
34+
35+
/// <summary>
36+
/// Catches BunqEception if the conf file does not exist.
37+
/// Catches ApiExceptoin if the session is inactive.
38+
/// </summary>
39+
/// <returns>True, if the restores ApiContext can be used, otherwise false.</returns>
40+
private static bool IsSessionActive()
41+
{
42+
try
43+
{
44+
var apiContext = ApiContext.Restore(FILENAME_CONTEXT_CONF);
45+
if (apiContext == null)
46+
return false;
47+
48+
User.List(apiContext);
49+
50+
return true;
51+
}
52+
catch (BunqException)
53+
{
54+
return false;
55+
}
56+
catch (ApiException)
57+
{
58+
return false;
59+
}
60+
}
61+
}
62+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp1.1</TargetFramework>
4+
<RootNamespace>Bunq.Sdk.Tests</RootNamespace>
5+
<LangVersion>5</LangVersion>
6+
<AssemblyName>BunqSdkCsharpTest.xunit.runner.json</AssemblyName>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
10+
<PackageReference Include="xunit" Version="2.2.0" />
11+
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
12+
<Content Include="xunit.runner.json">
13+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
14+
</Content>
15+
</ItemGroup>
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\BunqSdkCsharp.csproj" />
18+
</ItemGroup>
19+
</Project>

Tests/BunqSdkCsharpTest/Config.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.IO;
2+
using Bunq.Sdk.Model.Generated.Object;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace Bunq.Sdk.Tests
6+
{
7+
public class Config
8+
{
9+
private const string FIELD_CONFIG_FILE_PATH = "../../../Resources/config.json";
10+
private const string FIELD_USER_ID = "USER_ID";
11+
private const string FIELD_API_KEY = "API_KEY";
12+
private const string FIELD_PERMITTED_IP = "ipAddress";
13+
private const string FIELD_ATTACHMENT_PUBLIC_TEST = "AttachmentPublicTest";
14+
private const string FIELD_ATTACHMENT_PATH_IN = "PATH_IN";
15+
private const string FIELD_ATTACHMENT_DESCRIPTION = "DESCRIPTION";
16+
private const string FIELD_ATTACHMENT_CONTENT_TYPE = "CONTENT_TYPE";
17+
private const string FIELD_MONETARY_ACCOUNT_ID = "MA_ID";
18+
private const string FIELD_MONETARY_ACCOUNT_ID2 = "MA_ID2";
19+
private const string FIELD_COUNTER_PARTY_SELF = "CounterPartySelf";
20+
private const string FIELD_COUNTER_PARTY_OTHER = "CounterPartyOther";
21+
private const string FIELD_COUNTER_ALIAS = "Alias";
22+
private const string FIELD_COUNTER_TYPE = "Type";
23+
private const string FIELD_TAB_USAGE_SINGLE = "TabUsageSingleTest";
24+
private const string FIELD_CASH_REGISTER_ID = "CASH_REGISTER_ID";
25+
26+
public static int GetCashRegisterId()
27+
{
28+
return GetConfig()[FIELD_TAB_USAGE_SINGLE][FIELD_CASH_REGISTER_ID].ToObject<int>();
29+
}
30+
31+
public static Pointer GetCounterAliasOther()
32+
{
33+
var alias = GetConfig()[FIELD_COUNTER_PARTY_OTHER][FIELD_COUNTER_ALIAS].ToString();
34+
var type = GetConfig()[FIELD_COUNTER_PARTY_OTHER][FIELD_COUNTER_TYPE].ToString();
35+
36+
return new Pointer(type, alias);
37+
}
38+
39+
public static Pointer GetCounterAliasSelf()
40+
{
41+
var alias = GetConfig()[FIELD_COUNTER_PARTY_SELF][FIELD_COUNTER_ALIAS].ToString();
42+
var type = GetConfig()[FIELD_COUNTER_PARTY_SELF][FIELD_COUNTER_TYPE].ToString();
43+
44+
return new Pointer(type, alias);
45+
}
46+
47+
public static int GetSecondMonetaryAccountId()
48+
{
49+
return GetConfig()[FIELD_MONETARY_ACCOUNT_ID2].ToObject<int>();
50+
}
51+
52+
public static int GetMonetarytAccountId()
53+
{
54+
return GetConfig()[FIELD_MONETARY_ACCOUNT_ID].ToObject<int>();
55+
}
56+
57+
public static string GetAttachmentPathIn()
58+
{
59+
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_PATH_IN].ToString();
60+
}
61+
62+
public static string GetAttachmentDescrpition()
63+
{
64+
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_DESCRIPTION].ToString();
65+
}
66+
67+
public static string GetAttachmentContentType()
68+
{
69+
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_CONTENT_TYPE].ToString();
70+
}
71+
72+
public static string GetPermittedIp()
73+
{
74+
return GetConfig()[FIELD_PERMITTED_IP].ToString();
75+
}
76+
public static string GetApiKey()
77+
{
78+
return GetConfig()[FIELD_API_KEY].ToString();
79+
}
80+
81+
public static int GetUserId()
82+
{
83+
return GetConfig()[FIELD_USER_ID].ToObject<int>();
84+
}
85+
86+
private static JObject GetConfig()
87+
{
88+
var fileContentString = File.ReadAllText(FIELD_CONFIG_FILE_PATH);
89+
90+
return JObject.Parse(fileContentString);
91+
}
92+
93+
}
94+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using Bunq.Sdk.Context;
4+
using Bunq.Sdk.Http;
5+
using Bunq.Sdk.Model.Generated;
6+
using Xunit;
7+
8+
namespace Bunq.Sdk.Tests.Model.Generated
9+
{
10+
/// <summary>
11+
/// Tests:
12+
/// AttachmentPublic
13+
/// AttachmentPublicContent
14+
/// </summary>
15+
public class AttachmentPublicTest
16+
{
17+
/// <summary>
18+
/// Config values.
19+
/// </summary>
20+
private const string PATH_ATTACHMENT = "../../../Resources";
21+
22+
private static readonly string CONTENT_TYPE = Config.GetAttachmentContentType();
23+
private static readonly string ATTACHMENT_DESCRIPTION = Config.GetAttachmentDescrpition();
24+
private static readonly string ATTACHMENT_PATH_IN = Config.GetAttachmentPathIn();
25+
26+
/// <summary>
27+
/// API context to use for the test API calls.
28+
/// </summary>
29+
private static readonly ApiContext API_CONTEXT = ApiContextHandler.GetApiContext();
30+
31+
/// <summary>
32+
/// Tests if the file we upload is the file we are getting back once successfully uploaded does.
33+
/// this by comparing the content of the files.
34+
/// </summary>
35+
[Fact]
36+
public void TestAttachmentUploadAndRetrieval()
37+
{
38+
var fileContentBytes = File.ReadAllBytes(PATH_ATTACHMENT + ATTACHMENT_PATH_IN);
39+
var customHeaders = new Dictionary<string, string>
40+
{
41+
{ApiClient.HEADER_CONTENT_TYPE, CONTENT_TYPE},
42+
{ApiClient.HEADER_ATTACHMENT_DESCRIPTION, ATTACHMENT_DESCRIPTION}
43+
};
44+
45+
var attachmentUuid = AttachmentPublic.Create(API_CONTEXT, fileContentBytes, customHeaders);
46+
var responseBytes = AttachmentPublicContent.List(API_CONTEXT, attachmentUuid);
47+
48+
Assert.Equal(fileContentBytes, responseBytes);
49+
}
50+
}
51+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using Bunq.Sdk.Context;
4+
using Bunq.Sdk.Http;
5+
using Bunq.Sdk.Model.Generated;
6+
using Xunit;
7+
8+
namespace Bunq.Sdk.Tests.Model.Generated
9+
{
10+
/// <summary>
11+
/// Tests:
12+
/// Avatar
13+
/// AttachmentPublic
14+
/// AttachmentPublicContent
15+
/// </summary>
16+
public class AvatarTest
17+
{
18+
/// <summary>
19+
/// Config values.
20+
/// </summary>
21+
private const string PATH_TO_ATTACHMENT = "../../../Resources";
22+
private const int INDEX_FIRST = 0;
23+
24+
private static readonly string CONTEN_TYPE = Config.GetAttachmentContentType();
25+
private static readonly string ATTACHMENT_DECSRIPTION = Config.GetAttachmentDescrpition();
26+
private static readonly string ATTACHMENT_PATH_IN = Config.GetAttachmentPathIn();
27+
28+
/// <summary>
29+
/// API context to use for the test API calls.
30+
/// </summary>
31+
private static readonly ApiContext API_CONTEXT = ApiContextHandler.GetApiContext();
32+
33+
/// <summary>
34+
/// Tests the creation of an avatar by uploading a picture via AttachmentPublic and setting it as avatar
35+
/// via the uuid.
36+
/// </summary>
37+
[Fact]
38+
public void TestCreateAvatarAndRetrieval()
39+
{
40+
var fileContentByte = File.ReadAllBytes(PATH_TO_ATTACHMENT + ATTACHMENT_PATH_IN);
41+
var attachmentUuid = UploadAvatarAndGetUuid(fileContentByte);
42+
43+
var avatarMap = new Dictionary<string, object>
44+
{
45+
{Avatar.FIELD_ATTACHMENT_PUBLIC_UUID, attachmentUuid}
46+
};
47+
var avatarUuid = Avatar.Create(API_CONTEXT, avatarMap);
48+
49+
var attachmentUuidFromAvatar = Avatar.Get(API_CONTEXT, avatarUuid).Image[INDEX_FIRST].AttachmentPublicUuid;
50+
var revievedFileContentByte = AttachmentPublicContent.List(API_CONTEXT, attachmentUuidFromAvatar);
51+
52+
Assert.Equal(attachmentUuid, attachmentUuidFromAvatar);
53+
Assert.Equal(fileContentByte, revievedFileContentByte);
54+
}
55+
56+
private static string UploadAvatarAndGetUuid(byte[] fileContentByte)
57+
{
58+
var customHeaders = new Dictionary<string, string>
59+
{
60+
{ApiClient.HEADER_ATTACHMENT_DESCRIPTION, ATTACHMENT_DECSRIPTION},
61+
{ApiClient.HEADER_CONTENT_TYPE, CONTEN_TYPE},
62+
};
63+
64+
return AttachmentPublic.Create(API_CONTEXT, fileContentByte, customHeaders);
65+
}
66+
}
67+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Bunq.Sdk.Context;
4+
using Bunq.Sdk.Model.Generated;
5+
using Bunq.Sdk.Model.Generated.Object;
6+
using Xunit;
7+
8+
namespace Bunq.Sdk.Tests.Model.Generated
9+
{
10+
/// <summary>
11+
/// Tests:
12+
/// CardDebit
13+
/// CardName
14+
/// </summary>
15+
public class CardDebitTest
16+
{
17+
/// <summary>
18+
/// Config values.
19+
/// </summary>
20+
private const string PIN_CODE = "4045";
21+
private const int INDEX_FIRST = 0;
22+
private const int NONNEGATIVE_INTEGER_MINIMUM = 0;
23+
private const int BASE_DECIMAL = 10;
24+
private const int CARD_SECOND_LINE_LENGTH_MAXIMUM = 20;
25+
private const int NUMBER_ONE = 1;
26+
27+
private static readonly int USER_ID = Config.GetUserId();
28+
29+
/// <summary>
30+
/// API context used to for the test API calls.
31+
/// </summary>
32+
private static readonly ApiContext API_CONTEXT = ApiContextHandler.GetApiContext();
33+
34+
/// <summary>
35+
/// Tests ordering a new card and checks if the fields we have entered are indeed correct by.
36+
/// </summary>
37+
[Fact]
38+
public void TestOrderNewMaestroCard()
39+
{
40+
var cardDebitMap = new Dictionary<string, object>
41+
{
42+
{CardDebit.FIELD_ALIAS, GetAlias()},
43+
{CardDebit.FIELD_NAME_ON_CARD, GetAnAllowedName()},
44+
{CardDebit.FIELD_PIN_CODE, PIN_CODE},
45+
{CardDebit.FIELD_SECOND_LINE, GenerateRandomSecondLine()}
46+
};
47+
var cardDebit = CardDebit.Create(API_CONTEXT, cardDebitMap, USER_ID);
48+
49+
var cardFromCardEndpoint = Card.Get(API_CONTEXT, USER_ID, cardDebit.Id.Value);
50+
51+
Assert.Equal(cardDebit.SecondLine, cardFromCardEndpoint.SecondLine);
52+
Assert.Equal(cardDebit.Created, cardFromCardEndpoint.Created);
53+
Assert.Equal(cardDebit.NameOnCard, cardFromCardEndpoint.NameOnCard);
54+
}
55+
56+
private static string GetAnAllowedName()
57+
{
58+
return CardName.List(API_CONTEXT, USER_ID)[INDEX_FIRST].PossibleCardNameArray[INDEX_FIRST];
59+
}
60+
61+
private static string GenerateRandomSecondLine()
62+
{
63+
var random = new Random();
64+
65+
return random.Next(
66+
NONNEGATIVE_INTEGER_MINIMUM,
67+
(int) Math.Pow(BASE_DECIMAL, CARD_SECOND_LINE_LENGTH_MAXIMUM + NUMBER_ONE) - NUMBER_ONE
68+
).ToString();
69+
}
70+
71+
private static Pointer GetAlias()
72+
{
73+
return User.Get(API_CONTEXT, USER_ID).UserCompany.Alias[INDEX_FIRST];
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)