generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e5af3e
commit 11ac0e4
Showing
6 changed files
with
865 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
gym-rest-api/src/test/java/com/example/gym/config/SecuritySetup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.example.gym.config; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.example.gym.security.jwt.JwtTokenUtils; | ||
import com.example.gym.security.service.MyUserDetail; | ||
|
||
@Component | ||
@Profile("test") | ||
public class SecuritySetup { | ||
|
||
@Autowired | ||
private JwtTokenUtils jwtTokenUtils; | ||
|
||
@Autowired | ||
private UserDetailsService userDetailsService; | ||
|
||
public void setUpSecurityContext(String username, List<String> roles) { | ||
List<SimpleGrantedAuthority> authorities = roles.stream() | ||
.map(SimpleGrantedAuthority::new) | ||
.collect(Collectors.toList()); | ||
|
||
Authentication authentication = new UsernamePasswordAuthenticationToken( | ||
username, | ||
"password", | ||
authorities); | ||
|
||
SecurityContext context = SecurityContextHolder.createEmptyContext(); | ||
context.setAuthentication(authentication); | ||
SecurityContextHolder.setContext(context); | ||
} | ||
|
||
public String createTestToken(String username) { | ||
MyUserDetail userDetails = (MyUserDetail) userDetailsService.loadUserByUsername(username); | ||
|
||
return jwtTokenUtils.generateAccessToken(userDetails, userDetails.getEmail()); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
gym-rest-api/src/test/java/com/example/gym/config/TestConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.example.gym.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.text.SimpleDateFormat; | ||
|
||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.MongoDBContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@TestConfiguration | ||
@Slf4j | ||
@Profile("test") | ||
public class TestConfig { | ||
|
||
@SuppressWarnings("resource") | ||
@Container | ||
static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:latest")) | ||
.withReuse(true); | ||
|
||
static { | ||
mongoDBContainer.start(); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public MongoClient mongoClient() { | ||
return MongoClients.create(mongoDBContainer.getReplicaSetUrl()); | ||
} | ||
|
||
@Bean | ||
public MongoTemplate mongoTemplate(MongoClient mongoClient) { | ||
return new MongoTemplate(mongoClient, "testdb"); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public ObjectMapper objectMapper() { | ||
return new ObjectMapper() | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) | ||
.setSerializationInclusion(JsonInclude.Include.NON_NULL) | ||
.registerModule(new JavaTimeModule()) | ||
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")); | ||
} | ||
|
||
@DynamicPropertySource | ||
static void setProperties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.