content: package com.konsol.beatstream.service;
  
  import static org.assertj.core.api.Assertions.assertThat;
  
  import com.konsol.beatstream.IntegrationTest;
  import com.konsol.beatstream.domain.User;
  import com.konsol.beatstream.repository.UserRepository;
  import java.time.Instant;
  import java.time.temporal.ChronoUnit;
  import java.util.List;
  import java.util.Objects;
  import java.util.Optional;
  import org.apache.commons.lang3.RandomStringUtils;
  import org.junit.jupiter.api.AfterEach;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.cache.Cache;
  import org.springframework.cache.CacheManager;
  import tech.jhipster.security.RandomUtil;
  
  /**
   * Integration tests for {@link UserService}.
   */
  @IntegrationTest
  class UserServiceIT {
  
      private static final String DEFAULT_LOGIN = "johndoe_service";
  
      private static final String DEFAULT_EMAIL = "johndoe_service@localhost";
  
      private static final String DEFAULT_FIRSTNAME = "john";
  
      private static final String DEFAULT_LASTNAME = "doe";
  
      @Autowired
      private CacheManager cacheManager;
  
      private static final String DEFAULT_IMAGEURL = "http://placehold.it/50x50";
  
      private static final String DEFAULT_LANGKEY = "dummy";
  
      @Autowired
      private UserRepository userRepository;
  
      @Autowired
      private UserService userService;
  
      private User user;
  
      @BeforeEach
      public void init() {
          user = new User();
          user.setLogin(DEFAULT_LOGIN);
          user.setPassword(RandomStringUtils.randomAlphanumeric(60));
          user.setActivated(true);
          user.setEmail(DEFAULT_EMAIL);
          user.setFirstName(DEFAULT_FIRSTNAME);
          user.setLastName(DEFAULT_LASTNAME);
          user.setImageUrl(DEFAULT_IMAGEURL);
          user.setLangKey(DEFAULT_LANGKEY);
      }
  
      @AfterEach
      public void cleanupAndCheck() {
          cacheManager
              .getCacheNames()
              .stream()
              .map(cacheName -> this.cacheManager.getCache(cacheName))
              .filter(Objects::nonNull)
              .forEach(Cache::clear);
          userRepository.deleteAll();
      }
  
      @Test
      void assertThatUserMustExistToResetPassword() {
          userRepository.save(user);
          Optional<User> maybeUser = userService.requestPasswordReset("invalid.login@localhost");
          assertThat(maybeUser).isNotPresent();
  
          maybeUser = userService.requestPasswordReset(user.getEmail());
          assertThat(maybeUser).isPresent();
          assertThat(maybeUser.orElse(null).getEmail()).isEqualTo(user.getEmail());
          assertThat(maybeUser.orElse(null).getResetDate()).isNotNull();
          assertThat(maybeUser.orElse(null).getResetKey()).isNotNull();
      }
  
      @Test
      void assertThatOnlyActivatedUserCanRequestPasswordReset() {
          user.setActivated(false);
          userRepository.save(user);
  
          Optional<User> maybeUser = userService.requestPasswordReset(user.getLogin());
          assertThat(maybeUser).isNotPresent();
          userRepository.delete(user);
      }
  
      @Test
      void assertThatResetKeyMustNotBeOlderThan24Hours() {
          Instant daysAgo = Instant.now().minus(25, ChronoUnit.HOURS);
          String resetKey = RandomUtil.generateResetKey();
          user.setActivated(true);
          user.setResetDate(daysAgo);
          user.setResetKey(resetKey);
          userRepository.save(user);
  
          Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
          assertThat(maybeUser).isNotPresent();
          userRepository.delete(user);
      }
  
      @Test
      void assertThatResetKeyMustBeValid() {
          Instant daysAgo = Instant.now().minus(25, ChronoUnit.HOURS);
          user.setActivated(true);
          user.setResetDate(daysAgo);
          user.setResetKey("1234");
          userRepository.save(user);
  
          Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
          assertThat(maybeUser).isNotPresent();
          userRepository.delete(user);
      }
  
      @Test
      void assertThatUserCanResetPassword() {
          String oldPassword = user.getPassword();
          Instant daysAgo = Instant.now().minus(2, ChronoUnit.HOURS);
          String resetKey = RandomUtil.generateResetKey();
          user.setActivated(true);
          user.setResetDate(daysAgo);
          user.setResetKey(resetKey);
          userRepository.save(user);
  
          Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
          assertThat(maybeUser).isPresent();
          assertThat(maybeUser.orElse(null).getResetDate()).isNull();
          assertThat(maybeUser.orElse(null).getResetKey()).isNull();
          assertThat(maybeUser.orElse(null).getPassword()).isNotEqualTo(oldPassword);
  
          userRepository.delete(user);
      }
  
      @Test
      void assertThatNotActivatedUsersWithNotNullActivationKeyCreatedBefore3DaysAreDeleted() {
          Instant now = Instant.now();
          user.setActivated(false);
          user.setActivationKey(RandomStringUtils.random(20));
          User dbUser = userRepository.save(user);
          dbUser.setCreatedDate(now.minus(4, ChronoUnit.DAYS));
          userRepository.save(user);
          Instant threeDaysAgo = now.minus(3, ChronoUnit.DAYS);
          List<User> users = userRepository.findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(threeDaysAgo);
          assertThat(users).isNotEmpty();
          userService.removeNotActivatedUsers();
          users = userRepository.findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(threeDaysAgo);
          assertThat(users).isEmpty();
      }
  
      @Test
      void assertThatNotActivatedUsersWithNullActivationKeyCreatedBefore3DaysAreNotDeleted() {
          Instant now = Instant.now();
          user.setActivated(false);
          User dbUser = userRepository.save(user);
          dbUser.setCreatedDate(now.minus(4, ChronoUnit.DAYS));
          userRepository.save(user);
          Instant threeDaysAgo = now.minus(3, ChronoUnit.DAYS);
          List<User> users = userRepository.findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(threeDaysAgo);
          assertThat(users).isEmpty();
          userService.removeNotActivatedUsers();
          Optional<User> maybeDbUser = userRepository.findById(dbUser.getId());
          assertThat(maybeDbUser).contains(dbUser);
      }
  }
  