/** * @see org.openmrs.Attributable#getPossibleValues() */ @Override public List<User> getPossibleValues() { try { return Context.getUserService().getAllUsers(); } catch (Exception e) { return Collections.emptyList(); } }
@Override protected User getObjectByUuid(String uuid) { return Context.getUserService().getUserByUuid(uuid); } }
/** * @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 { Role r = es.getRole(text); setValue(r); //when a role is not found, no exception is generated. throw one to execute the catch block if (r == null) { throw new Exception(); } } catch (Exception ex) { Role r = es.getRoleByUuid(text); setValue(r); if (r == null) { log.error("Error setting text: " + text, ex); throw new IllegalArgumentException("Role not found: " + ex.getMessage()); } } } else { setValue(null); } }
/** * Test changing a user's password multiple times in the same transaction * * @see UserService#changePassword(String,String) */ @Test public void changePassword_shouldBeAbleToUpdatePasswordMultipleTimes() { User u = userService.getUserByUsername(ADMIN_USERNAME); assertNotNull("There needs to be a user with username 'admin' in the database", u); userService.changePassword("test", "Tester12"); userService.changePassword("Tester12", "Tester13"); }
@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)); }
@Test public void saveUser_shouldGrantNewRolesInRolesListToUser() { // add in some basic properties executeDataSet(XML_FILENAME); User u = userService.getUserByUsername(ADMIN_USERNAME); Role role1 = new Role(); role1.setDescription("testing1"); role1.setRole("test1"); Privilege p1 = userService.getAllPrivileges().get(0); Set<Privilege> privileges1 = new HashSet<>(); privileges1.add(p1); role1.setPrivileges(privileges1); Role role2 = new Role(); role2.setDescription("testing2"); role2.setRole("test2"); Privilege p2 = userService.getAllPrivileges().get(0); Set<Privilege> privileges2 = new HashSet<>(); privileges2.add(p2); role2.setPrivileges(privileges2); userService.saveUser(u.addRole(role1)); userService.saveUser(u.addRole(role2)); // so the contents are fetched from the db Context.evictFromSession(u); userService.getUser(u.getUserId()).hasRole("test1"); userService.getUser(u.getUserId()).hasRole("test2"); }
@Test(expected = ContextAuthenticationException.class) public void authenticate_shouldThrowAContextAuthenticationExceptionIfUsernameIsAnEmptyString() { //update a user with a username that is an empty string for this test UserService us = Context.getUserService(); User u = us.getUser(1); u.setUsername(""); u.getPerson().setGender("M"); us.saveUser(u); dao.authenticate("", "password"); }
/** * @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)); }
@Test public void createUser_shouldNotAllowExistingUser() { User someUser = userService.getUserByUsername(SOME_USERNAME); expectedException.expect(APIException.class); expectedException.expectMessage("This method can be used for only creating new users"); userService.createUser(someUser, SOME_VALID_PASSWORD); }
/** * @see EncounterService#canEditEncounter(Encounter, User) */ @Test public void canEditEncounter_shouldReturnTrueIfUserCanEditEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // add required privilege to role in which this user is Role role = Context.getUserService().getRole("Provider"); role.addPrivilege(encounter.getEncounterType().getEditPrivilege()); user.addRole(role); assertTrue(Context.getEncounterService().canEditEncounter(encounter, user)); }
User preliminaryFetchedUser = userService.getUser(2); assertNull(preliminaryFetchedUser); userService.createUser(user, "Openmr5xy"); Assert.assertNotNull("User was not created", userService.getUser(user.getUserId())); User fetchedUser = userService.getUser(shouldCreateUserWhoIsPatientAlreadyTestUserIdCreated); User fetchedUser3 = userService.getUser(3); if (fetchedUser3 != null) { throw new RuntimeException("There is a user with id #3"); List<User> allUsers = userService.getAllUsers(); assertEquals(10, allUsers.size());
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ROLES); Set<String> currentRoleNames = new HashSet<>(); for (Role role : Context.getUserService().getAllRoles()) { currentRoleNames.add(role.getRole().toUpperCase()); role.setRole(roleName); role.setDescription(entry.getValue()); Context.getUserService().saveRole(role); Context.addProxyPrivilege(PrivilegeConstants.MANAGE_PRIVILEGES); Set<String> currentPrivilegeNames = new HashSet<>(); for (Privilege privilege : Context.getUserService().getAllPrivileges()) { currentPrivilegeNames.add(privilege.getPrivilege().toUpperCase()); p.setPrivilege(privilegeName); p.setDescription(entry.getValue()); Context.getUserService().savePrivilege(p);
@Test public void createUser_shouldNotAllowForDuplicatedUsername() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setUsername(someUser.getUsername()); expectedException.expect(DAOException.class); expectedException.expectMessage( String.format("Username %s or system id %s is already in use.", newUser.getUsername(), Context.getUserService().generateSystemId())); userService.createUser(newUser, SOME_VALID_PASSWORD); }
/** * @see UserService#changeHashedPassword(User,String,String) */ @Test public void changeHashedPassword_shouldChangeTheHashedPasswordForTheGivenUser() { User user = userService.getUser(1); String salt = Security.getRandomToken(); String hash = Security.encodeString("new password" + salt); userService.changeHashedPassword(user, hash, salt); // TODO Review this a little further // This is the assert - checks to see if current user can use the new password userService.changePassword("new password", "Another new password1"); // try to change the password with the new one }
@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); }
@Test @SkipBaseSetup public void getUserByUuid_shouldFetchUserWithGivenUuid() throws SQLException { initializeInMemoryDatabase(); executeDataSet(XML_FILENAME); authenticate(); User user = userService.getUserByUuid("013c49c6-e132-11de-babe-001e378eb67e"); assertEquals("Did not fetch user with given uuid", user, userService.getUser(5505)); }
/** * @see UserService#changePassword(User,String,String) */ @Test public void changePassword_shouldChangePasswordForGivenUserIfOldPasswordIsNullAndChangingUserHavePrivileges() { executeDataSet(XML_FILENAME_WITH_DATA_FOR_CHANGE_PASSWORD_ACTION); //user 6001 has password userServiceTest User user6001 = userService.getUser(6001); String oldPassword = null; String newPassword = "newPasswordString123"; userService.changePassword(user6001, oldPassword, newPassword); Context.authenticate(user6001.getUsername(), newPassword); }
/** * @see UserService#getRoleByUuid(String) */ @Test public void getRoleByUuid_shouldReturnNullIfNoObjectFoundWithGivenUuid() { Assert.assertNull(userService.getRoleByUuid("some invalid uuid")); }
@Test public void getSearchLocales_shouldExcludeNotAllowedLocales() { //given adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_US, en_GB, pl, es")); User user = Context.getAuthenticatedUser(); user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "es_CL, en_US, pl"); Context.getUserService().saveUser(user); //when List<Locale> searchLocales = adminService.getSearchLocales(); //then assertTrue("en_US", searchLocales.contains(new Locale("en", "US"))); assertTrue("pl", searchLocales.contains(new Locale("pl"))); assertTrue("es", searchLocales.contains(new Locale("es"))); assertFalse("es_CL", searchLocales.contains(new Locale("es", "CL"))); }
/** * @see UserService#getUsers(String, List, boolean, Integer, Integer) */ @Test public void getUsers_shouldReturnUsersWhoseRolesInheritRequestedRoles() { executeDataSet(XML_FILENAME); List<Role> roles = new ArrayList<>(); roles.add(userService.getRole("Parent")); Assert.assertEquals(3, userService.getUsers(null, roles, true, null, null).size()); }