Skip to content

Commit

Permalink
backend: add use case tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavNasonov committed Dec 23, 2024
1 parent 3e5af3e commit 11ac0e4
Show file tree
Hide file tree
Showing 6 changed files with 865 additions and 0 deletions.
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 gym-rest-api/src/test/java/com/example/gym/config/TestConfig.java
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);
}

}





Loading

0 comments on commit 11ac0e4

Please sign in to comment.