Skip to content

Commit

Permalink
🎨 Small code refactors (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduwardpost authored Mar 17, 2024
1 parent ccda7c4 commit 3048821
Show file tree
Hide file tree
Showing 26 changed files with 153 additions and 156 deletions.
28 changes: 14 additions & 14 deletions src/uActivityPub.Tests/ContentPublishPostHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public void HandlePostsToFollowers()
iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(UActivitySettingsHelper.GetSettings);

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

Expand Down Expand Up @@ -148,11 +148,11 @@ public void HandlePostsToMultipleFollowers()
iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(UActivitySettingsHelper.GetSettings);

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

Expand Down Expand Up @@ -395,12 +395,12 @@ public void HandlePostsToFollowersForNotFoundUserThrowsInvalidOperation()
var contentTypeMock = new Mock<ISimpleContentType>();
var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();
iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(new List<uActivitySettings>
.Returns(new List<UActivitySettings>
{
new()
{
Id = 1,
Key = uActivitySettingKeys.ContentTypeAlias,
Key = UActivitySettingKeys.ContentTypeAlias,
Value = "unit"
}
});
Expand Down Expand Up @@ -451,11 +451,11 @@ public void HandlePostsToFollowersDoesNotCrashIfActorNoLongerExists()
iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(UActivitySettingsHelper.GetSettings);

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

Expand Down Expand Up @@ -545,11 +545,11 @@ public void HandlePostsToFollowersDoesNotCrashIfRequestFails()
iUActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(UActivitySettingsHelper.GetSettings);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ public ActivityPubControllerTests()
.Returns(_umbracoDatabaseMock.Object);

var settings = UActivitySettingsHelper.GetSettings();
settings.First(s => s.Key == uActivitySettingKeys.SingleUserMode).Value = "true";
settings.First(s => s.Key == UActivitySettingKeys.SingleUserMode).Value = "true";

_uActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(settings);
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings()
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(UActivitySettingKeys.SingleUserMode))
.Returns(new UActivitySettings()
{
Value = "true"
});
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserModeUserName))
.Returns(new uActivitySettings()
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(UActivitySettingKeys.SingleUserModeUserName))
.Returns(new UActivitySettings()
{
Value = "uActivityPub"
});
Expand Down
42 changes: 20 additions & 22 deletions src/uActivityPub.Tests/ControllerTests/WebfingerControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -105,19 +103,19 @@ public void HandleRequest_Returns_Webfinger_Response_When_Account_Is_Requested_I
var collectionStore = new Dictionary<string, StringValues> { { "resource", "acct:[email protected]" } };
_unitUnderTest.ControllerContext.HttpContext.Request.Query = new QueryCollection(collectionStore);

_uActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(UActivitySettingKeys.SingleUserMode))
.Returns(new UActivitySettings
{
Id = 1,
Key = uActivitySettingKeys.SingleUserMode,
Key = UActivitySettingKeys.SingleUserMode,
Value = "true"
});

_uActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserModeUserName))
.Returns(new uActivitySettings
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(UActivitySettingKeys.SingleUserModeUserName))
.Returns(new UActivitySettings
{
Id = 2,
Key = uActivitySettingKeys.SingleUserModeUserName,
Key = UActivitySettingKeys.SingleUserModeUserName,
Value = "uActivityPub"
});

Expand All @@ -132,13 +130,13 @@ public void HandleRequest_Returns_Webfinger_Response_When_Account_Is_Requested_I
var objectResponse = response.Result as OkObjectResult;
objectResponse.Should().NotBeNull();
objectResponse!.Value.Should().NotBeNull();
var webFingerResponse = objectResponse!.Value as WebFingerResponse;
var webFingerResponse = objectResponse.Value as WebFingerResponse;
webFingerResponse.Should().NotBeNull();
webFingerResponse!.Subject.Should().Be("acct:[email protected]");
webFingerResponse.Links.Length.Should().Be(1);
webFingerResponse.Links.First().Rel.Should().Be("self");
webFingerResponse.Links.First().Type.Should().Be("application/activity+json");
webFingerResponse.Links.First().Href.Should().Be("https://localhost.test/activitypub/actor/uactivitypub");
webFingerResponse.Links[0].Rel.Should().Be("self");
webFingerResponse.Links[0].Type.Should().Be("application/activity+json");
webFingerResponse.Links[0].Href.Should().Be("https://localhost.test/activitypub/actor/uactivitypub");
}

[Fact]
Expand All @@ -152,11 +150,11 @@ public void HandleRequest_Returns_Webfinger_Response_When_Account_Is_Requested_N
var collectionStore = new Dictionary<string, StringValues> { { "resource", "acct:[email protected]" } };
_unitUnderTest.ControllerContext.HttpContext.Request.Query = new QueryCollection(collectionStore);

_uActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(UActivitySettingKeys.SingleUserMode))
.Returns(new UActivitySettings
{
Id = 1,
Key = uActivitySettingKeys.SingleUserMode,
Key = UActivitySettingKeys.SingleUserMode,
Value = "false"
});

Expand All @@ -181,13 +179,13 @@ public void HandleRequest_Returns_Webfinger_Response_When_Account_Is_Requested_N
var objectResponse = response.Result as OkObjectResult;
objectResponse.Should().NotBeNull();
objectResponse!.Value.Should().NotBeNull();
var webFingerResponse = objectResponse!.Value as WebFingerResponse;
var webFingerResponse = objectResponse.Value as WebFingerResponse;
webFingerResponse.Should().NotBeNull();
webFingerResponse!.Subject.Should().Be("acct:[email protected]");
webFingerResponse.Links.Length.Should().Be(1);
webFingerResponse.Links.First().Rel.Should().Be("self");
webFingerResponse.Links.First().Type.Should().Be("application/activity+json");
webFingerResponse.Links.First().Href.Should().Be("https://localhost.test/activitypub/actor/uactivitypub");
webFingerResponse.Links[0].Rel.Should().Be("self");
webFingerResponse.Links[0].Type.Should().Be("application/activity+json");
webFingerResponse.Links[0].Href.Should().Be("https://localhost.test/activitypub/actor/uactivitypub");
}

[Fact]
Expand All @@ -201,11 +199,11 @@ public void HandleRequest_Returns_NotFound_Response_When_Non_Existing_Account_Is
var collectionStore = new Dictionary<string, StringValues> { { "resource", "acct:[email protected]" } };
_unitUnderTest.ControllerContext.HttpContext.Request.Query = new QueryCollection(collectionStore);

_uActivitySettingsServiceMock.Setup(x => x.GetSettings(uActivitySettingKeys.SingleUserMode))
.Returns(new uActivitySettings
_uActivitySettingsServiceMock.Setup(x => x.GetSettings(UActivitySettingKeys.SingleUserMode))
.Returns(new UActivitySettings
{
Id = 1,
Key = uActivitySettingKeys.SingleUserMode,
Key = UActivitySettingKeys.SingleUserMode,
Value = "false"
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.DataProtection.XmlEncryption;
using uActivityPub.Data;
using uActivityPub.Data;
using uActivityPub.Helpers;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Infrastructure.Persistence;
Expand Down Expand Up @@ -33,6 +32,6 @@ public void Handle_UmbracoApplicationStartedNotification_Should_InsertSettings_I
_unitUnderTest.Handle(notification);

// Assert
_dataBaseMock.Verify(x => x.Insert(uActivitySettingKeys.TableName, primaryKeyName, true, It.IsAny<uActivitySettings>()), Times.Exactly(6));
_dataBaseMock.Verify(x => x.Insert(UActivitySettingKeys.TableName, primaryKeyName, true, It.IsAny<UActivitySettings>()), Times.Exactly(6));
}
}
7 changes: 3 additions & 4 deletions src/uActivityPub.Tests/ServiceTests/ActorServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using FluentAssertions;
using Microsoft.Extensions.Options;
using uActivityPub.Helpers;
using uActivityPub.Models;
using uActivityPub.Services;
using uActivityPub.Services.ContentServices;
using uActivityPub.Tests.TestHelpers;
Expand Down Expand Up @@ -59,7 +58,7 @@ public void GetActor_With_Only_UserName_Should_Return_Actor_BasedOn_String()
const string userName = "uActivityPub";

var settings = UActivitySettingsHelper.GetSettings();
settings.First(s => s.Key == uActivitySettingKeys.SingleUserMode).Value = "true";
settings.First(s => s.Key == UActivitySettingKeys.SingleUserMode).Value = "true";

_uActivitySettingsServiceMock.Setup(x => x.GetAllSettings())
.Returns(settings);
Expand All @@ -81,7 +80,7 @@ public void GetActor_In_SingleUserMode_Creates_Actor()
const string actorUserName = "uactivitypub";

var settings = UActivitySettingsHelper.GetSettings();
settings.First(s => s.Key == uActivitySettingKeys.SingleUserMode).Value = "true";
settings.First(s => s.Key == UActivitySettingKeys.SingleUserMode).Value = "true";

_uActivitySettingsServiceMock.Setup(x => x.GetAllSettings()).Returns(settings);

Expand Down Expand Up @@ -115,7 +114,7 @@ public void Calling_Parameter_Constructor_In_MultiUserMode_Creates_Actor()
.Returns($"{actorUserName}@unit.test");

var settings = UActivitySettingsHelper.GetSettings();
settings.First(s => s.Key == uActivitySettingKeys.SingleUserMode).Value = "false";
settings.First(s => s.Key == UActivitySettingKeys.SingleUserMode).Value = "false";

_uActivitySettingsServiceMock.Setup(x => x.GetAllSettings()).Returns(settings);

Expand Down
1 change: 0 additions & 1 deletion src/uActivityPub.Tests/ServiceTests/InboxServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Newtonsoft.Json.Linq;
using uActivityPub.Data;
using uActivityPub.Models;
using uActivityPub.Services;
using uActivityPub.Services.ActivityPubServices;
using uActivityPub.Services.HelperServices;
using Umbraco.Cms.Core.Configuration.Models;
Expand Down
23 changes: 21 additions & 2 deletions src/uActivityPub.Tests/ServiceTests/OutboxServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using FluentAssertions;
using Microsoft.Extensions.Options;
using uActivityPub.Models;
using uActivityPub.Services;
Expand Down Expand Up @@ -163,5 +165,22 @@ public void GetPublicOutboxForNonExistingBlogRootReturnsNull()
//Assert
Assert.Null(outbox);
}


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

