From 80d63bd41d3e21b3b8d479e40017ad0b5631e5e3 Mon Sep 17 00:00:00 2001 From: DavidPL-coder Date: Fri, 17 Jun 2022 20:51:53 +0200 Subject: [PATCH 1/6] Add test to AntiSpamService.SetPunishment for "Warn" case. --- .../Muting/AntiSpamServiceTests.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs diff --git a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs new file mode 100644 index 000000000..c2d7a79b3 --- /dev/null +++ b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs @@ -0,0 +1,52 @@ +using NUnit.Framework; +using Moq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Watchman.Discord.UnitTests.TestObjectFactories; +using AutoFixture.NUnit3; +using Devscord.DiscordFramework.Commands.AntiSpam.Models; +using Watchman.Cqrs; +using Watchman.Discord.Areas.Muting.Services; +using Devscord.DiscordFramework.Services; +using Devscord.DiscordFramework.Middlewares.Contexts; +using Devscord.DiscordFramework.Services.Factories; +using Watchman.Discord.Areas.Muting.Services.Commands; +using Watchman.DomainModel.Muting; +using Devscord.DiscordFramework.Commands.Responses; + +namespace Watchman.Discord.UnitTests.Muting +{ + [TestFixture] + internal class AntiSpamServiceTests + { + private readonly TestContextsFactory testContextsFactory = new(); + + [Test, AutoData] + public async Task SetPunishment_ShouldSendResponseThatSpamAlertRecognizedWhenPunishmentOptionIsWarn(DateTime dateTime) + { + //Arrange + var contexts = testContextsFactory.CreateContexts(1, 1, 1); + var punishment = new Punishment(PunishmentOption.Warn, dateTime); + + var commandBusMock = new Mock(); + var messagesServiceMock = new Mock(); + var messagesServiceFactoryMock = new Mock(); + messagesServiceFactoryMock.Setup(x => x.Create(It.IsAny())) + .Returns(messagesServiceMock.Object); + var unmutingServiceMock = new Mock(); + + var service = new AntiSpamService(commandBusMock.Object, messagesServiceFactoryMock.Object, unmutingServiceMock.Object); + + //Act + await service.SetPunishment(contexts, punishment); + + //Assert + messagesServiceMock.Verify(x => x.SendResponse(It.IsAny>()), Times.Once); + commandBusMock.Verify(x => x.ExecuteAsync(It.IsAny()), Times.Never); + unmutingServiceMock.Verify(x => x.UnmuteInFuture(contexts, It.IsAny(), It.IsAny()), Times.Never); + } + } +} From 199dc3bb189766c29e21044c7493b6f6a5db45b0 Mon Sep 17 00:00:00 2001 From: DavidPL-coder Date: Fri, 17 Jun 2022 20:55:43 +0200 Subject: [PATCH 2/6] Refactor code. --- Watchman.Discord/Areas/Muting/Services/AntiSpamService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: From 5421026338c31eb90edaba0762fab6998e83868d Mon Sep 17 00:00:00 2001 From: DavidPL-coder Date: Fri, 17 Jun 2022 21:06:09 +0200 Subject: [PATCH 3/6] Add test to AntiSpamService.SetPunishment for "Mute" case. --- .../Muting/AntiSpamServiceTests.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs index c2d7a79b3..ef148f603 100644 --- a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs +++ b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs @@ -48,5 +48,25 @@ public async Task SetPunishment_ShouldSendResponseThatSpamAlertRecognizedWhenPun 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_ShouldMuteUserForSpamWhenPunishmentOptionIsMute(DateTime dateTime, TimeSpan forTime) + { + //Arrange + var contexts = testContextsFactory.CreateContexts(1, 1, 1); + var punishment = new Punishment(PunishmentOption.Mute, dateTime, 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); + } } } From 4422ec9c73ac70f9d953d51e14e63ca39b6ae5fe Mon Sep 17 00:00:00 2001 From: DavidPL-coder Date: Fri, 17 Jun 2022 21:07:59 +0200 Subject: [PATCH 4/6] Refactor code in Tests. --- Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs index ef148f603..058a11ce3 100644 --- a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs +++ b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs @@ -31,22 +31,18 @@ public async Task SetPunishment_ShouldSendResponseThatSpamAlertRecognizedWhenPun var contexts = testContextsFactory.CreateContexts(1, 1, 1); var punishment = new Punishment(PunishmentOption.Warn, dateTime); - var commandBusMock = new Mock(); var messagesServiceMock = new Mock(); var messagesServiceFactoryMock = new Mock(); messagesServiceFactoryMock.Setup(x => x.Create(It.IsAny())) .Returns(messagesServiceMock.Object); - var unmutingServiceMock = new Mock(); - var service = new AntiSpamService(commandBusMock.Object, messagesServiceFactoryMock.Object, unmutingServiceMock.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); - commandBusMock.Verify(x => x.ExecuteAsync(It.IsAny()), Times.Never); - unmutingServiceMock.Verify(x => x.UnmuteInFuture(contexts, It.IsAny(), It.IsAny()), Times.Never); } [Test, AutoData] From 26fcf4e832d982e4daae84b76996559e3de37ac0 Mon Sep 17 00:00:00 2001 From: DavidPL-coder Date: Fri, 17 Jun 2022 21:16:39 +0200 Subject: [PATCH 5/6] Add test to AntiSpamService.SetPunishment for "Nothing" case. --- .../Muting/AntiSpamServiceTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs index 058a11ce3..b3f8e0e8d 100644 --- a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs +++ b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs @@ -24,6 +24,28 @@ internal class AntiSpamServiceTests { private readonly TestContextsFactory testContextsFactory = new(); + [Test, AutoData] + public async Task SetPunishment_ShouldDoNothingWhenPunishmentOptionIsNothing(DateTime dateTime) + { + //Arrange + var contexts = testContextsFactory.CreateContexts(1, 1, 1); + var punishment = new Punishment(PunishmentOption.Nothing, dateTime); + + 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 dateTime) { From 44c80a2d4339cccef2cfb60e6b6e1c8e0ca4dde6 Mon Sep 17 00:00:00 2001 From: DavidPL-coder Date: Fri, 17 Jun 2022 21:18:32 +0200 Subject: [PATCH 6/6] Refactor in Tests. --- .../Muting/AntiSpamServiceTests.cs | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs index b3f8e0e8d..a4786c097 100644 --- a/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs +++ b/Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs @@ -1,21 +1,18 @@ -using NUnit.Framework; +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.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; -using Watchman.Discord.UnitTests.TestObjectFactories; -using AutoFixture.NUnit3; -using Devscord.DiscordFramework.Commands.AntiSpam.Models; using Watchman.Cqrs; using Watchman.Discord.Areas.Muting.Services; -using Devscord.DiscordFramework.Services; -using Devscord.DiscordFramework.Middlewares.Contexts; -using Devscord.DiscordFramework.Services.Factories; using Watchman.Discord.Areas.Muting.Services.Commands; +using Watchman.Discord.UnitTests.TestObjectFactories; using Watchman.DomainModel.Muting; -using Devscord.DiscordFramework.Commands.Responses; namespace Watchman.Discord.UnitTests.Muting { @@ -25,11 +22,11 @@ internal class AntiSpamServiceTests private readonly TestContextsFactory testContextsFactory = new(); [Test, AutoData] - public async Task SetPunishment_ShouldDoNothingWhenPunishmentOptionIsNothing(DateTime dateTime) + public async Task SetPunishment_ShouldDoNothingWhenPunishmentOptionIsNothing(DateTime givenAt) { //Arrange var contexts = testContextsFactory.CreateContexts(1, 1, 1); - var punishment = new Punishment(PunishmentOption.Nothing, dateTime); + var punishment = new Punishment(PunishmentOption.Nothing, givenAt); var commandBusMock = new Mock(); var messagesServiceFactoryMock = new Mock(); @@ -47,11 +44,11 @@ public async Task SetPunishment_ShouldDoNothingWhenPunishmentOptionIsNothing(Dat } [Test, AutoData] - public async Task SetPunishment_ShouldSendResponseThatSpamAlertRecognizedWhenPunishmentOptionIsWarn(DateTime dateTime) + public async Task SetPunishment_ShouldSendResponseThatSpamAlertRecognizedWhenPunishmentOptionIsWarn(DateTime givenAt) { //Arrange var contexts = testContextsFactory.CreateContexts(1, 1, 1); - var punishment = new Punishment(PunishmentOption.Warn, dateTime); + var punishment = new Punishment(PunishmentOption.Warn, givenAt); var messagesServiceMock = new Mock(); var messagesServiceFactoryMock = new Mock(); @@ -68,11 +65,11 @@ public async Task SetPunishment_ShouldSendResponseThatSpamAlertRecognizedWhenPun } [Test, AutoData] - public async Task SetPunishment_ShouldMuteUserForSpamWhenPunishmentOptionIsMute(DateTime dateTime, TimeSpan forTime) + public async Task SetPunishment_ShouldMuteUserForSpamWhenPunishmentOptionIsMute(DateTime givenAt, TimeSpan forTime) { //Arrange var contexts = testContextsFactory.CreateContexts(1, 1, 1); - var punishment = new Punishment(PunishmentOption.Mute, dateTime, forTime); + var punishment = new Punishment(PunishmentOption.Mute, givenAt, forTime); var commandBusMock = new Mock(); var unmutingServiceMock = new Mock();