/** * @see UserService#getUserByUsername(String) */ @Test public void getUserByUsername_shouldGetUserByUsername() { User user = userService.getUserByUsername(ADMIN_USERNAME); assertNotNull("username not found " + ADMIN_USERNAME, 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 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 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 changePassword_shouldUpdatePasswordOfGivenUserWhenLoggedInUserHasEditUsersPasswordPrivilege() { User user = userService.getUserByUsername(ADMIN_USERNAME); assertNotNull("There needs to be a user with username 'admin' in the database", user); userService.changePassword(user, "testTest123"); Context.authenticate(user.getUsername(), "testTest123"); }
@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); }
@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); }
/** * @see EncounterService#canEditEncounter(Encounter, User) */ @Test public void canEditEncounter_shouldReturnFalseIfUserCanNotEditEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left user as is - i.e. without required privilege assertFalse(Context.getEncounterService().canEditEncounter(encounter, user)); }
@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 EncounterService#canViewEncounter(Encounter, User) */ @Test public void canViewEncounter_shouldReturnFalseIfUserCanNotViewEncounter() { // get encounter that has type with view privilege set Encounter encounter = getEncounterWithViewPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left user as is - i.e. without required privilege assertFalse(Context.getEncounterService().canViewEncounter(encounter, user)); }
@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 EncounterService#saveEncounter(Encounter) */ @Test(expected = APIException.class) public void saveEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call saveEncounter(Encounter) method Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS); Context.getEncounterService().saveEncounter(encounter); }
/** * @see EncounterService#purgeEncounter(Encounter) */ @Test(expected = APIException.class) public void purgeEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call purgeEncounter(Encounter) method Context.addProxyPrivilege(PrivilegeConstants.PURGE_ENCOUNTERS); Context.getEncounterService().purgeEncounter(encounter); }
/** * @see EncounterService#canViewAllEncounterTypes(User) */ @Test public void canViewAllEncounterTypes_shouldReturnTrueWhenTheEncounterTypesViewPrivilegeColumnIsNull() { EncounterService encounterService = Context.getEncounterService(); // set viewPrivilege on each encounter type to null for (EncounterType encounterType : encounterService.getAllEncounterTypes()) { encounterType.setViewPrivilege(null); encounterService.saveEncounterType(encounterType); } User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); assertTrue(encounterService.canViewAllEncounterTypes(user)); }
/** * @see EncounterService#voidEncounter(Encounter, String) */ @Test(expected = APIException.class) public void voidEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call voidEncounter(Encounter,String) method Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS); Context.getEncounterService().voidEncounter(encounter, "test"); }
/** * @see EncounterService#canEditAllEncounterTypes(User) */ @Test public void canViewAllEncounterTypes_shouldReturnTrueWhenTheEncounterTypesEditPrivilegeColumnIsNull() { EncounterService encounterService = Context.getEncounterService(); // set editPrivilege on each encounter type to null for (EncounterType encounterType : encounterService.getAllEncounterTypes()) { encounterType.setEditPrivilege(null); encounterService.saveEncounterType(encounterType); } User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); assertTrue(encounterService.canEditAllEncounterTypes(user)); }
/** * @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)); }
/** * @see EncounterService#canViewEncounter(Encounter, User) */ @Test public void canViewEncounter_shouldReturnTrueIfUserCanViewEncounter() { // get encounter that has type with view privilege set Encounter encounter = getEncounterWithViewPrivilege(); 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().getViewPrivilege()); user.addRole(role); assertTrue(Context.getEncounterService().canViewEncounter(encounter, user)); }
@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)); }