Skip to content

Commit

Permalink
✨ added more unit tests and improved single user mode (#55)
Browse files Browse the repository at this point in the history
* Added more unit test usage

* Made webfinger controller work with single user mode and added unit tests

* More single user mode stuff

* Updated version

* Made webfinger controller work with single user mode and added unit tests

* More single user mode stuff

* Updated version
  • Loading branch information
eduwardpost authored Jan 27, 2024
1 parent 39d0965 commit 996759c
Show file tree
Hide file tree
Showing 25 changed files with 732 additions and 226 deletions.
169 changes: 122 additions & 47 deletions src/uActivityPub.Tests/ContentPublishPostHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using Microsoft.Extensions.Options;
using RichardSzalay.MockHttp;
using uActivityPub.Data;
using uActivityPub.Helpers;
using uActivityPub.Models;
using uActivityPub.Services;
using uActivityPub.Tests.TestHelpers;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
Expand Down Expand Up @@ -35,15 +37,21 @@ public ContentPublishPostHandlerTests()
public void HandlePostsToFollowers()
{
//Arrange

var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(uActivitySettingsHelper.GetSettings);



iUActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings
{
Id = 4,
Key = uActivitySettingKeys.SingleUserMode,
Value = "false"
});

contentTypeMock.Setup(x => x.Alias).Returns("article");
blogPostMock.Setup(x => x.ContentType).Returns(contentTypeMock.Object);
blogPostMock.Setup(x => x.GetValue<int>("authorName", null, null, false))
Expand All @@ -62,7 +70,7 @@ public void HandlePostsToFollowers()
{
new()
{
Actor = "testactor",
Actor = "test-actor",
Object = "",
Type = "Follow",
Id = 1
Expand All @@ -87,7 +95,7 @@ public void HandlePostsToFollowers()
Inbox = "http://localhost/inbox"
});

signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser(userMock.Object))
signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser("uActivityPub", 1))
.ReturnsAsync(("key", RSA.Create(2048)));
var singedRequestHandlerMock = new Mock<ISingedRequestHandler>();
singedRequestHandlerMock.Setup(x =>
Expand Down Expand Up @@ -130,14 +138,21 @@ public void HandlePostsToFollowers()
public void HandlePostsToMultipleFollowers()
{
//Arrange

var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(uActivitySettingsHelper.GetSettings);


iUActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings
{
Id = 4,
Key = uActivitySettingKeys.SingleUserMode,
Value = "false"
});

contentTypeMock.Setup(x => x.Alias).Returns("article");
blogPostMock.Setup(x => x.ContentType).Returns(contentTypeMock.Object);
blogPostMock.Setup(x => x.GetValue<int>("authorName", null, null, false))
Expand All @@ -156,14 +171,14 @@ public void HandlePostsToMultipleFollowers()
{
new()
{
Actor = "testactor",
Actor = "test-actor",
Object = "",
Type = "Follow",
Id = 1
},
new()
{
Actor = "testactor2",
Actor = "test-actor2",
Object = "",
Type = "Follow",
Id = 2
Expand All @@ -188,7 +203,7 @@ public void HandlePostsToMultipleFollowers()
Inbox = "http://localhost/inbox"
});

signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser(userMock.Object))
signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser("uActivityPub", 1))
.ReturnsAsync(("key", RSA.Create(2048)));
var singedRequestHandlerMock = new Mock<ISingedRequestHandler>();
singedRequestHandlerMock.Setup(x =>
Expand Down Expand Up @@ -235,11 +250,11 @@ public void HandlePostsToFollowersForNonArticle()
var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(uActivitySettingsHelper.GetSettings);
.Returns(uActivitySettingsHelper.GetSettings);


contentTypeMock.Setup(x => x.Alias).Returns("nonArticle");
blogPostMock.Setup(x => x.ContentType).Returns(contentTypeMock.Object);

Expand Down Expand Up @@ -274,14 +289,13 @@ public void HandlePostsToFollowersForNonArticle()
public void HandlePostsToFollowersWithAlreadyPostedArticle()
{
//Arrange

var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(uActivitySettingsHelper.GetSettings);

contentTypeMock.Setup(x => x.Alias).Returns("article");
blogPostMock.Setup(x => x.ContentType).Returns(contentTypeMock.Object);
blogPostMock.Setup(x => x.GetValue<int>("authorName", null, null, false))
Expand All @@ -298,7 +312,7 @@ public void HandlePostsToFollowersWithAlreadyPostedArticle()
{
new()
{
Actor = "testactor",
Actor = "test-actor",
Object = "",
Type = "Post",
Id = 1
Expand Down Expand Up @@ -328,6 +342,47 @@ public void HandlePostsToFollowersWithAlreadyPostedArticle()
Times.Never);
}

[Fact]
public void HandlePostsToFollowersForNotFoundContentAliasSettingThrowsInvalidOperation()
{
//Arrange
var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

contentTypeMock.Setup(x => x.Alias).Returns("article");


var databaseFactoryMock = new Mock<IUmbracoDatabaseFactory>();
var databaseMock = new Mock<IUmbracoDatabase>();
var userServiceMock = new Mock<IUserService>();
var signatureServiceMock = new Mock<ISignatureService>();
var singedRequestHandlerMock = new Mock<ISingedRequestHandler>();
var activityHelperMock = new Mock<IActivityHelper>();
var notification = new ContentPublishedNotification(blogPostMock.Object, null!);

var unitUnderTest = new ContentPublishPostHandler(databaseFactoryMock.Object, _webRouterSettingsMock.Object,
userServiceMock.Object, signatureServiceMock.Object, singedRequestHandlerMock.Object,
activityHelperMock.Object, iUActivitySettingsServiceMock.Object);

try
{
//Act
unitUnderTest.Handle(notification);
}
catch (Exception e)
{
//Assert
Assert.IsType<InvalidOperationException>(e);
databaseMock.Verify(
x => x.Insert("receivedActivityPubActivities", "Id", true, It.IsAny<ReceivedActivitiesSchema>()),
Times.Never);
singedRequestHandlerMock.Verify(
x => x.SendSingedPost(It.IsAny<Uri>(), It.IsAny<RSA>(), It.IsAny<string>(), It.IsAny<string>()),
Times.Never);
}
}

[Fact]
public void HandlePostsToFollowersForNotFoundUserThrowsInvalidOperation()
{
Expand All @@ -336,23 +391,24 @@ public void HandlePostsToFollowersForNotFoundUserThrowsInvalidOperation()
var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(new List<uActivitySettings>
{
new()
{
Id = 1,
Key = uActivitySettingKeys.ContentTypeAlias,
Value = "unit"
}
});

contentTypeMock.Setup(x => x.Alias).Returns("article");
blogPostMock.Setup(x => x.ContentType).Returns(contentTypeMock.Object);
blogPostMock.Setup(x => x.GetValue<int>("authorName", null, null, false))
.Returns(1);


var databaseFactoryMock = new Mock<IUmbracoDatabaseFactory>();
var databaseMock = new Mock<IUmbracoDatabase>();
databaseFactoryMock.Setup(x => x.CreateDatabase())
.Returns(databaseMock.Object);

databaseMock.Setup(x => x.Query<ReceivedActivitiesSchema>(It.IsAny<string>(), "Post", It.IsAny<string>()))
.Returns(new List<ReceivedActivitiesSchema>());
var userServiceMock = new Mock<IUserService>();


var signatureServiceMock = new Mock<ISignatureService>();
var singedRequestHandlerMock = new Mock<ISingedRequestHandler>();
var activityHelperMock = new Mock<IActivityHelper>();
Expand All @@ -361,32 +417,44 @@ public void HandlePostsToFollowersForNotFoundUserThrowsInvalidOperation()
var unitUnderTest = new ContentPublishPostHandler(databaseFactoryMock.Object, _webRouterSettingsMock.Object,
userServiceMock.Object, signatureServiceMock.Object, singedRequestHandlerMock.Object,
activityHelperMock.Object, iUActivitySettingsServiceMock.Object);

try
{
//Act
unitUnderTest.Handle(notification);
}
catch(Exception e)
catch (Exception e)
{
//Assert
Assert.IsType<InvalidOperationException>(e);
databaseMock.Verify(x => x.Insert("receivedActivityPubActivities", "Id", true, It.IsAny<ReceivedActivitiesSchema>()), Times.Never);
singedRequestHandlerMock.Verify(x => x.SendSingedPost(It.IsAny<Uri>(), It.IsAny<RSA>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
databaseMock.Verify(
x => x.Insert("receivedActivityPubActivities", "Id", true, It.IsAny<ReceivedActivitiesSchema>()),
Times.Never);
singedRequestHandlerMock.Verify(
x => x.SendSingedPost(It.IsAny<Uri>(), It.IsAny<RSA>(), It.IsAny<string>(), It.IsAny<string>()),
Times.Never);
}
}
[Fact]

[Fact]
public void HandlePostsToFollowersDoesNotCrashIfActorNoLongerExists()
{
//Arrange

var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(uActivitySettingsHelper.GetSettings);

iUActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings
{
Id = 4,
Key = uActivitySettingKeys.SingleUserMode,
Value = "false"
});

contentTypeMock.Setup(x => x.Alias).Returns("article");
blogPostMock.Setup(x => x.ContentType).Returns(contentTypeMock.Object);
Expand All @@ -406,7 +474,7 @@ public void HandlePostsToFollowersDoesNotCrashIfActorNoLongerExists()
{
new()
{
Actor = "testactor",
Actor = "test-actor",
Object = "",
Type = "Follow",
Id = 1
Expand All @@ -425,12 +493,12 @@ public void HandlePostsToFollowersDoesNotCrashIfActorNoLongerExists()

var signatureServiceMock = new Mock<ISignatureService>();
signatureServiceMock.Setup(x => x.GetActor(It.IsAny<string>()))
.ReturnsAsync((Actor) null!);
.ReturnsAsync((Actor)null!);

signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser(userMock.Object))
signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser("uActivityPub", 1))
.ReturnsAsync(("key", RSA.Create(2048)));
var singedRequestHandlerMock = new Mock<ISingedRequestHandler>();

var activityHelperMock = new Mock<IActivityHelper>();

var activity = new Activity
Expand Down Expand Up @@ -462,19 +530,26 @@ public void HandlePostsToFollowersDoesNotCrashIfActorNoLongerExists()
x => x.SendSingedPost(It.IsAny<Uri>(), It.IsAny<RSA>(), It.IsAny<string>(), It.IsAny<string>()),
Times.Never);
}
[Fact]

[Fact]
public void HandlePostsToFollowersDoesNotCrashIfRequestFails()
{
//Arrange

var blogPostMock = new Mock<IContent>();
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(uActivitySettingsHelper.GetSettings);

iUActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings
{
Id = 4,
Key = uActivitySettingKeys.SingleUserMode,
Value = "false"
});

contentTypeMock.Setup(x => x.Alias).Returns("article");
blogPostMock.Setup(x => x.ContentType).Returns(contentTypeMock.Object);
blogPostMock.Setup(x => x.GetValue<int>("authorName", null, null, false))
Expand All @@ -493,7 +568,7 @@ public void HandlePostsToFollowersDoesNotCrashIfRequestFails()
{
new()
{
Actor = "testactor",
Actor = "test-actor",
Object = "",
Type = "Follow",
Id = 1
Expand All @@ -518,7 +593,7 @@ public void HandlePostsToFollowersDoesNotCrashIfRequestFails()
Inbox = "http://localhost/inbox"
});

signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser(userMock.Object))
signatureServiceMock.Setup(x => x.GetPrimaryKeyForUser("uActivityPub", 1))
.ReturnsAsync(("key", RSA.Create(2048)));
var singedRequestHandlerMock = new Mock<ISingedRequestHandler>();
singedRequestHandlerMock.Setup(x =>
Expand Down
Loading

0 comments on commit 996759c

Please sign in to comment.