added actions tests

This commit is contained in:
Nicolas Constant 2021-02-15 23:31:22 -05:00
parent aea0244b2a
commit abce8e5ab5
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
4 changed files with 332 additions and 13 deletions

View file

@ -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);
}
}
}

View file

@ -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<int>
{
24
},
Host = "host"
};
var settings = new InstanceSettings
{
Domain = "domain"
};
var twitterUser = new SyncTwitterUser
{
Id = 24,
Acct = "acct"
};
#endregion
#region Mocks
var userServiceMock = new Mock<IUserService>();
userServiceMock
.Setup(x => x.SendRejectFollowAsync(
It.Is<ActivityFollow>(y => y.type == "Follow"),
It.IsNotNull<string>()
))
.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<int>
{
24
},
Host = "host"
};
var settings = new InstanceSettings
{
Domain = "domain"
};
var twitterUser = new SyncTwitterUser
{
Id = 24,
Acct = "acct"
};
#endregion
#region Mocks
var userServiceMock = new Mock<IUserService>();
userServiceMock
.Setup(x => x.SendRejectFollowAsync(
It.Is<ActivityFollow>(y => y.type == "Follow"),
It.IsNotNull<string>()
))
.Throws(new Exception());
#endregion
var action = new RejectFollowingAction(userServiceMock.Object, settings);
await action.ProcessAsync(follower, twitterUser);
#region Validations
userServiceMock.VerifyAll();
#endregion
}
}
}

View file

@ -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<int> { 1 }
};
#endregion
#region Mocks
var rejectAllFollowingsActionMock = new Mock<IRejectAllFollowingsAction>();
rejectAllFollowingsActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == follower.Id)))
.Returns(Task.CompletedTask);
var followersDalMock = new Mock<IFollowersDal>();
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 1)))
.ReturnsAsync(new[] {follower});
followersDalMock
.Setup(x => x.DeleteFollowerAsync(
It.Is<int>(y => y == 12)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
twitterUserDalMock
.Setup(x => x.DeleteTwitterUserAsync(
It.Is<int>(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<int> { 1 }
};
var followers = new List<Follower>
{
follower,
new Follower
{
Id = 11
}
};
#endregion
#region Mocks
var rejectAllFollowingsActionMock = new Mock<IRejectAllFollowingsAction>();
rejectAllFollowingsActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == follower.Id)))
.Returns(Task.CompletedTask);
var followersDalMock = new Mock<IFollowersDal>();
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 1)))
.ReturnsAsync(followers.ToArray());
followersDalMock
.Setup(x => x.DeleteFollowerAsync(
It.Is<int>(y => y == 12)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
#endregion
var action = new RemoveFollowerAction(followersDalMock.Object, twitterUserDalMock.Object, rejectAllFollowingsActionMock.Object);
await action.ProcessAsync(follower);
#region Validations
followersDalMock.VerifyAll();
twitterUserDalMock.VerifyAll();
rejectAllFollowingsActionMock.VerifyAll();
#endregion
}
}
}

View file

@ -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<Follower>
{
new Follower
{
Id = 48,
Followings = new List<int>{ 24 },
FollowingsSyncStatus = new Dictionary<int, long> { { 24, 1024 } }
}
};
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>();
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 24)))
.ReturnsAsync(followers.ToArray());
followersDalMock
.Setup(x => x.DeleteFollowerAsync(
It.Is<int>(y => y == 48)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
twitterUserDalMock
.Setup(x => x.DeleteTwitterUserAsync(
It.Is<int>(y => y == 24)))
.Returns(Task.CompletedTask);
var rejectFollowingActionMock = new Mock<IRejectFollowingAction>();
rejectFollowingActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == 48),
It.Is<SyncTwitterUser>(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<Follower>
{
new Follower
{
Id = 48,
Followings = new List<int>{ 24, 36 },
FollowingsSyncStatus = new Dictionary<int, long> { { 24, 1024 }, { 36, 24 } }
}
};
#endregion
#region Mocks
var followersDalMock = new Mock<IFollowersDal>();
followersDalMock
.Setup(x => x.GetFollowersAsync(
It.Is<int>(y => y == 24)))
.ReturnsAsync(followers.ToArray());
followersDalMock
.Setup(x => x.UpdateFollowerAsync(
It.Is<Follower>(y => y.Id == 48
&& y.Followings.Count == 1
&& y.FollowingsSyncStatus.Count == 1
)))
.Returns(Task.CompletedTask);
var twitterUserDalMock = new Mock<ITwitterUserDal>();
twitterUserDalMock
.Setup(x => x.DeleteTwitterUserAsync(
It.Is<int>(y => y == 24)))
.Returns(Task.CompletedTask);
var rejectFollowingActionMock = new Mock<IRejectFollowingAction>();
rejectFollowingActionMock
.Setup(x => x.ProcessAsync(
It.Is<Follower>(y => y.Id == 48),
It.Is<SyncTwitterUser>(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
}
}
}