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); }
/** * @see UserService#hasDuplicateUsername(User) */ @Test public void hasDuplicateUsername_shouldVerifyThatUsernameAndSystemIdIsUnique() { executeDataSet(XML_FILENAME); User user = new User(); user.setSystemId("8-3"); user.setUsername("a unique username"); Assert.assertTrue(userService.hasDuplicateUsername(user)); user = new User(); user.setSystemId("a unique system id"); user.setUsername("userWithSha512Hash"); Assert.assertTrue(userService.hasDuplicateUsername(user)); }
@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 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); }
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")); }
@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"); }
@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); }
@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); }
/** * This test makes sure that the AuditableInterceptor is registered on the session * * @see AuditableInterceptor#onFlushDirty(Object,Serializable,Object[],Object[],String[],Type[]) */ @Test public void onFlushDirty_shouldBeCalledWhenSavingAnAuditable() { User u = Context.getUserService().getUser(1); u.setUsername("asdf"); Date beforeDate = u.getDateChanged(); Context.getUserService().saveUser(u); Date afterDate = u.getDateChanged(); Assert.assertNotSame(beforeDate, afterDate); }
/** * @see UserValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfEmailAsUsernameDisabledAndEmailProvided() { User user = new User(); user.setUsername("test@example.com"); AdministrationService as = Context.getAdministrationService(); as.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "false")); Errors errors = new BindException(user, "user"); validator.validate(user, errors); Assert.assertTrue(errors.hasFieldErrors("username")); }
/** * @see UserValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfEmailAsUsernameEnabledAndEmailInvalid() { User user = new User(); user.setUsername("test@example.com"); AdministrationService as = Context.getAdministrationService(); as.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "true")); Errors errors = new BindException(user, "user"); validator.validate(user, errors); Assert.assertFalse(errors.hasFieldErrors("username")); }
@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 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 changePasswordUsingActivationKey_shouldUpdatePasswordIfActivationKeyIsCorrect() { 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); final String PASSWORD = "Admin123"; Context.authenticate(createdUser.getUsername(), "Openmr5xy"); userService.changePasswordUsingActivationKey(key, PASSWORD); Context.authenticate(createdUser.getUsername(), PASSWORD); }
@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()); }