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
2 changes: 2 additions & 0 deletions api/src/main/java/org/openmrs/api/CohortService.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public interface CohortService extends OpenmrsService {
* @throws APIException
* <strong>Should</strong> delete cohort from database
*/
@Authorized({ PrivilegeConstants.PURGE_COHORTS })
public Cohort purgeCohort(Cohort cohort) throws APIException;

/**
Expand Down Expand Up @@ -141,6 +142,7 @@ public interface CohortService extends OpenmrsService {
* <strong>Should</strong> never return null
* <strong>Should</strong> match cohorts by partial name
*/
@Authorized({ PrivilegeConstants.GET_PATIENT_COHORTS })
public List<Cohort> getCohorts(String nameFragment) throws APIException;

/**
Expand Down
27 changes: 27 additions & 0 deletions api/src/test/java/org/openmrs/api/CohortServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -34,6 +35,7 @@
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;
import org.openmrs.util.PrivilegeConstants;

/**
* Tests methods in the CohortService class TODO add all the rest of the tests
Expand Down Expand Up @@ -118,6 +120,19 @@ public void getCohortMembershipByUuid_shouldReturnNullIfNoObjectFoundWithGivenUu
assertNull(Context.getCohortService().getCohortMembershipByUuid("some invalid uuid"));
}

/**
* @see CohortService#purgeCohort(Cohort)
*/
@Test
public void purgeCohort_shouldFailIfUserDoesNotHaveThePurgeCohortsPrivilege() {
executeDataSet(COHORT_XML);
Cohort cohort = service.getAllCohorts(true).get(0);
Context.logout();
APIAuthenticationException exception = assertThrows(APIAuthenticationException.class,
() -> service.purgeCohort(cohort));
assertThat(exception.getMessage(), containsString(PrivilegeConstants.PURGE_COHORTS));
}

/**
* @see CohortService#purgeCohort(Cohort)
*/
Expand Down Expand Up @@ -147,6 +162,18 @@ public void getCohorts_shouldMatchCohortsByPartialName() {
assertEquals(0, matchedCohorts.size());
}

/**
* @see CohortService#getCohorts(String)
*/
@Test
public void getCohorts_shouldFailIfUserDoesNotHaveTheGetPatientCohortsPrivilege() {
executeDataSet(COHORT_XML);
Context.logout();
APIAuthenticationException exception = assertThrows(APIAuthenticationException.class,
() -> service.getCohorts("Example"));
assertThat(exception.getMessage(), containsString(PrivilegeConstants.GET_PATIENT_COHORTS));
}

/**
* @see CohortService#getCohorts(String)
*/
Expand Down
Loading