Skip to content

Commit

Permalink
Add some more tests regarding errors from the AccountsEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Apr 22, 2024
1 parent 8f6ea2f commit 80e702a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 5 deletions.
27 changes: 25 additions & 2 deletions src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RobinTTY.NordigenApiClient.Models;
using RobinTTY.NordigenApiClient.Models.Jwt;
using RobinTTY.NordigenApiClient.Tests.Shared;

namespace RobinTTY.NordigenApiClient.Tests.LiveApi;
Expand Down Expand Up @@ -32,7 +33,7 @@ public async Task CheckValidTokensAfterRequest()
}

/// <summary>
/// Tests the failure of authentication when trying to execute a request.
/// Tests the failure of authentication due to invalid credentials when trying to execute a request.
/// </summary>
[Test]
public async Task ExecuteRequestWithInvalidCredentials()
Expand All @@ -42,8 +43,8 @@ public async Task ExecuteRequestWithInvalidCredentials()
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
var apiClient = new NordigenClient(httpClient, invalidCredentials);

// Returns BasicError
var agreementsResponse = await apiClient.TokenEndpoint.GetTokenPair();

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(agreementsResponse, HttpStatusCode.Unauthorized);
Expand All @@ -52,6 +53,28 @@ public async Task ExecuteRequestWithInvalidCredentials()
});
}

/// <summary>
/// Tests the failure of authentication due to an invalid token when trying to execute a request.
/// </summary>
[Test]
public async Task ExecuteRequestWithUnauthorizedToken()
{
using var httpClient = new HttpClient();
var invalidCredentials = new NordigenClientCredentials("01234567-89ab-cdef-0123-456789abcdef",
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
var token = new JsonWebTokenPair(TestHelpers.Secrets[14], TestHelpers.Secrets[14]);
var apiClient = new NordigenClient(httpClient, invalidCredentials, token);

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

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(response, HttpStatusCode.Unauthorized);
AssertionHelpers.AssertBasicResponseMatchesExpectations(response.Error, "Invalid token",
"Token is invalid or expired");
});
}

/// <summary>
/// Tries to execute a request using credentials that haven't whitelisted the used IP. This should cause an error.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ namespace RobinTTY.NordigenApiClient.Tests.LiveApi.Endpoints;
public class AccountsEndpointTests
{
private Guid _accountId;
private Guid _nonExistingAccountId;
private const string InvalidGuid = "abcdefg";
private NordigenClient _apiClient = null!;

[OneTimeSetUp]
public void Setup()
{
_accountId = Guid.Parse(TestHelpers.Secrets[9]);
_nonExistingAccountId = Guid.Parse("f1d53c46-260d-4556-82df-4e5fed58e37c");
_apiClient = TestHelpers.GetConfiguredClient();
}

Expand Down Expand Up @@ -121,14 +124,29 @@ await _apiClient.AccountsEndpoint.GetTransactions(_accountId, startDate,
#endregion

#region RequestsWithErrors

/// <summary>
/// Tests the retrieval of an account that does not exist. This should return an error.
/// </summary>
[Test]
public async Task GetAccountWithInvalidGuid()
{
var accountResponse = await _apiClient.AccountsEndpoint.GetAccount(InvalidGuid);

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

/// <summary>
/// Tests the retrieval of an account that does not exist. This should return an error.
/// </summary>
[Test]
public async Task GetAccountThatDoesNotExist()
{
var accountResponse = await _apiClient.AccountsEndpoint.GetAccount("f1d53c46-260d-4556-82df-4e5fed58e37c");
var accountResponse = await _apiClient.AccountsEndpoint.GetAccount(_nonExistingAccountId);

Assert.Multiple(() =>
{
Expand All @@ -137,6 +155,21 @@ public async Task GetAccountThatDoesNotExist()
});
}

/// <summary>
/// Tests the retrieval of balances of an account that does not exist. This should return an error.
/// </summary>
[Test]
public async Task GetBalancesForAccountThatDoesNotExist()
{
var balancesResponse = await _apiClient.AccountsEndpoint.GetBalances(_nonExistingAccountId);

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(balancesResponse, HttpStatusCode.NotFound);
AssertionHelpers.AssertBasicResponseMatchesExpectations(balancesResponse.Error, $"Account ID {_nonExistingAccountId} not found", "Please check whether you specified a valid Account ID");
});
}

/// <summary>
/// Tests the retrieval of transactions within a specific time frame in the future. This should return an error.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace RobinTTY.NordigenApiClient.Tests.Mocks.Endpoints;

public class AccountsEndpointTests
{
private const string InvalidGuid = "abcdefg";

/// <summary>
/// Tests the retrieval of an account.
/// </summary>
Expand Down Expand Up @@ -126,6 +128,23 @@ await apiClient.AccountsEndpoint.GetTransactions(A.Dummy<Guid>(), startDate,
AssertionHelpers.AssertNordigenApiResponseIsSuccessful(balancesResponse, HttpStatusCode.OK);
Assert.That(balancesResponse.Result!.BookedTransactions, Has.Count.EqualTo(2));
}

/// <summary>
/// Tests the retrieval of an account that does not exist. This should return an error.
/// </summary>
[Test]
public async Task GetAccountWithInvalidGuid()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AccountsEndpointMockData.GetAccountWithInvalidGuid, HttpStatusCode.BadRequest);

var accountResponse = await apiClient.AccountsEndpoint.GetAccount(InvalidGuid);

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

/// <summary>
/// Tests the retrieval of an account that does not exist. This should return an error.
Expand All @@ -143,6 +162,24 @@ public async Task GetAccountThatDoesNotExist()
AssertionHelpers.AssertBasicResponseMatchesExpectations(accountResponse.Error, "Not found.", "Not found.");
});
}

