Skip to content

Commit

Permalink
Create mocked tests for agreements endpoint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Apr 25, 2024
1 parent 80e1c61 commit 456fdc4
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,6 @@ public async Task GetAgreement()
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(deleteResponse, HttpStatusCode.OK);
}

/// <summary>
/// Tests the retrieval of an agreement with an invalid guid.
/// </summary>
[Test]
public async Task GetAgreementWithInvalidGuid()
{
const string guid = "f84d7b8-dee4-4cd9-bc6d-842ef78f6028";
var response = await _apiClient.AgreementsEndpoint.GetAgreement(guid);
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
AssertionHelpers.AssertBasicResponseMatchesExpectations(response.Error, "Invalid EndUserAgreement ID",
$"{guid} is not a valid EndUserAgreement UUID. ");
}

/// <summary>
/// Tests the creation and deletion of an end user agreement.
/// </summary>
Expand Down Expand Up @@ -156,24 +143,20 @@ public async Task CreateAcceptAndDeleteAgreement()
#region RequestsWithErrors

/// <summary>
/// Tests the retrieving of an end user agreement with an invalid institution id.
/// Tests the retrieval of an agreement with an invalid guid.
/// </summary>
[Test]
public async Task GetAgreementWithInvalidInstitutionId()
public async Task GetAgreementWithInvalidGuid()
{
var agreement = new CreateAgreementRequest(90, 90,
["balances", "details", "transactions"], "invalid_institution");
const string guid = "f84d7b8-dee4-4cd9-bc6d-842ef78f6028";

var response = await _apiClient.AgreementsEndpoint.CreateAgreement(agreement);
var response = await _apiClient.AgreementsEndpoint.GetAgreement(guid);

AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
Assert.Multiple(() =>
{
Assert.That(response.Error!.InstitutionIdError, Is.Not.Null);
Assert.That(response.Error!.InstitutionIdError!.Summary,
Is.EqualTo("Unknown Institution ID invalid_institution"));
Assert.That(response.Error!.InstitutionIdError!.Detail,
Is.EqualTo("Get Institution IDs from /institutions/?country={$COUNTRY_CODE}"));
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
AssertionHelpers.AssertBasicResponseMatchesExpectations(response.Error, "Invalid EndUserAgreement ID",
$"{guid} is not a valid EndUserAgreement UUID. ");
});
}

Expand Down Expand Up @@ -232,15 +215,14 @@ public async Task CreateAgreementWithInvalidParamsAtPolishInstitution()
["balances", "transactions"], "PKO_BPKOPLPW");

var response = await _apiClient.AgreementsEndpoint.CreateAgreement(agreement);
var result = response.Error!;

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
Assert.That(new[] {result.InstitutionIdError, result.AgreementError}, Has.All.Null);
Assert.That(result.Detail,
Assert.That(new[] {response.Error!.InstitutionIdError, response.Error!.AgreementError}, Has.All.Null);
Assert.That(response.Error!.Detail,
Is.EqualTo("For this institution the following scopes are required together: ['details', 'balances']"));
Assert.That(result.Summary, Is.EqualTo("Institution access scope dependencies error"));
Assert.That(response.Error!.Summary, Is.EqualTo("Institution access scope dependencies error"));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ namespace RobinTTY.NordigenApiClient.Tests.Mocks.Endpoints;

public class AgreementsEndpointTests
{
#region RequestsWithSuccessfulResponse

/// <summary>
/// Tests the retrieval of end user agreements.
/// </summary>
[Test]
public async Task GetAgreements()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.GetAgreements, HttpStatusCode.OK);
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.GetAgreements,
HttpStatusCode.OK);

var agreements = await apiClient.AgreementsEndpoint.GetAgreements(100, 0);
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(agreements, HttpStatusCode.OK);
Expand Down Expand Up @@ -50,7 +53,8 @@ public async Task GetAgreements()
[Test]
public async Task GetAgreement()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.GetAgreement, HttpStatusCode.OK);
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.GetAgreement,
HttpStatusCode.OK);

var agreement = await apiClient.AgreementsEndpoint.GetAgreement(A.Dummy<Guid>());
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(agreement, HttpStatusCode.OK);
Expand All @@ -76,7 +80,8 @@ public async Task GetAgreement()
[Test]
public async Task CreateAgreement()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.CreateAgreement, HttpStatusCode.Created);
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.CreateAgreement,
HttpStatusCode.Created);

