diff --git a/src/main/java/org/justserve/cli/command/MakeOrgAdmin.java b/src/main/java/org/justserve/cli/command/MakeOrgAdmin.java index d9f382a..0702cd3 100644 --- a/src/main/java/org/justserve/cli/command/MakeOrgAdmin.java +++ b/src/main/java/org/justserve/cli/command/MakeOrgAdmin.java @@ -45,24 +45,22 @@ public void run() { } DynamicRoutingClient dynamicRoutingClient = dynamicRoutingClientProvider.get(); // since we allow submitting orgId's and orgUrl, convert any slugs to orgId's - Map orgUuidMap = Arrays.stream(orgs).distinct().parallel() - .collect(Collectors.toMap( - org -> org, - org -> org instanceof OrgId ? ((OrgId) org).getId() : dynamicRoutingClient - .getOrgIdFromSlug(((OrgSlug) org).getSlug()).body().getId() - )); - if (orgUuidMap.values().stream().anyMatch(Objects::isNull)) { - // provide a list of all invalid org slugs, not just the first failure - List invalidOrgSlugs = new ArrayList<>(); - orgUuidMap.entrySet().stream() - .filter(entry -> entry.getValue() == null) - .map(entry -> (OrgSlug) entry.getKey()) - .forEach(invalidOrgSlugs::add); - printError("The following organization slugs are invalid: " + invalidOrgSlugs.stream() - .map(OrgSlug::getSlug) - .collect(Collectors.joining(", "))); - return; - } + Map orgUuidMap = Arrays.stream(orgs).distinct().parallel() + .map(org -> { + if (org instanceof OrgId) { + return new AbstractMap.SimpleEntry<>(org, ((OrgId) org).getId()); + } + try { + return new AbstractMap.SimpleEntry<>(org, dynamicRoutingClient + .getOrgIdFromSlug(((OrgSlug) org).getSlug()).body().getId()); + } catch (NullPointerException noOrgFound) { + err(String.format("The org '%s' is not found on JustServe", ((OrgSlug) org).getSlug())); + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + log.atTrace().log("Finished converting any slugs to org id's."); BoundaryPermissionClient boundaryPermissionClient = boundaryPermissionClientProvider.get(); Map successfulReassignments = new HashMap<>(); diff --git a/src/main/resources/schema.yml b/src/main/resources/schema.yml index d68296f..04b0316 100644 --- a/src/main/resources/schema.yml +++ b/src/main/resources/schema.yml @@ -880,8 +880,8 @@ components: organizationType: { $ref: '#/components/schemas/OrganizationType' } title: { type: string, nullable: true } logo: { type: string, nullable: true } - url: { type: string, nullable: true } - internalURL: { type: string, nullable: true } + url: { type: string, nullable: true, description: "this provides only the url slug" } + internalURL: { type: string, nullable: true, description: "this currently returns null when an org is searched for by location" } website: { type: string, nullable: true } description: { type: string, nullable: true } contactName: { type: string, nullable: true } diff --git a/src/test/groovy/org/justserve/JustServeSpec.groovy b/src/test/groovy/org/justserve/JustServeSpec.groovy index 277ddad..749dc82 100644 --- a/src/test/groovy/org/justserve/JustServeSpec.groovy +++ b/src/test/groovy/org/justserve/JustServeSpec.groovy @@ -159,7 +159,7 @@ class JustServeSpec extends Specification { * @param count The number of organizations to create. * @return A list of UUIDs for the created organizations. */ - List createOrgs(int count) { + List createTestOrgs(int count) { return (1..count).collect { createOrg() } diff --git a/src/test/groovy/org/justserve/cli/command/MakeOrgAdminSpec.groovy b/src/test/groovy/org/justserve/cli/command/MakeOrgAdminSpec.groovy index f465bd6..94082ba 100644 --- a/src/test/groovy/org/justserve/cli/command/MakeOrgAdminSpec.groovy +++ b/src/test/groovy/org/justserve/cli/command/MakeOrgAdminSpec.groovy @@ -11,7 +11,7 @@ class MakeOrgAdminSpec extends BaseCommandSpec { def "can make a user an admin to #orgCount org(s) using the #orgFlag and #userFlag flags #title"() { given: UUID userID = createUser().body().id - def orgs = createOrgs(orgCount).join(",") + def orgs = createTestOrgs(orgCount).join(",") when: def (outputStream, errorStream) = executeCommand(context as ApplicationContext, ["makeOrgAdmin", orgFlag, orgs, userFlag, userID] as String[]) @@ -53,7 +53,7 @@ class MakeOrgAdminSpec extends BaseCommandSpec { if (orgCount == 1) { orgs = fakeId } else { - orgs = createOrgs(orgCount - 1).join(",") + "," + fakeId + orgs = createTestOrgs(orgCount - 1).join(",") + "," + fakeId } when: @@ -75,4 +75,35 @@ class MakeOrgAdminSpec extends BaseCommandSpec { "-o" | 1 | "--user" | ctx | "as an authorized user successfully makes the changes" | _ } + + + def "can make a user an admin to #orgCount where at least one org Slug does not exist on JustServe"() { + given: + UUID userID = createUser().body().id + String orgs + def fakeSlug = faker.internet().slug().toString() + if (orgCount == 1) { + orgs = fakeSlug + } else { + orgs=authOrgClient.searchByLocation(createSearchRequestForElkGrove()).body().getOrganizations().url.take(orgCount - 1).join(",")+","+fakeSlug + } + + when: + def (outputStream, errorStream) = executeCommand(context as ApplicationContext, ["makeOrgAdmin", orgFlag, orgs, userFlag, userID] as String[]) + + then: + verifyAll { + (outputStream as String).contains("successfully reassigned ${orgCount - 1 } orgs to user ${userID}") + (errorStream as String).contains("Error The org '${fakeSlug}' is not found on JustServe") + } + + where: + orgFlag | orgCount | userFlag | context | title | _ + "-o" | 3 | "-u" | ctx | "as an authorized user successfully makes the changes" | _ + "--org" | 3 | "-u" | ctx | "as an authorized user successfully makes the changes" | _ + "-o" | 3 | "--user" | ctx | "as an authorized user successfully makes the changes" | _ + "-o" | 1 | "-u" | ctx | "as an authorized user successfully makes the changes" | _ + "--org" | 1 | "-u" | ctx | "as an authorized user successfully makes the changes" | _ + "-o" | 1 | "--user" | ctx | "as an authorized user successfully makes the changes" | _ + } } diff --git a/src/test/groovy/org/justserve/client/DynamicRoutingClientSpec.groovy b/src/test/groovy/org/justserve/client/DynamicRoutingClientSpec.groovy index 16bb270..7b5f6e9 100644 --- a/src/test/groovy/org/justserve/client/DynamicRoutingClientSpec.groovy +++ b/src/test/groovy/org/justserve/client/DynamicRoutingClientSpec.groovy @@ -11,16 +11,19 @@ class DynamicRoutingClientSpec extends JustServeSpec { @Shared DynamicRoutingClient noAuthClient, authClient + @Shared + String realOrgSlug + def setupSpec() { noAuthClient = noAuthCtx.getBean(DynamicRoutingClient) authClient = ctx.getBean(DynamicRoutingClient) + realOrgSlug = authOrgClient.searchByLocation(createSearchRequestForElkGrove()).body().getOrganizations().url.first().toString() } def "get orgId for #url"() { when: HttpResponse response = client.getOrgIdFromSlug(url) - then: response.status() == expectedStatus if (expectedStatus == HttpStatus.OK) { @@ -28,10 +31,10 @@ class DynamicRoutingClientSpec extends JustServeSpec { } where: - url | expectedStatus | client - "accessleisure_sacramento" | HttpStatus.OK | authClient //TODO add actual orgs, not hardtyped ones - "accessleisure_sacramento" | HttpStatus.OK | noAuthClient - "1234" | HttpStatus.NOT_FOUND | authClient - "1234" | HttpStatus.NOT_FOUND | noAuthClient + url | expectedStatus | client + realOrgSlug | HttpStatus.OK | authClient + realOrgSlug | HttpStatus.OK | noAuthClient + "1234" | HttpStatus.NOT_FOUND | authClient + "1234" | HttpStatus.NOT_FOUND | noAuthClient } }