|
| 1 | +using System.Net; |
| 2 | +using System.Security.Claims; |
| 3 | + |
| 4 | +using Microsoft.ApplicationInsights; |
| 5 | +using Microsoft.ApplicationInsights.Extensibility; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.AspNetCore.Mvc; |
| 8 | +using Microsoft.AspNetCore.Mvc.ViewFeatures; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | + |
| 12 | +using Moq; |
| 13 | +using MX.Api.Abstractions; |
| 14 | +using MX.Observability.ApplicationInsights.Auditing; |
| 15 | +using Newtonsoft.Json; |
| 16 | + |
| 17 | +using XtremeIdiots.Portal.Repository.Abstractions.Constants.V1; |
| 18 | +using XtremeIdiots.Portal.Repository.Abstractions.Models.V1.ConnectedPlayers; |
| 19 | +using XtremeIdiots.Portal.Repository.Abstractions.Models.V1.UserProfiles; |
| 20 | +using XtremeIdiots.Portal.Repository.Api.Client.V1; |
| 21 | +using XtremeIdiots.Portal.Web.Controllers; |
| 22 | +using XtremeIdiots.Portal.Web.Models; |
| 23 | + |
| 24 | +namespace XtremeIdiots.Portal.Web.Tests.Controllers; |
| 25 | + |
| 26 | +public class ProfileControllerTests |
| 27 | +{ |
| 28 | + private readonly Mock<IRepositoryApiClient> mockRepositoryApiClient = new(MockBehavior.Default) { DefaultValue = DefaultValue.Mock }; |
| 29 | + private readonly TelemetryClient telemetryClient = new(new TelemetryConfiguration()); |
| 30 | + private readonly Mock<ILogger<ProfileController>> mockLogger = new(); |
| 31 | + private readonly Mock<IConfiguration> mockConfiguration = new(); |
| 32 | + private readonly IAuditLogger auditLogger = new Mock<IAuditLogger>().Object; |
| 33 | + |
| 34 | + private ProfileController CreateSut(ClaimsPrincipal? user = null) |
| 35 | + { |
| 36 | + var controller = new ProfileController( |
| 37 | + mockRepositoryApiClient.Object, |
| 38 | + telemetryClient, |
| 39 | + mockLogger.Object, |
| 40 | + mockConfiguration.Object, |
| 41 | + auditLogger); |
| 42 | + |
| 43 | + var httpContext = new DefaultHttpContext |
| 44 | + { |
| 45 | + User = user ?? new ClaimsPrincipal(new ClaimsIdentity("TestAuth")) |
| 46 | + }; |
| 47 | + |
| 48 | + controller.ControllerContext = new ControllerContext { HttpContext = httpContext }; |
| 49 | + controller.TempData = new TempDataDictionary(httpContext, Mock.Of<ITempDataProvider>()); |
| 50 | + |
| 51 | + return controller; |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task Manage_WhenUserClaimMissing_ReturnsEmptyProfileModel() |
| 56 | + { |
| 57 | + // Arrange |
| 58 | + var sut = CreateSut(); |
| 59 | + |
| 60 | + // Act |
| 61 | + var result = await sut.Manage(); |
| 62 | + |
| 63 | + // Assert |
| 64 | + var viewResult = Assert.IsType<ViewResult>(result); |
| 65 | + var model = Assert.IsType<ProfileManageViewModel>(viewResult.Model); |
| 66 | + |
| 67 | + Assert.Null(model.UserProfileId); |
| 68 | + Assert.Null(model.ActiveActivationCode); |
| 69 | + Assert.Empty(model.LinkedPlayers); |
| 70 | + Assert.Equal(0, model.TotalLinkedPlayers); |
| 71 | + Assert.False(model.IsLinkedPlayersCapped); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public async Task ActivateConnectedPlayerCode_WhenProfileNotFound_DoesNotCallActivationEndpoint() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + mockRepositoryApiClient |
| 79 | + .Setup(x => x.UserProfiles.V1.GetUserProfileByXtremeIdiotsId(It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 80 | + .ReturnsAsync(new ApiResult<UserProfileDto>(HttpStatusCode.NotFound)); |
| 81 | + |
| 82 | + var user = new ClaimsPrincipal( |
| 83 | + new ClaimsIdentity([ |
| 84 | + new Claim(UserProfileClaimType.XtremeIdiotsId, "123456") |
| 85 | + ], "TestAuth")); |
| 86 | + |
| 87 | + var sut = CreateSut(user); |
| 88 | + |
| 89 | + // Act |
| 90 | + var result = await sut.ActivateConnectedPlayerCode(); |
| 91 | + |
| 92 | + // Assert |
| 93 | + var redirectResult = Assert.IsType<RedirectToActionResult>(result); |
| 94 | + Assert.Equal(nameof(ProfileController.Manage), redirectResult.ActionName); |
| 95 | + |
| 96 | + mockRepositoryApiClient |
| 97 | + .Verify(x => x.ConnectedPlayers.V1.ActivateConnectedPlayerActivationCode( |
| 98 | + It.IsAny<ActivateConnectedPlayerActivationCodeDto>(), |
| 99 | + It.IsAny<CancellationToken>()), Times.Never); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public async Task ActivateConnectedPlayerCode_WhenProfileFound_CallsActivationEndpoint() |
| 104 | + { |
| 105 | + // Arrange |
| 106 | + var userProfileId = Guid.NewGuid(); |
| 107 | + var userProfile = CreateUserProfileDto(userProfileId); |
| 108 | + |
| 109 | + mockRepositoryApiClient |
| 110 | + .Setup(x => x.UserProfiles.V1.GetUserProfileByXtremeIdiotsId(It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 111 | + .ReturnsAsync(new ApiResult<UserProfileDto>(HttpStatusCode.OK, new ApiResponse<UserProfileDto>(userProfile))); |
| 112 | + |
| 113 | + var activationDto = CreateActivationCodeDto(userProfileId, "ABC123"); |
| 114 | + |
| 115 | + mockRepositoryApiClient |
| 116 | + .Setup(x => x.ConnectedPlayers.V1.ActivateConnectedPlayerActivationCode(It.IsAny<ActivateConnectedPlayerActivationCodeDto>(), It.IsAny<CancellationToken>())) |
| 117 | + .ReturnsAsync(new ApiResult<ConnectedPlayerActivationCodeDto>(HttpStatusCode.OK, new ApiResponse<ConnectedPlayerActivationCodeDto>(activationDto))); |
| 118 | + |
| 119 | + var user = new ClaimsPrincipal( |
| 120 | + new ClaimsIdentity([ |
| 121 | + new Claim(UserProfileClaimType.XtremeIdiotsId, "123456") |
| 122 | + ], "TestAuth")); |
| 123 | + |
| 124 | + var sut = CreateSut(user); |
| 125 | + |
| 126 | + // Act |
| 127 | + var result = await sut.ActivateConnectedPlayerCode(); |
| 128 | + |
| 129 | + // Assert |
| 130 | + var redirectResult = Assert.IsType<RedirectToActionResult>(result); |
| 131 | + Assert.Equal(nameof(ProfileController.Manage), redirectResult.ActionName); |
| 132 | + |
| 133 | + mockRepositoryApiClient |
| 134 | + .Verify(x => x.ConnectedPlayers.V1.ActivateConnectedPlayerActivationCode( |
| 135 | + It.Is<ActivateConnectedPlayerActivationCodeDto>(dto => dto.UserProfileId == userProfileId), |
| 136 | + It.IsAny<CancellationToken>()), Times.Once); |
| 137 | + } |
| 138 | + |
| 139 | + private static UserProfileDto CreateUserProfileDto(Guid userProfileId) |
| 140 | + { |
| 141 | + var json = JsonConvert.SerializeObject(new |
| 142 | + { |
| 143 | + UserProfileId = userProfileId, |
| 144 | + XtremeIdiotsForumId = "123456", |
| 145 | + DisplayName = "Test User", |
| 146 | + UserProfileClaims = Array.Empty<object>() |
| 147 | + }); |
| 148 | + |
| 149 | + return JsonConvert.DeserializeObject<UserProfileDto>(json)!; |
| 150 | + } |
| 151 | + |
| 152 | + private static ConnectedPlayerActivationCodeDto CreateActivationCodeDto(Guid userProfileId, string code) |
| 153 | + { |
| 154 | + var json = JsonConvert.SerializeObject(new |
| 155 | + { |
| 156 | + ConnectedPlayerActivationCodeId = Guid.NewGuid(), |
| 157 | + UserProfileId = userProfileId, |
| 158 | + Code = code, |
| 159 | + ExpiresAtUtc = DateTime.UtcNow.AddMinutes(5), |
| 160 | + AttemptCount = 0, |
| 161 | + MaxAttempts = 3, |
| 162 | + IsActive = true, |
| 163 | + ActivatedAtUtc = DateTime.UtcNow |
| 164 | + }); |
| 165 | + |
| 166 | + return JsonConvert.DeserializeObject<ConnectedPlayerActivationCodeDto>(json)!; |
| 167 | + } |
| 168 | +} |
0 commit comments