var agreementRequest = new CreateAgreementRequest(145, 145,
["balances", "details", "transactions"], "SANDBOXFINANCE_SFIN0000");
Expand All @@ -101,14 +106,118 @@ public async Task CreateAgreement()
[Test]
public async Task DeleteAgreement()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.DeleteAgreement, HttpStatusCode.OK);
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AgreementsEndpointMockData.DeleteAgreement,
HttpStatusCode.OK);

var result = await apiClient.AgreementsEndpoint.DeleteAgreement(A.Dummy<Guid>());
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(result, HttpStatusCode.OK);
Assert.Multiple(() =>
{
Assert.That(result.Result?.Summary, Is.EqualTo("End User Agreement deleted"));
Assert.That(result.Result?.Detail, Is.EqualTo("End User Agreement bb37bc52-5b1d-44f9-b1cd-ec9594f25387 deleted"));
Assert.That(result.Result?.Detail,
Is.EqualTo("End User Agreement bb37bc52-5b1d-44f9-b1cd-ec9594f25387 deleted"));
});
}

#endregion

#region RequestsWithErrors

/// <summary>
/// Tests the retrieval of an agreement with an invalid guid.
/// </summary>
[Test]
public async Task GetAgreementWithInvalidGuid()
{
const string guid = "f84d7b8-dee4-4cd9-bc6d-842ef78f6028";
var apiClient = TestHelpers.GetMockClient(
TestHelpers.MockData.AgreementsEndpointMockData.GetAgreementWithInvalidGuid,
HttpStatusCode.BadRequest);

var response = await apiClient.AgreementsEndpoint.GetAgreement(guid);

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
AssertionHelpers.AssertBasicResponseMatchesExpectations(response.Error, "Invalid EndUserAgreement ID",
$"{guid} is not a valid EndUserAgreement UUID. ");
});
}

/// <summary>
/// Tests the creation of an end user agreement with an invalid institution id.
/// </summary>
[Test]
public async Task CreateAgreementWithInvalidInstitutionId()
{
var apiClient = TestHelpers.GetMockClient(
TestHelpers.MockData.AgreementsEndpointMockData.CreateAgreementWithInvalidInstitutionId,
HttpStatusCode.BadRequest);
var agreement = new CreateAgreementRequest(90, 90,
["balances", "details", "transactions"], "invalid_institution");

var response = await apiClient.AgreementsEndpoint.CreateAgreement(agreement);

AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
Assert.Multiple(() =>
{
Assert.That(response.Error!.InstitutionIdError, Is.Not.Null);
Assert.That(response.Error!.InstitutionIdError!.Summary,
Is.EqualTo("Unknown Institution ID invalid_institution"));
Assert.That(response.Error!.InstitutionIdError!.Detail,
Is.EqualTo("Get Institution IDs from /institutions/?country={$COUNTRY_CODE}"));
});
}

/// <summary>
/// Tests the creation of an end user agreement with invalid parameters.
/// </summary>
[Test]
public async Task CreateAgreementWithInvalidParams()
{
var apiClient = TestHelpers.GetMockClient(
TestHelpers.MockData.AgreementsEndpointMockData.CreateAgreementWithInvalidParams,
HttpStatusCode.BadRequest);
var agreement = new CreateAgreementRequest(200, 200,
["balances", "details", "transactions", "invalid", "invalid2"], "SANDBOXFINANCE_SFIN0000");

var response = await apiClient.AgreementsEndpoint.CreateAgreement(agreement);
var result = response.Error!;

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
Assert.That(new[] {result.InstitutionIdError, result.AgreementError}, Has.All.Null);
Assert.That(result.AccessScopeError!.Detail,
Is.EqualTo("Choose one or several from ['balances', 'details', 'transactions']"));
Assert.That(result.AccessValidForDaysError!.Detail,
Is.EqualTo("access_valid_for_days must be > 0 and <= 180"));
Assert.That(result.MaxHistoricalDaysError!.Detail,
Is.EqualTo(
"max_historical_days must be > 0 and <= SANDBOXFINANCE_SFIN0000 transaction_total_days (90)"));
});
}

