From abce8e5ab542f73e51b2b63b5ed322a02910e2ad Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Mon, 15 Feb 2021 23:31:22 -0500 Subject: [PATCH] added actions tests --- .../Actions/RemoveTwitterAccountAction.cs | 9 +- .../Actions/RejectFollowingActionTests.cs | 100 +++++++++++++- .../Actions/RemoveFollowerActionTests.cs | 108 ++++++++++++++- .../RemoveTwitterAccountActionTests.cs | 128 +++++++++++++++++- 4 files changed, 332 insertions(+), 13 deletions(-) diff --git a/src/BirdsiteLive.Moderation/Actions/RemoveTwitterAccountAction.cs b/src/BirdsiteLive.Moderation/Actions/RemoveTwitterAccountAction.cs index 1babf23..714cca6 100644 --- a/src/BirdsiteLive.Moderation/Actions/RemoveTwitterAccountAction.cs +++ b/src/BirdsiteLive.Moderation/Actions/RemoveTwitterAccountAction.cs @@ -1,12 +1,7 @@ -using System; -using System.Linq; +using System.Linq; using System.Threading.Tasks; -using BirdsiteLive.ActivityPub; -using BirdsiteLive.ActivityPub.Converters; -using BirdsiteLive.Common.Settings; using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Models; -using BirdsiteLive.Domain; namespace BirdsiteLive.Moderation.Actions { @@ -56,7 +51,7 @@ namespace BirdsiteLive.Moderation.Actions } // Remove twitter user - await _twitterUserDal.DeleteTwitterUserAsync(twitterUser.Acct); + await _twitterUserDal.DeleteTwitterUserAsync(twitterUserId); } } } \ No newline at end of file diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs index 870f1bd..7d7b1bf 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RejectFollowingActionTests.cs @@ -1,7 +1,103 @@ -namespace BirdsiteLive.Moderation.Tests.Actions +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using BirdsiteLive.ActivityPub; +using BirdsiteLive.Common.Settings; +using BirdsiteLive.DAL.Models; +using BirdsiteLive.Domain; +using BirdsiteLive.Moderation.Actions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace BirdsiteLive.Moderation.Tests.Actions { + [TestClass] public class RejectFollowingActionTests { - + [TestMethod] + public async Task ProcessAsync() + { + #region Stubs + var follower = new Follower + { + Followings = new List + { + 24 + }, + Host = "host" + }; + + var settings = new InstanceSettings + { + Domain = "domain" + }; + + var twitterUser = new SyncTwitterUser + { + Id = 24, + Acct = "acct" + }; + #endregion + + #region Mocks + var userServiceMock = new Mock(); + userServiceMock + .Setup(x => x.SendRejectFollowAsync( + It.Is(y => y.type == "Follow"), + It.IsNotNull() + )) + .ReturnsAsync(true); + #endregion + + var action = new RejectFollowingAction(userServiceMock.Object, settings); + await action.ProcessAsync(follower, twitterUser); + + #region Validations + userServiceMock.VerifyAll(); + #endregion + } + + [TestMethod] + public async Task ProcessAsync_Exception() + { + #region Stubs + var follower = new Follower + { + Followings = new List + { + 24 + }, + Host = "host" + }; + + var settings = new InstanceSettings + { + Domain = "domain" + }; + + var twitterUser = new SyncTwitterUser + { + Id = 24, + Acct = "acct" + }; + #endregion + + #region Mocks + var userServiceMock = new Mock(); + userServiceMock + .Setup(x => x.SendRejectFollowAsync( + It.Is(y => y.type == "Follow"), + It.IsNotNull() + )) + .Throws(new Exception()); + #endregion + + var action = new RejectFollowingAction(userServiceMock.Object, settings); + await action.ProcessAsync(follower, twitterUser); + + #region Validations + userServiceMock.VerifyAll(); + #endregion + } } } \ No newline at end of file diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs index a2f8751..4def0ed 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveFollowerActionTests.cs @@ -1,7 +1,111 @@ -namespace BirdsiteLive.Moderation.Tests.Actions +using System.Collections.Generic; +using System.Threading.Tasks; +using BirdsiteLive.DAL.Contracts; +using BirdsiteLive.DAL.Models; +using BirdsiteLive.Moderation.Actions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace BirdsiteLive.Moderation.Tests.Actions { + [TestClass] public class RemoveFollowerActionTests { - + [TestMethod] + public async Task ProcessAsync_NoMoreFollowings() + { + #region Stubs + var follower = new Follower + { + Id = 12, + Followings = new List { 1 } + }; + #endregion + + #region Mocks + var rejectAllFollowingsActionMock = new Mock(); + rejectAllFollowingsActionMock + .Setup(x => x.ProcessAsync( + It.Is(y => y.Id == follower.Id))) + .Returns(Task.CompletedTask); + + var followersDalMock = new Mock(); + followersDalMock + .Setup(x => x.GetFollowersAsync( + It.Is(y => y == 1))) + .ReturnsAsync(new[] {follower}); + + followersDalMock + .Setup(x => x.DeleteFollowerAsync( + It.Is(y => y == 12))) + .Returns(Task.CompletedTask); + + var twitterUserDalMock = new Mock(); + twitterUserDalMock + .Setup(x => x.DeleteTwitterUserAsync( + It.Is(y => y == 1))) + .Returns(Task.CompletedTask); + #endregion + + var action = new RemoveFollowerAction(followersDalMock.Object, twitterUserDalMock.Object, rejectAllFollowingsActionMock.Object); + await action.ProcessAsync(follower); + + #region Validations + followersDalMock.VerifyAll(); + twitterUserDalMock.VerifyAll(); + rejectAllFollowingsActionMock.VerifyAll(); + #endregion + } + + [TestMethod] + public async Task ProcessAsync_HaveFollowings() + { + #region Stubs + var follower = new Follower + { + Id = 12, + Followings = new List { 1 } + }; + + var followers = new List + { + follower, + new Follower + { + Id = 11 + } + }; + #endregion + + #region Mocks + var rejectAllFollowingsActionMock = new Mock(); + rejectAllFollowingsActionMock + .Setup(x => x.ProcessAsync( + It.Is(y => y.Id == follower.Id))) + .Returns(Task.CompletedTask); + + var followersDalMock = new Mock(); + followersDalMock + .Setup(x => x.GetFollowersAsync( + It.Is(y => y == 1))) + .ReturnsAsync(followers.ToArray()); + + followersDalMock + .Setup(x => x.DeleteFollowerAsync( + It.Is(y => y == 12))) + .Returns(Task.CompletedTask); + + var twitterUserDalMock = new Mock(); + #endregion + + var action = new RemoveFollowerAction(followersDalMock.Object, twitterUserDalMock.Object, rejectAllFollowingsActionMock.Object); + await action.ProcessAsync(follower); + + #region Validations + followersDalMock.VerifyAll(); + twitterUserDalMock.VerifyAll(); + rejectAllFollowingsActionMock.VerifyAll(); + #endregion + } } } \ No newline at end of file diff --git a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs index a0451c7..5e9342b 100644 --- a/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs +++ b/src/Tests/BirdsiteLive.Moderation.Tests/Actions/RemoveTwitterAccountActionTests.cs @@ -1,7 +1,131 @@ -namespace BirdsiteLive.Moderation.Tests.Actions +using System.Collections.Generic; +using System.Threading.Tasks; +using BirdsiteLive.DAL.Contracts; +using BirdsiteLive.DAL.Models; +using BirdsiteLive.Moderation.Actions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace BirdsiteLive.Moderation.Tests.Actions { + [TestClass] public class RemoveTwitterAccountActionTests { - + [TestMethod] + public async Task ProcessAsync_RemoveFollower() + { + #region Stubs + var twitter = new SyncTwitterUser + { + Id = 24, + Acct = "my-acct" + }; + + var followers = new List + { + new Follower + { + Id = 48, + Followings = new List{ 24 }, + FollowingsSyncStatus = new Dictionary { { 24, 1024 } } + } + }; + #endregion + + #region Mocks + var followersDalMock = new Mock(); + followersDalMock + .Setup(x => x.GetFollowersAsync( + It.Is(y => y == 24))) + .ReturnsAsync(followers.ToArray()); + + followersDalMock + .Setup(x => x.DeleteFollowerAsync( + It.Is(y => y == 48))) + .Returns(Task.CompletedTask); + + var twitterUserDalMock = new Mock(); + twitterUserDalMock + .Setup(x => x.DeleteTwitterUserAsync( + It.Is(y => y == 24))) + .Returns(Task.CompletedTask); + + var rejectFollowingActionMock = new Mock(); + rejectFollowingActionMock + .Setup(x => x.ProcessAsync( + It.Is(y => y.Id == 48), + It.Is(y => y.Acct == twitter.Acct))) + .Returns(Task.CompletedTask); + #endregion + + var action = new RemoveTwitterAccountAction(followersDalMock.Object, twitterUserDalMock.Object, rejectFollowingActionMock.Object); + await action.ProcessAsync(twitter); + + #region Validations + followersDalMock.VerifyAll(); + twitterUserDalMock.VerifyAll(); + rejectFollowingActionMock.VerifyAll(); + #endregion + } + + [TestMethod] + public async Task ProcessAsync_KeepFollower() + { + #region Stubs + var twitter = new SyncTwitterUser + { + Id = 24, + Acct = "my-acct" + }; + + var followers = new List + { + new Follower + { + Id = 48, + Followings = new List{ 24, 36 }, + FollowingsSyncStatus = new Dictionary { { 24, 1024 }, { 36, 24 } } + } + }; + #endregion + + #region Mocks + var followersDalMock = new Mock(); + followersDalMock + .Setup(x => x.GetFollowersAsync( + It.Is(y => y == 24))) + .ReturnsAsync(followers.ToArray()); + + followersDalMock + .Setup(x => x.UpdateFollowerAsync( + It.Is(y => y.Id == 48 + && y.Followings.Count == 1 + && y.FollowingsSyncStatus.Count == 1 + ))) + .Returns(Task.CompletedTask); + + var twitterUserDalMock = new Mock(); + twitterUserDalMock + .Setup(x => x.DeleteTwitterUserAsync( + It.Is(y => y == 24))) + .Returns(Task.CompletedTask); + + var rejectFollowingActionMock = new Mock(); + rejectFollowingActionMock + .Setup(x => x.ProcessAsync( + It.Is(y => y.Id == 48), + It.Is(y => y.Acct == twitter.Acct))) + .Returns(Task.CompletedTask); + #endregion + + var action = new RemoveTwitterAccountAction(followersDalMock.Object, twitterUserDalMock.Object, rejectFollowingActionMock.Object); + await action.ProcessAsync(twitter); + + #region Validations + followersDalMock.VerifyAll(); + twitterUserDalMock.VerifyAll(); + rejectFollowingActionMock.VerifyAll(); + #endregion + } } } \ No newline at end of file