From b5cea05399a3efc709c37fc401430e0742c6d905 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 24 Nov 2026 14:39:31 +0000 Subject: [PATCH] TEST: Give the cloud's reason when a live 51Did test gets no 51Did When the cloud answers without a 51Did, it says why in idprobglobalnullreason or idproblicnullreason. The live 51Did tests dropped that reason and always named the fodid product, so a key with FODid but without CloudV5FODiD failed with a message naming the product it had. The tests now keep the reason with the identifiers and put it in each failure and skip, with "no reason given" when there is none. They name FODid for a non-marketing 51Did, and CloudV5FODiD for the standard and personalized usages and for consent strings, as the Node.js and Python tests do. New unit tests cover reading the reason and the message. --- .../pipeline/did/DidClientLiveTests.java | 83 ++++++++++++------- .../fiftyone/pipeline/did/LiveCloudKeys.java | 58 +++++++++++-- .../pipeline/did/LiveCloudKeysTests.java | 58 +++++++++++++ 3 files changed, 162 insertions(+), 37 deletions(-) diff --git a/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/DidClientLiveTests.java b/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/DidClientLiveTests.java index 363f2ec00..8cd506e67 100644 --- a/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/DidClientLiveTests.java +++ b/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/DidClientLiveTests.java @@ -37,6 +37,7 @@ import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.CompletionException; @@ -55,9 +56,11 @@ * creates a 51Did through the cloud {@code json} endpoint, which is one use * against the resource key, plus one for each cloud call it then makes. *

