From 80e702a0a0bbe5a292e0d24479ef1bfe3d9d1012 Mon Sep 17 00:00:00 2001 From: RobinTTY Date: Tue, 23 Apr 2024 01:15:18 +0200 Subject: [PATCH] Add some more tests regarding errors from the AccountsEndpoint --- .../LiveApi/CredentialTests.cs | 27 +++++++++++++- .../Endpoints/AccountsEndpointTests.cs | 35 +++++++++++++++++- .../Mocks/Endpoints/AccountsEndpointTests.cs | 37 +++++++++++++++++++ .../Mocks/Responses/MockResponsesModel.cs | 7 +++- .../Mocks/Responses/responses.json | 10 +++++ 5 files changed, 111 insertions(+), 5 deletions(-) diff --git a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs index 228cd51..de59c57 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs +++ b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs @@ -1,4 +1,5 @@ using RobinTTY.NordigenApiClient.Models; +using RobinTTY.NordigenApiClient.Models.Jwt; using RobinTTY.NordigenApiClient.Tests.Shared; namespace RobinTTY.NordigenApiClient.Tests.LiveApi; @@ -32,7 +33,7 @@ public async Task CheckValidTokensAfterRequest() } /// - /// 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. /// [Test] public async Task ExecuteRequestWithInvalidCredentials() @@ -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); @@ -52,6 +53,28 @@ public async Task ExecuteRequestWithInvalidCredentials() }); } + /// + /// Tests the failure of authentication due to an invalid token when trying to execute a request. + /// + [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"); + }); + } + /// /// Tries to execute a request using credentials that haven't whitelisted the used IP. This should cause an error. /// diff --git a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/AccountsEndpointTests.cs b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/AccountsEndpointTests.cs index 11e710d..273b4ac 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/AccountsEndpointTests.cs +++ b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/AccountsEndpointTests.cs @@ -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(); } @@ -121,6 +124,21 @@ await _apiClient.AccountsEndpoint.GetTransactions(_accountId, startDate, #endregion #region RequestsWithErrors + + /// + /// Tests the retrieval of an account that does not exist. This should return an error. + /// + [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. "); + }); + } /// /// Tests the retrieval of an account that does not exist. This should return an error. @@ -128,7 +146,7 @@ await _apiClient.AccountsEndpoint.GetTransactions(_accountId, startDate, [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(() => { @@ -137,6 +155,21 @@ public async Task GetAccountThatDoesNotExist() }); } + /// + /// Tests the retrieval of balances of an account that does not exist. This should return an error. + /// + [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"); + }); + } + /// /// Tests the retrieval of transactions within a specific time frame in the future. This should return an error. /// diff --git a/src/RobinTTY.NordigenApiClient.Tests/Mocks/Endpoints/AccountsEndpointTests.cs b/src/RobinTTY.NordigenApiClient.Tests/Mocks/Endpoints/AccountsEndpointTests.cs index 273c3d8..685edc2 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/Mocks/Endpoints/AccountsEndpointTests.cs +++ b/src/RobinTTY.NordigenApiClient.Tests/Mocks/Endpoints/AccountsEndpointTests.cs @@ -6,6 +6,8 @@ namespace RobinTTY.NordigenApiClient.Tests.Mocks.Endpoints; public class AccountsEndpointTests { + private const string InvalidGuid = "abcdefg"; + /// /// Tests the retrieval of an account. /// @@ -126,6 +128,23 @@ await apiClient.AccountsEndpoint.GetTransactions(A.Dummy(), startDate, AssertionHelpers.AssertNordigenApiResponseIsSuccessful(balancesResponse, HttpStatusCode.OK); Assert.That(balancesResponse.Result!.BookedTransactions, Has.Count.EqualTo(2)); } + + /// + /// Tests the retrieval of an account that does not exist. This should return an error. + /// + [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. "); + }); + } /// /// Tests the retrieval of an account that does not exist. This should return an error. @@ -143,6 +162,24 @@ public async Task GetAccountThatDoesNotExist() AssertionHelpers.AssertBasicResponseMatchesExpectations(accountResponse.Error, "Not found.", "Not found."); }); } + + /// + /// Tests the retrieval of balances of an account that does not exist. This should return an error. + /// + [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()); + + 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"); + }); + } /// /// Tests the retrieval of transactions within a specific time frame in the future. This should return an error. diff --git a/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/MockResponsesModel.cs b/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/MockResponsesModel.cs index 2ecde16..f1f5384 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/MockResponsesModel.cs +++ b/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/MockResponsesModel.cs @@ -27,8 +27,9 @@ 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; @@ -36,7 +37,9 @@ AccountsError getAccountThatDoesNotExist 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( diff --git a/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/responses.json b/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/responses.json index ea2a600..1a00ef2 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/responses.json +++ b/src/RobinTTY.NordigenApiClient.Tests/Mocks/Responses/responses.json @@ -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",