Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions back/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ dependencies {
// Redis
implementation("org.springframework.boot:spring-boot-starter-data-redis")

// Embedded Redis
testImplementation("com.github.codemonstur:embedded-redis:1.4.3")

// Session
implementation("org.springframework.session:spring-session-data-redis")

Expand Down
52 changes: 52 additions & 0 deletions back/src/main/java/com/back/global/config/EmbeddedRedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.back.global.config;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import redis.embedded.RedisServer;

import java.io.IOException;

/**
* 로컬 및 테스트 환경 임베디드 Redis 설정
* - test 프로필에서만 활성화
*/
@Configuration
@Profile("test")
public class EmbeddedRedisConfig {

@Value("${spring.data.redis.port}")
private int redisPort;

private RedisServer redisServer;

@PostConstruct
public void startRedis() {
try {
redisServer = new RedisServer(redisPort);
redisServer.start();
System.out.println("========================================");
System.out.println("Embedded Redis started on port " + redisPort);
System.out.println("========================================");
} catch (IOException e) {
System.err.println("Embedded Redis start failed: " + e.getMessage());
System.err.println("This is OK if external Redis is already running");
}
}

@PreDestroy
public void stopRedis() {
try {
if (redisServer != null && redisServer.isActive()) {
redisServer.stop();
System.out.println("========================================");
System.out.println("Embedded Redis stopped");
System.out.println("========================================");
}
} catch (Exception e) {
System.err.println("Error stopping embedded Redis: " + e.getMessage());
}
}
}
10 changes: 9 additions & 1 deletion back/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
spring:
data:
redis:
host: localhost
port: 6379
datasource:
url: jdbc:h2:mem:db_test;MODE=PostgreSQL;
username: sa
Expand All @@ -10,4 +14,8 @@ spring:
h2:
console:
enabled: true
path: /h2-console
path: /h2-console
management:
health:
redis:
enabled: false
8 changes: 4 additions & 4 deletions back/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
spring:
application:
name: back
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
- org.springframework.boot.autoconfigure.session.SessionAutoConfiguration
#autoconfigure:
# exclude:
# - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
# - org.springframework.boot.autoconfigure.session.SessionAutoConfiguration
config:
import: optional:file:.env[.properties]
flyway:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
Expand Down Expand Up @@ -122,8 +121,9 @@ void t3() throws Exception {
.andExpect(jsonPath("$.email").value(email))
.andReturn();

MockHttpSession session = (MockHttpSession) result.getRequest().getSession(false);
assertThat(session).isNotNull();
var jsessionid = result.getResponse().getCookie("JSESSIONID");
assertThat(jsessionid).isNotNull();
assertThat(jsessionid.getValue()).isNotBlank();
}

@Test
Expand All @@ -144,14 +144,11 @@ void t4() throws Exception {
@Test
@DisplayName("성공 - 게스트 로그인")
void t5() throws Exception {
MvcResult result = mvc.perform(post(BASE + "/guest").with(csrf()))
mvc.perform(post(BASE + "/guest").with(csrf()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.email").exists())
.andExpect(jsonPath("$.role").value("GUEST"))
.andReturn();

MockHttpSession session = (MockHttpSession) result.getRequest().getSession(false);
assertThat(session).isNotNull();
.andExpect(cookie().exists("JSESSIONID"));
}

@Test
Expand All @@ -178,10 +175,11 @@ void t7() throws Exception {
.andExpect(status().isOk())
.andReturn();

MockHttpSession session = (MockHttpSession) loginRes.getRequest().getSession(false);
var jsessionid = loginRes.getResponse().getCookie("JSESSIONID");
assertThat(jsessionid).isNotNull();

mvc.perform(get(BASE + "/me")
.session(session))
.cookie(jsessionid))
.andExpect(status().isOk())
.andExpect(jsonPath("$.email").value(email));
}
Expand All @@ -200,12 +198,12 @@ void t8() throws Exception {
.andExpect(status().isOk())
.andReturn();

MockHttpSession session = (MockHttpSession) loginRes.getRequest().getSession(false);
assertThat(session).isNotNull();
var jsessionid = loginRes.getResponse().getCookie("JSESSIONID");
assertThat(jsessionid).isNotNull();

mvc.perform(post(BASE + "/logout")
.with(csrf())
.session(session))
.cookie(jsessionid))
.andExpect(status().isOk());
}
}
Loading