added processor tests

This commit is contained in:
Nicolas Constant 2021-02-16 00:20:35 -05:00
parent abce8e5ab5
commit 2ff6128d70
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
8 changed files with 415 additions and 23 deletions

View file

@ -1,6 +1,5 @@
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain.Repository;
using BirdsiteLive.Moderation.Actions;
@ -28,6 +27,8 @@ namespace BirdsiteLive.Moderation.Processors
public async Task ProcessAsync(ModerationTypeEnum type)
{
if (type == ModerationTypeEnum.None) return;
var followers = await _followersDal.GetAllFollowersAsync();
foreach (var follower in followers)

View file

@ -27,6 +27,8 @@ namespace BirdsiteLive.Moderation.Processors
public async Task ProcessAsync(ModerationTypeEnum type)
{
if (type == ModerationTypeEnum.None) return;
var twitterUsers = await _twitterUserDal.GetAllTwitterUsersAsync();
foreach (var user in twitterUsers)

View file

@ -35,7 +35,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>();
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetTwitterUserAsync(
It.Is<int>(y => y == 24)))
@ -45,7 +45,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
Acct = "acct"
});
var userServiceMock = new Mock<IUserService>();
var userServiceMock = new Mock<IUserService>(MockBehavior.Strict);
userServiceMock
.Setup(x => x.SendRejectFollowAsync(
It.Is<ActivityFollow>(y => y.type == "Follow"),
@ -83,7 +83,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>();
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetTwitterUserAsync(
It.Is<int>(y => y == 24)))
@ -93,7 +93,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
Acct = "acct"
});
var userServiceMock = new Mock<IUserService>();
var userServiceMock = new Mock<IUserService>(MockBehavior.Strict);
userServiceMock
.Setup(x => x.SendRejectFollowAsync(
It.Is<ActivityFollow>(y => y.type == "Follow"),

View file

@ -40,7 +40,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var userServiceMock = new Mock<IUserService>();
var userServiceMock = new Mock<IUserService>(MockBehavior.Strict);
userServiceMock
.Setup(x => x.SendRejectFollowAsync(
It.Is<ActivityFollow>(y => y.type == "Follow"),
@ -83,7 +83,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var userServiceMock = new Mock<IUserService>();
var userServiceMock = new Mock<IUserService>(MockBehavior.Strict);
userServiceMock
.Setup(x => x.SendRejectFollowAsync(
It.Is<ActivityFollow>(y => y.type == "Follow"),

View file

@ -23,13 +23,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var rejectAllFollowingsActionMock = new Mock<IRejectAllFollowingsAction>();
var rejectAllFollowingsActionMock = new Mock<IRejectAllFollowingsAction>(MockBehavior.Strict);
rejectAllFollowingsActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == follower.Id)))
.Returns(Task.CompletedTask);
var followersDalMock = new Mock<IFollowersDal>();
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 1)))
@ -40,7 +40,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
It.Is<int>(y => y == 12)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.DeleteTwitterUserAsync(
It.Is<int>(y => y == 1)))
@ -78,13 +78,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var rejectAllFollowingsActionMock = new Mock<IRejectAllFollowingsAction>();
var rejectAllFollowingsActionMock = new Mock<IRejectAllFollowingsAction>(MockBehavior.Strict);
rejectAllFollowingsActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == follower.Id)))
.Returns(Task.CompletedTask);
var followersDalMock = new Mock<IFollowersDal>();
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 1)))
@ -95,7 +95,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
It.Is<int>(y => y == 12)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
#endregion
var action = new RemoveFollowerAction(followersDalMock.Object, twitterUserDalMock.Object, rejectAllFollowingsActionMock.Object);

View file

@ -33,7 +33,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>();
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 24)))
@ -44,13 +44,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions
It.Is<int>(y => y == 48)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.DeleteTwitterUserAsync(
It.Is<int>(y => y == 24)))
.Returns(Task.CompletedTask);
var rejectFollowingActionMock = new Mock<IRejectFollowingAction>();
var rejectFollowingActionMock = new Mock<IRejectFollowingAction>(MockBehavior.Strict);
rejectFollowingActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == 48),
@ -90,7 +90,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>();
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 24)))
@ -104,13 +104,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions
)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.DeleteTwitterUserAsync(
It.Is<int>(y => y == 24)))
.Returns(Task.CompletedTask);
var rejectFollowingActionMock = new Mock<IRejectFollowingAction>();
var rejectFollowingActionMock = new Mock<IRejectFollowingAction>(MockBehavior.Strict);
rejectFollowingActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == 48),

