From: David ‘Bombe’ Roden Date: Wed, 8 Jan 2025 20:33:41 +0000 (+0100) Subject: ✅ Add test for WebOfTrustPlugin.addIdentity() X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=49a0b24e8336004514e803683dc5f1c3b440fc38;p=jFCPlib.git ✅ Add test for WebOfTrustPlugin.addIdentity() --- diff --git a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java index a615b58..f63c6cf 100644 --- a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java +++ b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java @@ -28,6 +28,7 @@ import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; import static net.pterodactylus.fcp.test.IdentityMatchers.hasContexts; import static net.pterodactylus.fcp.test.IdentityMatchers.hasProperties; +import static net.pterodactylus.fcp.test.IdentityMatchers.isIdentity; import static net.pterodactylus.fcp.test.IdentityMatchers.isOwnIdentity; import static net.pterodactylus.fcp.test.Matchers.hasField; import static net.pterodactylus.fcp.test.Matchers.isNamed; @@ -325,6 +326,54 @@ public class WebOfTrustPluginTest { } + public static class AddIdentityTests extends Common { + + @Test + public void addIdentitySendsCorrectMessage() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithNickname(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123"); + assertThat(fcpConnection.sentMessages.get(0), allOf( + isNamed(equalTo("FCPPluginMessage")), + hasField("Identifier", not(nullValue())), + hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")), + hasField("Param.RequestURI", equalTo("USK@foo,bar/baz/123")) + )); + } + + @Test + public void addIdentityParsesIdentityWithNicknameCorrectly() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithNickname(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity addedIdentity = webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123"); + assertThat(addedIdentity, isIdentity(equalTo("new-id"), equalTo("new-nick"), equalTo("USK@foo,bar/baz/123"))); + } + + @Test + public void addIdentityParsesIdentityWithoutNicknameCorrectly() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithoutNickname(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity addedIdentity = webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123"); + assertThat(addedIdentity, isIdentity(equalTo("new-id"), nullValue(), equalTo("USK@foo,bar/baz/123"))); + } + + @Test + public void addIdentityFailsWhenDifferentReplyIsSentByPlugin() { + FcpConnection fcpConnection = createConnectionThatSendsOtherMessage(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + assertThrows(FcpException.class, () -> webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123")); + } + + private TestFcpConnection createConnectionThatConfirmsAddedIdentityWithNickname() { + return createConnection("IdentityAdded", entries("ID", "new-id", "Nickname", "new-nick")); + } + + private TestFcpConnection createConnectionThatConfirmsAddedIdentityWithoutNickname() { + return createConnection("IdentityAdded", entries("ID", "new-id")); + } + + } + private static class Common { @SafeVarargs diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java index 310b6cb..982e154 100644 --- a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java @@ -79,4 +79,32 @@ public class IdentityMatchers { }; } + public static Matcher isIdentity(Matcher identifier, Matcher nickname, Matcher requestUri) { + return new TypeSafeDiagnosingMatcher(Identity.class) { + @Override + protected boolean matchesSafely(Identity identity, Description mismatchDescription) { + if (!identifier.matches(identity.getIdentifier())) { + identifier.describeMismatch(identity.getIdentifier(), mismatchDescription); + return false; + } + if (!nickname.matches(identity.getNickname())) { + nickname.describeMismatch(identity.getNickname(), mismatchDescription); + return false; + } + if (!requestUri.matches(identity.getRequestUri())) { + requestUri.describeMismatch(identity.getRequestUri(), mismatchDescription); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is identity (").appendValue(identifier).appendText(", nicknamed ").appendValue(nickname) + .appendText(", public uri ").appendValue(requestUri).appendText(")"); + } + + }; + } + }