[Test]
public async Task CreateAgreementWithInvalidParamsAtPolishInstitution()
{
var apiClient = TestHelpers.GetMockClient(
TestHelpers.MockData.AgreementsEndpointMockData.CreateAgreementWithInvalidParamsAtPolishInstitution,
HttpStatusCode.BadRequest);
var agreement = new CreateAgreementRequest(90, 90,
["balances", "transactions"], "PKO_BPKOPLPW");

var response = await apiClient.AgreementsEndpoint.CreateAgreement(agreement);

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.BadRequest);
Assert.That(new[] {response.Error!.InstitutionIdError, response.Error!.AgreementError}, Has.All.Null);
Assert.That(response.Error!.Detail,
Is.EqualTo("For this institution the following scopes are required together: ['details', 'balances']"));
Assert.That(response.Error!.Summary, Is.EqualTo("Institution access scope dependencies error"));
});
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,26 @@ internal class AgreementsEndpointMockData(
ResponsePage<Agreement> getAgreements,
Agreement createAgreement,
Agreement getAgreement,
BasicResponse deleteAgreement)
BasicResponse deleteAgreement,
BasicResponse getAgreementWithInvalidGuid,
CreateAgreementError createAgreementWithInvalidInstitutionId,
CreateAgreementError createAgreementWithInvalidParams,
CreateAgreementError createAgreementWithInvalidParamsAtPolishInstitution)
{
public ResponsePage<Agreement> GetAgreements { get; set; } = getAgreements;
public Agreement CreateAgreement { get; set; } = createAgreement;
public Agreement GetAgreement { get; set; } = getAgreement;
public BasicResponse DeleteAgreement { get; set; } = deleteAgreement;

public BasicResponse GetAgreementWithInvalidGuid { get; set; } = getAgreementWithInvalidGuid;

public CreateAgreementError CreateAgreementWithInvalidInstitutionId { get; set; } =
createAgreementWithInvalidInstitutionId;

public CreateAgreementError CreateAgreementWithInvalidParams { get; set; } = createAgreementWithInvalidParams;

public CreateAgreementError CreateAgreementWithInvalidParamsAtPolishInstitution { get; set; } =
createAgreementWithInvalidParamsAtPolishInstitution;
}

internal class InstitutionsEndpointMockData(List<Institution> getInstitutions, Institution getInstitution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,42 @@
"DeleteAgreement": {
"summary": "End User Agreement deleted",
"detail": "End User Agreement bb37bc52-5b1d-44f9-b1cd-ec9594f25387 deleted"
},
"GetAgreementWithInvalidGuid": {
"summary": "Invalid EndUserAgreement ID",
"detail": "f84d7b8-dee4-4cd9-bc6d-842ef78f6028 is not a valid EndUserAgreement UUID. ",
"status_code": 400
},
"CreateAgreementWithInvalidInstitutionId": {
"institution_id": {
"summary": "Unknown Institution ID invalid_institution",
"detail": "Get Institution IDs from /institutions/?country\u003d{$COUNTRY_CODE}"
},
"status_code": 400
},
"CreateAgreementWithInvalidParams": {
"max_historical_days": {
"summary": "Incorrect max_historical_days",
"detail": "max_historical_days must be \u003e 0 and \u003c\u003d SANDBOXFINANCE_SFIN0000 transaction_total_days (90)"
},
"access_valid_for_days": {
"summary": "Incorrect access_valid_for_days",
"detail": "access_valid_for_days must be \u003e 0 and \u003c\u003d 180"
},
"access_scope": {
"summary": "Unknown value \u0027[\u0027invalid2\u0027, \u0027invalid\u0027]\u0027 in access_scope",
"detail": "Choose one or several from [\u0027balances\u0027, \u0027details\u0027, \u0027transactions\u0027]"
},
"status_code": 400
},
"CreateAgreementWithInvalidParamsAtPolishInstitution": {
"summary": [
"Institution access scope dependencies error"
],
"detail": [
"For this institution the following scopes are required together: [\u0027details\u0027, \u0027balances\u0027]"
],
"status_code": 400
}
},
"InstitutionsEndpointMockData": {
Expand Down

0 comments on commit 456fdc4

Please sign in to comment.