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

issue #4101 - handle patch in fhir-smart interceptor #4103

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -458,6 +458,15 @@ public void beforeDelete(FHIRPersistenceEvent event) throws FHIRPersistenceInter

@Override
public void beforeUpdate(FHIRPersistenceEvent event) throws FHIRPersistenceInterceptorException {
beforeUpdateOrPatch(event);
}

@Override
public void beforePatch(FHIRPersistenceEvent event) throws FHIRPersistenceInterceptorException {
beforeUpdateOrPatch(event);
}

private void beforeUpdateOrPatch(FHIRPersistenceEvent event) throws FHIRPersistenceInterceptorException {
DecodedJWT jwt = JWT.decode(getAccessToken());
Set<String> patientIdFromToken = getPatientIdFromToken(jwt);
Map<ContextType, List<Scope>> scopesFromToken = getScopesFromToken(jwt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,74 @@ public void testUpdate(String scopeString, List<String> contextIds, Set<Resource
assertTrue(e.getMessage().equals("securityContext is not supported for resource type Binary"));
}
}
}

@Test(dataProvider = "scopeStringProvider")
public void testPatch(String scopeString, List<String> contextIds, Set<ResourceType> resourceTypesPermittedByScope, Permission permission) {
FHIRRequestContext.get().setHttpHeaders(buildRequestHeaders(scopeString, contextIds));

try {
properties.put(FHIRPersistenceEvent.PROPNAME_RESOURCE_TYPE, "Patient");
FHIRPersistenceEvent event = new FHIRPersistenceEvent(patient, properties);
event.setPrevFhirResource(patient);
interceptor.beforePatch(event);
assertTrue(shouldSucceed(resourceTypesPermittedByScope, PATIENT, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, PATIENT, WRITE_APPROVED, permission));
} catch (FHIRPersistenceInterceptorException e) {
assertFalse(shouldSucceed(resourceTypesPermittedByScope, PATIENT, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, PATIENT, WRITE_APPROVED, permission));
}

try {
properties.put(FHIRPersistenceEvent.PROPNAME_RESOURCE_TYPE, "Observation");
FHIRPersistenceEvent event = new FHIRPersistenceEvent(observation, properties);
event.setPrevFhirResource(observation);
interceptor.beforePatch(event);
assertTrue(shouldSucceed(resourceTypesPermittedByScope, OBSERVATION, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, OBSERVATION, WRITE_APPROVED, permission));
} catch (FHIRPersistenceInterceptorException e) {
assertFalse(shouldSucceed(resourceTypesPermittedByScope, OBSERVATION, READ_APPROVED, permission) &&
Copy link
Collaborator

@punktilious punktilious Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be an || instead of && ?

I'm assuming you want both of these to be false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the && is right. If either one of these is false, then we expect the method under test to throw a FHIRPersistenceInterceptorException.

shouldSucceed(resourceTypesPermittedByScope, OBSERVATION, WRITE_APPROVED, permission));
}

try {
properties.put(FHIRPersistenceEvent.PROPNAME_RESOURCE_TYPE, "Condition");
FHIRPersistenceEvent event = new FHIRPersistenceEvent(condition, properties);
event.setPrevFhirResource(condition);
interceptor.beforePatch(event);
assertTrue(shouldSucceed(resourceTypesPermittedByScope, CONDITION, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, CONDITION, WRITE_APPROVED, permission));
} catch (FHIRPersistenceInterceptorException e) {
assertFalse(shouldSucceed(resourceTypesPermittedByScope, CONDITION, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, CONDITION, WRITE_APPROVED, permission));
}

// Test beforePatch Binary Resource which does not have a securityContext. Should Succeed
try {
properties.put(FHIRPersistenceEvent.PROPNAME_RESOURCE_TYPE, "Binary");
FHIRPersistenceEvent event = new FHIRPersistenceEvent(binary, properties);
event.setPrevFhirResource(binary);
interceptor.beforePatch(event);
assertTrue(shouldSucceed(resourceTypesPermittedByScope, BINARY, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, BINARY, WRITE_APPROVED, permission));
} catch (FHIRPersistenceInterceptorException e) {
assertFalse(shouldSucceed(resourceTypesPermittedByScope, BINARY, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, BINARY, WRITE_APPROVED, permission));
}

// Test beforePatch Binary Resource which has a securityContext. Should Fail since securityContext is not supported.
try {
properties.put(FHIRPersistenceEvent.PROPNAME_RESOURCE_TYPE, "Binary");
FHIRPersistenceEvent event = new FHIRPersistenceEvent(binaryWithSecurityContext, properties);
event.setPrevFhirResource(binaryWithSecurityContext);
interceptor.beforePatch(event);
fail("Did not receive the expected FHIRPersistenceInterceptorException");
} catch (FHIRPersistenceInterceptorException e) {
if (shouldSucceed(resourceTypesPermittedByScope, BINARY, READ_APPROVED, permission) &&
shouldSucceed(resourceTypesPermittedByScope, BINARY, WRITE_APPROVED, permission)) {
assertTrue(e.getMessage().equals("securityContext is not supported for resource type Binary"));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else?

Copy link
Member Author

@lmsurpre lmsurpre Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
// else beforePatch was rejected due to normal fhir-smart behavior (non-securityContext-related)

}
}

@Test(dataProvider = "scopeStringProvider")
Expand Down