Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boban/6061 admin organization query #6107

Merged
merged 13 commits into from
Jul 13, 2023
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 @@ -3,12 +3,12 @@
import gov.cdc.usds.simplereport.api.model.ApiFacility;
import gov.cdc.usds.simplereport.api.model.ApiOrganization;
import gov.cdc.usds.simplereport.api.model.ApiPendingOrganization;
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlArgumentException;
import gov.cdc.usds.simplereport.config.AuthorizationConfiguration;
import gov.cdc.usds.simplereport.db.model.Facility;
import gov.cdc.usds.simplereport.db.model.Organization;
import gov.cdc.usds.simplereport.service.OrganizationQueueService;
import gov.cdc.usds.simplereport.service.OrganizationService;
import gov.cdc.usds.simplereport.service.model.OrganizationRoles;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -22,23 +22,25 @@
@Controller
public class OrganizationResolver {

private OrganizationService _organizationService;
private OrganizationQueueService _organizationQueueService;
private final OrganizationService _organizationService;
private final OrganizationQueueService _organizationQueueService;

public OrganizationResolver(OrganizationService os, OrganizationQueueService oqs) {
_organizationService = os;
_organizationQueueService = oqs;
}

@QueryMapping
public Optional<ApiOrganization> organization() {
Optional<OrganizationRoles> roles = _organizationService.getCurrentOrganizationRoles();
return roles.map(
r -> {
Organization o = r.getOrganization();
Set<Facility> fs = r.getFacilities();
return new ApiOrganization(o, fs);
});
@AuthorizationConfiguration.RequireGlobalAdminUser
public ApiOrganization organization(@Argument UUID id) {
Organization org;
try {
org = _organizationService.getOrganizationById(id);
} catch (IllegalGraphqlArgumentException e) {
return null;
}
var facilities = _organizationService.getFacilities(org);
return new ApiOrganization(org, facilities);
}

/**
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/graphql/admin.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extend type Query {
organizations(identityVerified: Boolean): [Organization!]!
pendingOrganizations: [PendingOrganization!]!
organization(id: ID!): Organization
}
extend type Mutation {
resendToReportStream(testEventIds: [ID!]!, fhirOnly: Boolean!): Boolean
Expand Down
4 changes: 0 additions & 4 deletions backend/src/main/resources/graphql/main.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,6 @@ type UploadSubmissionPage {
# Queries and mutations for everyday use

type Query {
organization: Organization
@deprecated(
reason: "this information is already loaded from the 'whoami' endpoint"
)
deviceType: [DeviceType!]!
@deprecated(reason: "use the pluralized form to reduce confusion")
deviceTypes: [DeviceType!]!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ protected Map<String, UUID> extractAllFacilitiesInOrg() {
String originalUsername = getUsername();
useOrgAdmin();
Iterator<JsonNode> facilitiesIter =
runQuery("org-settings-query").get("organization").get("testingFacility").elements();
runQuery("org-settings-query")
.get("whoami")
.get("organization")
.get("testingFacility")
.elements();
Map<String, UUID> facilities = new HashMap<>();
while (facilitiesIter.hasNext()) {
JsonNode facility = facilitiesIter.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gov.cdc.usds.simplereport.api.organization;

import static org.assertj.core.api.Assertions.assertThat;

import gov.cdc.usds.simplereport.api.graphql.BaseGraphqlTest;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;

class OrganizationIntegrationTest extends BaseGraphqlTest {

@Test
void organizationQuery_asSuperUser_passes() {
var org = _orgService.getOrganizationsByName("Dis Organization").get(0);
useSuperUser();
var result = runQuery("organization-query", Map.of("id", org.getInternalId()));
assertThat(result.get("organization").get("internalId").asText())
.isEqualTo(org.getInternalId().toString());
}

@Test
void organizationQuery_invalidID_returnsNull() {
useSuperUser();
var result = runQuery("organization-query", Map.of("id", UUID.randomUUID()));
assertThat(result.get("organization").isNull()).isTrue();
}

@Test
void organizationQuery_asOrgAdmin_fails() {
var org = _orgService.getOrganizationsByName("Dis Organization").get(0);
useOrgAdmin();
runQuery("organization-query", Map.of("id", org.getInternalId()), "Unauthorized");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package gov.cdc.usds.simplereport.api.organization;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import gov.cdc.usds.simplereport.api.model.ApiPendingOrganization;
import gov.cdc.usds.simplereport.api.model.accountrequest.OrganizationAccountRequest;
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlArgumentException;
import gov.cdc.usds.simplereport.db.model.Organization;
import gov.cdc.usds.simplereport.db.model.OrganizationQueueItem;
import gov.cdc.usds.simplereport.service.OrganizationQueueService;
import gov.cdc.usds.simplereport.service.OrganizationService;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -18,6 +24,7 @@
@ExtendWith(SpringExtension.class)
class OrganizationResolverTest {
@Mock OrganizationQueueService mockedOrganizationQueueService;
@Mock OrganizationService organizationService;

@InjectMocks OrganizationResolver organizationMutationResolver;

Expand All @@ -40,4 +47,32 @@ void getPendingOrganizations_success() {
assertThat(expected).hasSameSizeAs(result);
assertThat(expected.get(0).getExternalId()).isEqualTo(result.get(0).getExternalId());
}

@Test
void organization_success() {
var id = UUID.randomUUID();
var org = new Organization("name", "type", "123", true);

when(organizationService.getOrganizationById(id)).thenReturn(org);

organizationMutationResolver.organization(id);

verify(organizationService).getOrganizationById(id);
verify(organizationService).getFacilities(org);
}

@Test
void organization_null() {
var id = UUID.randomUUID();
var org = new Organization("name", "type", "123", true);

when(organizationService.getOrganizationById(id))
.thenThrow(new IllegalGraphqlArgumentException("error"));

var actual = organizationMutationResolver.organization(id);

assertThat(actual).isNull();
verify(organizationService).getOrganizationById(id);
verify(organizationService, times(0)).getFacilities(org);
}
}
46 changes: 24 additions & 22 deletions backend/src/test/resources/graphql-test/org-settings-query.graphql
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
{
organization {
internalId
name
externalId
testingFacility {
id
cliaNumber
whoami {
organization {
internalId
name
street
streetTwo
city
county
state
zipCode
phone
email
orderingProvider {
firstName
middleName
lastName
suffix
NPI
externalId
testingFacility {
id
cliaNumber
name
street
streetTwo
city
county
state
zipCode
phone
email
orderingProvider {
firstName
middleName
lastName
suffix
NPI
street
streetTwo
city
county
state
zipCode
phone
__typename
}
__typename
}
__typename
}
}
}
15 changes: 15 additions & 0 deletions backend/src/test/resources/graphql-test/organization-query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query organization ($id: ID!){
organization(id: $id) {
internalId
name
type
externalId
identityVerified
patientSelfRegistrationLink
facilities{
id
name
}
id
}
}
johanna-skylight marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
5 changes: 4 additions & 1 deletion frontend/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ export type Query = {
deviceTypes: Array<DeviceType>;
facilities?: Maybe<Array<Maybe<Facility>>>;
facility?: Maybe<Facility>;
/** @deprecated this information is already loaded from the 'whoami' endpoint */
organization?: Maybe<Organization>;
organizationLevelDashboardMetrics?: Maybe<OrganizationLevelDashboardMetrics>;
organizations: Array<Organization>;
Expand Down Expand Up @@ -677,6 +676,10 @@ export type QueryFacilityArgs = {
id: Scalars["ID"];
};

export type QueryOrganizationArgs = {
id: Scalars["ID"];
};

export type QueryOrganizationLevelDashboardMetricsArgs = {
endDate: Scalars["DateTime"];
startDate: Scalars["DateTime"];
Expand Down
Loading