Skip to content

Commit

Permalink
Add mocked institutions endpoint tests, simplify return type
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Apr 26, 2024
1 parent 8e5b8e6 commit 3e9cdff
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public void Setup()
/// <summary>
/// Tests the retrieving of institutions for all countries and a specific country (Great Britain).
/// </summary>
/// <returns></returns>
[Test]
public async Task GetInstitutions()
{
Expand All @@ -40,7 +39,6 @@ public async Task GetInstitutions()
/// <summary>
/// Tests the retrieving of institutions with various query parameters set.
/// </summary>
/// <returns></returns>
[Test]
public async Task GetInstitutionsWithFlags()
{
Expand Down Expand Up @@ -78,20 +76,18 @@ public async Task GetInstitutionsWithFlags()
/// <summary>
/// Tests the retrieving of a specific institution.
/// </summary>
/// <returns></returns>
[Test]
public async Task GetInstitution()
{
var response = await _apiClient.InstitutionsEndpoint.GetInstitution("SANDBOXFINANCE_SFIN0000");
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(response, HttpStatusCode.OK);

var result = response.Result!;
Assert.Multiple(() =>
{
Assert.That(result.Bic, Is.EqualTo("SFIN0000"));
Assert.That(result.Id, Is.EqualTo("SANDBOXFINANCE_SFIN0000"));
Assert.That(result.Name, Is.EqualTo("Sandbox Finance"));
Assert.That(result.TransactionTotalDays, Is.EqualTo(90));
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(response, HttpStatusCode.OK);
Assert.That(response.Result!.Bic, Is.EqualTo("SFIN0000"));
Assert.That(response.Result!.Id, Is.EqualTo("SANDBOXFINANCE_SFIN0000"));
Assert.That(response.Result!.Name, Is.EqualTo("Sandbox Finance"));
Assert.That(response.Result!.TransactionTotalDays, Is.EqualTo(90));
});
}

Expand All @@ -102,19 +98,34 @@ public async Task GetInstitution()
/// <summary>
/// Tests the retrieving of institutions for a country which is not covered by the API.
/// </summary>
/// <returns></returns>
[Test]
public async Task GetInstitutionsForNotCoveredCountry()
{
var response = await _apiClient.InstitutionsEndpoint.GetInstitutions("US");
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
Assert.That(response.Error!.Detail, Is.EqualTo("US is not a valid choice."));
Assert.That(response.Error!.Summary, Is.EqualTo("Invalid country choice."));
});
}

/// <summary>
/// Tests the retrieving of an institution with an invalid id.
/// </summary>
[Test]
public async Task GetNonExistingInstitution()
{
var response = await _apiClient.InstitutionsEndpoint.GetInstitution("invalid_id");

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.NotFound);
Assert.That(response.Error!.Detail, Is.EqualTo("Not found."));
Assert.That(response.Error!.Summary, Is.EqualTo("Not found."));
});
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ private async Task ExecuteExampleRequest(NordigenClient apiClient)
public async Task ExecuteRequestsUntilRateLimitReached()
{
var apiClient = TestHelpers.GetConfiguredClient();
NordigenApiResponse<List<Institution>, InstitutionsError>? unsuccessfulRequest = null;
NordigenApiResponse<List<Institution>, BasicResponse>? unsuccessfulRequest = null;

while (unsuccessfulRequest is null)
{
var tasks = new ConcurrentBag<Task<NordigenApiResponse<List<Institution>, InstitutionsError>>>();
var tasks = new ConcurrentBag<Task<NordigenApiResponse<List<Institution>, BasicResponse>>>();
Parallel.For(0, 10, _ =>
{
var task = apiClient.InstitutionsEndpoint.GetInstitutions("LI");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ namespace RobinTTY.NordigenApiClient.Tests.Mocks.Endpoints;
public class InstitutionsEndpointTests
{
#region RequestsWithSuccessfulResponse

/// <summary>
/// Tests the retrieving of institutions for all countries and a specific country (Great Britain).
/// </summary>
[Test]
public async Task GetInstitutions()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.InstitutionsEndpointMockData.GetInstitutions, HttpStatusCode.OK);

var institutions = await apiClient.InstitutionsEndpoint.GetInstitutions();
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(institutions, HttpStatusCode.OK);
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.InstitutionsEndpointMockData.GetInstitutions,
HttpStatusCode.OK);

var result = institutions.Result!.ToList();
var institutions = await apiClient.InstitutionsEndpoint.GetInstitutions();

Assert.Multiple(() => { Assert.That(result, Has.Count.EqualTo(2)); });
Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(institutions, HttpStatusCode.OK);
Assert.That(institutions.Result!, Has.Count.EqualTo(2));
});
}

