diff --git a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs new file mode 100644 index 000000000..a4786c097 --- /dev/null +++ b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs @@ -0,0 +1,87 @@ +using AutoFixture.NUnit3; +using Devscord.DiscordFramework.Commands.AntiSpam.Models; +using Devscord.DiscordFramework.Commands.Responses; +using Devscord.DiscordFramework.Middlewares.Contexts; +using Devscord.DiscordFramework.Services; +using Devscord.DiscordFramework.Services.Factories; +using Moq; +using NUnit.Framework; +using System; +using System.Threading.Tasks; +using Watchman.Cqrs; +using Watchman.Discord.Areas.Muting.Services; +using Watchman.Discord.Areas.Muting.Services.Commands; +using Watchman.Discord.UnitTests.TestObjectFactories; +using Watchman.DomainModel.Muting; + +namespace Watchman.Discord.UnitTests.Muting +{ + [TestFixture] + internal class AntiSpamServiceTests + { + private readonly TestContextsFactory testContextsFactory = new(); + + [Test, AutoData] + public async Task SetPunishment_ShouldDoNothingWhenPunishmentOptionIsNothing(DateTime givenAt) + { + //Arrange + var contexts = testContextsFactory.CreateContexts(1, 1, 1); + var punishment = new Punishment(PunishmentOption.Nothing, givenAt); + + var commandBusMock = new Mock(); + var messagesServiceFactoryMock = new Mock(); + var unmutingServiceMock = new Mock(); + + var service = new AntiSpamService(commandBusMock.Object, messagesServiceFactoryMock.Object, unmutingServiceMock.Object); + + //Act + await service.SetPunishment(contexts, punishment); + + //Assert + messagesServiceFactoryMock.Verify(x => x.Create(contexts), Times.Never); + commandBusMock.Verify(x => x.ExecuteAsync(It.IsAny()), Times.Never); + unmutingServiceMock.Verify(x => x.UnmuteInFuture(contexts, It.IsAny(), It.IsAny()), Times.Never); + } + + [Test, AutoData] + public async Task SetPunishment_ShouldSendResponseThatSpamAlertRecognizedWhenPunishmentOptionIsWarn(DateTime givenAt) + { + //Arrange + var contexts = testContextsFactory.CreateContexts(1, 1, 1); + var punishment = new Punishment(PunishmentOption.Warn, givenAt); + + var messagesServiceMock = new Mock(); + var messagesServiceFactoryMock = new Mock(); + messagesServiceFactoryMock.Setup(x => x.Create(It.IsAny())) + .Returns(messagesServiceMock.Object); + + var service = new AntiSpamService(commandBus: null, messagesServiceFactoryMock.Object, unmutingService: null); + + //Act + await service.SetPunishment(contexts, punishment); + + //Assert + messagesServiceMock.Verify(x => x.SendResponse(It.IsAny>()), Times.Once); + } + + [Test, AutoData] + public async Task SetPunishment_ShouldMuteUserForSpamWhenPunishmentOptionIsMute(DateTime givenAt, TimeSpan forTime) + { + //Arrange + var contexts = testContextsFactory.CreateContexts(1, 1, 1); + var punishment = new Punishment(PunishmentOption.Mute, givenAt, forTime); + + var commandBusMock = new Mock(); + var unmutingServiceMock = new Mock(); + + var service = new AntiSpamService(commandBusMock.Object, messagesServiceFactory: null, unmutingServiceMock.Object); + + //Act + await service.SetPunishment(contexts, punishment); + + //Assert + commandBusMock.Verify(x => x.ExecuteAsync(It.IsAny()), Times.Once); + unmutingServiceMock.Verify(x => x.UnmuteInFuture(contexts, It.IsAny(), It.IsAny()), Times.Once); + } + } +} diff --git a/Watchman.Discord/Areas/Muting/Services/AntiSpamService.cs b/Watchman.Discord/Areas/Muting/Services/AntiSpamService.cs index d030dddd3..b25e656e9 100644 --- a/Watchman.Discord/Areas/Muting/Services/AntiSpamService.cs +++ b/Watchman.Discord/Areas/Muting/Services/AntiSpamService.cs @@ -39,10 +39,10 @@ public async Task SetPunishment(Contexts contexts, Punishment punishment) } Log.Information("Spam recognized! User: {user} on channel: {channel} server: {server}", contexts.User.Name, contexts.Channel.Name, contexts.Server.Name); - var messagesService = this._messagesServiceFactory.Create(contexts); switch (punishment.PunishmentOption) { case PunishmentOption.Warn: + var messagesService = this._messagesServiceFactory.Create(contexts); await messagesService.SendResponse(x => x.SpamAlertRecognized(contexts)); break; case PunishmentOption.Mute: