private User userWithValidPerson() { Person person = new Person(); person.addName(new PersonName("jane", "sue", "doe")); person.setGender("F"); return new User(person); }
@Test public void shouldClearMillisecondsWhenSavingANewObject() { Date dateWithMillisecond = new Date(567L); Date dateWithoutMillisecond = new Date(0L); Person person = new Person(); person.addName(new PersonName("Alice", null, "Paul")); person.setGender("F"); person.setBirthdate(dateWithMillisecond); personService.savePerson(person); Context.flushSession(); assertThat(person.getBirthdate(), is(dateWithoutMillisecond)); }
private Person createPersonWithNameAndAddress() { Person person = new Person(); person.setGender("M"); PersonName name = new PersonName("givenName", "middleName", "familyName"); person.addName(name); PersonAddress address = new PersonAddress(); address.setAddress1("some address"); person.addAddress(address); return person; } }
@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 org.openmrs.validator.PersonValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { Person person = new Person(1); person.setBirthdate(new Date()); person.setGender("g"); person.setPersonVoided(true); person.setPersonVoidReason("voidReason"); Errors errors = new BindException(person, "person"); PersonValidator personValidator = new PersonValidator(); personValidator.validate(person, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see PersonService#savePerson(Person) */ @Test public void savePerson_shouldUpdateExistingObjectWhenPersonIdIsNotNull() throws Exception { Person personSaved = Context.getPersonService().getPerson(1); Assert.assertNotNull(personSaved.getId()); personSaved.setGender("M"); Person personUpdated = Context.getPersonService().savePerson(personSaved); Assert.assertEquals("M", personUpdated.getGender()); }
/** * @see PersonSaveHandler#handle(Person,User, Date,String) */ @Test public void handle_shouldIgnoreBlankAddresses() { PersonSaveHandler handler = new PersonSaveHandler(); Person person = new Person(); PersonName personName = new PersonName("John","","Smith"); person.addName(personName); person.setGender("M"); PersonAddress personAddress = new PersonAddress(); personAddress.setAddress1(" "); person.addAddress(personAddress); handler.handle(person,null,null,null); Assert.assertEquals(0,person.getAddresses().size()); } }
/** * @see PersonService#savePerson(Person) */ @Test public void savePerson_shouldCreateNewObjectWhenPersonIdIsNull() throws Exception { User user = Context.getAuthenticatedUser(); Person person = new Person(); person.setPersonCreator(user); person.setPersonDateCreated(new Date()); person.setPersonChangedBy(user); person.setPersonDateChanged(new Date()); person.setGender("F"); Assert.assertNull(person.getId()); person.addName(new PersonName("givenName", "middleName", "familyName")); Person personSaved = Context.getPersonService().savePerson(person); Assert.assertNotNull(personSaved.getId()); }
/** * @see org.openmrs.validator.PersonValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { Person person = new Person(1); person.setBirthdate(new Date()); person.setGender("too long text too long too long text too long text too long text"); person.setPersonVoided(true); person .setPersonVoidReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); Errors errors = new BindException(person, "person"); PersonValidator personValidator = new PersonValidator(); personValidator.validate(person, errors); Assert.assertTrue(errors.hasFieldErrors("gender")); Assert.assertTrue(errors.hasFieldErrors("personVoidReason")); }
@Test public void getUserByActivationKey_shouldGetUserByActivationKey(){ User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); User createdUser = userService.createUser(u, "Openmr5xy"); String key="h4ph0fpNzQCIPSw8plJI"; int validTime = 10*60*1000; //equivalent to 10 minutes for token to be valid Long tokenTime = System.currentTimeMillis() + validTime; LoginCredential credentials = dao.getLoginCredential(createdUser); credentials.setActivationKey("b071c88d6d877922e35af2e6a90dd57d37ac61143a03bb986c5f353566f3972a86ce9b2604c31a22dfa467922dcfd54fa7d18b0a7c7648d94ca3d97a88ea2fd0:"+tokenTime); dao.updateLoginCredential(credentials); assertEquals(createdUser, userService.getUserByActivationKey(key)); }
@Test public void getUserByActivationKey_shouldReturnNullIfTokenTimeExpired(){ User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); User createdUser = userService.createUser(u, "Openmr5xy"); String key="h4ph0fpNzQCIPSw8plJI"; int validTime = 10*60*1000; //equivalent to 10 minutes for token to be valid Long tokenTime = System.currentTimeMillis() - validTime; LoginCredential credentials = dao.getLoginCredential(createdUser); credentials.setActivationKey("b071c88d6d877922e35af2e6a90dd57d37ac61143a03bb986c5f353566f3972a86ce9b2604c31a22dfa467922dcfd54fa7d18b0a7c7648d94ca3d97a88ea2fd0:"+tokenTime); dao.updateLoginCredential(credentials); assertNull(userService.getUserByActivationKey(key)); }
@Test public void validate_shouldNotFailWhenDeathCauseNotCodedIsSet() { Person person = new Patient(1); person.setDead(true); person.setCauseOfDeathNonCoded("Some text describing Cause of Death"); person.setPersonVoided(true); person.setPersonVoidReason("voidReason"); person.setGender("g"); Errors errors = new BindException(person, "patient"); PersonValidator personValidator = new PersonValidator(); personValidator.validate(person, errors); Assert.assertFalse(errors.hasErrors()); }
@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"); }
@Test public void onSave_shouldPopulatePersonChangedByandPersonDateChangedIfPersonAlreadyExists() { Person person = Context.getPersonService().getPerson(1); Assert.assertNull(person.getPersonChangedBy()); Assert.assertNull(person.getPersonDateChanged()); person.setGender("F"); Context.flushSession(); Context.getPersonService().savePerson(person); Assert.assertNotNull(person.getPersonChangedBy()); Assert.assertNotNull(person.getPersonDateChanged()); }
/** * @see PersonService#savePerson(Person) */ @Test public void savePerson_shouldSetThePreferredNameAndAddressIfNoneIsSpecified() throws Exception { Person person = new Person(); person.setGender("M"); PersonName name = new PersonName("givenName", "middleName", "familyName"); person.addName(name); PersonAddress address = new PersonAddress(); address.setAddress1("some address"); person.addAddress(address); personService.savePerson(person); assertTrue(name.getPreferred()); assertTrue(address.getPreferred()); }
@Test public void changePasswordUsingActivationKey_shouldNotUpdatePasswordIfActivationKeyIsIncorrect() { User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); User createdUser = userService.createUser(u, "Openmr5xy"); String key = "wrongactivationkeyin"; Context.authenticate(createdUser.getUsername(), "Openmr5xy"); expectedException.expect(InvalidActivationKeyException.class); expectedException.expectMessage(messages.getMessage("activation.key.not.correct")); userService.changePasswordUsingActivationKey(key, "Pa55w0rd"); }
/** * @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 UserValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { User user = new User(); user.setUsername("test"); user.setSystemId("systemId"); user.setRetireReason("retireReason"); 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()); }
@Test public void createUser_shouldCreateNewUserWithBasicElements() { assertTrue("The context needs to be correctly authenticated to by a user", Context.isAuthenticated()); User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); User createdUser = userService.createUser(u, "Openmr5xy"); // if we're returning the object from create methods, check validity assertTrue("The user returned by the create user method should equal the passed in user", createdUser.equals(u)); createdUser = userService.getUserByUsername("bwolfe"); assertTrue("The created user should equal the passed in user", createdUser.equals(u)); }
@Ignore("TRUNK-5425") @Test public void setUserActivationKey_shouldCreateUserActivationKey() throws MessageException { User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); Context.getAdministrationService().setGlobalProperty(OpenmrsConstants.GP_HOST_URL, "http://localhost:8080/openmrs/admin/users/changePassword.form/{activationKey}"); User createdUser = userService.createUser(u, "Openmr5xy"); assertNull(dao.getLoginCredential(createdUser).getActivationKey()); assertEquals(createdUser, userService.setUserActivationKey(createdUser)); assertNotNull(dao.getLoginCredential(createdUser).getActivationKey()); }