Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T/155 IAntiSpamService SetPunishment unit tests #235

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Watchman.Discord.UnitTests/Muting/AntiSpamServiceTests.cs
Original file line number Diff line number Diff line change
@@ -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<ICommandBus>();
var messagesServiceFactoryMock = new Mock<IMessagesServiceFactory>();
var unmutingServiceMock = new Mock<IUnmutingService>();

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<MuteUserOrOverwriteCommand>()), Times.Never);
unmutingServiceMock.Verify(x => x.UnmuteInFuture(contexts, It.IsAny<MuteEvent>(), It.IsAny<UserContext>()), 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<IMessagesService>();
var messagesServiceFactoryMock = new Mock<IMessagesServiceFactory>();
messagesServiceFactoryMock.Setup(x => x.Create(It.IsAny<Contexts>()))
.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<Func<IResponsesService, string>>()), 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<ICommandBus>();
var unmutingServiceMock = new Mock<IUnmutingService>();

var service = new AntiSpamService(commandBusMock.Object, messagesServiceFactory: null, unmutingServiceMock.Object);

//Act
await service.SetPunishment(contexts, punishment);

//Assert
commandBusMock.Verify(x => x.ExecuteAsync(It.IsAny<MuteUserOrOverwriteCommand>()), Times.Once);
unmutingServiceMock.Verify(x => x.UnmuteInFuture(contexts, It.IsAny<MuteEvent>(), It.IsAny<UserContext>()), Times.Once);
}
}
}
2 changes: 1 addition & 1 deletion Watchman.Discord/Areas/Muting/Services/AntiSpamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down