19. Run Application (#1)
This commit is contained in:
@ -56,6 +56,13 @@
|
|||||||
<artifactId>spring-security-test</artifactId>
|
<artifactId>spring-security-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.javafaker</groupId>
|
||||||
|
<artifactId>javafaker</artifactId>
|
||||||
|
<version>1.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.domain;
|
package net.shyshkin.study.fullstack.supportportal.backend.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.*;
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
@ -18,6 +15,7 @@ import java.time.LocalDateTime;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||||
|
@Builder
|
||||||
public class User implements Serializable {
|
public class User implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -4372214856545239049L;
|
private static final long serialVersionUID = -4372214856545239049L;
|
||||||
|
|||||||
@ -19,4 +19,7 @@ spring:
|
|||||||
datasource:
|
datasource:
|
||||||
url: jdbc:mysql://localhost:23306/support-portal
|
url: jdbc:mysql://localhost:23306/support-portal
|
||||||
jpa:
|
jpa:
|
||||||
show-sql: true
|
show-sql: true
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
net.shyshkin: debug
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package net.shyshkin.study.fullstack.supportportal.backend.domain;
|
||||||
|
|
||||||
|
import com.github.javafaker.Faker;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
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.UUID;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@SpringBootTest
|
||||||
|
@ActiveProfiles("local")
|
||||||
|
class UserPrincipalTest {
|
||||||
|
|
||||||
|
public static final Faker FAKER = Faker.instance();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserRepository userRepository;
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
User fakeUser = createRandomUser();
|
||||||
|
user = userRepository.save(fakeUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void displayUser() {
|
||||||
|
//given
|
||||||
|
Long id = user.getId();
|
||||||
|
int expectedAuthoritiesLength = user.getAuthorities().length + user.getRoles().length;
|
||||||
|
|
||||||
|
//when
|
||||||
|
Optional<User> savedUserOptional = userRepository.findById(id);
|
||||||
|
assertThat(savedUserOptional)
|
||||||
|
.hasValueSatisfying(userSaved -> assertThat(userSaved)
|
||||||
|
.hasNoNullFieldsOrProperties()
|
||||||
|
.satisfies(u -> log.debug("Saved User: {}", u)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
Optional<UserPrincipal> userPrincipalOptional = savedUserOptional.map(UserPrincipal::new);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertThat(userPrincipalOptional)
|
||||||
|
.hasValueSatisfying(userPrincipal -> assertThat(userPrincipal.getAuthorities())
|
||||||
|
.isNotNull()
|
||||||
|
.isNotEmpty()
|
||||||
|
.hasSize(expectedAuthoritiesLength)
|
||||||
|
.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user