From 4f5f99ce965dbc7c2169819f892a195e9aad8779 Mon Sep 17 00:00:00 2001 From: RobinTTY Date: Sat, 20 Apr 2024 02:09:34 +0200 Subject: [PATCH] Move CredentialTests to TokenEndpointTests where appropriate, improve constructors by throwing exception when null is provided as argument --- .../LiveApi/CredentialTests.cs | 16 -------------- .../LiveApi/Endpoints/TokenEndpointTests.cs | 22 +++++++++++++++++++ .../Models/Jwt/JsonWebTokenPair.cs | 4 ++-- .../Models/NordigenClientCredentials.cs | 4 ++-- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs index d8cbdd0..4920d0e 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs +++ b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/CredentialTests.cs @@ -25,22 +25,6 @@ public async Task MakeRequestWithInvalidCredentials() "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"); var apiClient = new NordigenClient(httpClient, invalidCredentials); - // Returns BasicError - var agreementsResponse = await apiClient.AgreementsEndpoint.GetAgreements(10, 0); - Assert.Multiple(() => - { - Assert.That(agreementsResponse.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized)); - AssertErrorMatchesExpectation(agreementsResponse.Error!); - }); - - // Returns InstitutionsError - var institutionResponse = await apiClient.InstitutionsEndpoint.GetInstitutions(); - Assert.Multiple(() => - { - Assert.That(institutionResponse.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized)); - AssertErrorMatchesExpectation(institutionResponse.Error!); - }); - // Returns AccountsError var balancesResponse = await apiClient.AccountsEndpoint.GetBalances(_secrets[9]); Assert.Multiple(() => diff --git a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/TokenEndpointTests.cs b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/TokenEndpointTests.cs index 65b954d..0969351 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/TokenEndpointTests.cs +++ b/src/RobinTTY.NordigenApiClient.Tests/LiveApi/Endpoints/TokenEndpointTests.cs @@ -70,4 +70,26 @@ public async Task ReuseExpiredToken() var result = await apiClient.RequisitionsEndpoint.GetRequisitions(10, 0); AssertionHelpers.AssertNordigenApiResponseIsSuccessful(result, HttpStatusCode.OK); } + + /// + /// Tests the failure of authentication when trying to get a new token pair. + /// + [Test] + public async Task GetTokenPairWithInvalidCredentials() + { + using var httpClient = new HttpClient(); + var invalidCredentials = new NordigenClientCredentials("01234567-89ab-cdef-0123-456789abcdef", + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"); + var apiClient = new NordigenClient(httpClient, invalidCredentials); + + // Returns BasicError + var agreementsResponse = await apiClient.AgreementsEndpoint.GetAgreements(10, 0); + Assert.Multiple(() => + { + Assert.That(agreementsResponse.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized)); + AssertionHelpers.AssertNordigenApiResponseIsUnsuccessful(agreementsResponse, HttpStatusCode.Unauthorized); + AssertionHelpers.AssertBasicResponseMatchesExpectations(agreementsResponse.Error, "Authentication failed", + "No active account found with the given credentials"); + }); + } } diff --git a/src/RobinTTY.NordigenApiClient/Models/Jwt/JsonWebTokenPair.cs b/src/RobinTTY.NordigenApiClient/Models/Jwt/JsonWebTokenPair.cs index 08898e0..1f7a7de 100644 --- a/src/RobinTTY.NordigenApiClient/Models/Jwt/JsonWebTokenPair.cs +++ b/src/RobinTTY.NordigenApiClient/Models/Jwt/JsonWebTokenPair.cs @@ -42,8 +42,8 @@ public class JsonWebTokenPair [JsonConstructor] public JsonWebTokenPair(JsonWebToken accessToken, JsonWebToken refreshToken, int accessExpires, int refreshExpires) { - AccessToken = accessToken; - RefreshToken = refreshToken; + AccessToken = accessToken ?? throw new ArgumentNullException(nameof(accessToken)); + RefreshToken = refreshToken ?? throw new ArgumentNullException(nameof(refreshToken)); AccessExpires = accessExpires; RefreshExpires = refreshExpires; } diff --git a/src/RobinTTY.NordigenApiClient/Models/NordigenClientCredentials.cs b/src/RobinTTY.NordigenApiClient/Models/NordigenClientCredentials.cs index 6924064..7fee811 100644 --- a/src/RobinTTY.NordigenApiClient/Models/NordigenClientCredentials.cs +++ b/src/RobinTTY.NordigenApiClient/Models/NordigenClientCredentials.cs @@ -26,7 +26,7 @@ public class NordigenClientCredentials /// Secret Nordigen key. public NordigenClientCredentials(string secretId, string secretKey) { - SecretId = secretId; - SecretKey = secretKey; + SecretId = secretId ?? throw new ArgumentNullException(nameof(secretId)); + SecretKey = secretKey ?? throw new ArgumentNullException(nameof(secretKey)); } }