diff --git a/src/BirdsiteLive.Moderation/Processors/FollowerModerationProcessor.cs b/src/BirdsiteLive.Moderation/Processors/FollowerModerationProcessor.cs index 18c9b14..99d72f6 100644 --- a/src/BirdsiteLive.Moderation/Processors/FollowerModerationProcessor.cs +++ b/src/BirdsiteLive.Moderation/Processors/FollowerModerationProcessor.cs @@ -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) diff --git a/src/BirdsiteLive.Moderation/Processors/TwitterAccountModerationProcessor.cs b/src/BirdsiteLive.Moderation/Processors/TwitterAccountModerationProcessor.cs index 3c267bb..91e3931 100644 --- a/src/BirdsiteLive.Moderation/Processors/TwitterAccountModerationProcessor.cs +++ b/src/BirdsiteLive.Moderation/Processors/TwitterAccountModerationProcessor.cs @@ -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) diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectAllFollowingsActionTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectAllFollowingsActionTests.cs index 3fda47b..3ed5c78 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectAllFollowingsActionTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectAllFollowingsActionTests.cs @@ -35,7 +35,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var twitterUserDalMock = new Mock(); + var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock .Setup(x => x.GetTwitterUserAsync( It.Is(y => y == 24))) @@ -45,7 +45,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions Acct = "acct" }); - var userServiceMock = new Mock(); + var userServiceMock = new Mock(MockBehavior.Strict); userServiceMock .Setup(x => x.SendRejectFollowAsync( It.Is(y => y.type == "Follow"), @@ -83,7 +83,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var twitterUserDalMock = new Mock(); + var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock .Setup(x => x.GetTwitterUserAsync( It.Is(y => y == 24))) @@ -93,7 +93,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions Acct = "acct" }); - var userServiceMock = new Mock(); + var userServiceMock = new Mock(MockBehavior.Strict); userServiceMock .Setup(x => x.SendRejectFollowAsync( It.Is(y => y.type == "Follow"), diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs index 7d7b1bf..74f037e 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs @@ -40,7 +40,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var userServiceMock = new Mock(); + var userServiceMock = new Mock(MockBehavior.Strict); userServiceMock .Setup(x => x.SendRejectFollowAsync( It.Is(y => y.type == "Follow"), @@ -83,7 +83,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var userServiceMock = new Mock(); + var userServiceMock = new Mock(MockBehavior.Strict); userServiceMock .Setup(x => x.SendRejectFollowAsync( It.Is(y => y.type == "Follow"), diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs index 4def0ed..3b83739 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs @@ -23,13 +23,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var rejectAllFollowingsActionMock = new Mock(); + var rejectAllFollowingsActionMock = new Mock(MockBehavior.Strict); rejectAllFollowingsActionMock .Setup(x => x.ProcessAsync( It.Is(y => y.Id == follower.Id))) .Returns(Task.CompletedTask); - var followersDalMock = new Mock(); + var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock .Setup(x => x.GetFollowersAsync( It.Is(y => y == 1))) @@ -40,7 +40,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions It.Is(y => y == 12))) .Returns(Task.CompletedTask); - var twitterUserDalMock = new Mock(); + var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock .Setup(x => x.DeleteTwitterUserAsync( It.Is(y => y == 1))) @@ -78,13 +78,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var rejectAllFollowingsActionMock = new Mock(); + var rejectAllFollowingsActionMock = new Mock(MockBehavior.Strict); rejectAllFollowingsActionMock .Setup(x => x.ProcessAsync( It.Is(y => y.Id == follower.Id))) .Returns(Task.CompletedTask); - var followersDalMock = new Mock(); + var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock .Setup(x => x.GetFollowersAsync( It.Is(y => y == 1))) @@ -95,7 +95,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions It.Is(y => y == 12))) .Returns(Task.CompletedTask); - var twitterUserDalMock = new Mock(); + var twitterUserDalMock = new Mock(MockBehavior.Strict); #endregion var action = new RemoveFollowerAction(followersDalMock.Object, twitterUserDalMock.Object, rejectAllFollowingsActionMock.Object); diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs index 5e9342b..c0f5920 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs @@ -33,7 +33,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var followersDalMock = new Mock(); + var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock .Setup(x => x.GetFollowersAsync( It.Is(y => y == 24))) @@ -44,13 +44,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions It.Is(y => y == 48))) .Returns(Task.CompletedTask); - var twitterUserDalMock = new Mock(); + var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock .Setup(x => x.DeleteTwitterUserAsync( It.Is(y => y == 24))) .Returns(Task.CompletedTask); - var rejectFollowingActionMock = new Mock(); + var rejectFollowingActionMock = new Mock(MockBehavior.Strict); rejectFollowingActionMock .Setup(x => x.ProcessAsync( It.Is(y => y.Id == 48), @@ -90,7 +90,7 @@ namespace BirdsiteLive.Moderation.Tests.Actions #endregion #region Mocks - var followersDalMock = new Mock(); + var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock .Setup(x => x.GetFollowersAsync( It.Is(y => y == 24))) @@ -104,13 +104,13 @@ namespace BirdsiteLive.Moderation.Tests.Actions ))) .Returns(Task.CompletedTask); - var twitterUserDalMock = new Mock(); + var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock .Setup(x => x.DeleteTwitterUserAsync( It.Is(y => y == 24))) .Returns(Task.CompletedTask); - var rejectFollowingActionMock = new Mock(); + var rejectFollowingActionMock = new Mock(MockBehavior.Strict); rejectFollowingActionMock .Setup(x => x.ProcessAsync( It.Is(y => y.Id == 48), diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Processors/FollowerModerationProcessorTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Processors/FollowerModerationProcessorTests.cs index 3acaa4f..3bc4946 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Processors/FollowerModerationProcessorTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Processors/FollowerModerationProcessorTests.cs @@ -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(MockBehavior.Strict); + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + var removeFollowerActionMock = new Mock(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 + { + new Follower + { + Acct = "acct", + Host = "host" + } + }; + #endregion + + #region Mocks + var followersDalMock = new Mock(MockBehavior.Strict); + followersDalMock + .Setup(x => x.GetAllFollowersAsync()) + .ReturnsAsync(allFollowers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.Follower), + It.Is(y => y == "@acct@host"))) + .Returns(ModeratedTypeEnum.WhiteListed); + + var removeFollowerActionMock = new Mock(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 + { + new Follower + { + Acct = "acct", + Host = "host" + } + }; + #endregion + + #region Mocks + var followersDalMock = new Mock(MockBehavior.Strict); + followersDalMock + .Setup(x => x.GetAllFollowersAsync()) + .ReturnsAsync(allFollowers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.Follower), + It.Is(y => y == "@acct@host"))) + .Returns(ModeratedTypeEnum.None); + + var removeFollowerActionMock = new Mock(MockBehavior.Strict); + removeFollowerActionMock + .Setup(x => x.ProcessAsync( + It.Is(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 + { + new Follower + { + Acct = "acct", + Host = "host" + } + }; + #endregion + + #region Mocks + var followersDalMock = new Mock(MockBehavior.Strict); + followersDalMock + .Setup(x => x.GetAllFollowersAsync()) + .ReturnsAsync(allFollowers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.Follower), + It.Is(y => y == "@acct@host"))) + .Returns(ModeratedTypeEnum.BlackListed); + + var removeFollowerActionMock = new Mock(MockBehavior.Strict); + removeFollowerActionMock + .Setup(x => x.ProcessAsync( + It.Is(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 + { + new Follower + { + Acct = "acct", + Host = "host" + } + }; + #endregion + + #region Mocks + var followersDalMock = new Mock(MockBehavior.Strict); + followersDalMock + .Setup(x => x.GetAllFollowersAsync()) + .ReturnsAsync(allFollowers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.Follower), + It.Is(y => y == "@acct@host"))) + .Returns(ModeratedTypeEnum.None); + + var removeFollowerActionMock = new Mock(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 + } } } \ No newline at end of file diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Processors/TwitterAccountModerationProcessorTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Processors/TwitterAccountModerationProcessorTests.cs index 8cf2020..21d1288 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Processors/TwitterAccountModerationProcessorTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Processors/TwitterAccountModerationProcessorTests.cs @@ -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(MockBehavior.Strict); + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + var removeTwitterAccountActionMock = new Mock(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 + { + new SyncTwitterUser + { + Acct = "acct" + } + }; + #endregion + + #region Mocks + var twitterUserDalMock = new Mock(MockBehavior.Strict); + twitterUserDalMock + .Setup(x => x.GetAllTwitterUsersAsync()) + .ReturnsAsync(allUsers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.TwitterAccount), + It.Is(y => y == "acct"))) + .Returns(ModeratedTypeEnum.WhiteListed); + + var removeTwitterAccountActionMock = new Mock(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 + { + new SyncTwitterUser + { + Acct = "acct" + } + }; + #endregion + + #region Mocks + var twitterUserDalMock = new Mock(MockBehavior.Strict); + twitterUserDalMock + .Setup(x => x.GetAllTwitterUsersAsync()) + .ReturnsAsync(allUsers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.TwitterAccount), + It.Is(y => y == "acct"))) + .Returns(ModeratedTypeEnum.None); + + var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); + removeTwitterAccountActionMock + .Setup(x => x.ProcessAsync( + It.Is(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 + { + new SyncTwitterUser + { + Acct = "acct" + } + }; + #endregion + + #region Mocks + var twitterUserDalMock = new Mock(MockBehavior.Strict); + twitterUserDalMock + .Setup(x => x.GetAllTwitterUsersAsync()) + .ReturnsAsync(allUsers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.TwitterAccount), + It.Is(y => y == "acct"))) + .Returns(ModeratedTypeEnum.BlackListed); + + var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); + removeTwitterAccountActionMock + .Setup(x => x.ProcessAsync( + It.Is(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 + { + new SyncTwitterUser + { + Acct = "acct" + } + }; + #endregion + + #region Mocks + var twitterUserDalMock = new Mock(MockBehavior.Strict); + twitterUserDalMock + .Setup(x => x.GetAllTwitterUsersAsync()) + .ReturnsAsync(allUsers.ToArray()); + + var moderationRepositoryMock = new Mock(MockBehavior.Strict); + moderationRepositoryMock + .Setup(x => x.CheckStatus( + It.Is(y => y == ModerationEntityTypeEnum.TwitterAccount), + It.Is(y => y == "acct"))) + .Returns(ModeratedTypeEnum.None); + + var removeTwitterAccountActionMock = new Mock(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 + } } } \ No newline at end of file