|
3 | 3 | using Microsoft.AspNetCore.Authorization; |
4 | 4 | using Microsoft.AspNetCore.Http; |
5 | 5 | using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.AspNetCore.Mvc.ViewFeatures; |
6 | 7 | using Microsoft.Extensions.Configuration; |
7 | 8 | using Microsoft.Extensions.Logging; |
8 | 9 | using Moq; |
9 | 10 | using MX.Observability.ApplicationInsights.Auditing; |
10 | 11 | using MX.Api.Abstractions; |
| 12 | +using XtremeIdiots.Portal.Repository.Abstractions.Constants.V1; |
11 | 13 | using Newtonsoft.Json; |
12 | 14 | using System.Net; |
13 | 15 | using System.Reflection; |
14 | 16 | using System.Security.Claims; |
15 | 17 | using XtremeIdiots.Portal.Repository.Abstractions.Models.V1.Configurations; |
| 18 | +using XtremeIdiots.Portal.Repository.Abstractions.Models.V1.GameServers; |
16 | 19 | using XtremeIdiots.Portal.Repository.Api.Client.V1; |
| 20 | +using XtremeIdiots.Portal.Web.Auth.Constants; |
17 | 21 | using XtremeIdiots.Portal.Web.Controllers; |
| 22 | +using XtremeIdiots.Portal.Web.Models; |
18 | 23 | using XtremeIdiots.Portal.Web.ViewModels; |
19 | 24 |
|
20 | 25 | namespace XtremeIdiots.Portal.Web.Tests.Controllers; |
@@ -43,6 +48,7 @@ private GameServersController CreateSut(ClaimsPrincipal? user = null) |
43 | 48 | User = user ?? new ClaimsPrincipal(new ClaimsIdentity("TestAuth")) |
44 | 49 | }; |
45 | 50 | controller.ControllerContext = new ControllerContext { HttpContext = httpContext }; |
| 51 | + controller.TempData = new TempDataDictionary(httpContext, Mock.Of<ITempDataProvider>()); |
46 | 52 |
|
47 | 53 | return controller; |
48 | 54 | } |
@@ -195,4 +201,108 @@ private static MethodInfo GetPrivateInstanceMethod(string name) |
195 | 201 | Assert.NotNull(method); |
196 | 202 | return method; |
197 | 203 | } |
| 204 | + |
| 205 | + [Fact] |
| 206 | + public async Task Edit_WhenUserCannotEditFileTransport_PreservesExistingFileTransportValues() |
| 207 | + { |
| 208 | + // Arrange |
| 209 | + var existingServer = CreateGameServerDto(ftpEnabled: true, fileTransportEnabled: true, fileTransportType: "Ftp"); |
| 210 | + var updateResultDto = JsonConvert.DeserializeObject<GameServerDto>(JsonConvert.SerializeObject(new |
| 211 | + { |
| 212 | + GameServerId = existingServer.GameServerId, |
| 213 | + Title = existingServer.Title, |
| 214 | + GameType = existingServer.GameType, |
| 215 | + Hostname = existingServer.Hostname, |
| 216 | + QueryPort = existingServer.QueryPort, |
| 217 | + AgentEnabled = existingServer.AgentEnabled, |
| 218 | + FtpEnabled = existingServer.FtpEnabled, |
| 219 | + RconEnabled = existingServer.RconEnabled, |
| 220 | + BanFileSyncEnabled = existingServer.BanFileSyncEnabled, |
| 221 | + BanFileRootPath = existingServer.BanFileRootPath, |
| 222 | + ServerListEnabled = existingServer.ServerListEnabled, |
| 223 | + ServerListPosition = existingServer.ServerListPosition |
| 224 | + }))!; |
| 225 | + |
| 226 | + mockRepositoryApiClient |
| 227 | + .Setup(x => x.GameServers.V1.GetGameServer(existingServer.GameServerId, It.IsAny<CancellationToken>())) |
| 228 | + .ReturnsAsync(new ApiResult<GameServerDto>(HttpStatusCode.OK, new ApiResponse<GameServerDto>(existingServer))); |
| 229 | + |
| 230 | + EditGameServerDto? capturedUpdate = null; |
| 231 | + mockRepositoryApiClient |
| 232 | + .Setup(x => x.GameServers.V1.UpdateGameServer(It.IsAny<EditGameServerDto>(), It.IsAny<CancellationToken>())) |
| 233 | + .Callback<EditGameServerDto, CancellationToken>((dto, _) => capturedUpdate = dto) |
| 234 | + .ReturnsAsync(new ApiResult<GameServerDto>(HttpStatusCode.OK, new ApiResponse<GameServerDto>(updateResultDto))); |
| 235 | + |
| 236 | + mockAuthorizationService |
| 237 | + .Setup(x => x.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), AuthPolicies.GameServers_Write)) |
| 238 | + .ReturnsAsync(AuthorizationResult.Success()); |
| 239 | + |
| 240 | + mockAuthorizationService |
| 241 | + .Setup(x => x.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), AuthPolicies.GameServers_Credentials_FileTransport_Write)) |
| 242 | + .ReturnsAsync(AuthorizationResult.Failed()); |
| 243 | + |
| 244 | + mockAuthorizationService |
| 245 | + .Setup(x => x.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), AuthPolicies.GameServers_Credentials_Rcon_Write)) |
| 246 | + .ReturnsAsync(AuthorizationResult.Failed()); |
| 247 | + |
| 248 | + var model = new GameServerEditViewModel |
| 249 | + { |
| 250 | + GameServer = new GameServerViewModel |
| 251 | + { |
| 252 | + GameServerId = existingServer.GameServerId, |
| 253 | + Title = existingServer.Title, |
| 254 | + GameType = existingServer.GameType, |
| 255 | + Hostname = existingServer.Hostname, |
| 256 | + QueryPort = existingServer.QueryPort, |
| 257 | + AgentEnabled = existingServer.AgentEnabled, |
| 258 | + FileTransportEnabled = true, |
| 259 | + FileTransportType = FileTransportType.Sftp, |
| 260 | + RconEnabled = existingServer.RconEnabled, |
| 261 | + BanFileSyncEnabled = existingServer.BanFileSyncEnabled, |
| 262 | + BanFileRootPath = existingServer.BanFileRootPath, |
| 263 | + ServerListEnabled = existingServer.ServerListEnabled |
| 264 | + } |
| 265 | + }; |
| 266 | + |
| 267 | + var sut = CreateSut(); |
| 268 | + |
| 269 | + // Act |
| 270 | + var result = await sut.Edit(model, CancellationToken.None); |
| 271 | + |
| 272 | + // Assert |
| 273 | + var redirect = Assert.IsType<RedirectToActionResult>(result); |
| 274 | + Assert.Equal("Index", redirect.ActionName); |
| 275 | + Assert.NotNull(capturedUpdate); |
| 276 | + Assert.True(capturedUpdate!.FtpEnabled); |
| 277 | + |
| 278 | + var optionalTransportType = capturedUpdate.GetType().GetProperty("FileTransportType", BindingFlags.Public | BindingFlags.Instance); |
| 279 | + if (optionalTransportType is not null) |
| 280 | + { |
| 281 | + var value = optionalTransportType.GetValue(capturedUpdate); |
| 282 | + Assert.Equal("Ftp", value?.ToString()); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + private static GameServerDto CreateGameServerDto(bool ftpEnabled, bool fileTransportEnabled, string fileTransportType) |
| 287 | + { |
| 288 | + var json = JsonConvert.SerializeObject(new |
| 289 | + { |
| 290 | + GameServerId = Guid.NewGuid(), |
| 291 | + Title = "Test Server", |
| 292 | + GameType = GameType.CallOfDuty4, |
| 293 | + Hostname = "127.0.0.1", |
| 294 | + QueryPort = 28960, |
| 295 | + AgentEnabled = false, |
| 296 | + FileTransportEnabled = fileTransportEnabled, |
| 297 | + FileTransportType = fileTransportType, |
| 298 | + FtpEnabled = ftpEnabled, |
| 299 | + RconEnabled = false, |
| 300 | + BanFileSyncEnabled = false, |
| 301 | + BanFileRootPath = "/", |
| 302 | + ServerListEnabled = false, |
| 303 | + ServerListPosition = 1 |
| 304 | + }); |
| 305 | + |
| 306 | + return JsonConvert.DeserializeObject<GameServerDto>(json)!; |
| 307 | + } |
198 | 308 | } |
0 commit comments