39 lines
1018 B
C#
39 lines
1018 B
C#
|
|
using System.Security.Claims;
|
||
|
|
using Microsoft.AspNetCore.Http;
|
||
|
|
|
||
|
|
namespace backend.Tests.Utilities;
|
||
|
|
|
||
|
|
public static class MockHttpContext
|
||
|
|
{
|
||
|
|
public static HttpContext Create(
|
||
|
|
string userId = "test-user-id",
|
||
|
|
string email = "test@example.com",
|
||
|
|
string role = "Participant",
|
||
|
|
bool isAuthenticated = true)
|
||
|
|
{
|
||
|
|
var claims = new List<Claim>();
|
||
|
|
|
||
|
|
if (isAuthenticated)
|
||
|
|
{
|
||
|
|
claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
|
||
|
|
claims.Add(new Claim(ClaimTypes.Email, email));
|
||
|
|
claims.Add(new Claim(ClaimTypes.Role, role));
|
||
|
|
}
|
||
|
|
|
||
|
|
var identity = new ClaimsIdentity(claims, isAuthenticated ? "TestAuthType" : null);
|
||
|
|
var principal = new ClaimsPrincipal(identity);
|
||
|
|
|
||
|
|
var httpContext = new DefaultHttpContext
|
||
|
|
{
|
||
|
|
User = principal
|
||
|
|
};
|
||
|
|
|
||
|
|
return httpContext;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static HttpContext CreateAnonymous()
|
||
|
|
{
|
||
|
|
return Create(isAuthenticated: false);
|
||
|
|
}
|
||
|
|
}
|