|
| 1 | +using MediatR; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using Microsoft.AspNetCore.Identity; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using Microsoft.AspNetCore.Mvc.RazorPages; |
| 6 | +using Moq; |
| 7 | +using SecurityService.BusinessLogic.Requests; |
| 8 | +using SecurityService.Database; |
| 9 | +using SecurityService.Models; |
| 10 | +using SecurityService.UnitTests.Infrastructure; |
| 11 | +using Shouldly; |
| 12 | +using SimpleResults; |
| 13 | + |
| 14 | +namespace SecurityService.UnitTests.Pages; |
| 15 | + |
| 16 | +public class GrantsPageModelTests |
| 17 | +{ |
| 18 | + [Fact] |
| 19 | + public async Task OnGetAsync_WhenUserNotFound_RedirectsToLogin() |
| 20 | + { |
| 21 | + var userManager = IdentityMocks.CreateUserManager(); |
| 22 | + userManager.Setup(m => m.GetUserAsync(It.IsAny<System.Security.Claims.ClaimsPrincipal>())) |
| 23 | + .ReturnsAsync((ApplicationUser?)null); |
| 24 | + |
| 25 | + var mediator = new Mock<IMediator>(); |
| 26 | + var model = CreateModel(userManager, mediator, new DefaultHttpContext()); |
| 27 | + |
| 28 | + var result = await model.OnGetAsync(CancellationToken.None); |
| 29 | + |
| 30 | + result.ShouldBeOfType<RedirectResult>(); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task OnGetAsync_WhenUserFound_QueriesGrantsAndReturnsPage() |
| 35 | + { |
| 36 | + var user = new ApplicationUser { Id = "user-1" }; |
| 37 | + var userManager = IdentityMocks.CreateUserManager(); |
| 38 | + userManager.Setup(m => m.GetUserAsync(It.IsAny<System.Security.Claims.ClaimsPrincipal>())) |
| 39 | + .ReturnsAsync(user); |
| 40 | + |
| 41 | + var grants = new List<GrantDetails> |
| 42 | + { |
| 43 | + new GrantDetails("auth-1", "client-1", "Client One", new[] { "openid" }, DateTimeOffset.UtcNow) |
| 44 | + }; |
| 45 | + |
| 46 | + var mediator = new Mock<IMediator>(); |
| 47 | + mediator.Setup(m => m.Send(It.Is<SecurityServiceQueries.GetUserGrantsQuery>(q => q.UserId == "user-1"), It.IsAny<CancellationToken>())) |
| 48 | + .ReturnsAsync(Result.Success(grants)); |
| 49 | + |
| 50 | + var model = CreateModel(userManager, mediator, new DefaultHttpContext()); |
| 51 | + |
| 52 | + var result = await model.OnGetAsync(CancellationToken.None); |
| 53 | + |
| 54 | + result.ShouldBeOfType<PageResult>(); |
| 55 | + model.Grants.ShouldHaveSingleItem(); |
| 56 | + mediator.Verify(m => m.Send(It.Is<SecurityServiceQueries.GetUserGrantsQuery>(q => q.UserId == "user-1"), It.IsAny<CancellationToken>()), Times.Once); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task OnPostRevokeAsync_WhenUserNotFound_RedirectsToLogin() |
| 61 | + { |
| 62 | + var userManager = IdentityMocks.CreateUserManager(); |
| 63 | + userManager.Setup(m => m.GetUserAsync(It.IsAny<System.Security.Claims.ClaimsPrincipal>())) |
| 64 | + .ReturnsAsync((ApplicationUser?)null); |
| 65 | + |
| 66 | + var mediator = new Mock<IMediator>(MockBehavior.Strict); |
| 67 | + var model = CreateModel(userManager, mediator, new DefaultHttpContext()); |
| 68 | + |
| 69 | + var result = await model.OnPostRevokeAsync("auth-1", CancellationToken.None); |
| 70 | + |
| 71 | + result.ShouldBeOfType<RedirectResult>(); |
| 72 | + mediator.Verify(m => m.Send(It.IsAny<SecurityServiceCommands.RevokeGrantCommand>(), It.IsAny<CancellationToken>()), Times.Never); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public async Task OnPostRevokeAsync_WhenRevokeSucceeds_RedirectsToPage() |
| 77 | + { |
| 78 | + var user = new ApplicationUser { Id = "user-1" }; |
| 79 | + var userManager = IdentityMocks.CreateUserManager(); |
| 80 | + userManager.Setup(m => m.GetUserAsync(It.IsAny<System.Security.Claims.ClaimsPrincipal>())) |
| 81 | + .ReturnsAsync(user); |
| 82 | + |
| 83 | + var mediator = new Mock<IMediator>(); |
| 84 | + mediator.Setup(m => m.Send(It.IsAny<SecurityServiceCommands.RevokeGrantCommand>(), It.IsAny<CancellationToken>())) |
| 85 | + .ReturnsAsync(Result.Success()); |
| 86 | + |
| 87 | + var model = CreateModel(userManager, mediator, new DefaultHttpContext()); |
| 88 | + |
| 89 | + var result = await model.OnPostRevokeAsync("auth-1", CancellationToken.None); |
| 90 | + |
| 91 | + result.ShouldBeOfType<RedirectToPageResult>(); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task OnPostRevokeAsync_WhenRevokeFails_ReturnsPageWithStatusMessage() |
| 96 | + { |
| 97 | + var user = new ApplicationUser { Id = "user-1" }; |
| 98 | + var userManager = IdentityMocks.CreateUserManager(); |
| 99 | + userManager.Setup(m => m.GetUserAsync(It.IsAny<System.Security.Claims.ClaimsPrincipal>())) |
| 100 | + .ReturnsAsync(user); |
| 101 | + |
| 102 | + var mediator = new Mock<IMediator>(); |
| 103 | + mediator.Setup(m => m.Send(It.IsAny<SecurityServiceCommands.RevokeGrantCommand>(), It.IsAny<CancellationToken>())) |
| 104 | + .ReturnsAsync(Result.Failure("The authorization could not be revoked.")); |
| 105 | + mediator.Setup(m => m.Send(It.IsAny<SecurityServiceQueries.GetUserGrantsQuery>(), It.IsAny<CancellationToken>())) |
| 106 | + .ReturnsAsync(Result.Success(new List<GrantDetails>())); |
| 107 | + |
| 108 | + var model = CreateModel(userManager, mediator, new DefaultHttpContext()); |
| 109 | + |
| 110 | + var result = await model.OnPostRevokeAsync("auth-1", CancellationToken.None); |
| 111 | + |
| 112 | + result.ShouldBeOfType<PageResult>(); |
| 113 | + model.StatusMessage.ShouldBe("The authorization could not be revoked."); |
| 114 | + } |
| 115 | + |
| 116 | + private static SecurityService.Pages.Account.Grants.IndexModel CreateModel( |
| 117 | + Mock<UserManager<ApplicationUser>> userManager, |
| 118 | + Mock<IMediator> mediator, |
| 119 | + HttpContext httpContext) |
| 120 | + { |
| 121 | + return new SecurityService.Pages.Account.Grants.IndexModel(userManager.Object, mediator.Object) |
| 122 | + { |
| 123 | + PageContext = new PageContext |
| 124 | + { |
| 125 | + HttpContext = httpContext |
| 126 | + } |
| 127 | + }; |
| 128 | + } |
| 129 | +} |
0 commit comments