View file

@ -1,7 +1,204 @@
namespace BirdsiteLive.Moderation.Tests.Processors
using System.Collections.Generic;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain.Repository;
using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Moderation.Processors;
using Castle.DynamicProxy.Generators.Emitters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace BirdsiteLive.Moderation.Tests.Processors
{
[TestClass]
public class FollowerModerationProcessorTests
{
[TestMethod]
public async Task ProcessAsync_None()
{
#region Mocks
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
var removeFollowerActionMock = new Mock<IRemoveFollowerAction>(MockBehavior.Strict);
#endregion
var processor = new FollowerModerationProcessor(followersDalMock.Object, moderationRepositoryMock.Object, removeFollowerActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.None);
#region Validations
followersDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeFollowerActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_WhiteListing_WhiteListed()
{
#region Stubs
var allFollowers = new List<Follower>
{
new Follower
{
Acct = "acct",
Host = "host"
}
};
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetAllFollowersAsync())
.ReturnsAsync(allFollowers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.Follower),
It.Is<string>(y => y == "@acct@host")))
.Returns(ModeratedTypeEnum.WhiteListed);
var removeFollowerActionMock = new Mock<IRemoveFollowerAction>(MockBehavior.Strict);
#endregion
var processor = new FollowerModerationProcessor(followersDalMock.Object, moderationRepositoryMock.Object, removeFollowerActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.WhiteListing);
#region Validations
followersDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeFollowerActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_WhiteListing_NotWhiteListed()
{
#region Stubs
var allFollowers = new List<Follower>
{
new Follower
{
Acct = "acct",
Host = "host"
}
};
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetAllFollowersAsync())
.ReturnsAsync(allFollowers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.Follower),
It.Is<string>(y => y == "@acct@host")))
.Returns(ModeratedTypeEnum.None);
var removeFollowerActionMock = new Mock<IRemoveFollowerAction>(MockBehavior.Strict);
removeFollowerActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Acct == "acct")))
.Returns(Task.CompletedTask);
#endregion
var processor = new FollowerModerationProcessor(followersDalMock.Object, moderationRepositoryMock.Object, removeFollowerActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.WhiteListing);
#region Validations
followersDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeFollowerActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_BlackListing_BlackListed()
{
#region Stubs
var allFollowers = new List<Follower>
{
new Follower
{
Acct = "acct",
Host = "host"
}
};
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetAllFollowersAsync())
.ReturnsAsync(allFollowers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.Follower),
It.Is<string>(y => y == "@acct@host")))
.Returns(ModeratedTypeEnum.BlackListed);
var removeFollowerActionMock = new Mock<IRemoveFollowerAction>(MockBehavior.Strict);
removeFollowerActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Acct == "acct")))
.Returns(Task.CompletedTask);
#endregion
var processor = new FollowerModerationProcessor(followersDalMock.Object, moderationRepositoryMock.Object, removeFollowerActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.BlackListing);
#region Validations
followersDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeFollowerActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_BlackListing_NotBlackListed()
{
#region Stubs
var allFollowers = new List<Follower>
{
new Follower
{
Acct = "acct",
Host = "host"
}
};
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>(MockBehavior.Strict);
followersDalMock
.Setup(x => x.GetAllFollowersAsync())
.ReturnsAsync(allFollowers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.Follower),
It.Is<string>(y => y == "@acct@host")))
.Returns(ModeratedTypeEnum.None);
var removeFollowerActionMock = new Mock<IRemoveFollowerAction>(MockBehavior.Strict);
#endregion
var processor = new FollowerModerationProcessor(followersDalMock.Object, moderationRepositoryMock.Object, removeFollowerActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.BlackListing);
#region Validations
followersDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeFollowerActionMock.VerifyAll();
#endregion
}
}
}

View file

