Search in sources :

Example 21 with User

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

the class UserServiceTest method getUsersByName_shouldFetchVoidedUsersWhenincludeVoidedIsTrue.

@Test
public void getUsersByName_shouldFetchVoidedUsersWhenincludeVoidedIsTrue() {
    User voidedUser = userService.getUser(501);
    // assertTrue(voidedUser.getVoided());
    // this generates an error:
    // org.hibernate.QueryException: illegal attempt to dereference
    // collection [user0_.user_id.names] with element property reference [givenName]
    // [from org.openmrs.User u where u.names.givenName = :givenName and u.names.familyName
    // = :familyName]
    List<User> users = userService.getUsersByName("Bruno", "Otterbourg", true);
    assertTrue(users.contains(voidedUser));
}
Also used : User(org.openmrs.User) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 22 with User

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

the class UserServiceTest method saveUser_shouldUpdateUsersUsername.

@Test
public void saveUser_shouldUpdateUsersUsername() {
    User u = userService.getUserByUsername(ADMIN_USERNAME);
    assertNotNull("There needs to be a user with username 'admin' in the database", u);
    u.setUsername("admin2");
    userService.saveUser(u);
    User u2 = userService.getUserByUsername("admin2");
    assertEquals("The fetched user should equal the user we tried to update", u, u2);
}
Also used : User(org.openmrs.User) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 23 with User

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

the class UserServiceTest method changePassword_shouldNotUpdatePasswordOfGivenUserWhenLoggedInUserDoesNotHaveEditUsersPasswordPrivilege.

@Test
public void changePassword_shouldNotUpdatePasswordOfGivenUserWhenLoggedInUserDoesNotHaveEditUsersPasswordPrivilege() {
    executeDataSet(XML_FILENAME_WITH_DATA_FOR_CHANGE_PASSWORD_ACTION);
    User user = userService.getUser(6001);
    assertFalse(user.hasPrivilege(PrivilegeConstants.EDIT_USER_PASSWORDS));
    Context.authenticate(user.getUsername(), "userServiceTest");
    expectedException.expect(APIAuthenticationException.class);
    expectedException.expectMessage(messages.getMessage("error.privilegesRequired", new Object[] { PrivilegeConstants.EDIT_USER_PASSWORDS }, null));
    userService.changePassword(user, "testTest123");
}
Also used : User(org.openmrs.User) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 24 with User

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

the class AdministrationServiceTest method getSearchLocales_shouldIncludeCurrentlySelectedFullLocaleAndLangugage.

/**
 * @see AdministrationService#getSearchLocales(User)
 */
@Test
public void getSearchLocales_shouldIncludeCurrentlySelectedFullLocaleAndLangugage() {
    // given
    Context.getAdministrationService().saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB"));
    User user = Context.getAuthenticatedUser();
    user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "");
    Context.getUserService().saveUser(user);
    Context.setLocale(new Locale("en", "GB"));
    // when
    List<Locale> searchLocales = Context.getAdministrationService().getSearchLocales();
    // then
    Assert.assertEquals(Context.getLocale(), searchLocales.get(0));
    Assert.assertEquals(new Locale(Context.getLocale().getLanguage()), searchLocales.get(1));
}
Also used : Locale(java.util.Locale) User(org.openmrs.User) GlobalProperty(org.openmrs.GlobalProperty) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 25 with User

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

the class UserValidator method validate.

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if retired and retireReason is null
 * @should fail validation if retired and retireReason is empty
 * @should fail validation if retired and retireReason is whitespace
 * @should pass validation if all required fields have proper values
 * @should fail validation if email as username enabled and email invalid
 * @should fail validation if email as username disabled and email provided
 * @should not throw NPE when user is null
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    User user = (User) obj;
    if (user == null) {
        errors.reject("error.general");
    } else {
        if (user.getRetired() && StringUtils.isBlank(user.getRetireReason())) {
            errors.rejectValue("retireReason", "error.null");
        }
        if (user.getPerson() == null) {
            errors.rejectValue("person", "error.null");
        } else {
            // check that required person details are filled out
            Person person = user.getPerson();
            if (person.getGender() == null) {
                errors.rejectValue("person.gender", "error.null");
            }
            if (person.getDead() == null) {
                errors.rejectValue("person.dead", "error.null");
            }
            if (person.getVoided() == null) {
                errors.rejectValue("person.voided", "error.null");
            }
            if (person.getPersonName() == null || StringUtils.isEmpty(person.getPersonName().getFullName())) {
                errors.rejectValue("person", "Person.names.length");
            }
            errors.pushNestedPath("person");
            try {
                personValidator.validate(person, errors);
            } finally {
                errors.popNestedPath();
            }
        }
        AdministrationService as = Context.getAdministrationService();
        boolean emailAsUsername;
        try {
            Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
            emailAsUsername = Boolean.parseBoolean(as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "false"));
        } finally {
            Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
        }
        if (emailAsUsername) {
            boolean isValidUserName = isUserNameAsEmailValid(user.getUsername());
            if (!isValidUserName) {
                errors.rejectValue("username", "error.username.email");
            }
        } else {
            boolean isValidUserName = isUserNameValid(user.getUsername());
            if (!isValidUserName) {
                errors.rejectValue("username", "error.username.pattern");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "username", "systemId", "retireReason");
    }
}
Also used : User(org.openmrs.User) AdministrationService(org.openmrs.api.AdministrationService) Person(org.openmrs.Person)

Aggregations

User (org.openmrs.User)201 Test (org.junit.Test)150 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)132 Date (java.util.Date)38 Person (org.openmrs.Person)33 Encounter (org.openmrs.Encounter)21 Patient (org.openmrs.Patient)18 PersonName (org.openmrs.PersonName)17 Role (org.openmrs.Role)13 GlobalProperty (org.openmrs.GlobalProperty)11 Location (org.openmrs.Location)11 ArrayList (java.util.ArrayList)10 EncounterType (org.openmrs.EncounterType)10 Locale (java.util.Locale)7 UserService (org.openmrs.api.UserService)7 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)7 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 EncounterRole (org.openmrs.EncounterRole)6 PatientIdentifier (org.openmrs.PatientIdentifier)6