- * The resource key has to carry the fodid product. A key without it gets - * an answer from the service with no identifier in it, which these tests - * report as an entitlement failure naming the product. + * The resource key has to carry the FODid product, and the CloudV5FODiD + * product for the marketing usages. A key without them gets an answer from + * the service with no identifier in it, which these tests report as an + * entitlement failure carrying the service's reason and naming the + * product. */ @RunWith(Parameterized.class) public class DidClientLiveTests { @@ -161,10 +164,11 @@ public void verifyFullThenRedeemWithTheLicence_ReadsTheVerdict() LiveCloudKeys.noLicenseKey(licenseKeyName), client.hasLicenceKey()); - List identifiers = identifiersFor("id.usage", "standard"); + Answer answer = identifiersFor("id.usage", "standard"); + List identifiers = answer.identifiers; Assume.assumeTrue( - "This resource key returned no marketing 51Did, so there was " - + "nothing to verify. Use a key entitled to the standard usage.", + LiveCloudKeys.notEntitled("id.usage=standard", answer.reason, + LiveCloudKeys.MARKETING), identifiers.isEmpty() == false); FodId fodId = identifiers.get(0); assertEquals(Usage.STANDARD, fodId.getUsage()); @@ -234,11 +238,12 @@ public void anIdentifierForAnotherAddress_ReportsEveryFactor() LiveCloudKeys.noLicenseKey(licenseKeyName), client.hasLicenceKey()); - List identifiers = identifiersCreatedFor( + Answer answer = identifiersCreatedFor( "id.usage", "standard", CREATED_FOR); + List identifiers = answer.identifiers; Assume.assumeTrue( - "This resource key returned no marketing 51Did, so there was " - + "nothing to verify. Use a key entitled to the standard usage.", + LiveCloudKeys.notEntitled("id.usage=standard", answer.reason, + LiveCloudKeys.MARKETING), identifiers.isEmpty() == false); FodId fodId = identifiers.get(0); @@ -334,7 +339,8 @@ private FodId create() throws Exception { if (value == null) { fail(LiveCloudKeys.notEntitled( "a request for FODiD.IdProbGlobal with " - + "id.usage=non-marketing")); + + "id.usage=non-marketing", + LiveCloudKeys.nullReason(fodid), LiveCloudKeys.FODID)); } return FodId.fromBase64(value); } @@ -368,13 +374,28 @@ private static String readAll(InputStream stream) throws IOException { private static final String MODEL_TERMS_FOR_MARKETING_2 = "https://m4ow.uk/mtm/2.txt"; + /** + * Every identifier an answer from the {@code json} endpoint carried, + * and the reason the service gave when it carried none. + */ + private static final class Answer { + final List identifiers; + final String reason; + + Answer(List identifiers, String reason) { + this.identifiers = Collections.unmodifiableList(identifiers); + this.reason = reason; + } + } + /** * Asks the {@code json} endpoint for a 51Did with the given query - * parameter and returns every identifier it answered with. An empty - * list means the resource key is not entitled to that usage, which the - * caller fails on, naming the product the key is missing. + * parameter and returns every identifier it answered with. No + * identifier means the resource key is not entitled to that usage, + * which the caller fails on with the service's reason, naming the + * product the key is missing. */ - private List identifiersFor(String name, String value) + private Answer identifiersFor(String name, String value) throws Exception { String url = client.getEndpoint() + "json?resource=" + DidClient.encode(resourceKey) @@ -392,7 +413,7 @@ private List identifiersFor(String name, String value) * present the identifier from another. Both keys travel in the form * body, because a query string is written to access logs. */ - private List identifiersCreatedFor( + private Answer identifiersCreatedFor( String name, String value, String clientIp) throws Exception { String form = "resource=" + DidClient.encode(resourceKey) + "&license=" + DidClient.encode(licenseKey) @@ -415,9 +436,9 @@ private List identifiersCreatedFor( /** * Every identifier in the answer to a request sent to the {@code json} - * endpoint. + * endpoint, with the service's reason when there is none. */ - private static List readIdentifiers(HttpURLConnection connection) + private static Answer readIdentifiers(HttpURLConnection connection) throws Exception { int status = connection.getResponseCode(); String body = readAll(status >= 400 @@ -428,7 +449,7 @@ private static List readIdentifiers(HttpURLConnection connection) List identifiers = new ArrayList<>(); JSONObject fodid = new JSONObject(body).optJSONObject("fodid"); if (fodid == null) { - return identifiers; + return new Answer(identifiers, null); } for (String field : new String[] { "idprobglobal", "idproblic" }) { String found = fodid.optString(field, null); @@ -436,7 +457,7 @@ private static List readIdentifiers(HttpURLConnection connection) identifiers.add(FodId.fromBase64(found)); } } - return identifiers; + return new Answer(identifiers, LiveCloudKeys.nullReason(fodid)); } /** @@ -477,9 +498,12 @@ private void assertAligned( public void everyUsageReadsBackTheTermsAndFlagsTheServiceWrote() throws Exception { String[][] cases = { - { "non-marketing", "NON_MARKETING", null }, - { "standard", "STANDARD", MODEL_TERMS_FOR_MARKETING_2 }, - { "personalized", "PERSONALIZED", MODEL_TERMS_FOR_MARKETING_2 }, + { "non-marketing", "NON_MARKETING", null, + LiveCloudKeys.FODID }, + { "standard", "STANDARD", MODEL_TERMS_FOR_MARKETING_2, + LiveCloudKeys.MARKETING }, + { "personalized", "PERSONALIZED", MODEL_TERMS_FOR_MARKETING_2, + LiveCloudKeys.MARKETING }, }; int checked = 0; @@ -488,9 +512,11 @@ public void everyUsageReadsBackTheTermsAndFlagsTheServiceWrote() Usage usage = Usage.valueOf(one[1]); String terms = one[2]; - List identifiers = identifiersFor("id.usage", name); + Answer answer = identifiersFor("id.usage", name); + List identifiers = answer.identifiers; if (identifiers.isEmpty()) { - fail(LiveCloudKeys.notEntitled("id.usage=" + name)); + fail(LiveCloudKeys.notEntitled( + "id.usage=" + name, answer.reason, one[3])); } for (int i = 0; i < identifiers.size(); i++) { assertAligned(name + "[" + i + "]", identifiers.get(i), @@ -506,8 +532,7 @@ public void everyUsageReadsBackTheTermsAndFlagsTheServiceWrote() assertTrue( "The live cloud service returned no marketing 51Did, so no " + "terms address was read. The resource key has to carry the " - + "fodid product and be entitled to the standard or " - + "personalized usage.", + + LiveCloudKeys.MARKETING + " product.", checked > 0); } @@ -543,10 +568,12 @@ public void consentStringSetsTheUsageIsIndirectBit() throws Exception { // No id.usage is sent. A stated usage wins over a consent // string, so sending one would leave the bit clear and this // would prove the opposite of what it says. - List identifiers = identifiersFor("tcstring", one[0]); + Answer answer = identifiersFor("tcstring", one[0]); + List identifiers = answer.identifiers; if (identifiers.isEmpty()) { fail(LiveCloudKeys.notEntitled( - "a consent string granting " + usage)); + "a consent string granting " + usage, answer.reason, + LiveCloudKeys.MARKETING)); } for (int i = 0; i < identifiers.size(); i++) { // A consent string granting a marketing usage produces a diff --git a/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeys.java b/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeys.java index 9159b0304..434345bf7 100644 --- a/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeys.java +++ b/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeys.java @@ -22,6 +22,7 @@ package fiftyone.pipeline.did; +import org.json.JSONObject; /** * Where the tests that call the live 51Degrees cloud service read their @@ -77,7 +78,8 @@ static String noResourceKey(String name) { + "need a resource key, and they are skipped because " + name + " is not set as an " + "environment variable or as a system property. " - + "The key has to carry the fodid product, because " + + "The key has to carry the " + FODID + " product, and the " + + MARKETING + " product for the marketing usages, because " + "these tests ask the cloud for FODiD.IdProbGlobal and " + "FODiD.IdProbLic. Create a key at " + "https://51degrees.com/configure."; @@ -108,16 +110,54 @@ static String noLicenseKey(String licenseName) { : "Set " + licenseName + " to redeem a sealed result."; } + /** + * The product a key needs for a non-marketing 51Did. + */ + static final String FODID = "FODid"; + + /** + * The product a key needs, as well as {@link #FODID}, for a marketing + * 51Did, being the standard and personalized usages. + */ + static final String MARKETING = "CloudV5FODiD"; + + /** + * The reason the cloud gave for answering without a 51Did, taken from + * the nullreason of the global identifier and then of the licensed + * one, or null when the answer has no fodid section or gives no + * reason. + */ + static String nullReason(JSONObject fodid) { + if (fodid == null) { + return null; + } + for (String field : new String[] { + "idprobglobalnullreason", "idproblicnullreason" }) { + String reason = fodid.optString(field, "").trim(); + if (reason.isEmpty() == false) { + return reason; + } + } + return null; + } + /** * What to fail with when the cloud answered but returned no 51Did, - * which is the resource key not carrying the product rather than the - * service being broken. + * carrying the reason the cloud gave and naming the product a key + * needs for that 51Did. + * + * @param detail what was asked for + * @param reason the cloud's nullreason, or null when it gave none + * @param product {@link #FODID} or {@link #MARKETING} */ - static String notEntitled(String detail) { - return "The live 51Degrees cloud service answered, but returned no " - + "51Did for " + detail + ". That is the resource key not " - + "carrying the fodid product rather than a fault in the " - + "service or in this package. Use a key entitled to fodid, " - + "from https://51degrees.com/configure."; + static String notEntitled(String detail, String reason, String product) { + return "The live 51Degrees cloud service answered without a 51Did " + + "for " + detail + " (" + + (reason == null ? "no reason given" : reason) + + "), which is what a key that does not carry the " + product + + " product gets. To see the products a key carries, open " + + "https://cloud.51degrees.com/api/v4/accessibleproperties" + + "?resource=. Create a key at " + + "https://51degrees.com/configure."; } } diff --git a/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeysTests.java b/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeysTests.java index f47c2d6ec..2e6ac16e4 100644 --- a/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeysTests.java +++ b/pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeysTests.java @@ -22,6 +22,7 @@ package fiftyone.pipeline.did; +import org.json.JSONObject; import org.junit.Test; import static org.junit.Assert.assertArrayEquals; @@ -117,4 +118,61 @@ public void noLicenseKey_SaysWhenThePassHasNone() { String message = LiveCloudKeys.noLicenseKey(null); assertTrue(message, message.contains("no license key")); } + + /** + * The reason the production cloud service gives a key without the + * marketing product, so a failure on such a key says why. + */ + private static final String REFUSED = "Can not form IdProbGlobal. For " + + "marketing (personalized or standard) usages a CloudV5FODiD " + + "product needs to be a part of the license."; + + @Test + public void nullReason_GivesTheGlobalReason() { + JSONObject fodid = new JSONObject() + .put("idprobglobal", JSONObject.NULL) + .put("idprobglobalnullreason", REFUSED); + assertEquals(REFUSED, LiveCloudKeys.nullReason(fodid)); + } + + @Test + public void nullReason_GivesTheLicensedReasonWhenTheGlobalHasNone() { + JSONObject fodid = new JSONObject() + .put("idprobglobalnullreason", " ") + .put("idproblicnullreason", REFUSED); + assertEquals(REFUSED, LiveCloudKeys.nullReason(fodid)); + } + + @Test + public void nullReason_GivesNullWhenTheServiceGaveNone() { + assertNull(LiveCloudKeys.nullReason(new JSONObject())); + } + + @Test + public void nullReason_GivesNullWithNoFodidSection() { + assertNull(LiveCloudKeys.nullReason(null)); + } + + /** + * The reason travels into the message, so a key without the marketing + * product reads as that and not as a fault in the service. + */ + @Test + public void notEntitled_CarriesTheReasonAndNamesTheProduct() { + String message = LiveCloudKeys.notEntitled( + "id.usage=standard", REFUSED, LiveCloudKeys.MARKETING); + assertTrue(message, message.contains("id.usage=standard")); + assertTrue(message, message.contains("(" + REFUSED + ")")); + assertTrue(message, + message.contains("does not carry the CloudV5FODiD product")); + } + + @Test + public void notEntitled_SaysWhenTheServiceGaveNoReason() { + String message = LiveCloudKeys.notEntitled( + "id.usage=non-marketing", null, LiveCloudKeys.FODID); + assertTrue(message, message.contains("(no reason given)")); + assertTrue(message, + message.contains("does not carry the FODid product")); + } }