Testing User Details Service (#2)
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.domain;
|
package net.shyshkin.study.fullstack.supportportal.backend.domain;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@ -10,6 +11,7 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@ToString
|
||||||
public class UserPrincipal implements UserDetails {
|
public class UserPrincipal implements UserDetails {
|
||||||
|
|
||||||
private final User user;
|
private final User user;
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
class SupportPortalBackendApplicationTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package net.shyshkin.study.fullstack.supportportal.backend.common;
|
||||||
|
|
||||||
|
import com.github.javafaker.Faker;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ActiveProfiles("local")
|
||||||
|
public abstract class BaseUserTest {
|
||||||
|
|
||||||
|
public static final Faker FAKER = Faker.instance();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
protected UserRepository userRepository;
|
||||||
|
|
||||||
|
protected User user;
|
||||||
|
|
||||||
|
protected User createRandomUser() {
|
||||||
|
return User.builder()
|
||||||
|
.email(FAKER.bothify("????##@example.com"))
|
||||||
|
.firstName(FAKER.name().firstName())
|
||||||
|
.lastName(FAKER.name().lastName())
|
||||||
|
.username(FAKER.name().username())
|
||||||
|
.password("bad_password")
|
||||||
|
.userId(UUID.randomUUID().toString())
|
||||||
|
.isActive(true)
|
||||||
|
.isNotLocked(true)
|
||||||
|
.joinDate(LocalDateTime.now())
|
||||||
|
.profileImageUrl("http://url_to_profile_img")
|
||||||
|
.lastLoginDate(LocalDateTime.now())
|
||||||
|
.lastLoginDateDisplay(LocalDateTime.now())
|
||||||
|
.roles(new String[]{"ROLE_ADMIN", "ROLE_USER"})
|
||||||
|
.authorities(new String[]{"user:delete", "user:read"})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,30 +1,16 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.domain;
|
package net.shyshkin.study.fullstack.supportportal.backend.domain;
|
||||||
|
|
||||||
import com.github.javafaker.Faker;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository;
|
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@SpringBootTest
|
class UserPrincipalTest extends BaseUserTest {
|
||||||
@ActiveProfiles("local")
|
|
||||||
class UserPrincipalTest {
|
|
||||||
|
|
||||||
public static final Faker FAKER = Faker.instance();
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
UserRepository userRepository;
|
|
||||||
private User user;
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
@ -57,23 +43,4 @@ class UserPrincipalTest {
|
|||||||
.hasSize(expectedAuthoritiesLength)
|
.hasSize(expectedAuthoritiesLength)
|
||||||
.satisfies(authorities -> log.debug("Authorities: {}", authorities)));
|
.satisfies(authorities -> log.debug("Authorities: {}", authorities)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private User createRandomUser() {
|
|
||||||
return User.builder()
|
|
||||||
.email(FAKER.bothify("????##@example.com"))
|
|
||||||
.firstName(FAKER.name().firstName())
|
|
||||||
.lastName(FAKER.name().lastName())
|
|
||||||
.username(FAKER.name().username())
|
|
||||||
.password("bad_password")
|
|
||||||
.userId(UUID.randomUUID().toString())
|
|
||||||
.isActive(true)
|
|
||||||
.isNotLocked(true)
|
|
||||||
.joinDate(LocalDateTime.now())
|
|
||||||
.profileImageUrl("http://url_to_profile_img")
|
|
||||||
.lastLoginDate(LocalDateTime.now())
|
|
||||||
.lastLoginDateDisplay(LocalDateTime.now())
|
|
||||||
.roles(new String[]{"ROLE_ADMIN", "ROLE_USER"})
|
|
||||||
.authorities(new String[]{"user:delete", "user:read"})
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
package net.shyshkin.study.fullstack.supportportal.backend.service;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
||||||
|
import org.assertj.core.api.ThrowableAssert;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
class UserServiceTest extends BaseUserTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserService userService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void loadUserByUsername_present() throws InterruptedException {
|
||||||
|
|
||||||
|
//given
|
||||||
|
User fakeUser = createRandomUser();
|
||||||
|
user = userRepository.save(fakeUser);
|
||||||
|
String username = user.getUsername();
|
||||||
|
Thread.sleep(500);
|
||||||
|
|
||||||
|
//when
|
||||||
|
UserDetails userDetails = userService.loadUserByUsername(username);
|
||||||
|
|
||||||
|
//then
|
||||||
|
log.debug("User Details: {}", userDetails);
|
||||||
|
assertThat(userDetails).isNotNull();
|
||||||
|
assertThat(userDetails.getUsername()).isEqualTo(username);
|
||||||
|
|
||||||
|
Optional<User> byUsername = userRepository.findByUsername(username);
|
||||||
|
assertThat(byUsername)
|
||||||
|
.hasValueSatisfying(
|
||||||
|
u -> assertThat(u.getLastLoginDate().minus(500L, ChronoUnit.MILLIS))
|
||||||
|
.isCloseTo(u.getLastLoginDateDisplay(), within(150, ChronoUnit.MILLIS)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void loadUserByUsername_absent() {
|
||||||
|
|
||||||
|
//given
|
||||||
|
String username = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
//when
|
||||||
|
ThrowableAssert.ThrowingCallable execution = () -> {
|
||||||
|
UserDetails userDetails = userService.loadUserByUsername(username);
|
||||||
|
};
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertThatThrownBy(execution)
|
||||||
|
.isInstanceOf(UsernameNotFoundException.class)
|
||||||
|
.hasMessage("User with username `" + username + "` not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user