From e12b93b7d1793103074a854c82dcd47b2c176441 Mon Sep 17 00:00:00 2001 From: olivia-you Date: Fri, 11 Feb 2022 11:08:22 -0500 Subject: [PATCH] add unit tests --- .../auth/RuleBulkExportImplTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleBulkExportImplTest.java diff --git a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleBulkExportImplTest.java b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleBulkExportImplTest.java new file mode 100644 index 000000000000..db3c57194ede --- /dev/null +++ b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleBulkExportImplTest.java @@ -0,0 +1,75 @@ +package ca.uhn.fhir.rest.server.interceptor.auth; + +import ca.uhn.fhir.interceptor.api.Pointcut; +import ca.uhn.fhir.rest.api.RestOperationTypeEnum; +import ca.uhn.fhir.rest.api.server.RequestDetails; +import ca.uhn.fhir.rest.api.server.bulk.BulkDataExportOptions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class RuleBulkExportImplTest { + + @Mock + private RequestDetails myRequestDetails; + @Mock + private IRuleApplier myRuleApplier; + @Mock + private Set myFlags; + + @Test + public void testDenyBulkRequestWithInvalidResourcesTypes() { + RuleBulkExportImpl myRule = new RuleBulkExportImpl("a"); + RestOperationTypeEnum myOperation = RestOperationTypeEnum.EXTENDED_OPERATION_SERVER; + Pointcut myPointcut = Pointcut.STORAGE_INITIATE_BULK_EXPORT; + + Set myTypes = new HashSet<>(); + myTypes.add("Patient"); + myTypes.add("Practitioner"); + myRule.setResourceTypes(myTypes); + + Set myWantTypes = new HashSet<>(); + myWantTypes.add("Questionnaire"); + + BulkDataExportOptions options = new BulkDataExportOptions(); + options.setResourceTypes(myWantTypes); + when(myRequestDetails.getAttribute(any())).thenReturn(options); + + AuthorizationInterceptor.Verdict verdict = myRule.applyRule(myOperation, myRequestDetails, null, null, null, myRuleApplier, myFlags, myPointcut); + assertEquals(PolicyEnum.DENY, verdict.getDecision()); + } + + @Test + public void testBulkRequestWithValidResourcesTypes() { + RuleBulkExportImpl myRule = new RuleBulkExportImpl("a"); + RestOperationTypeEnum myOperation = RestOperationTypeEnum.EXTENDED_OPERATION_SERVER; + Pointcut myPointcut = Pointcut.STORAGE_INITIATE_BULK_EXPORT; + + Set myTypes = new HashSet<>(); + myTypes.add("Patient"); + myTypes.add("Practitioner"); + myRule.setResourceTypes(myTypes); + + Set myWantTypes = new HashSet<>(); + myWantTypes.add("Patient"); + myWantTypes.add("Practitioner"); + + BulkDataExportOptions options = new BulkDataExportOptions(); + options.setResourceTypes(myWantTypes); + when(myRequestDetails.getAttribute(any())).thenReturn(options); + + AuthorizationInterceptor.Verdict verdict = myRule.applyRule(myOperation, myRequestDetails, null, null, null, myRuleApplier, myFlags, myPointcut); + assertNull(verdict); + } + +}