/// <summary>
Expand All @@ -29,12 +31,11 @@ public async Task GetInstitutions()
[Test]
public async Task GetInstitution()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.InstitutionsEndpointMockData.GetInstitution, HttpStatusCode.OK);

var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.InstitutionsEndpointMockData.GetInstitution,
HttpStatusCode.OK);

var institution = await apiClient.InstitutionsEndpoint.GetInstitution("N26_NTSBDEB1");
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(institution, HttpStatusCode.OK);

var result = institution.Result!;
var expectedSupportedFeatures = new[]
{
"account_selection",
Expand All @@ -43,21 +44,67 @@ public async Task GetInstitution()
"payments",
"private_accounts"
};

Assert.Multiple(() =>
{
Assert.That(result.Bic, Is.EqualTo("NTSBDEB1"));
Assert.That(result.Id, Is.EqualTo("N26_NTSBDEB1"));
Assert.That(result.Name, Is.EqualTo("N26 Bank"));
Assert.That(result.TransactionTotalDays, Is.EqualTo(90));
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(institution, HttpStatusCode.OK);
Assert.That(institution.Result!.Bic, Is.EqualTo("NTSBDEB1"));
Assert.That(institution.Result!.Id, Is.EqualTo("N26_NTSBDEB1"));
Assert.That(institution.Result!.Name, Is.EqualTo("N26 Bank"));
Assert.That(institution.Result!.TransactionTotalDays, Is.EqualTo(90));

Assert.That(result.SupportedPayments?.SinglePayment, Contains.Item(PaymentProduct.SepaCreditTransfers));
Assert.That(result.SupportedPayments?.SinglePayment,
Assert.That(institution.Result!.SupportedPayments?.SinglePayment,
Contains.Item(PaymentProduct.SepaCreditTransfers));
Assert.That(institution.Result!.SupportedPayments?.SinglePayment,
Contains.Item(PaymentProduct.InstantSepaCreditTransfer));
Assert.That(result.SupportedFeatures, Is.EqualTo(expectedSupportedFeatures));
Assert.That(result.IdentificationCodes, Is.Empty);
Assert.That(institution.Result!.SupportedFeatures, Is.EqualTo(expectedSupportedFeatures));
Assert.That(institution.Result!.IdentificationCodes, Is.Empty);
});
}

#endregion

#region RequestsWithErrors

/// <summary>
/// Tests the retrieving of institutions for a country which is not covered by the API.
/// </summary>
[Test]
public async Task GetInstitutionsForNotCoveredCountry()
{
var apiClient = TestHelpers.GetMockClient(
TestHelpers.MockData.InstitutionsEndpointMockData.GetInstitutionsForNotCoveredCountry,
HttpStatusCode.BadRequest);

var response = await apiClient.InstitutionsEndpoint.GetInstitutions("US");

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
Assert.That(response.Error!.Detail, Is.EqualTo("US is not a valid choice."));
Assert.That(response.Error!.Summary, Is.EqualTo("Invalid country choice."));
});
}


/// <summary>
/// Tests the retrieving of an institution with an invalid id.
/// </summary>
[Test]
public async Task GetNonExistingInstitution()
{
var apiClient = TestHelpers.GetMockClient(
TestHelpers.MockData.InstitutionsEndpointMockData.GetNonExistingInstitution,
HttpStatusCode.NotFound);

var response = await apiClient.InstitutionsEndpoint.GetInstitution("invalid_id");

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.NotFound);
Assert.That(response.Error!.Detail, Is.EqualTo("Not found."));
Assert.That(response.Error!.Summary, Is.EqualTo("Not found."));
});
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ internal class AgreementsEndpointMockData(
createAgreementWithInvalidParamsAtPolishInstitution;
}

