From d1c5a592471857bbd592000c54762a107cd0804f Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Mon, 7 Feb 2022 19:33:08 -0500 Subject: [PATCH] fix tests --- .../RefreshTwitterUserStatusProcessor.cs | 3 + .../RefreshTwitterUserStatusProcessorTests.cs | 323 ++++++++++++++++-- 2 files changed, 306 insertions(+), 20 deletions(-) diff --git a/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs b/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs index 3d29d67..739d50b 100644 --- a/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs +++ b/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs @@ -45,14 +45,17 @@ namespace BirdsiteLive.Pipeline.Processors catch (UserNotFoundException) { await ProcessNotFoundUserAsync(user); + continue; } catch (UserHasBeenSuspendedException) { await ProcessNotFoundUserAsync(user); + continue; } catch (RateLimitExceededException) { await ProcessRateLimitExceededAsync(user); + continue; } catch (Exception) { diff --git a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs index d5fbeef..ae4994f 100644 --- a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs +++ b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs @@ -159,25 +159,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors twitterUserServiceMock .Setup(x => x.GetUser(It.Is(y => y == acct2))) - .Returns((TwitterUser) null); - - twitterUserServiceMock - .Setup(x => x.PurgeUser(It.Is(y => y == acct2))); + .Throws(new UserNotFoundException()); var twitterUserDalMock = new Mock(MockBehavior.Strict); - twitterUserDalMock - .Setup(x => x.GetTwitterUserAsync(It.Is(y => y == acct2))) - .ReturnsAsync(new SyncTwitterUser - { - Id = userId2, - FetchingErrorCount = 0 - }); - - twitterUserDalMock - .Setup(x => x.UpdateTwitterUserAsync(It.Is(y => y.Id == userId2 && y.FetchingErrorCount == 1))) - .Returns(Task.CompletedTask); var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); + removeTwitterAccountActionMock + .Setup(x => x.ProcessAsync(It.Is(y => y.Acct == acct2))) + .Returns(Task.CompletedTask); #endregion var processor = new RefreshTwitterUserStatusProcessor(twitterUserServiceMock.Object, twitterUserDalMock.Object, removeTwitterAccountActionMock.Object, settings); @@ -194,7 +183,71 @@ namespace BirdsiteLive.Pipeline.Tests.Processors } [TestMethod] - public async Task ProcessAsync_Unfound_OverThreshold_Test() + public async Task ProcessAsync_Suspended_Test() + { + #region Stubs + var userId1 = 1; + var acct1 = "user1"; + + var userId2 = 2; + var acct2 = "user2"; + + var users = new List + { + new SyncTwitterUser + { + Id = userId1, + Acct = acct1 + }, + new SyncTwitterUser + { + Id = userId2, + Acct = acct2 + } + }; + + var settings = new InstanceSettings + { + FailingTwitterUserCleanUpThreshold = 300 + }; + #endregion + + #region Mocks + var twitterUserServiceMock = new Mock(MockBehavior.Strict); + twitterUserServiceMock + .Setup(x => x.GetUser(It.Is(y => y == acct1))) + .Returns(new TwitterUser + { + Protected = false + }); + + twitterUserServiceMock + .Setup(x => x.GetUser(It.Is(y => y == acct2))) + .Throws(new UserHasBeenSuspendedException()); + + var twitterUserDalMock = new Mock(MockBehavior.Strict); + + var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); + removeTwitterAccountActionMock + .Setup(x => x.ProcessAsync(It.Is(y => y.Acct == acct2))) + .Returns(Task.CompletedTask); + #endregion + + var processor = new RefreshTwitterUserStatusProcessor(twitterUserServiceMock.Object, twitterUserDalMock.Object, removeTwitterAccountActionMock.Object, settings); + var result = await processor.ProcessAsync(users.ToArray(), CancellationToken.None); + + #region Validations + Assert.AreEqual(1, result.Length); + Assert.IsTrue(result.Any(x => x.User.Id == userId1)); + + twitterUserServiceMock.VerifyAll(); + twitterUserDalMock.VerifyAll(); + removeTwitterAccountActionMock.VerifyAll(); + #endregion + } + + [TestMethod] + public async Task ProcessAsync_Error_Test() { #region Stubs var userId1 = 1; @@ -236,8 +289,83 @@ namespace BirdsiteLive.Pipeline.Tests.Processors .Setup(x => x.GetUser(It.Is(y => y == acct2))) .Returns((TwitterUser)null); + //twitterUserServiceMock + // .Setup(x => x.PurgeUser(It.Is(y => y == acct2))); + + var twitterUserDalMock = new Mock(MockBehavior.Strict); + twitterUserDalMock + .Setup(x => x.GetTwitterUserAsync(It.Is(y => y == acct2))) + .ReturnsAsync(new SyncTwitterUser + { + Id = userId2, + FetchingErrorCount = 0 + }); + + twitterUserDalMock + .Setup(x => x.UpdateTwitterUserAsync(It.Is(y => y.Id == userId2 && y.FetchingErrorCount == 1))) + .Returns(Task.CompletedTask); + + var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); + #endregion + + var processor = new RefreshTwitterUserStatusProcessor(twitterUserServiceMock.Object, twitterUserDalMock.Object, removeTwitterAccountActionMock.Object, settings); + var result = await processor.ProcessAsync(users.ToArray(), CancellationToken.None); + + #region Validations + Assert.AreEqual(1, result.Length); + Assert.IsTrue(result.Any(x => x.User.Id == userId1)); + + twitterUserServiceMock.VerifyAll(); + twitterUserDalMock.VerifyAll(); + removeTwitterAccountActionMock.VerifyAll(); + #endregion + } + + [TestMethod] + public async Task ProcessAsync_Error_OverThreshold_Test() + { + #region Stubs + var userId1 = 1; + var acct1 = "user1"; + + var userId2 = 2; + var acct2 = "user2"; + + var users = new List + { + new SyncTwitterUser + { + Id = userId1, + Acct = acct1 + }, + new SyncTwitterUser + { + Id = userId2, + Acct = acct2 + } + }; + + var settings = new InstanceSettings + { + FailingTwitterUserCleanUpThreshold = 300 + }; + #endregion + + #region Mocks + var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.PurgeUser(It.Is(y => y == acct2))); + .Setup(x => x.GetUser(It.Is(y => y == acct1))) + .Returns(new TwitterUser + { + Protected = false + }); + + twitterUserServiceMock + .Setup(x => x.GetUser(It.Is(y => y == acct2))) + .Returns((TwitterUser)null); + + //twitterUserServiceMock + // .Setup(x => x.PurgeUser(It.Is(y => y == acct2))); var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock @@ -313,7 +441,22 @@ namespace BirdsiteLive.Pipeline.Tests.Processors Protected = true }); + //twitterUserServiceMock + // .Setup(x => x.PurgeUser(It.Is(y => y == acct2))); + var twitterUserDalMock = new Mock(MockBehavior.Strict); + twitterUserDalMock + .Setup(x => x.GetTwitterUserAsync(It.Is(y => y == acct2))) + .ReturnsAsync(new SyncTwitterUser + { + Id = userId2, + FetchingErrorCount = 0 + }); + + twitterUserDalMock + .Setup(x => x.UpdateTwitterUserAsync(It.Is(y => y.Id == userId2 && y.FetchingErrorCount == 1))) + .Returns(Task.CompletedTask); + var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); #endregion @@ -331,7 +474,147 @@ namespace BirdsiteLive.Pipeline.Tests.Processors } [TestMethod] - public async Task ProcessAsync_Unfound_NotInit_Test() + public async Task ProcessAsync_Protected_OverThreshold_Test() + { + #region Stubs + var userId1 = 1; + var acct1 = "user1"; + + var userId2 = 2; + var acct2 = "user2"; + + var users = new List + { + new SyncTwitterUser + { + Id = userId1, + Acct = acct1 + }, + new SyncTwitterUser + { + Id = userId2, + Acct = acct2 + } + }; + + var settings = new InstanceSettings + { + FailingTwitterUserCleanUpThreshold = 300 + }; + #endregion + + #region Mocks + var twitterUserServiceMock = new Mock(MockBehavior.Strict); + twitterUserServiceMock + .Setup(x => x.GetUser(It.Is(y => y == acct1))) + .Returns(new TwitterUser + { + Protected = false + }); + + twitterUserServiceMock + .Setup(x => x.GetUser(It.Is(y => y == acct2))) + .Returns(new TwitterUser + { + Protected = true + }); + + //twitterUserServiceMock + // .Setup(x => x.PurgeUser(It.Is(y => y == acct2))); + + var twitterUserDalMock = new Mock(MockBehavior.Strict); + twitterUserDalMock + .Setup(x => x.GetTwitterUserAsync(It.Is(y => y == acct2))) + .ReturnsAsync(new SyncTwitterUser + { + Id = userId2, + FetchingErrorCount = 500 + }); + + var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); + removeTwitterAccountActionMock + .Setup(x => x.ProcessAsync(It.Is(y => y.Id == userId2))) + .Returns(Task.CompletedTask); + #endregion + + var processor = new RefreshTwitterUserStatusProcessor(twitterUserServiceMock.Object, twitterUserDalMock.Object, removeTwitterAccountActionMock.Object, settings); + var result = await processor.ProcessAsync(users.ToArray(), CancellationToken.None); + + #region Validations + Assert.AreEqual(1, result.Length); + Assert.IsTrue(result.Any(x => x.User.Id == userId1)); + + twitterUserServiceMock.VerifyAll(); + twitterUserDalMock.VerifyAll(); + removeTwitterAccountActionMock.VerifyAll(); + #endregion + } + + //[TestMethod] + //public async Task ProcessAsync_Protected_Test() + //{ + // #region Stubs + // var userId1 = 1; + // var acct1 = "user1"; + + // var userId2 = 2; + // var acct2 = "user2"; + + // var users = new List + // { + // new SyncTwitterUser + // { + // Id = userId1, + // Acct = acct1 + // }, + // new SyncTwitterUser + // { + // Id = userId2, + // Acct = acct2 + // } + // }; + + // var settings = new InstanceSettings + // { + // FailingTwitterUserCleanUpThreshold = 300 + // }; + // #endregion + + // #region Mocks + // var twitterUserServiceMock = new Mock(MockBehavior.Strict); + // twitterUserServiceMock + // .Setup(x => x.GetUser(It.Is(y => y == acct1))) + // .Returns(new TwitterUser + // { + // Protected = false + // }); + + // twitterUserServiceMock + // .Setup(x => x.GetUser(It.Is(y => y == acct2))) + // .Returns(new TwitterUser + // { + // Protected = true + // }); + + // var twitterUserDalMock = new Mock(MockBehavior.Strict); + // var removeTwitterAccountActionMock = new Mock(MockBehavior.Strict); + // #endregion + + // var processor = new RefreshTwitterUserStatusProcessor(twitterUserServiceMock.Object, twitterUserDalMock.Object, removeTwitterAccountActionMock.Object, settings); + // var result = await processor.ProcessAsync(users.ToArray(), CancellationToken.None); + + // #region Validations + // Assert.AreEqual(1, result.Length); + // Assert.IsTrue(result.Any(x => x.User.Id == userId1)); + + // twitterUserServiceMock.VerifyAll(); + // twitterUserDalMock.VerifyAll(); + // removeTwitterAccountActionMock.VerifyAll(); + // #endregion + //} + + [TestMethod] + public async Task ProcessAsync_Error_NotInit_Test() { #region Stubs var userId1 = 1; @@ -361,8 +644,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors .Setup(x => x.GetUser(It.Is(y => y == acct1))) .Returns((TwitterUser)null); - twitterUserServiceMock - .Setup(x => x.PurgeUser(It.Is(y => y == acct1))); + //twitterUserServiceMock + // .Setup(x => x.PurgeUser(It.Is(y => y == acct1))); var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock