-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fbdeea
commit a572938
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|