-
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.
setting: Controller 테스트 코드 작성을 위한 환경 세팅 (#35)
* test: Controller 테스트 코드 작성을 위해 필요한 코드 추가 * test: AdminController 테스트 코드 작성 * fix: JpaAuditing 관련 config Application 실행 코드로부터 분리 * feat: Application의 timezone을 Asia/Seoul로 설정 * fix: Spring Rest Docs 관련 코드 제거 * fix: Jib 빌드 시 테스트 코드 무시 명령 제거
- Loading branch information
1 parent
c03ad80
commit 0d4b9ce
Showing
10 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
9 changes: 9 additions & 0 deletions
9
packy-api/src/main/java/com/dilly/global/config/JpaAuditingConfig.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,9 @@ | ||
package com.dilly.global.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
||
@Configuration | ||
@EnableJpaAuditing | ||
public class JpaAuditingConfig { | ||
} |
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,12 @@ | ||
package com.dilly; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@ActiveProfiles("test") | ||
class ApiApplicationTest { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packy-api/src/test/java/com/dilly/admin/api/AdminControllerTest.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,27 @@ | ||
package com.dilly.admin.api; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.dilly.global.ControllerTestSupport; | ||
import com.dilly.global.WithCustomMockUser; | ||
|
||
class AdminControllerTest extends ControllerTestSupport { | ||
|
||
@DisplayName("서버 상태를 확인한다.") | ||
@Test | ||
@WithCustomMockUser | ||
void healthCheck() throws Exception { | ||
// given // when // then | ||
// TODO: active profile이 null로 뜸 | ||
mockMvc.perform( | ||
get("/api/v1/admin/health") | ||
) | ||
.andDo(print()) | ||
.andExpect(status().isOk()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packy-api/src/test/java/com/dilly/global/ControllerTestSupport.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,30 @@ | ||
package com.dilly.global; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import com.dilly.admin.api.AdminController; | ||
import com.dilly.admin.application.AdminService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@WebMvcTest( | ||
controllers = { | ||
AdminController.class | ||
} | ||
) | ||
public abstract class ControllerTestSupport { | ||
static { | ||
System.setProperty("spring.config.name", "application-test"); | ||
} | ||
|
||
@Autowired | ||
protected MockMvc mockMvc; | ||
|
||
@Autowired | ||
protected ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
protected AdminService adminService; | ||
} |
13 changes: 13 additions & 0 deletions
13
packy-api/src/test/java/com/dilly/global/WithCustomMockUser.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,13 @@ | ||
package com.dilly.global; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import org.springframework.security.test.context.support.WithSecurityContext; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithSecurityContext(factory = WithCustomMockUserSecurityContextFactory.class) | ||
public @interface WithCustomMockUser { | ||
|
||
String id() default "1"; | ||
} |
74 changes: 74 additions & 0 deletions
74
packy-api/src/test/java/com/dilly/global/WithCustomMockUserSecurityContextFactory.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,74 @@ | ||
package com.dilly.global; | ||
|
||
import java.security.Key; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
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.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.test.context.support.WithSecurityContextFactory; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import com.dilly.member.Role; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.io.Decoders; | ||
import io.jsonwebtoken.security.Keys; | ||
|
||
@ActiveProfiles("test") | ||
public class WithCustomMockUserSecurityContextFactory implements WithSecurityContextFactory<WithCustomMockUser> { | ||
|
||
@Value("${jwt.secret}") | ||
String secretKey; | ||
|
||
@Override | ||
public SecurityContext createSecurityContext(WithCustomMockUser annotation) { | ||
final SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); | ||
|
||
Claims claims; | ||
UserDetails principal; | ||
Collection<? extends GrantedAuthority> authorities; | ||
|
||
long now = (new Date()).getTime(); | ||
long acessTokenExpireTime = 1000 * 60 * 30; | ||
Date accessTokenExpiresIn = new Date(now + acessTokenExpireTime); | ||
|
||
byte[] keyBytes = Decoders.BASE64.decode(secretKey); | ||
Key key = Keys.hmacShaKeyFor(keyBytes); | ||
|
||
String accessToken = Jwts.builder() | ||
.setSubject(annotation.id()) | ||
.claim("provider", "KAKAO") | ||
.claim("nickname", "테스트") | ||
.claim("auth", Role.ROLE_USER) | ||
.setExpiration(accessTokenExpiresIn) // 토큰 만료 시간 설정 | ||
.signWith(key, SignatureAlgorithm.HS512) | ||
.compact(); // 토큰 생성 | ||
|
||
claims = Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(accessToken) | ||
.getBody(); | ||
authorities = Arrays.stream(claims.get("auth").toString().split(",")) | ||
.map(SimpleGrantedAuthority::new) | ||
.toList(); | ||
principal = new User(claims.getSubject(), "", authorities); | ||
|
||
final UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( | ||
principal, "", authorities | ||
); | ||
securityContext.setAuthentication(authenticationToken); | ||
return securityContext; | ||
|
||
} | ||
} |
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,2 @@ | ||
jwt: | ||
secret: 'testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest' |