Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
* <p>
* 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 {
Expand Down Expand Up @@ -161,10 +164,11 @@ public void verifyFullThenRedeemWithTheLicence_ReadsTheVerdict()
LiveCloudKeys.noLicenseKey(licenseKeyName),
client.hasLicenceKey());

List<FodId> identifiers = identifiersFor("id.usage", "standard");
Answer answer = identifiersFor("id.usage", "standard");
List<FodId> 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());
Expand Down Expand Up @@ -234,11 +238,12 @@ public void anIdentifierForAnotherAddress_ReportsEveryFactor()
LiveCloudKeys.noLicenseKey(licenseKeyName),
client.hasLicenceKey());

List<FodId> identifiers = identifiersCreatedFor(
Answer answer = identifiersCreatedFor(
"id.usage", "standard", CREATED_FOR);
List<FodId> 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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<FodId> identifiers;
final String reason;

Answer(List<FodId> 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<FodId> identifiersFor(String name, String value)
private Answer identifiersFor(String name, String value)
throws Exception {
String url = client.getEndpoint() + "json?resource="
+ DidClient.encode(resourceKey)
Expand All @@ -392,7 +413,7 @@ private List<FodId> 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<FodId> identifiersCreatedFor(
private Answer identifiersCreatedFor(
String name, String value, String clientIp) throws Exception {
String form = "resource=" + DidClient.encode(resourceKey)
+ "&license=" + DidClient.encode(licenseKey)
Expand All @@ -415,9 +436,9 @@ private List<FodId> 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<FodId> readIdentifiers(HttpURLConnection connection)
private static Answer readIdentifiers(HttpURLConnection connection)
throws Exception {
int status = connection.getResponseCode();
String body = readAll(status >= 400
Expand All @@ -428,15 +449,15 @@ private static List<FodId> readIdentifiers(HttpURLConnection connection)
List<FodId> 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);
if (found != null && !found.isEmpty()) {
identifiers.add(FodId.fromBase64(found));
}
}
return identifiers;
return new Answer(identifiers, LiveCloudKeys.nullReason(fodid));
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -488,9 +512,11 @@ public void everyUsageReadsBackTheTermsAndFlagsTheServiceWrote()
Usage usage = Usage.valueOf(one[1]);
String terms = one[2];

List<FodId> identifiers = identifiersFor("id.usage", name);
Answer answer = identifiersFor("id.usage", name);
List<FodId> 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),
Expand All @@ -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);
}

Expand Down Expand Up @@ -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<FodId> identifiers = identifiersFor("tcstring", one[0]);
Answer answer = identifiersFor("tcstring", one[0]);
List<FodId> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package fiftyone.pipeline.did;

import org.json.JSONObject;

/**
* Where the tests that call the live 51Degrees cloud service read their
Expand Down Expand Up @@ -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.";
Expand Down Expand Up @@ -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=<key>. Create a key at "
+ "https://51degrees.com/configure.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package fiftyone.pipeline.did;

import org.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
Expand Down Expand Up @@ -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"));
}
}
Loading