/** * @see org.openmrs.api.UserService#hasDuplicateUsername(org.openmrs.User) */ @Override @Transactional(readOnly = true) public boolean hasDuplicateUsername(User user) throws APIException { return dao.hasDuplicateUsername(user.getUsername(), user.getSystemId(), user.getUserId()); }
/** * Checks whether user is Daemon. * However this is not the preferred method for checking to see if the current thread is a daemon thread, * rather use Daemon.isDeamonThread(). * isDaemonThread is preferred for checking to see if you are in that thread or if the current thread is daemon. * * @param user user whom we are checking if daemon * @return true if user is Daemon * @should return true for a daemon user * @should return false if the user is not a daemon */ public static boolean isDaemonUser(User user) { return DAEMON_USER_UUID.equals(user.getUuid()); }
private void invokeValidateAndAssertHasErrorRetireReason(String invalidRetireReason) { User user = new User(); user.setUsername("test"); user.setRetireReason(invalidRetireReason); user.setRetired(true); Errors errors = new BindException(user, "user"); validator.validate(user, errors); Assert.assertTrue(errors.hasFieldErrors("retireReason")); }
/** * @see org.openmrs.Attributable#getDisplayString() */ @Override public String getDisplayString() { String returnString = ""; if (getPersonName() != null) { returnString += getPersonName().getFullName() + " "; } returnString += "(" + getUsername() + ")"; return returnString; }
private void updatePassword(User user, String newPassword) { OpenmrsUtil.validatePassword(user.getUsername(), newPassword, user.getSystemId()); dao.changePassword(user, newPassword); }
@Test public void saveUser_shouldFailToCreateTheUserWithAWeakPassword() { assertTrue("The context needs to be correctly authenticated to by a user", Context.isAuthenticated()); UserService us = userService; User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); expectedException.expect(PasswordException.class); us.createUser(u, "short"); }
Context.clearSession(); User user = new User(personToMakeUser); user.setUsername("bwolfe"); user.setSystemId("asdf"); user.addRole(new Role("Some Role", "This is a test role")); //included in xml file assertTrue(user.getUsername().equals("bwolfe")); assertTrue(user.hasRole("Some Role")); Assert.assertNotNull("User was not created", userService.getUser(user.getUserId())); Integer shouldCreateUserWhoIsPatientAlreadyTestUserIdCreated = user.getUserId(); assertNotNull("Uh oh, the username was not saved", fetchedUser.getUsername()); assertTrue("Uh oh, the username was not saved", fetchedUser.getUsername().equals("bwolfe")); assertTrue("Uh oh, the role was not assigned", fetchedUser.hasRole("Some Role"));
@Test public void createUser_shouldNotAllowSystemIdEqualsExistingUsername() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setSystemId(someUser.getUsername()); expectedException.expect(DAOException.class); expectedException.expectMessage( String.format("Username %s or system id %s is already in use.", newUser.getUsername(), newUser.getSystemId())); userService.createUser(newUser, SOME_VALID_PASSWORD); }
@Test public void createUser_shouldNotAllowSystemIdEqualsUsernameWithLuhnCheckDigit() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setUsername(someUser.getUsername()); newUser.setSystemId(decorateWithLuhnIdentifier(someUser.getUsername())); expectedException.expect(DAOException.class); expectedException.expectMessage( String.format("Username %s or system id %s is already in use.", newUser.getUsername(), newUser.getSystemId())); userService.createUser(newUser, SOME_VALID_PASSWORD); }
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 { Person person = user.getPerson(); if (person.getGender() == null) { errors.rejectValue("person.gender", "error.null"); boolean isValidUserName = isUserNameAsEmailValid(user.getUsername()); if (!isValidUserName) { errors.rejectValue("username", "error.username.email"); boolean isValidUserName = isUserNameValid(user.getUsername()); if (!isValidUserName) { errors.rejectValue("username", "error.username.pattern"); if (!StringUtils.isBlank(user.getEmail())) { if(!isEmailValid(user.getEmail())) { errors.rejectValue("email", "error.email.invalid");
/** * @see UserValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() { User user = new User(); user.setUsername("test"); user.setRetired(true); user.setRetireReason("for the lulz"); user.setPerson(new Person(999)); user.getPerson().addName(new PersonName("Users", "Need", "People")); user.getPerson().setGender("F"); Errors errors = new BindException(user, "user"); validator.validate(user, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see ContextDAO#authenticate(String,String) */ @Test public void authenticate_shouldAuthenticateGivenSystemIdAndPassword() { User u = dao.authenticate("1-8", "test"); Assert.assertEquals("Should be the 1-8 user", "1-8", u.getSystemId()); }
public void authenticateMockUser() { User user = new User(); user.setUuid("1010d442-e134-11de-babe-001e378eb67e"); user.setUserId(1); user.setUsername("admin"); user.addRole(new Role(RoleConstants.SUPERUSER)); Person person = new Person(); person.setUuid("6adb7c42-cfd2-4301-b53b-ff17c5654ff7"); person.setId(1); person.addName(new PersonName("Bob", "", "Smith")); Calendar calendar = Calendar.getInstance(); calendar.set(1980, 01, 01); person.setBirthdate(calendar.getTime()); person.setGender("M"); user.setPerson(person); when(userContext.getAuthenticatedUser()).thenReturn(user); when(userContext.isAuthenticated()).thenReturn(true); }
@Test public void createUser_shouldNotAllowUsernameEqualsExistingSystemId() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setUsername(someUser.getSystemId()); 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); }
String output = "<update tableName=\"users\"><column name=\"uuid\" value=\"" + user.getUuid() + "\"/><where> user_id = '" + user.getUserId() + "' and system_id = '" + user.getSystemId().replace("'", "\\'") + "'</where></update>"; System.out.println(output);
@Test public void changePasswordUsingSecretAnswer_shouldUpdatePasswordIfSecretIsCorrect() { 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"); userService.changePasswordUsingSecretAnswer("answer", "userServiceTest2"); Context.authenticate(user.getUsername(), "userServiceTest2"); }
/** * @see UserController#retireUser(User,String,WebRequest) * @throws Exception * @verifies void a patient */ @Test public void retireUser_shouldRetireAUser() throws Exception { User user = service.getUserByUuid(getUuid()); Assert.assertFalse(user.isRetired()); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + user.getUuid()); req.addParameter("!purge", ""); req.addParameter("reason", "unit test"); handle(req); User retiredUser = service.getUserByUuid(getUuid()); Assert.assertTrue(retiredUser.isRetired()); Assert.assertEquals("unit test", retiredUser.getRetireReason()); }
/** * @since 1.5 * @see org.openmrs.OpenmrsObject#getId() */ @Override public Integer getId() { return getUserId(); }
@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"); }
/** * @see org.openmrs.Attributable#hydrate(java.lang.String) */ @Override public User hydrate(String userId) { try { return Context.getUserService().getUser(Integer.valueOf(userId)); } catch (Exception e) { return new User(); } }