diff --git a/SharedLibraryCore/BaseController.cs b/SharedLibraryCore/BaseController.cs index dbda938f5..9c343c79e 100644 --- a/SharedLibraryCore/BaseController.cs +++ b/SharedLibraryCore/BaseController.cs @@ -57,6 +57,13 @@ namespace SharedLibraryCore ViewBag.Version = Manager.Version; ViewBag.IsFluid = false; ViewBag.EnableColorCodes = Manager.GetApplicationSettings().Configuration().EnableColorCodes; + + Client ??= new EFClient() + { + ClientId = -1, + Level = EFClient.Permission.User, + CurrentAlias = new EFAlias() { Name = "Webfront Guest" } + }; } protected async Task SignInAsync(ClaimsPrincipal claimsPrinciple) @@ -72,13 +79,6 @@ namespace SharedLibraryCore public override void OnActionExecuting(ActionExecutingContext context) { - Client = Client ?? new EFClient() - { - ClientId = -1, - Level = EFClient.Permission.User, - CurrentAlias = new EFAlias() { Name = "Webfront Guest" } - }; - if (!HttpContext.Connection.RemoteIpAddress.GetAddressBytes().SequenceEqual(LocalHost)) { try diff --git a/Tests/ApplicationTests/ControllerTests.cs b/Tests/ApplicationTests/ControllerTests.cs new file mode 100644 index 000000000..28a103800 --- /dev/null +++ b/Tests/ApplicationTests/ControllerTests.cs @@ -0,0 +1,146 @@ +using ApplicationTests.Fixtures; +using ApplicationTests.Mocks; +using FakeItEasy; +using IW4MAdmin; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.Extensions.DependencyInjection; +using NUnit.Framework; +using SharedLibraryCore.Commands; +using SharedLibraryCore.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WebfrontCore.Controllers; + +namespace ApplicationTests +{ + public class ControllerTests + { + private IServiceProvider serviceProvider; + private IDatabaseContextFactory contextFactory; + private IW4MServer server; + private IManager manager; + + [SetUp] + public void Setup() + { + serviceProvider = new ServiceCollection() + .BuildBase() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .BuildServiceProvider() + .SetupTestHooks(); + + contextFactory = serviceProvider.GetRequiredService(); + server = serviceProvider.GetRequiredService(); + manager = serviceProvider.GetRequiredService(); + A.CallTo(() => manager.GetServers()) + .Returns(new[] { server }); + A.CallTo(() => manager.GetActiveClients()) + .Returns(new[] { ClientGenerators.CreateBasicClient(server) }); + } + + #region ACTION_CONTROLLER + [Test] + public async Task Test_BanCommand_Redirects_WithCommandText() + { + var controller = serviceProvider.GetRequiredService(); + var expectedCommandText = "!ban @1 test"; + var expectedEndpoint = server.EndPoint; + + var result = await controller.BanAsync(1, "test", 6) as RedirectToActionResult; + + Assert.AreEqual(expectedEndpoint, result.RouteValues["serverId"]); + Assert.AreEqual(expectedCommandText, result.RouteValues["command"]); + } + + [Test] + public async Task Test_UnbanCommand_Redirects_WithCommandText() + { + var controller = serviceProvider.GetRequiredService(); + var expectedCommandText = "!unban @1 test"; + var expectedEndpoint = server.EndPoint; + + var result = await controller.UnbanAsync(1, "test") as RedirectToActionResult; + + Assert.AreEqual(expectedEndpoint, result.RouteValues["serverId"]); + Assert.AreEqual(expectedCommandText, result.RouteValues["command"]); + } + + [Test] + public async Task Test_Say_Redirects_WithCommandText() + { + var controller = serviceProvider.GetRequiredService(); + var expectedCommandText = "!say test"; + var expectedEndpoint = server.EndPoint; + + var result = await controller.ChatAsync(expectedEndpoint, "test") as RedirectToActionResult; + + Assert.AreEqual(expectedEndpoint, result.RouteValues["serverId"]); + Assert.AreEqual(expectedCommandText, result.RouteValues["command"]); + } + + [Test] + public async Task Test_Kick_Redirects_WithCommandText() + { + var controller = serviceProvider.GetRequiredService(); + var expectedCommandText = "!kick 0 test"; + var expectedEndpoint = server.EndPoint; + + var result = await controller.KickAsync(1, "test") as RedirectToActionResult; + + Assert.AreEqual(expectedEndpoint, result.RouteValues["serverId"]); + Assert.AreEqual(expectedCommandText, result.RouteValues["command"]); + } + + [Test] + public async Task Test_Flag_Redirects_WithCommandText() + { + var controller = serviceProvider.GetRequiredService(); + var expectedCommandText = "!flag @1 test"; + var expectedEndpoint = server.EndPoint; + + var result = await controller.FlagAsync(1, "test") as RedirectToActionResult; + + Assert.AreEqual(expectedEndpoint, result.RouteValues["serverId"]); + Assert.AreEqual(expectedCommandText, result.RouteValues["command"]); + } + + [Test] + public async Task Test_Unflag_Redirects_WithCommandText() + { + var controller = serviceProvider.GetRequiredService(); + var expectedCommandText = "!unflag @1 test"; + var expectedEndpoint = server.EndPoint; + + var result = await controller.UnflagAsync(1, "test") as RedirectToActionResult; + + Assert.AreEqual(expectedEndpoint, result.RouteValues["serverId"]); + Assert.AreEqual(expectedCommandText, result.RouteValues["command"]); + } + + [Test] + public async Task Test_TempBan_Redirects_WithCommandText() + { + var controller = serviceProvider.GetRequiredService(); + var expectedCommandText = "!tempban @1 1G test"; // 'G' because no localization is loaded (GLOBAL_WEEKS) + var expectedEndpoint = server.EndPoint; + + var result = await controller.BanAsync(1, "test", 5) as RedirectToActionResult; + + Assert.AreEqual(expectedEndpoint, result.RouteValues["serverId"]); + Assert.AreEqual(expectedCommandText, result.RouteValues["command"]); + } + #endregion + } +} diff --git a/WebfrontCore/Controllers/ActionController.cs b/WebfrontCore/Controllers/ActionController.cs index b9127dc54..e6b846944 100644 --- a/WebfrontCore/Controllers/ActionController.cs +++ b/WebfrontCore/Controllers/ActionController.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SharedLibraryCore; +using SharedLibraryCore.Commands; using SharedLibraryCore.Configuration; using SharedLibraryCore.Interfaces; using WebfrontCore.ViewModels; @@ -15,10 +16,47 @@ namespace WebfrontCore.Controllers public class ActionController : BaseController { private readonly ApplicationConfiguration _appConfig; + private readonly string _banCommandName; + private readonly string _tempbanCommandName; + private readonly string _unbanCommandName; + private readonly string _sayCommandName; + private readonly string _kickCommandName; + private readonly string _flagCommandName; + private readonly string _unflagCommandName; - public ActionController(IManager manager) : base(manager) + public ActionController(IManager manager, IEnumerable registeredCommands) : base(manager) { _appConfig = manager.GetApplicationSettings().Configuration(); + + foreach (var cmd in registeredCommands) + { + var type = cmd.GetType().Name; + + switch (type) + { + case nameof(BanCommand): + _banCommandName = cmd.Name; + break; + case nameof(TempBanCommand): + _tempbanCommandName = cmd.Name; + break; + case nameof(UnbanCommand): + _unbanCommandName = cmd.Name; + break; + case nameof(SayCommand): + _sayCommandName = cmd.Name; + break; + case nameof(KickCommand): + _kickCommandName = cmd.Name; + break; + case nameof(FlagClientCommand): + _flagCommandName = cmd.Name; + break; + case nameof(UnflagClientCommand): + _unflagCommandName = cmd.Name; + break; + } + } } public IActionResult BanForm() @@ -83,8 +121,8 @@ namespace WebfrontCore.Controllers } string command = Duration == 6 ? - $"{_appConfig.CommandPrefix}ban @{targetId} {Reason}" : - $"{_appConfig.CommandPrefix}tempban @{targetId} {duration} {Reason}"; + $"{_appConfig.CommandPrefix}{_banCommandName} @{targetId} {Reason}" : + $"{_appConfig.CommandPrefix}{_tempbanCommandName} @{targetId} {duration} {Reason}"; var server = Manager.GetServers().First(); @@ -123,7 +161,7 @@ namespace WebfrontCore.Controllers return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new { serverId = server.EndPoint, - command = $"{_appConfig.CommandPrefix}unban @{targetId} {Reason}" + command = $"{_appConfig.CommandPrefix}{_unbanCommandName} @{targetId} {Reason}" })); } @@ -259,7 +297,7 @@ namespace WebfrontCore.Controllers return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new { serverId = server.EndPoint, - command = $"{_appConfig.CommandPrefix}say {message}" + command = $"{_appConfig.CommandPrefix}{_sayCommandName} {message}" })); } @@ -297,7 +335,7 @@ namespace WebfrontCore.Controllers return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new { serverId = server.EndPoint, - command = $"{_appConfig.CommandPrefix}flag @{targetId} {reason}" + command = $"{_appConfig.CommandPrefix}{_flagCommandName} @{targetId} {reason}" })); } @@ -329,7 +367,7 @@ namespace WebfrontCore.Controllers return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new { serverId = server.EndPoint, - command = $"{_appConfig.CommandPrefix}unflag @{targetId} {reason}" + command = $"{_appConfig.CommandPrefix}{_unflagCommandName} @{targetId} {reason}" })); } @@ -372,7 +410,7 @@ namespace WebfrontCore.Controllers return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new { serverId = client.CurrentServer.EndPoint, - command = $"{_appConfig.CommandPrefix}kick {client.ClientNumber} {reason}" + command = $"{_appConfig.CommandPrefix}{_kickCommandName} {client.ClientNumber} {reason}" })); } }