@Test
public void testExternalUserShouldBeUpdatedWhenExpired()
{

    // Check that there is no external user
    assertThat(strongboxUserManager.findByUsername(TEST_USER)).isNull();
    // Load and cache external user
    assertThat(userDetailsService.loadUserByUsername(TEST_USER)).isNotNull();
    // Check that external user cached
    User externalUser = strongboxUserManager.findByUsername(TEST_USER);
    String externalUserId = ((UserEntry)externalUser).getObjectId();
    assertThat(externalUser).isNotNull();
    assertThat(externalUserId).isNotNull();
    // Check that external user can't be modyfied
    User externalUserToSave = externalUser;
    assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> orientDbUserService.save(externalUserToSave))
                                                          .withMessageMatching("Can't modify external users.");
    // Update external password
    String newPassword = UUID.randomUUID().toString();
    UserDto userToUpdate = new UserDto();
    userToUpdate.setUsername(TEST_USER);
    userToUpdate.setPassword(newPassword);
    yamlUserService.updateAccountDetailsByUsername(new EncodedPasswordUser(userToUpdate, passwordEncoder));
    // Invalidate user cache
    expireUser(TEST_USER);
    // Update user cahce
    assertThat(userDetailsService.loadUserByUsername(TEST_USER)).isNotNull();
    // Cached user password should be updated
    externalUser = strongboxUserManager.findByUsername(TEST_USER);
    assertThat(externalUser).isNotNull();
    assertThat(passwordEncoder.matches(newPassword, externalUser.getPassword())).isTrue();
    // ID shouldn't be changed
    assertThat(((UserEntry)externalUser).getObjectId()).isEqualTo(externalUserId);
}