/// <summary>
/// Tests the retrieval of balances of an account that does not exist. This should return an error.
/// </summary>
[Test]
public async Task GetBalancesForAccountThatDoesNotExist()
{
var apiClient = TestHelpers.GetMockClient(TestHelpers.MockData.AccountsEndpointMockData.GetBalancesForAccountThatDoesNotExist, HttpStatusCode.NotFound);

var nonExistingAccountId = Guid.Parse("f1d53c46-260d-4556-82df-4e5fed58e37c");
var balancesResponse = await apiClient.AccountsEndpoint.GetBalances(A.Dummy<Guid>());

Assert.Multiple(() =>
{
AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(balancesResponse, HttpStatusCode.NotFound);
AssertionHelpers.AssertBasicResponseMatchesExpectations(balancesResponse.Error, $"Account ID {nonExistingAccountId} not found", "Please check whether you specified a valid Account ID");
});
}

/// <summary>
/// Tests the retrieval of transactions within a specific time frame in the future. This should return an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ internal class AccountsEndpointMockData(
AccountTransactionsWrapper getTransactions,
AccountTransactionsWrapper getTransactionRange,
AccountsError getTransactionRangeInFuture,
AccountsError getAccountThatDoesNotExist
)
AccountsError getAccountWithInvalidGuid,
AccountsError getAccountThatDoesNotExist,
AccountsError getBalancesForAccountThatDoesNotExist)
{
public BankAccount GetAccount { get; set; } = getAccount;
public BalanceJsonWrapper GetBalances { get; set; } = getBalances;
public BankAccountDetailsWrapper GetAccountDetails { get; set; } = getAccountDetails;
public AccountTransactionsWrapper GetTransactions { get; set; } = getTransactions;
public AccountTransactionsWrapper GetTransactionRange { get; set; } = getTransactionRange;
public AccountsError GetTransactionRangeInFuture { get; set; } = getTransactionRangeInFuture;
public AccountsError GetAccountWithInvalidGuid { get; set; } = getAccountWithInvalidGuid;
public AccountsError GetAccountThatDoesNotExist { get; set; } = getAccountThatDoesNotExist;
public AccountsError GetBalancesForAccountThatDoesNotExist { get; set; } = getBalancesForAccountThatDoesNotExist;
}

internal class AgreementsEndpointMockData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,21 @@
]
}
},
"GetAccountWithInvalidGuid": {
"summary": "Invalid Account ID",
"detail": "abcdefg is not a valid Account UUID. ",
"status_code": 400
},
"GetAccountThatDoesNotExist": {
"detail": "Not found.",
"summary": "Not found.",
"status_code": 404
},
"GetBalancesForAccountThatDoesNotExist": {
"summary": "Account ID f1d53c46-260d-4556-82df-4e5fed58e37c not found",
"detail": "Please check whether you specified a valid Account ID",
"status_code": 404
},
"GetTransactionRangeInFuture": {
"date_from": {
"summary": "Date can't be in future",
Expand Down

0 comments on commit 80e702a

Please sign in to comment.