Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tl-Roberto-Mancinelli committed Dec 5, 2024
1 parent 2fbdeea commit a572938
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test/TrueLayer.Tests/Auth/AuthApiCacheDecoratorTests.cs
Original file line number Diff line number Diff line change
@@ -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<GetAuthTokenResponse>(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<GetAuthTokenResponse>(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<GetAuthTokenResponse>(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();
}
}
5 changes: 5 additions & 0 deletions test/TrueLayer.Tests/Mocks/AuthApiMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public void SetGetAuthToken(ApiResponse<GetAuthTokenResponse> response)
_response = response;
}

public void ResetGetAuthToken()
{
_response = null;
}

public ValueTask<ApiResponse<GetAuthTokenResponse>> GetAuthToken(GetAuthTokenRequest authTokenRequest, CancellationToken cancellationToken = default)
{
return new ValueTask<ApiResponse<GetAuthTokenResponse>>(_response!);
Expand Down
24 changes: 24 additions & 0 deletions test/TrueLayer.Tests/Mocks/InMemoryAuthTokenCacheMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using TrueLayer.Auth;

namespace TrueLayer.Tests.Mocks;

public class InMemoryAuthTokenCacheMock : IAuthTokenCache
{
private readonly Dictionary<string, ApiResponse<GetAuthTokenResponse>?> _dictionary = new();

public bool TryGetValue(string key, out ApiResponse<GetAuthTokenResponse>? value)
{
return _dictionary.TryGetValue(key, out value);
}

public void Set(string key, ApiResponse<GetAuthTokenResponse> value, TimeSpan absoluteExpirationRelativeToNow)
{
_dictionary.Add(key, value);
}

public bool IsEmpty => _dictionary.Count == 0;
}


0 comments on commit a572938

Please sign in to comment.