From 43b6a7c7c6f7ca1cfa5426a1400c23931a39b88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sat, 18 Jan 2025 11:38:39 +0100 Subject: [PATCH] Fix unit tests --- .../java/org/shredzone/acme4j/Metadata.java | 13 ++-- .../main/java/org/shredzone/acme4j/Order.java | 4 +- .../org/shredzone/acme4j/OrderBuilder.java | 14 ++-- .../shredzone/acme4j/OrderBuilderTest.java | 70 +++++++------------ 4 files changed, 41 insertions(+), 60 deletions(-) diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java index cddb7692..e81edbf4 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java @@ -134,25 +134,24 @@ public class Metadata { /** * Returns whether the CA supports the profile feature. * - * @since 3.5 - * @throws AcmeNotSupportedException if the server does not support the profile feature. + * @since 3.5.0 */ public boolean isProfileAllowed() { - return meta.getFeature("profile").optional().isPresent(); + return meta.get("profiles").isPresent(); } /** * Returns whether the CA supports the requested profile. + *

+ * Also returns {@code false} if profiles are not allowed in general. * - * @since 3.5 - * @throws AcmeNotSupportedException if the server does not support the requested profile. + * @since 3.5.0 */ public boolean isProfileAllowed(String profile) { - return meta.getFeature("profile").optional() + return meta.get("profiles").optional() .map(Value::asObject) .orElseGet(JSON::empty) .get(profile) - .optional() .isPresent(); } diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java index 54ea4e18..214411c9 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Order.java @@ -440,11 +440,11 @@ public class Order extends AcmeJsonResource implements PollableResource { /** * Returns the selected profile. * - * @since 3.5 + * @since 3.5.0 * @throws AcmeNotSupportedException if profile is not supported */ public String getProfile() { - return getJSON().getFeature("profile").toString(); + return getJSON().getFeature("profile").asString(); } @Override diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/OrderBuilder.java b/acme4j-client/src/main/java/org/shredzone/acme4j/OrderBuilder.java index 7d8271cb..8aee901b 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/OrderBuilder.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/OrderBuilder.java @@ -273,15 +273,15 @@ public class OrderBuilder { /** * Notifies the CA of the desired profile of the ordered certificate. *

- * Optional, only supported if the CA supports profiles. However, in this - * case the client may include this field. + * Optional, only supported if the CA supports profiles. However, in this case the + * client may include this field. * * @param profile * Identifier of the desired profile * @return itself - * @draft This method is currently based on RFC draft draft-aaron-acme-profiles. It may be changed or removed - * without notice to reflect future changes to the draft. SemVer rules do not apply - * here. + * @draft This method is currently based on RFC draft draft-aaron-acme-profiles. It + * may be changed or removed without notice to reflect future changes to the draft. + * SemVer rules do not apply here. * @since 3.5.0 */ public OrderBuilder profile(String profile) { @@ -376,7 +376,7 @@ public class OrderBuilder { } if (profile != null && !session.getMetadata().isProfileAllowed(profile)) { - throw new AcmeNotSupportedException("profile with value " + profile); + throw new AcmeNotSupportedException("profile: " + profile); } var hasAncestorDomain = identifierSet.stream() @@ -421,7 +421,7 @@ public class OrderBuilder { claims.put("replaces", replaces); } - if(profile != null) { + if (profile != null) { claims.put("profile", profile); } diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/OrderBuilderTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/OrderBuilderTest.java index dc803fe8..68b24192 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/OrderBuilderTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/OrderBuilderTest.java @@ -20,6 +20,7 @@ import static org.shredzone.acme4j.toolbox.AcmeUtils.parseTimestamp; import static org.shredzone.acme4j.toolbox.TestUtils.getJSON; import static org.shredzone.acme4j.toolbox.TestUtils.url; +import java.io.IOException; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.URL; @@ -342,7 +343,6 @@ public class OrderBuilderTest { */ @Test public void testProfileOrderCertificate() throws Exception { - var provider = new TestableConnectionProvider() { @Override public int sendSignedRequest(URL url, JSONBuilder claims, Login login) { @@ -354,7 +354,7 @@ public class OrderBuilderTest { @Override public JSON readJsonResponse() { - return getJSON("requestAutoRenewOrderResponse"); + return getJSON("requestProfileOrderResponse"); } @Override @@ -365,8 +365,8 @@ public class OrderBuilderTest { var login = provider.createLogin(); - provider.putMetadata("profile",JSON.parse( - "{\"classic\": true}" + provider.putMetadata("profiles",JSON.parse( + "{\"classic\": \"The same profile you're accustomed to\"}" ).toMap()); provider.putTestResource(Resource.NEW_ORDER, resourceUrl); @@ -381,72 +381,54 @@ public class OrderBuilderTest { } provider.close(); - provider.close(); } /** - * Test that a profile {@link Order} cannot be created if the profile is unsupported by the CA. + * Test that a profile {@link Order} cannot be created if the profile is unsupported + * by the CA. */ @Test public void testUnsupportedProfileOrderCertificateFails() throws Exception { + var provider = new TestableConnectionProvider(); + provider.putMetadata("profiles",JSON.parse( + "{\"classic\": \"The same profile you're accustomed to\"}" + ).toMap()); + provider.putTestResource(Resource.NEW_ORDER, resourceUrl); - var provider = new TestableConnectionProvider() { - @Override - public int sendSignedRequest(URL url, JSONBuilder claims, Login login) { - assertThat(url).isEqualTo(resourceUrl); - assertThatJson(claims.toString()).isEqualTo(getJSON("requestProfileOrderRequest").toString()); - assertThat(login).isNotNull(); - return HttpURLConnection.HTTP_CREATED; - } + var login = provider.createLogin(); - @Override - public JSON readJsonResponse() { - return getJSON("requestAutoRenewOrderResponse"); - } - - @Override - public URL getLocation() { - return locationUrl; - } - }; - - assertThrows(AcmeNotSupportedException.class, () -> { - provider.putTestResource(Resource.NEW_ORDER, resourceUrl); - - var login = provider.createLogin(); - - var account = new Account(login); + var account = new Account(login); + assertThatExceptionOfType(AcmeNotSupportedException.class).isThrownBy(() -> { account.newOrder() .domain("example.org") .profile("invalid") .create(); - - provider.close(); - }); + }).withMessage("Server does not support profile: invalid"); + provider.close(); } /** - * Test that a profile {@link Order} cannot be created if the feature is unsupported by the CA. + * Test that a profile {@link Order} cannot be created if the feature is unsupported + * by the CA. */ @Test - public void testProfileOrderCertificateFails() { - assertThrows(AcmeNotSupportedException.class, () -> { - var provider = new TestableConnectionProvider(); - provider.putTestResource(Resource.NEW_ORDER, resourceUrl); + public void testProfileOrderCertificateFails() throws IOException { + var provider = new TestableConnectionProvider(); + provider.putTestResource(Resource.NEW_ORDER, resourceUrl); - var login = provider.createLogin(); + var login = provider.createLogin(); - var account = new Account(login); + var account = new Account(login); + assertThatExceptionOfType(AcmeNotSupportedException.class).isThrownBy(() -> { account.newOrder() .domain("example.org") .profile("classic") .create(); + }).withMessage("Server does not support profile"); - provider.close(); - }); + provider.close(); } - /** * Test that the ARI replaces field is set. */