From 6bb356ea027657fc654a6e56e1d65e42ea3c58a1 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 20 Jan 2024 21:44:44 +0100 Subject: [PATCH] Add JSON-LD expand/compact/canonicalize tests --- .../Federation/ActivityStreams/LDHelpers.cs | 4 +- .../Cryptography/LdSignatureTests.cs | 2 - Iceshrimp.Tests/GlobalUsings.cs | 3 +- Iceshrimp.Tests/Serialization/JsonLdTests.cs | 48 +++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 Iceshrimp.Tests/Serialization/JsonLdTests.cs diff --git a/Iceshrimp.Backend/Core/Federation/ActivityStreams/LDHelpers.cs b/Iceshrimp.Backend/Core/Federation/ActivityStreams/LDHelpers.cs index 06e92178..eaf2e87d 100644 --- a/Iceshrimp.Backend/Core/Federation/ActivityStreams/LDHelpers.cs +++ b/Iceshrimp.Backend/Core/Federation/ActivityStreams/LDHelpers.cs @@ -56,6 +56,6 @@ public static class LdHelpers { public static JArray? Expand(object obj) => Expand(JToken.FromObject(obj)); public static JObject? Compact(JToken? json) => JsonLdProcessor.Compact(json, DefaultContext, Options); public static JArray? Expand(JToken? json) => JsonLdProcessor.Expand(json, Options); - public static string Canonicalize(JArray json) => JsonLdProcessor.Canonicalize(json); - public static string Canonicalize(JObject json) => JsonLdProcessor.Canonicalize([json]); + public static string Canonicalize(JArray json) => JsonLdProcessor.Canonicalize(json.DeepClone() as JArray); + public static string Canonicalize(JObject json) => JsonLdProcessor.Canonicalize([json.DeepClone()]); } \ No newline at end of file diff --git a/Iceshrimp.Tests/Cryptography/LdSignatureTests.cs b/Iceshrimp.Tests/Cryptography/LdSignatureTests.cs index 9e724393..ab2c8cb8 100644 --- a/Iceshrimp.Tests/Cryptography/LdSignatureTests.cs +++ b/Iceshrimp.Tests/Cryptography/LdSignatureTests.cs @@ -1,6 +1,4 @@ -using System.Diagnostics; using System.Security.Cryptography; -using FluentAssertions; using Iceshrimp.Backend.Core.Federation.ActivityStreams; using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types; using Iceshrimp.Backend.Core.Federation.Cryptography; diff --git a/Iceshrimp.Tests/GlobalUsings.cs b/Iceshrimp.Tests/GlobalUsings.cs index ab67c7ea..944e5449 100644 --- a/Iceshrimp.Tests/GlobalUsings.cs +++ b/Iceshrimp.Tests/GlobalUsings.cs @@ -1 +1,2 @@ -global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file +global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using FluentAssertions; \ No newline at end of file diff --git a/Iceshrimp.Tests/Serialization/JsonLdTests.cs b/Iceshrimp.Tests/Serialization/JsonLdTests.cs new file mode 100644 index 00000000..efab413d --- /dev/null +++ b/Iceshrimp.Tests/Serialization/JsonLdTests.cs @@ -0,0 +1,48 @@ +using Iceshrimp.Backend.Core.Federation.ActivityStreams; +using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types; +using Iceshrimp.Backend.Core.Helpers; + +namespace Iceshrimp.Tests.Serialization; + +[TestClass] +public class JsonLdTests { + private ASActor _actor = null!; + + [TestInitialize] + public void Initialize() { + _actor = new ASActor { + Id = $"https://example.org/users/{IdHelpers.GenerateSlowflakeId()}", + Type = ["https://www.w3.org/ns/activitystreams#Person"], + Url = new ASLink("https://example.org/@test"), + Username = "test", + DisplayName = "Test account", + IsCat = false, + IsDiscoverable = true, + IsLocked = true + }; + } + + [TestMethod] + public void RoundtripTest() { + var expanded = LdHelpers.Expand(_actor)!; + expanded.Should().NotBeNull(); + + var canonicalized = LdHelpers.Canonicalize(expanded); + canonicalized.Should().NotBeNull(); + + var compacted = LdHelpers.Compact(expanded); + compacted.Should().NotBeNull(); + + var expanded2 = LdHelpers.Expand(compacted)!; + expanded2.Should().NotBeNull(); + expanded2.Should().BeEquivalentTo(expanded); + + var compacted2 = LdHelpers.Compact(expanded2)!; + compacted2.Should().NotBeNull(); + compacted2.Should().BeEquivalentTo(compacted); + + var canonicalized2 = LdHelpers.Canonicalize(expanded2); + canonicalized2.Should().NotBeNull(); + canonicalized2.Should().BeEquivalentTo(canonicalized); + } +} \ No newline at end of file