From 2fbdeea52dd97e2eef225cb0b6c184da547bd277 Mon Sep 17 00:00:00 2001 From: Roberto Mancinelli Date: Thu, 5 Dec 2024 15:11:53 +0100 Subject: [PATCH] minor --- src/TrueLayer/Auth/AuthApiCacheDecorator.cs | 2 +- test/TrueLayer.Tests/ApiClientTests.cs | 17 +++----- test/TrueLayer.Tests/MockBankClient.cs | 44 --------------------- 3 files changed, 6 insertions(+), 57 deletions(-) delete mode 100644 test/TrueLayer.Tests/MockBankClient.cs diff --git a/src/TrueLayer/Auth/AuthApiCacheDecorator.cs b/src/TrueLayer/Auth/AuthApiCacheDecorator.cs index cab51fa3..4e885e7f 100644 --- a/src/TrueLayer/Auth/AuthApiCacheDecorator.cs +++ b/src/TrueLayer/Auth/AuthApiCacheDecorator.cs @@ -8,7 +8,7 @@ internal class AuthApiCacheDecorator : IAuthApi { private readonly IAuthApi _client; private readonly IAuthTokenCache _authTokenCache; - private readonly TimeSpan _minTimeToRenew = TimeSpan.FromSeconds(30); + private readonly TimeSpan _minTimeToRenew = TimeSpan.FromMinutes(1); private const string KeyPrefix = "tl-auth-token-"; public AuthApiCacheDecorator(IAuthApi client, IAuthTokenCache authTokenCache) diff --git a/test/TrueLayer.Tests/ApiClientTests.cs b/test/TrueLayer.Tests/ApiClientTests.cs index 7429bdd0..18aebebe 100644 --- a/test/TrueLayer.Tests/ApiClientTests.cs +++ b/test/TrueLayer.Tests/ApiClientTests.cs @@ -19,7 +19,7 @@ public class ApiClientTests : IDisposable private readonly MockHttpMessageHandler _httpMessageHandler; private readonly ApiClient _apiClient; private readonly TestResponse _stub; - private readonly string _privateKey = @"-----BEGIN EC PRIVATE KEY----- + private const string PrivateKey = @"-----BEGIN EC PRIVATE KEY----- MIHcAgEBBEIALJ2sKM+8mVDfTIlk50rqB5lkxaLBt+OECvhXq3nEaB+V0nqljZ9c 5aHRN3qqxMzNLvxFQ+4twifa4ezkMK2/j5WgBwYFK4EEACOhgYkDgYYABADmhZbj i8bgJRfMTdtzy+5VbS5ScMaKC1LQfhII+PTzGzOr+Ts7Qv8My5cmYU5qarGK3tWF @@ -185,7 +185,7 @@ public async Task Given_request_fails_returns_unsuccessful_response(HttpStatusCo [Fact] public async Task Given_request_fails_returns_problem_details() { - string json = @" + const string json = @" { ""type"": ""https://docs.truelayer.com/errors#invalid_parameters"", ""title"": ""Validation Error"", @@ -244,7 +244,7 @@ public async Task Generates_request_signature_when_signing_key_and_body_provided key = "value" }; - var signingKey = new SigningKey { KeyId = Guid.NewGuid().ToString(), PrivateKey = _privateKey }; + var signingKey = new SigningKey { KeyId = Guid.NewGuid().ToString(), PrivateKey = PrivateKey }; var requestUri = new Uri("http://localhost/signing"); var idempotencyKey = Guid.NewGuid().ToString(); @@ -265,12 +265,7 @@ public async Task Generates_request_signature_when_signing_key_and_body_provided [Fact] public async Task Generates_request_signature_when_signing_key_and_no_content_provided() { - var obj = new - { - key = "value" - }; - - var signingKey = new SigningKey { KeyId = Guid.NewGuid().ToString(), PrivateKey = _privateKey }; + var signingKey = new SigningKey { KeyId = Guid.NewGuid().ToString(), PrivateKey = PrivateKey }; var requestUri = new Uri("http://localhost/signing"); var idempotencyKey = Guid.NewGuid().ToString(); @@ -281,7 +276,7 @@ public async Task Generates_request_signature_when_signing_key_and_no_content_pr .WithHeaders(CustomHeaders.IdempotencyKey, idempotencyKey) .Respond(HttpStatusCode.OK, MediaTypeNames.Application.Json, "{}"); - var response = await _apiClient.PostAsync( + await _apiClient.PostAsync( requestUri, null, idempotencyKey: idempotencyKey, @@ -311,8 +306,6 @@ public async Task Omits_signature_when_no_signing_key_provided() idempotencyKey: idempotencyKey); } - public record UserAgentResponse(string Value); - private static void AssertSame(TestResponse? response, TestResponse expected) { response.Should().NotBeNull(); diff --git a/test/TrueLayer.Tests/MockBankClient.cs b/test/TrueLayer.Tests/MockBankClient.cs deleted file mode 100644 index b8611191..00000000 --- a/test/TrueLayer.Tests/MockBankClient.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; -using System.Threading.Tasks; - -namespace TrueLayer.Tests -{ - public class MockBankClient : IDisposable - { - private readonly HttpClient _httpClient = new(); - - public async Task Authorize(string redirectUrl) - { - var bankSession = ParseMockBankUrl(redirectUrl); - - string body = "{ \"action\": \"Execute\" }"; - - var request = new HttpRequestMessage(HttpMethod.Post, bankSession.AuthUrl) - { - Content = new StringContent(body, Encoding.UTF8, "application/json") - }; - - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bankSession.AuthToken); - - using HttpResponseMessage response = await _httpClient.SendAsync(request); - response.EnsureSuccessStatusCode(); - } - - private static (string AuthUrl, string AuthToken) ParseMockBankUrl(string redirectUrl) - { - string[] parts = redirectUrl.Split('/', '=', '#'); - string authToken = parts[^1]; - string authId = parts[^3]; - - return ($"https://pay-mock-connect.truelayer-sandbox.com/api/single-immediate-payments/{authId}/action", authToken); - } - - public void Dispose() - { - _httpClient.Dispose(); - } - } -}