Search in sources :

Example 16 with Privilege

use of org.openmrs.Privilege in project openmrs-core by openmrs.

the class EncounterServiceTest method canEditAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToEditEncounters.

/**
 * @see EncounterService#canEditAllEncounterTypes(User)
 */
@Test
public void canEditAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToEditEncounters() {
    EncounterService encounterService = Context.getEncounterService();
    EncounterType encounterType = new EncounterType("testing", "desc");
    Privilege editPrivilege = Context.getUserService().getPrivilege("Some Privilege For Edit Encounter Types");
    encounterType.setEditPrivilege(editPrivilege);
    encounterService.saveEncounterType(encounterType);
    User user = Context.getUserService().getUserByUsername("test_user");
    assertNotNull(user);
    assertFalse(encounterService.canEditAllEncounterTypes(user));
    Role role = Context.getUserService().getRole("Provider");
    role.addPrivilege(editPrivilege);
    user.addRole(role);
    assertTrue(encounterService.canEditAllEncounterTypes(user));
}
Also used : Role(org.openmrs.Role) EncounterRole(org.openmrs.EncounterRole) User(org.openmrs.User) EncounterType(org.openmrs.EncounterType) Privilege(org.openmrs.Privilege) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 17 with Privilege

use of org.openmrs.Privilege in project openmrs-core by openmrs.

the class EncounterServiceTest method canViewAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToViewEncounters.

/**
 * @see EncounterService#canViewAllEncounterTypes(User)
 */
@Test
public void canViewAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToViewEncounters() {
    EncounterService encounterService = Context.getEncounterService();
    EncounterType encounterType = new EncounterType("testing", "desc");
    Privilege viewPrivilege = Context.getUserService().getPrivilege("Some Privilege For View Encounter Types");
    encounterType.setViewPrivilege(viewPrivilege);
    encounterService.saveEncounterType(encounterType);
    User user = Context.getUserService().getUserByUsername("test_user");
    assertNotNull(user);
    assertFalse(encounterService.canViewAllEncounterTypes(user));
    Role role = Context.getUserService().getRole("Provider");
    role.addPrivilege(viewPrivilege);
    user.addRole(role);
    assertTrue(encounterService.canViewAllEncounterTypes(user));
}
Also used : Role(org.openmrs.Role) EncounterRole(org.openmrs.EncounterRole) User(org.openmrs.User) EncounterType(org.openmrs.EncounterType) Privilege(org.openmrs.Privilege) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 18 with Privilege

use of org.openmrs.Privilege in project openmrs-core by openmrs.

the class PrivilegeEditor method setAsText.

/**
 * @should set using name
 * @should set using uuid
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    UserService es = Context.getUserService();
    if (StringUtils.hasText(text)) {
        try {
            Privilege p = es.getPrivilege(text);
            setValue(p);
            // when a privilege is not found, no exception is generated. throw one to execute the catch block
            if (p == null) {
                throw new Exception();
            }
        } catch (Exception ex) {
            Privilege p = es.getPrivilegeByUuid(text);
            setValue(p);
            if (p == null) {
                log.error("Error setting text: " + text, ex);
                throw new IllegalArgumentException("Privilege not found: " + ex.getMessage());
            }
        }
    } else {
        setValue(null);
    }
}
Also used : UserService(org.openmrs.api.UserService) Privilege(org.openmrs.Privilege)

Example 19 with Privilege

use of org.openmrs.Privilege in project openmrs-core by openmrs.

the class PrivilegeValidatorTest method validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace.

/**
 * @see PrivilegeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace() {
    Privilege priv = new Privilege();
    priv.setPrivilege("Wallhacking");
    priv.setDescription(null);
    Errors errors = new BindException(priv, "priv");
    new PrivilegeValidator().validate(priv, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    priv.setDescription("");
    errors = new BindException(priv, "priv");
    new PrivilegeValidator().validate(priv, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    priv.setDescription(" ");
    errors = new BindException(priv, "priv");
    new PrivilegeValidator().validate(priv, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Privilege(org.openmrs.Privilege) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 20 with Privilege

use of org.openmrs.Privilege in project openmrs-core by openmrs.

the class PrivilegeValidatorTest method validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues.

/**
 * @see PrivilegeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() {
    Privilege priv = new Privilege();
    priv.setPrivilege("Wallhacking");
    priv.setDescription("idspispopd");
    Errors errors = new BindException(priv, "priv");
    new PrivilegeValidator().validate(priv, errors);
    Assert.assertFalse(errors.hasErrors());
    Assert.assertNotNull(priv.getName());
    Assert.assertEquals(priv.getPrivilege(), "Wallhacking");
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Privilege(org.openmrs.Privilege) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Privilege (org.openmrs.Privilege)25 Test (org.junit.Test)15 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)15 Role (org.openmrs.Role)6 BindException (org.springframework.validation.BindException)5 Errors (org.springframework.validation.Errors)5 EncounterType (org.openmrs.EncounterType)4 User (org.openmrs.User)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Encounter (org.openmrs.Encounter)2 EncounterRole (org.openmrs.EncounterRole)2 APIException (org.openmrs.api.APIException)2 UserService (org.openmrs.api.UserService)2 Document (org.w3c.dom.Document)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 GlobalProperty (org.openmrs.GlobalProperty)1 AdministrationService (org.openmrs.api.AdministrationService)1