update action controller to dynamically generate command names in case of overridden names (issue #152)
This commit is contained in:
parent
f040dd5159
commit
ed2b01f229
@ -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
|
||||
|
146
Tests/ApplicationTests/ControllerTests.cs
Normal file
146
Tests/ApplicationTests/ControllerTests.cs
Normal file
@ -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<ActionController>()
|
||||
.AddSingleton<IManagerCommand, BanCommand>()
|
||||
.AddSingleton<IManagerCommand, TempBanCommand>()
|
||||
.AddSingleton<IManagerCommand, UnbanCommand>()
|
||||
.AddSingleton<IManagerCommand, KickCommand>()
|
||||
.AddSingleton<IManagerCommand, FlagClientCommand>()
|
||||
.AddSingleton<IManagerCommand, UnflagClientCommand>()
|
||||
.AddSingleton<IManagerCommand, SayCommand>()
|
||||
.BuildServiceProvider()
|
||||
.SetupTestHooks();
|
||||
|
||||
contextFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>();
|
||||
server = serviceProvider.GetRequiredService<IW4MServer>();
|
||||
manager = serviceProvider.GetRequiredService<IManager>();
|
||||
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<ActionController>();
|
||||
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<ActionController>();
|
||||
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<ActionController>();
|
||||
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<ActionController>();
|
||||
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<ActionController>();
|
||||
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<ActionController>();
|
||||
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<ActionController>();
|
||||
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
|
||||
}
|
||||
}
|
@ -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<IManagerCommand> 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}"
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user