@ -1,7 +1,199 @@
namespace BirdsiteLive.Moderation.Tests.Processors
using System.Collections.Generic;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain.Repository;
using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Moderation.Processors;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace BirdsiteLive.Moderation.Tests.Processors
{
[TestClass]
public class TwitterAccountModerationProcessorTests
{
[TestMethod]
public async Task ProcessAsync_None()
{
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
var removeTwitterAccountActionMock = new Mock<IRemoveTwitterAccountAction>(MockBehavior.Strict);
#endregion
var processor = new TwitterAccountModerationProcessor(twitterUserDalMock.Object, moderationRepositoryMock.Object, removeTwitterAccountActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.None);
#region Validations
twitterUserDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeTwitterAccountActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_WhiteListing_WhiteListed()
{
#region Stubs
var allUsers = new List<SyncTwitterUser>
{
new SyncTwitterUser
{
Acct = "acct"
}
};
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetAllTwitterUsersAsync())
.ReturnsAsync(allUsers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.TwitterAccount),
It.Is<string>(y => y == "acct")))
.Returns(ModeratedTypeEnum.WhiteListed);
var removeTwitterAccountActionMock = new Mock<IRemoveTwitterAccountAction>(MockBehavior.Strict);
#endregion
var processor = new TwitterAccountModerationProcessor(twitterUserDalMock.Object, moderationRepositoryMock.Object, removeTwitterAccountActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.WhiteListing);
#region Validations
twitterUserDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeTwitterAccountActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_WhiteListing_NotWhiteListed()
{
#region Stubs
var allUsers = new List<SyncTwitterUser>
{
new SyncTwitterUser
{
Acct = "acct"
}
};
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetAllTwitterUsersAsync())
.ReturnsAsync(allUsers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.TwitterAccount),
It.Is<string>(y => y == "acct")))
.Returns(ModeratedTypeEnum.None);
var removeTwitterAccountActionMock = new Mock<IRemoveTwitterAccountAction>(MockBehavior.Strict);
removeTwitterAccountActionMock
.Setup(x => x.ProcessAsync(
It.Is<SyncTwitterUser>(y => y.Acct == "acct")))
.Returns(Task.CompletedTask);
#endregion
var processor = new TwitterAccountModerationProcessor(twitterUserDalMock.Object, moderationRepositoryMock.Object, removeTwitterAccountActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.WhiteListing);
#region Validations
twitterUserDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeTwitterAccountActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_BlackListing_BlackListed()
{
#region Stubs
var allUsers = new List<SyncTwitterUser>
{
new SyncTwitterUser
{
Acct = "acct"
}
};
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetAllTwitterUsersAsync())
.ReturnsAsync(allUsers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.TwitterAccount),
It.Is<string>(y => y == "acct")))
.Returns(ModeratedTypeEnum.BlackListed);
var removeTwitterAccountActionMock = new Mock<IRemoveTwitterAccountAction>(MockBehavior.Strict);
removeTwitterAccountActionMock
.Setup(x => x.ProcessAsync(
It.Is<SyncTwitterUser>(y => y.Acct == "acct")))
.Returns(Task.CompletedTask);
#endregion
var processor = new TwitterAccountModerationProcessor(twitterUserDalMock.Object, moderationRepositoryMock.Object, removeTwitterAccountActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.BlackListing);
#region Validations
twitterUserDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeTwitterAccountActionMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task ProcessAsync_BlackListing_NotBlackListed()
{
#region Stubs
var allUsers = new List<SyncTwitterUser>
{
new SyncTwitterUser
{
Acct = "acct"
}
};
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetAllTwitterUsersAsync())
.ReturnsAsync(allUsers.ToArray());
var moderationRepositoryMock = new Mock<IModerationRepository>(MockBehavior.Strict);
moderationRepositoryMock
.Setup(x => x.CheckStatus(
It.Is<ModerationEntityTypeEnum>(y => y == ModerationEntityTypeEnum.TwitterAccount),
It.Is<string>(y => y == "acct")))
.Returns(ModeratedTypeEnum.None);
var removeTwitterAccountActionMock = new Mock<IRemoveTwitterAccountAction>(MockBehavior.Strict);
#endregion
var processor = new TwitterAccountModerationProcessor(twitterUserDalMock.Object, moderationRepositoryMock.Object, removeTwitterAccountActionMock.Object);
await processor.ProcessAsync(ModerationTypeEnum.BlackListing);
#region Validations
twitterUserDalMock.VerifyAll();
moderationRepositoryMock.VerifyAll();
removeTwitterAccountActionMock.VerifyAll();
#endregion
}
}
}