internal class InstitutionsEndpointMockData(List<Institution> getInstitutions, Institution getInstitution)
internal class InstitutionsEndpointMockData(
List<Institution> getInstitutions,
Institution getInstitution,
InstitutionsErrorInternal getInstitutionsForNotCoveredCountry,
BasicResponse getNonExistingInstitution)
{
public List<Institution> GetInstitutions { get; set; } = getInstitutions;
public Institution GetInstitution { get; set; } = getInstitution;
public InstitutionsErrorInternal GetInstitutionsForNotCoveredCountry { get; set; } = getInstitutionsForNotCoveredCountry;
public BasicResponse GetNonExistingInstitution { get; set; } = getNonExistingInstitution;
}

internal class RequisitionsEndpointMockData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@
"private_accounts"
],
"identification_codes": []
},
"GetInstitutionsForNotCoveredCountry": {
"country": {
"summary": "Invalid country choice.",
"detail": "US is not a valid choice."
},
"status_code": 400
},
"GetNonExistingInstitution": {
"summary": "Not found.",
"detail": "Not found.",
"status_code": 404
}
},
"RequisitionsEndpointMockData": {
Expand Down
2 changes: 1 addition & 1 deletion src/RobinTTY.NordigenApiClient.Tests/Shared/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static TestHelpers()
Converters =
{
new JsonWebTokenConverter(), new GuidConverter(),
new CultureSpecificDecimalConverter(), new InstitutionsErrorConverter()
new CultureSpecificDecimalConverter()
}
};
MockData = JsonSerializer.Deserialize<MockResponsesModel>(json, JsonSerializerOptions) ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface IInstitutionsEndpoint
/// A <see cref="NordigenApiResponse{TResponse, TError}" /> containing a list of supported institutions if the
/// request was successful.
/// </returns>
Task<NordigenApiResponse<List<Institution>, InstitutionsError>> GetInstitutions(string? country = null,
Task<NordigenApiResponse<List<Institution>, BasicResponse>> GetInstitutions(string? country = null,
bool? accessScopesSupported = null, bool? accountSelectionSupported = null,
bool? businessAccountsSupported = null,
bool? cardAccountsSupported = null, bool? corporateAccountsSupported = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class InstitutionsEndpoint : IInstitutionsEndpoint
internal InstitutionsEndpoint(NordigenClient client) => _nordigenClient = client;

/// <inheritdoc />
public async Task<NordigenApiResponse<List<Institution>, InstitutionsError>> GetInstitutions(string? country = null,
public async Task<NordigenApiResponse<List<Institution>, BasicResponse>> GetInstitutions(string? country = null,
bool? accessScopesSupported = null, bool? accountSelectionSupported = null,
bool? businessAccountsSupported = null,
bool? cardAccountsSupported = null, bool? corporateAccountsSupported = null,
Expand All @@ -27,6 +27,8 @@ public async Task<NordigenApiResponse<List<Institution>, InstitutionsError>> Get
{
var query = new List<KeyValuePair<string, string>>();
if (country != null) query.Add(new KeyValuePair<string, string>("country", country));

// Add any required query parameter
if (accessScopesSupported.HasValue)
query.Add(GetSupportFlagQuery("access_scopes_supported", accessScopesSupported.Value));
if (accountSelectionSupported.HasValue)
Expand All @@ -49,8 +51,11 @@ public async Task<NordigenApiResponse<List<Institution>, InstitutionsError>> Get
if (ssnVerificationSupported.HasValue)
query.Add(GetSupportFlagQuery("ssn_verification_supported", ssnVerificationSupported.Value));

return await _nordigenClient.MakeRequest<List<Institution>, InstitutionsError>(
var response = await _nordigenClient.MakeRequest<List<Institution>, InstitutionsErrorInternal>(
NordigenEndpointUrls.InstitutionsEndpoint, HttpMethod.Get, cancellationToken, query);

return new NordigenApiResponse<List<Institution>, BasicResponse>(response.StatusCode, response.IsSuccess, response.Result,
response.Error);
}

private static KeyValuePair<string, string> GetSupportFlagQuery(string flag, bool value)
Expand Down

This file was deleted.

39 changes: 0 additions & 39 deletions src/RobinTTY.NordigenApiClient/Models/Errors/InstitutionsError.cs

This file was deleted.

Loading

0 comments on commit 3e9cdff

Please sign in to comment.