From a572938430b86480e575d576ee54ce794e9ad67b Mon Sep 17 00:00:00 2001 From: Roberto Mancinelli Date: Thu, 5 Dec 2024 15:45:56 +0100 Subject: [PATCH] add tests --- .../Auth/AuthApiCacheDecoratorTests.cs | 81 +++++++++++++++++++ test/TrueLayer.Tests/Mocks/AuthApiMock.cs | 5 ++ .../Mocks/InMemoryAuthTokenCacheMock.cs | 24 ++++++ 3 files changed, 110 insertions(+) create mode 100644 test/TrueLayer.Tests/Auth/AuthApiCacheDecoratorTests.cs create mode 100644 test/TrueLayer.Tests/Mocks/InMemoryAuthTokenCacheMock.cs diff --git a/test/TrueLayer.Tests/Auth/AuthApiCacheDecoratorTests.cs b/test/TrueLayer.Tests/Auth/AuthApiCacheDecoratorTests.cs new file mode 100644 index 00000000..f1b17542 --- /dev/null +++ b/test/TrueLayer.Tests/Auth/AuthApiCacheDecoratorTests.cs @@ -0,0 +1,81 @@ +using System; +using System.Net; +using System.Threading.Tasks; +using FluentAssertions; +using TrueLayer.Auth; +using TrueLayer.Models; +using TrueLayer.Tests.Mocks; +using Xunit; + +namespace TrueLayer.Tests.Auth; + +public class AuthApiCacheDecoratorTests +{ + private readonly InMemoryAuthTokenCacheMock _authTokenCache = new(); + private readonly AuthApiMock _authApiMock = new(); + private readonly AuthApiCacheDecorator _authClient; + + public AuthApiCacheDecoratorTests() + { + _authClient = new AuthApiCacheDecorator(_authApiMock ,_authTokenCache); + } + + [Fact] + public async Task GetAuthToken_ResponseInCache_ReturnsCachedAuthToken() + { + const string scope = AuthorizationScope.Payments; + + var expectedResponse = new GetAuthTokenResponse("token", 3600, "Bearer", scope); + var request = new GetAuthTokenRequest(scope); + _authApiMock.SetGetAuthToken(new ApiResponse(expectedResponse, HttpStatusCode.OK, "trace")); + await _authClient.GetAuthToken(request); + _authApiMock.ResetGetAuthToken(); + + //Act + var response = await _authClient.GetAuthToken(request); + + //Assert + response.StatusCode.Should().Be(HttpStatusCode.OK); + response.Data.Should().BeEquivalentTo(expectedResponse); + } + + [Fact] + public async Task GetAuthToken_SuccessfulResponse_SetCache() + { + const string scope = AuthorizationScope.Payments; + + var expectedResponse = new GetAuthTokenResponse("token123", 3600, "Bearer", scope); + _authApiMock.SetGetAuthToken(new ApiResponse(expectedResponse, HttpStatusCode.OK, "trace")); + + var request = new GetAuthTokenRequest(scope); + + //Act + var response = await _authClient.GetAuthToken(request); + + //Assert + response.StatusCode.Should().Be(HttpStatusCode.OK); + response.Data.Should().BeEquivalentTo(expectedResponse); + _authTokenCache.TryGetValue($"tl-auth-token-{scope}", out var cachedResponse); + cachedResponse!.StatusCode.Should().Be(HttpStatusCode.OK); + cachedResponse.Data.Should().BeEquivalentTo(expectedResponse); + } + + [Fact] + public async Task GetAuthToken_UnsuccessfulResponse_CacheIsNotSet() + { + const string scope = AuthorizationScope.Payments; + + var expectedResponse = new ProblemDetails("Type", "Title", null, null, null); + _authApiMock.SetGetAuthToken(new ApiResponse(expectedResponse, HttpStatusCode.BadRequest, "trace")); + + var request = new GetAuthTokenRequest(scope); + + //Act + var response = await _authClient.GetAuthToken(request); + + //Assert + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); + response.Problem.Should().BeEquivalentTo(expectedResponse); + _authTokenCache.IsEmpty.Should().BeTrue(); + } +} diff --git a/test/TrueLayer.Tests/Mocks/AuthApiMock.cs b/test/TrueLayer.Tests/Mocks/AuthApiMock.cs index a86600f5..8ece655a 100644 --- a/test/TrueLayer.Tests/Mocks/AuthApiMock.cs +++ b/test/TrueLayer.Tests/Mocks/AuthApiMock.cs @@ -13,6 +13,11 @@ public void SetGetAuthToken(ApiResponse response) _response = response; } + public void ResetGetAuthToken() + { + _response = null; + } + public ValueTask> GetAuthToken(GetAuthTokenRequest authTokenRequest, CancellationToken cancellationToken = default) { return new ValueTask>(_response!); diff --git a/test/TrueLayer.Tests/Mocks/InMemoryAuthTokenCacheMock.cs b/test/TrueLayer.Tests/Mocks/InMemoryAuthTokenCacheMock.cs new file mode 100644 index 00000000..25516e74 --- /dev/null +++ b/test/TrueLayer.Tests/Mocks/InMemoryAuthTokenCacheMock.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using TrueLayer.Auth; + +namespace TrueLayer.Tests.Mocks; + +public class InMemoryAuthTokenCacheMock : IAuthTokenCache +{ + private readonly Dictionary?> _dictionary = new(); + + public bool TryGetValue(string key, out ApiResponse? value) + { + return _dictionary.TryGetValue(key, out value); + } + + public void Set(string key, ApiResponse value, TimeSpan absoluteExpirationRelativeToNow) + { + _dictionary.Add(key, value); + } + + public bool IsEmpty => _dictionary.Count == 0; +} + +