-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyControllerTests2.cs
52 lines (44 loc) · 1.37 KB
/
MyControllerTests2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading.Tasks;
[TestFixture]
public class MyControllerTests
{
private Mock<HttpContext> _httpContextMock;
private Mock<IPrincipal> _principalMock;
[SetUp]
public void Setup()
{
_httpContextMock = new Mock<HttpContext>();
_principalMock = new Mock<IPrincipal>();
// Set up the user with claims
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "testuser"),
new Claim(ClaimTypes.Role, "Admin")
};
var identity = new ClaimsIdentity(claims, "TestAuthType");
var claimsPrincipal = new ClaimsPrincipal(identity);
// Mock the user
_principalMock.Setup(p => p.Identity).Returns(identity);
_httpContextMock.Setup(h => h.User).Returns(claimsPrincipal);
}
[Test]
public async Task MyControllerAction_ReturnsSuccess_WhenUserIsAuthenticated()
{
// Arrange
var controller = new MyController();
controller.ControllerContext = new ControllerContext
{
HttpContext = _httpContextMock.Object
};
// Act
var result = await controller.MyAction();
// Assert
Assert.IsInstanceOf<OkResult>(result);
}
}