var iUActivitySettingsServiceMock = new Mock<IUActivitySettingsService>();

var unitUnderTest = new OutboxService(
null!,
_webRouterSettingsMock.Object,
null!,
iUActivitySettingsServiceMock.Object);

// Act & Assert
unitUnderTest.Invoking(x => x.GetPublicOutbox("uActivityPub"))
.Should().ThrowExactly<InvalidOperationException>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using RichardSzalay.MockHttp;
using uActivityPub.Data;
using uActivityPub.Models;
using uActivityPub.Services;
using uActivityPub.Services.HelperServices;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Infrastructure.Persistence;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public UActivitySettingsServiceTests()
public void GetAllSettings_Returns_All_Settings_In_Database()
{
// Arrange
_dataBaseMock.Setup(x => x.Fetch<uActivitySettings>(It.IsAny<string>()))
_dataBaseMock.Setup(x => x.Fetch<UActivitySettings>(It.IsAny<string>()))
.Returns(UActivitySettingsHelper.GetSettings());

// Act
Expand All @@ -46,14 +46,14 @@ public void GetAllSettings_Returns_All_Settings_In_Database()
public void GetSetting_Returns_Specific_Settings_In_Database()
{
// Arrange
_dataBaseMock.Setup(x => x.Fetch<uActivitySettings>(It.IsAny<string>()))
_dataBaseMock.Setup(x => x.Fetch<UActivitySettings>(It.IsAny<string>()))
.Returns(UActivitySettingsHelper.GetSettings());

// Act
var setting = _unitUnderTest.GetSettings(uActivitySettingKeys.SingleUserMode);
var setting = _unitUnderTest.GetSettings(UActivitySettingKeys.SingleUserMode);

// Assert
setting.Should().NotBeNull();
setting!.Key.Should().Be(uActivitySettingKeys.SingleUserMode);
setting!.Key.Should().Be(UActivitySettingKeys.SingleUserMode);
}
}
26 changes: 13 additions & 13 deletions src/uActivityPub.Tests/TestHelpers/uActivitySettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,49 @@ namespace uActivityPub.Tests.TestHelpers;

public static class UActivitySettingsHelper
{
public static List<uActivitySettings> GetSettings()
public static List<UActivitySettings> GetSettings()
{
return
[
new uActivitySettings()
new UActivitySettings()
{
Id = 1,
Key = uActivitySettingKeys.ContentTypeAlias,
Key = UActivitySettingKeys.ContentTypeAlias,
Value = "article"
},

new uActivitySettings()
new UActivitySettings()
{
Id = 2,
Key = uActivitySettingKeys.ListContentTypeAlias,
Key = UActivitySettingKeys.ListContentTypeAlias,
Value = "articleList"
},

new uActivitySettings()
new UActivitySettings()
{
Id = 3,
Key = uActivitySettingKeys.UserNameContentAlias,
Key = UActivitySettingKeys.UserNameContentAlias,
Value = "author"
},

new uActivitySettings()
new UActivitySettings()
{
Id = 4,
Key = uActivitySettingKeys.SingleUserMode,
Key = UActivitySettingKeys.SingleUserMode,
Value = "false"
},

new uActivitySettings()
new UActivitySettings()
{
Id = 5,
Key = uActivitySettingKeys.SingleUserModeUserName,
Key = UActivitySettingKeys.SingleUserModeUserName,
Value = "uActivityPub"
},

new uActivitySettings()
new UActivitySettings()
{
Id = 6,
Key = uActivitySettingKeys.GravatarEmail,
Key = UActivitySettingKeys.GravatarEmail,
Value = "[email protected]"
}
];
Expand Down
2 changes: 0 additions & 2 deletions src/uActivityPub/Composers/UActivityPubComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using uActivityPub.Authorization;
using uActivityPub.Data;
using uActivityPub.Data.Migrations;
using uActivityPub.Helpers;
using uActivityPub.Notifications;
using uActivityPub.Notifications.Handlers;
using uActivityPub.Services;
using uActivityPub.Services.ActivityPubServices;
Expand Down
Loading

0 comments on commit 3048821

Please sign in to comment.