Search in sources :

Example 21 with CurrentUserService

use of org.hisp.dhis.user.CurrentUserService in project dhis2-core by dhis2.

the class DataApprovalServiceTest method testMayApproveSameLevel.

@Test
@Category(IntegrationTest.class)
public void testMayApproveSameLevel() {
    Set<OrganisationUnit> units = newHashSet(organisationUnitB);
    CurrentUserService currentUserService = new MockCurrentUserService(units, null, DataApproval.AUTH_APPROVE, AUTH_APPR_LEVEL);
    userService.addUser(currentUserService.getCurrentUser());
    setCurrentUserServiceDependencies(currentUserService);
    Date date = new Date();
    // Level 4 (organisationUnitD and organisationUnitF ready)
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 3 (organisationUnitC) and Level 4 (organisationUnitF) ready
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitD, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit C.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level4, workflow1234, periodA, organisationUnitD, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 2 (organisationUnitB) ready
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitF, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit F.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level4, workflow1234, periodA, organisationUnitF, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitE, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit E.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level3, workflow1234, periodA, organisationUnitE, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitC, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit C.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level3, workflow1234, periodA, organisationUnitC, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo));
    assertEquals("APPROVED_HERE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo));
    assertEquals("APPROVED_HERE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo));
    // Level 1 (organisationUnitA) ready
    dataApprovalService.approveData(newArrayList(new DataApproval(level2, workflow1234, periodA, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    assertEquals("APPROVED_HERE level=level2 approve=F unapprove=T accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo));
    // Level 1 (organisationUnitA) try to approve
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level1, workflow1234, periodA, organisationUnitA, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit A.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DataMayNotBeApprovedException(org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException) Date(java.util.Date) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 22 with CurrentUserService

use of org.hisp.dhis.user.CurrentUserService in project dhis2-core by dhis2.

the class DataApprovalServiceTest method testAddAllAndGetDataApprovalStatus.

@Test
@Category(IntegrationTest.class)
public void testAddAllAndGetDataApprovalStatus() {
    Set<OrganisationUnit> units = newHashSet(organisationUnitA);
    CurrentUserService currentUserService = new MockCurrentUserService(units, null, DataApproval.AUTH_APPROVE, DataApproval.AUTH_APPROVE_LOWER_LEVELS);
    userService.addUser(currentUserService.getCurrentUser());
    setCurrentUserServiceDependencies(currentUserService);
    Date date = new Date();
    DataApproval dataApprovalA = new DataApproval(level1, workflow12A, periodA, organisationUnitA, defaultOptionCombo, NOT_ACCEPTED, date, userA);
    DataApproval dataApprovalB = new DataApproval(level2, workflow12A, periodA, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA);
    DataApproval dataApprovalC = new DataApproval(level2, workflow12A, periodB, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA);
    DataApproval dataApprovalD = new DataApproval(level2, workflow12B, periodA, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA);
    // Must be approved before A.
    dataApprovalService.approveData(newArrayList(dataApprovalB, dataApprovalC, dataApprovalD));
    dataApprovalService.approveData(newArrayList(dataApprovalA));
    DataApprovalStatus status = dataApprovalService.getDataApprovalStatus(workflow12A, periodA, organisationUnitA, defaultOptionCombo);
    assertEquals(DataApprovalState.APPROVED_HERE, status.getState());
    assertNotNull(status);
    assertEquals(organisationUnitA.getUid(), status.getOrganisationUnitUid());
    assertEquals(date, status.getCreated());
    assertEquals(userA.getId(), status.getCreator().getId());
    DataApprovalLevel level = status.getApprovedLevel();
    assertNotNull(level);
    assertEquals(level1.getName(), level.getName());
    status = dataApprovalService.getDataApprovalStatus(workflow12A, periodA, organisationUnitB, defaultOptionCombo);
    assertEquals(DataApprovalState.APPROVED_ABOVE, status.getState());
    assertNotNull(status);
    assertEquals(organisationUnitB.getUid(), status.getOrganisationUnitUid());
    assertEquals(date, status.getCreated());
    assertEquals(userA.getId(), status.getCreator().getId());
    level = status.getApprovedLevel();
    assertNotNull(level);
    assertEquals(level2.getName(), level.getName());
    status = dataApprovalService.getDataApprovalStatus(workflow12A, periodB, organisationUnitB, defaultOptionCombo);
    assertEquals(DataApprovalState.APPROVED_HERE, status.getState());
    assertNotNull(status);
    assertEquals(organisationUnitB.getUid(), status.getOrganisationUnitUid());
    assertEquals(date, status.getCreated());
    assertEquals(userA.getId(), status.getCreator().getId());
    level = status.getApprovedLevel();
    assertNotNull(level);
    assertEquals(level2.getName(), level.getName());
    status = dataApprovalService.getDataApprovalStatus(workflow12B, periodA, organisationUnitB, defaultOptionCombo);
    assertEquals(DataApprovalState.APPROVED_HERE, status.getState());
    assertNotNull(status);
    assertEquals(organisationUnitB.getUid(), status.getOrganisationUnitUid());
    assertEquals(date, status.getCreated());
    assertEquals(userA.getId(), status.getCreator().getId());
    level = status.getApprovedLevel();
    assertNotNull(level);
    assertEquals(level2.getName(), level.getName());
    status = dataApprovalService.getDataApprovalStatus(workflow12B, periodB, organisationUnitB, defaultOptionCombo);
    assertEquals(DataApprovalState.UNAPPROVED_READY, status.getState());
    level = status.getApprovedLevel();
    assertNull(level);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Date(java.util.Date) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 23 with CurrentUserService

use of org.hisp.dhis.user.CurrentUserService in project dhis2-core by dhis2.

the class DataApprovalServiceTest method testGetDataApprovalStateOtherPeriodTypes.

@Test
@Category(IntegrationTest.class)
public void testGetDataApprovalStateOtherPeriodTypes() {
    Set<OrganisationUnit> units = newHashSet(organisationUnitA);
    CurrentUserService currentUserService = new MockCurrentUserService(units, null, DataApproval.AUTH_APPROVE, DataApproval.AUTH_APPROVE_LOWER_LEVELS);
    setCurrentUserServiceDependencies(currentUserService);
    assertEquals(DataApprovalState.UNAPPROVED_WAITING, dataApprovalService.getDataApprovalStatus(workflow1234, periodA, organisationUnitA, defaultOptionCombo).getState());
    assertEquals(DataApprovalState.UNAPPROVED_READY, dataApprovalService.getDataApprovalStatus(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getState());
    assertEquals(DataApprovalState.UNAPPROVED_WAITING, dataApprovalService.getDataApprovalStatus(workflow1234, periodB, organisationUnitA, defaultOptionCombo).getState());
    assertEquals(DataApprovalState.UNAPPROVED_WAITING, dataApprovalService.getDataApprovalStatus(workflow1234, periodY, organisationUnitA, defaultOptionCombo).getState());
    assertEquals(DataApprovalState.UNAPPROVED_READY, dataApprovalService.getDataApprovalStatus(workflow1234, periodY, organisationUnitD, defaultOptionCombo).getState());
    assertEquals(DataApprovalState.UNAPPROVED_READY, dataApprovalService.getDataApprovalStatus(workflow1234, periodD, organisationUnitD, defaultOptionCombo).getState());
    assertEquals(DataApprovalState.UNAPPROVED_READY, dataApprovalService.getDataApprovalStatus(workflow1234, periodW, organisationUnitD, defaultOptionCombo).getState());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 24 with CurrentUserService

use of org.hisp.dhis.user.CurrentUserService in project dhis2-core by dhis2.

the class DataApprovalLevelServiceTest method testGetUserReadApprovalLevels_1B.

@Test
public void testGetUserReadApprovalLevels_1B() {
    //
    // Test 1: Like when a user may capture data within their own district
    // but view data in other districts within their province.
    //
    // Variation B: User *has* approval at lower levels authority.
    //
    organisationUnitService.addOrganisationUnit(organisationUnitE);
    organisationUnitService.addOrganisationUnit(organisationUnitF);
    organisationUnitService.addOrganisationUnit(organisationUnitG);
    organisationUnitService.addOrganisationUnit(organisationUnitH);
    dataApprovalLevelService.addDataApprovalLevel(level1, 1);
    dataApprovalLevelService.addDataApprovalLevel(level2, 2);
    dataApprovalLevelService.addDataApprovalLevel(level3, 3);
    dataApprovalLevelService.addDataApprovalLevel(level4, 4);
    dataApprovalLevelService.addDataApprovalLevel(level5, 5);
    Set<OrganisationUnit> assignedOrgUnits = new HashSet<>();
    assignedOrgUnits.add(organisationUnitC);
    Set<OrganisationUnit> dataViewOrgUnits = new HashSet<>();
    dataViewOrgUnits.add(organisationUnitB);
    CurrentUserService currentUserService = new MockCurrentUserService(assignedOrgUnits, dataViewOrgUnits, DataApproval.AUTH_APPROVE_LOWER_LEVELS);
    setDependency(dataApprovalLevelService, "currentUserService", currentUserService, CurrentUserService.class);
    Map<OrganisationUnit, Integer> readApprovalLevels = dataApprovalLevelService.getUserReadApprovalLevels();
    assertEquals(2, readApprovalLevels.size());
    assertEquals(APPROVAL_LEVEL_UNAPPROVED, (int) readApprovalLevels.get(organisationUnitC));
    assertEquals(3, (int) readApprovalLevels.get(organisationUnitB));
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) HashSet(java.util.HashSet) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 25 with CurrentUserService

use of org.hisp.dhis.user.CurrentUserService in project dhis2-core by dhis2.

the class DataApprovalLevelServiceTest method testGetUserDataApprovalLevelsAcceptMuchLower.

@Test
public void testGetUserDataApprovalLevelsAcceptMuchLower() {
    dataApprovalLevelService.addDataApprovalLevel(level4);
    Set<OrganisationUnit> assignedOrgUnits = new HashSet<>();
    assignedOrgUnits.add(organisationUnitB);
    Set<OrganisationUnit> dataViewOrgUnits = new HashSet<>();
    dataViewOrgUnits.add(organisationUnitB);
    CurrentUserService currentUserService = new MockCurrentUserService(assignedOrgUnits, dataViewOrgUnits, DataApproval.AUTH_ACCEPT_LOWER_LEVELS);
    setDependency(dataApprovalLevelService, "currentUserService", currentUserService, CurrentUserService.class);
    List<DataApprovalLevel> levels = dataApprovalLevelService.getUserDataApprovalLevels();
    assertEquals("04", levelNames(levels));
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) HashSet(java.util.HashSet) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

MockCurrentUserService (org.hisp.dhis.mock.MockCurrentUserService)34 CurrentUserService (org.hisp.dhis.user.CurrentUserService)34 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)28 Test (org.junit.Test)28 DhisTest (org.hisp.dhis.DhisTest)19 IntegrationTest (org.hisp.dhis.IntegrationTest)18 Date (java.util.Date)17 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)17 Category (org.junit.experimental.categories.Category)17 HashSet (java.util.HashSet)9 DhisSpringTest (org.hisp.dhis.DhisSpringTest)9 DataMayNotBeApprovedException (org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException)4 Expression (org.hisp.dhis.expression.Expression)2 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)2 Attribute (org.hisp.dhis.attribute.Attribute)1 AttributeValue (org.hisp.dhis.attribute.AttributeValue)1 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)1 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)1 DataValue (org.hisp.dhis.datavalue.DataValue)1 MockBatchHandlerFactory (org.hisp.dhis.mock.batchhandler.MockBatchHandlerFactory)1