@Test public void createUser_shouldNotAllowNullPassword() { User unsavedUser = new User(); expectedException.expect(ValidationException.class); userService.createUser(unsavedUser, null); }
@Test public void createUser_shouldNotAllowBlankPassword() { User unsavedUser = new User(); expectedException.expect(ValidationException.class); userService.createUser(unsavedUser, ""); }
@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); }
@Test public void createUser_shouldNotAllowDuplicatedSystemId() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setSystemId(someUser.getSystemId()); 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); }
/** * @see PatientService#voidPatient(Patient,String) */ @Test public void voidPatient_shouldRetireUsers() throws Exception { //given Patient patient = patientService.getPatient(2); User user = new User(patient); Context.getUserService().createUser(user, "Admin123"); Assert.assertFalse(Context.getUserService().getUsersByPerson(patient, false).isEmpty()); //when patientService.voidPatient(patient, "reason"); //then Assert.assertTrue(Context.getUserService().getUsersByPerson(patient, false).isEmpty()); }
@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_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); }
@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 PatientService#unvoidPatient(Patient) */ @Test public void unvoidPatient_shouldNotUnretireUsers() throws Exception { //given Patient patient = patientService.getPatient(2); User user = new User(patient); Context.getUserService().createUser(user, "Admin123"); patientService.voidPatient(patient, "reason"); //when patientService.unvoidPatient(patient); //then Assert.assertTrue(Context.getUserService().getUsersByPerson(patient, false).isEmpty()); }
/** * @see PersonService#voidPerson(Person,String) */ @Test public void voidPerson_shouldRetireUsers() throws Exception { //given Person person = personService.getPerson(2); User user = new User(person); Context.getUserService().createUser(user, "Admin123"); Assert.assertFalse(Context.getUserService().getUsersByPerson(person, false).isEmpty()); //when personService.voidPerson(person, "reason"); //then assertTrue(Context.getUserService().getUsersByPerson(person, false).isEmpty()); }
/** * @see PersonService#unvoidPerson(Person) */ @Test public void unvoidPerson_shouldNotUnretireUsers() throws Exception { //given Person person = personService.getPerson(2); User user = new User(person); Context.getUserService().createUser(user, "Admin123"); personService.voidPerson(person, "reason"); //when personService.unvoidPerson(person); //then assertTrue(Context.getUserService().getUsersByPerson(person, false).isEmpty()); }
@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); }
/** * @see PersonVoidHandler#handle(Person,User,Date,String) */ @Test public void handle_shouldRetireUsers() { //given VoidHandler<Person> handler = new PersonVoidHandler(); Person person = Context.getPersonService().getPerson(2); User user = new User(person); Context.getUserService().createUser(user, "Admin123"); Assert.assertFalse(Context.getUserService().getUsersByPerson(person, false).isEmpty()); //when handler.handle(person, null, null, "reason"); //then Assert.assertTrue(Context.getUserService().getUsersByPerson(person, false).isEmpty()); }
/** * @see AuditableInterceptor#onSave(Object,Serializable,Object[],String[],Type[]) */ @Test public void onSave_shouldBeCalledWhenSavingOpenmrsObject() { User u = new User(); u.setSystemId("user"); u.setPerson(Context.getPersonService().getPerson(1)); Context.getUserService().createUser(u, "Admin123"); Assert.assertSame(Context.getAuthenticatedUser(), u.getCreator()); Assert.assertNotNull(u.getDateCreated()); }
@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 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 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"); }
@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()); }