Fix unit tests

master
Richard Körber 2025-01-18 11:38:39 +01:00
parent c0fede3b1a
commit 43b6a7c7c6
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
4 changed files with 41 additions and 60 deletions

View File

@ -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.
* <p>
* 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();
}

View File

@ -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

View File

@ -273,15 +273,15 @@ public class OrderBuilder {
/**
* Notifies the CA of the desired profile of the ordered certificate.
* <p>
* Optional, only supported if the CA supports profiles. However, in this
* case the client <em>may</em> include this field.
* Optional, only supported if the CA supports profiles. However, in this case the
* client <em>may</em> 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()

View File

@ -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() {
@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;
}
@Override
public JSON readJsonResponse() {
return getJSON("requestAutoRenewOrderResponse");
}
@Override
public URL getLocation() {
return locationUrl;
}
};
assertThrows(AcmeNotSupportedException.class, () -> {
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 login = provider.createLogin();
var account = new Account(login);
assertThatExceptionOfType(AcmeNotSupportedException.class).isThrownBy(() -> {
account.newOrder()
.domain("example.org")
.profile("invalid")
.create();
}).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, () -> {
public void testProfileOrderCertificateFails() throws IOException {
var provider = new TestableConnectionProvider();
provider.putTestResource(Resource.NEW_ORDER, resourceUrl);
var login = provider.createLogin();
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();
});
}
/**
* Test that the ARI replaces field is set.
*/