From d0b16b25a4480585ff5e59d01f14ae8ca417974f Mon Sep 17 00:00:00 2001 From: Marek Lesko Date: Sun, 22 Mar 2026 10:57:35 +0000 Subject: [PATCH] Change --- Tests/TestSupport.cs | 69 ++++++++++++++++++++++++++++++++++++++++++++ Tests/Tests.csproj | 19 +++++++----- 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 Tests/TestSupport.cs diff --git a/Tests/TestSupport.cs b/Tests/TestSupport.cs new file mode 100644 index 0000000..50b3fe6 --- /dev/null +++ b/Tests/TestSupport.cs @@ -0,0 +1,69 @@ +using System.Security.Claims; +using Api.Models; +using Api.Services; +using Microsoft.EntityFrameworkCore; + +namespace Tests; + +internal static class TestSupport +{ + public static AppDbContext CreateDbContext(string? databaseName = null) + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName ?? Guid.NewGuid().ToString()) + .Options; + + return new AppDbContext(options); + } + + public static ClaimsPrincipal CreatePrincipal(params Claim[] claims) + { + return new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth")); + } +} + +internal sealed class OAuthValidationServiceStub : IOAuthValidationService +{ + public (bool IsValid, ClaimsPrincipal? Principal, string? ErrorMessage) ValidationResult { get; set; } + + public (string Email, string? FirstName, string? LastName, string? ProfilePictureUrl, string ProviderId) UserInfo { get; set; } + + public (string Email, string? FirstName, string? LastName, string? ProfilePictureUrl, string ProviderId) AsyncUserInfo { get; set; } + + public Task<(bool IsValid, ClaimsPrincipal? Principal, string? ErrorMessage)> ValidateIdTokenAsync(string idToken, string provider) + { + return Task.FromResult(ValidationResult); + } + + public (string Email, string? FirstName, string? LastName, string? ProfilePictureUrl, string ProviderId) ExtractUserInfo(ClaimsPrincipal principal, string provider) + { + return UserInfo; + } + + public Task<(string Email, string? FirstName, string? LastName, string? ProfilePictureUrl, string ProviderId)> ExtractUserInfoAsync(ClaimsPrincipal principal, string provider, string? idToken = null, string? accessToken = null) + { + return Task.FromResult(AsyncUserInfo); + } +} + +internal sealed class JwtServiceStub : IJwtService +{ + public string GeneratedToken { get; set; } = "test-access-token"; + + public DateTime Expiration { get; set; } = DateTime.UtcNow.AddHours(1); + + public string GenerateToken(User user) + { + return GeneratedToken; + } + + public ClaimsPrincipal? ValidateToken(string token) + { + throw new NotSupportedException(); + } + + public DateTime GetTokenExpiration(string token) + { + return Expiration; + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 5382ecd..ef8c0df 100755 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -8,13 +8,18 @@ true - - - - - - - + + + + + + + + + + + +