2020-04-26 22:12:49 -04:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using System;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using IW4MAdmin;
|
|
|
|
|
using FakeItEasy;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using ApplicationTests.Fixtures;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SharedLibraryCore.Commands;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using ApplicationTests.Mocks;
|
2020-05-16 12:54:01 -04:00
|
|
|
|
using SharedLibraryCore.Services;
|
|
|
|
|
using static SharedLibraryCore.Database.Models.EFClient;
|
2020-10-17 11:47:56 -04:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using FluentAssertions.Extensions;
|
2020-11-12 20:46:17 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-04-26 22:12:49 -04:00
|
|
|
|
|
|
|
|
|
namespace ApplicationTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class CommandTests
|
|
|
|
|
{
|
|
|
|
|
private IServiceProvider serviceProvider;
|
|
|
|
|
private ITranslationLookup transLookup;
|
|
|
|
|
private CommandConfiguration cmdConfig;
|
2020-05-16 12:54:01 -04:00
|
|
|
|
private ApplicationConfiguration appConfig;
|
|
|
|
|
private EventHandlerMock mockEventHandler;
|
|
|
|
|
private ClientService clientService;
|
|
|
|
|
private IManager manager;
|
2020-04-26 22:12:49 -04:00
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
|
|
|
|
serviceProvider = new ServiceCollection()
|
2020-05-16 12:54:01 -04:00
|
|
|
|
.BuildBase(new EventHandlerMock(true))
|
2020-05-24 22:22:26 -04:00
|
|
|
|
.AddSingleton(A.Fake<ClientService>())
|
2020-10-17 11:47:56 -04:00
|
|
|
|
.AddSingleton<LoadMapCommand>()
|
|
|
|
|
.AddSingleton<SetLevelCommand>()
|
|
|
|
|
.AddSingleton<RunAsCommand>()
|
|
|
|
|
.AddSingleton<PrivateMessageAdminsCommand>()
|
2020-05-16 12:54:01 -04:00
|
|
|
|
.BuildServiceProvider()
|
|
|
|
|
.SetupTestHooks();
|
2020-04-26 22:12:49 -04:00
|
|
|
|
|
2020-05-16 12:54:01 -04:00
|
|
|
|
mockEventHandler = serviceProvider.GetRequiredService<EventHandlerMock>();
|
|
|
|
|
manager = serviceProvider.GetRequiredService<IManager>();
|
2020-04-26 22:12:49 -04:00
|
|
|
|
transLookup = serviceProvider.GetRequiredService<ITranslationLookup>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
clientService = serviceProvider.GetRequiredService<ClientService>();
|
|
|
|
|
appConfig = serviceProvider.GetRequiredService<ApplicationConfiguration>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
appConfig.MapChangeDelaySeconds = 1;
|
|
|
|
|
cmdConfig = serviceProvider.GetRequiredService<CommandConfiguration>();
|
2020-11-12 20:46:17 -05:00
|
|
|
|
serviceProvider.GetService<IW4MServer>().RconParser =
|
|
|
|
|
serviceProvider.GetService<IRConParser>();
|
|
|
|
|
|
|
|
|
|
Utilities.DefaultLogger = serviceProvider.GetRequiredService<ILogger>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
|
|
|
|
|
A.CallTo(() => manager.GetClientService())
|
|
|
|
|
.Returns(clientService);
|
2020-04-26 22:12:49 -04:00
|
|
|
|
|
2020-05-16 12:54:01 -04:00
|
|
|
|
A.CallTo(() => clientService.UpdateLevel(A<Permission>.Ignored, A<EFClient>.Ignored, A<EFClient>.Ignored))
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => manager.GetCommands())
|
2020-04-26 22:12:49 -04:00
|
|
|
|
.Returns(new Command[]
|
|
|
|
|
{
|
|
|
|
|
new ImpersonatableCommand(cmdConfig, transLookup),
|
2020-07-31 21:40:03 -04:00
|
|
|
|
new NonImpersonatableCommand(cmdConfig, transLookup),
|
|
|
|
|
new MockCommand(cmdConfig, transLookup)
|
2020-04-26 22:12:49 -04:00
|
|
|
|
});
|
|
|
|
|
|
2020-05-16 12:54:01 -04:00
|
|
|
|
A.CallTo(() => manager.AddEvent(A<GameEvent>.Ignored))
|
|
|
|
|
.Invokes((fakeCall) => mockEventHandler.HandleEvent(manager, fakeCall.Arguments[0] as GameEvent));
|
2020-04-26 22:12:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region RUNAS
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_RunAsFailsOnSelf()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<RunAsCommand>();
|
2020-04-26 22:12:49 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = target
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Command));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_RunAsFailsOnHigherPrivilege()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<RunAsCommand>();
|
2020-04-26 22:12:49 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = EFClient.Permission.Administrator;
|
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.NetworkId = 100;
|
|
|
|
|
origin.Level = EFClient.Permission.Moderator;
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Command));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_RunAsFailsOnSamePrivilege()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<RunAsCommand>();
|
2020-04-26 22:12:49 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = EFClient.Permission.Administrator;
|
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.NetworkId = 100;
|
|
|
|
|
origin.Level = EFClient.Permission.Administrator;
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Command));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_RunAsFailsOnDisallowedCommand()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<RunAsCommand>();
|
2020-04-26 22:12:49 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = EFClient.Permission.Moderator;
|
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.NetworkId = 100;
|
|
|
|
|
origin.Level = EFClient.Permission.Administrator;
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Owner = server,
|
|
|
|
|
Data = nameof(NonImpersonatableCommand)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
// failed when validating the command
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Command && _event.FailReason == GameEvent.EventFailReason.Invalid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_RunAsQueuesEventAndResponse()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<RunAsCommand>();
|
2020-04-26 22:12:49 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = EFClient.Permission.Moderator;
|
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.NetworkId = 100;
|
|
|
|
|
origin.Level = EFClient.Permission.Administrator;
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = nameof(ImpersonatableCommand),
|
|
|
|
|
Owner = server
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell /*&& _event.Target == origin todo: fake the command result*/ ));
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Command && !_event.Failed));
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2020-05-16 12:54:01 -04:00
|
|
|
|
|
|
|
|
|
#region SETLEVEL
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelFailOnSelf()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.Owner;
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = target,
|
|
|
|
|
Data = "Administrator",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Owner, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelFailWithSourcePrivilegeTooLow()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Moderator;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.Administrator;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Administrator",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Administrator, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelFailWithExistingOwner_AndOnlyOneOwnerAllowed()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Owner",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.User, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelFailWithStepPrivilegesDisabled_AndNonOwner()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.SeniorAdmin;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.Moderator;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Administrator",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Moderator, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelFailWithStepPrivilegesEnabled_ButNewPermissionTooHigh()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Moderator;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
appConfig.EnableSteppedHierarchy = true;
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Moderator",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.User, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelFailInvalidGroup()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Owner;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Banned",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.User, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelSucceedWithNoExistingOwner_AndOnlyOneOwnerAllowed()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Owner;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(0));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Owner",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Owner, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission && !_event.Failed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelOwnerSucceedWithMultiOwnerAllowed()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Owner;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
appConfig.EnableMultipleOwners = true;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Owner",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Owner, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission && !_event.Failed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelOwnerSucceedWithMultiOwnerAllowed_AndSteppedPrivileges()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Owner;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
appConfig.EnableMultipleOwners = true;
|
|
|
|
|
appConfig.EnableSteppedHierarchy = true;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Owner",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Owner, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission && !_event.Failed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelSucceedWithSteppedPrivileges()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Moderator;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
appConfig.EnableSteppedHierarchy = true;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Trusted",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Trusted, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission && !_event.Failed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelSucceed()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Owner;
|
|
|
|
|
var target = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
target.Level = Permission.User;
|
|
|
|
|
appConfig.EnableSteppedHierarchy = true;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = target,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "Trusted",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.Trusted, target.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission && !_event.Failed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_SetLevelSucceed_AndFindsIngameClient()
|
|
|
|
|
{
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<SetLevelCommand>();
|
2020-05-16 12:54:01 -04:00
|
|
|
|
var origin = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
origin.Level = Permission.Owner;
|
|
|
|
|
var databaseTarget = ClientGenerators.CreateDatabaseClient();
|
|
|
|
|
databaseTarget.Level = Permission.Administrator;
|
|
|
|
|
|
|
|
|
|
var ingameTarget = ClientGenerators.CreateBasicClient(server);
|
|
|
|
|
ingameTarget.Level = Permission.Administrator;
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => manager.GetActiveClients())
|
|
|
|
|
.Returns(new[] { ingameTarget });
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => clientService.GetOwnerCount())
|
|
|
|
|
.Returns(Task.FromResult(1));
|
|
|
|
|
|
|
|
|
|
var gameEvent = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Target = databaseTarget,
|
|
|
|
|
Origin = origin,
|
|
|
|
|
Data = "User",
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Permission.User, ingameTarget.Level);
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
Assert.IsNotEmpty(mockEventHandler.Events.Where(_event => _event.Type == GameEvent.EventType.ChangePermission && !_event.Failed));
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2020-07-31 21:40:03 -04:00
|
|
|
|
|
|
|
|
|
#region PREFIX_PROCESSING
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_CommandProcessing_IsBroadcastCommand()
|
|
|
|
|
{
|
|
|
|
|
string broadcastPrefix = "@@";
|
|
|
|
|
var config = ConfigurationGenerators.CreateApplicationConfiguration();
|
|
|
|
|
config.BroadcastCommandPrefix = broadcastPrefix;
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
|
|
|
|
|
var cmd = EventGenerators.GenerateEvent(GameEvent.EventType.Command, $"{broadcastPrefix}{nameof(MockCommand)}", server);
|
|
|
|
|
|
|
|
|
|
var result = await CommandProcessing.ValidateCommand(cmd, config);
|
|
|
|
|
Assert.AreEqual(nameof(MockCommand), result.Name);
|
|
|
|
|
Assert.IsTrue(result.IsBroadcast);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2020-09-26 18:17:21 -04:00
|
|
|
|
|
|
|
|
|
#region PMADMINS
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_PrivateMessageAdmins_HappyPath()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<PrivateMessageAdminsCommand>();
|
2020-09-26 18:17:21 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var origin = ClientGenerators.CreateDatabaseClient();
|
|
|
|
|
origin.Level = Permission.Administrator;
|
|
|
|
|
origin.CurrentServer = server;
|
|
|
|
|
var gameEvent = EventGenerators.GenerateEvent(GameEvent.EventType.Command, "", server);
|
|
|
|
|
cmdConfig.Commands.Add(nameof(PrivateMessageAdminsCommand), new CommandProperties { SupportedGames = new[] { server.GameName } });
|
2020-10-17 11:47:56 -04:00
|
|
|
|
|
2020-09-26 18:17:21 -04:00
|
|
|
|
server.Clients[0] = origin;
|
|
|
|
|
server.Clients[1] = origin;
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
int expectedEvents = 2;
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(expectedEvents, mockEventHandler.Events.Count(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_PrivateMessageAdmins_GameNotSupported()
|
|
|
|
|
{
|
2020-10-17 11:47:56 -04:00
|
|
|
|
var cmd = serviceProvider.GetRequiredService<PrivateMessageAdminsCommand>();
|
2020-09-26 18:17:21 -04:00
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var origin = ClientGenerators.CreateDatabaseClient();
|
|
|
|
|
origin.Level = Permission.Administrator;
|
|
|
|
|
origin.CurrentServer = server;
|
|
|
|
|
var gameEvent = EventGenerators.GenerateEvent(GameEvent.EventType.Command, "", server);
|
|
|
|
|
gameEvent.Origin = origin;
|
|
|
|
|
cmdConfig.Commands.Add(nameof(PrivateMessageAdminsCommand), new CommandProperties());
|
|
|
|
|
|
|
|
|
|
server.Clients[0] = origin;
|
|
|
|
|
server.Clients[1] = origin;
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
int expectedEvents = 1;
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(expectedEvents, mockEventHandler.Events.Count(_event => _event.Type == GameEvent.EventType.Tell));
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2020-10-17 11:47:56 -04:00
|
|
|
|
|
|
|
|
|
#region LOADMAP
|
|
|
|
|
[Test]
|
|
|
|
|
public void Test_LoadMap_WaitsAppropriateTime_BeforeExecutingCommand()
|
|
|
|
|
{
|
|
|
|
|
var cmd = serviceProvider.GetRequiredService<LoadMapCommand>();
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var rconParser = serviceProvider.GetRequiredService<IRConParser>();
|
|
|
|
|
server.Maps.Add(new Map()
|
|
|
|
|
{
|
|
|
|
|
Name = "mp_test",
|
|
|
|
|
Alias = "test"
|
|
|
|
|
});
|
|
|
|
|
var gameEvent = EventGenerators.GenerateEvent(GameEvent.EventType.Command, server.Maps.First().Name, server);
|
|
|
|
|
|
|
|
|
|
Func<Task> act = () => cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
act.ExecutionTime().Should().BeCloseTo(appConfig.MapChangeDelaySeconds.Seconds(), 500.Milliseconds());
|
|
|
|
|
A.CallTo(() => rconParser.ExecuteCommandAsync(A<IRConConnection>.Ignored, A<string>.Ignored))
|
|
|
|
|
.MustHaveHappened();
|
|
|
|
|
}
|
2020-11-03 21:04:11 -05:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Test_LoadMap_FindsMapName_FromPartialAlias()
|
|
|
|
|
{
|
|
|
|
|
var cmd = serviceProvider.GetRequiredService<LoadMapCommand>();
|
|
|
|
|
var server = serviceProvider.GetRequiredService<IW4MServer>();
|
|
|
|
|
var rconParser = serviceProvider.GetRequiredService<IRConParser>();
|
|
|
|
|
server.Maps.Add(new Map()
|
|
|
|
|
{
|
|
|
|
|
Name = "mp_test",
|
|
|
|
|
Alias = "test"
|
|
|
|
|
});
|
|
|
|
|
var gameEvent = EventGenerators.GenerateEvent(GameEvent.EventType.Command, server.Maps.First().Name, server);
|
|
|
|
|
|
|
|
|
|
await cmd.ExecuteAsync(gameEvent);
|
|
|
|
|
|
|
|
|
|
A.CallTo(() => rconParser.ExecuteCommandAsync(A<IRConConnection>.Ignored, A<string>.That.Contains(server.Maps[0].Name)))
|
|
|
|
|
.MustHaveHappened();
|
|
|
|
|
}
|
2020-10-17 11:47:56 -04:00
|
|
|
|
#endregion
|
2020-04-26 22:12:49 -04:00
|
|
|
|
}
|
|
|
|
|
}
|