Skip to content

Commit

Permalink
Merge pull request #313 from Codiary-UMC-6th/test/#312-setting-jwt-test
Browse files Browse the repository at this point in the history
Test: test ๊ด€๋ จ ์„ค์ •
  • Loading branch information
lee-haeseung authored Jan 20, 2025
2 parents 7a549ca + eb25620 commit f7dc214
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dependencies {
// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

// MySQL
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down Expand Up @@ -66,6 +68,8 @@ dependencies {

// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

testImplementation 'org.assertj:assertj-core:3.24.2'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.codiary.backend.global.apiPayload.exception.handler.MemberHandler;
import com.codiary.backend.global.jwt.JwtTokenProvider;
import com.codiary.backend.global.jwt.TokenInfo;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -19,9 +21,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class AuthService {
Expand Down Expand Up @@ -98,7 +97,7 @@ public MemberResponseDTO.MemberTokenResponseDTO login(MemberRequestDTO.MemberLog

// 3. ์ธ์ฆ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ JWT ํ† ํฐ ์ƒ์„ฑ
Member getMember = memberRepository.findByEmail(request.email()).orElseThrow();
TokenInfo tokenInfo = jwtTokenProvider.generateToken(authentication, getMember.getMemberId());
TokenInfo tokenInfo = jwtTokenProvider.generateToken(authentication);

return MemberResponseDTO.MemberTokenResponseDTO.builder()
.email(getMember.getEmail())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public MemberResponseDTO.MemberTokenResponseDTO kakaoLogin(String code, String r
}
Member member = memberRepository.findByEmail(userEmail).get();


TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail(), member.getMemberId());
TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail());

return MemberResponseDTO.MemberTokenResponseDTO.builder()
.email(member.getEmail())
Expand Down Expand Up @@ -102,7 +101,7 @@ public MemberResponseDTO.MemberTokenResponseDTO naverLogin(String code, String s
}
Member member = memberRepository.findByEmail(userEmail).get();

TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail(), member.getMemberId());
TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail());

return MemberResponseDTO.MemberTokenResponseDTO.builder()
.email(member.getEmail())
Expand Down Expand Up @@ -130,7 +129,7 @@ public MemberResponseDTO.MemberTokenResponseDTO githubLogin(String code) {
}
Member member = memberRepository.findByEmail(userEmail).get();

TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail(), member.getMemberId());
TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail());

return MemberResponseDTO.MemberTokenResponseDTO.builder()
.email(member.getEmail())
Expand Down Expand Up @@ -161,7 +160,7 @@ public MemberResponseDTO.MemberTokenResponseDTO googleLogin(String code, String
}
Member member = memberRepository.findByEmail(userEmail).get();

TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail(), member.getMemberId());
TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getEmail());

return MemberResponseDTO.MemberTokenResponseDTO.builder()
.email(member.getEmail())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public JwtTokenProvider(@Value("${jwt.secret}") String secretKey, CustomMemberDe
}

// token ์ƒ์„ฑ
public TokenInfo generateToken(Authentication authentication, Long memberId) {
public TokenInfo generateToken(Authentication authentication) {
// ๊ถŒํ•œ ๊ฐ€์ ธ์˜ค๊ธฐ
String authorities = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
Expand All @@ -55,7 +55,6 @@ public TokenInfo generateToken(Authentication authentication, Long memberId) {
String accessToken = Jwts.builder()
.setSubject(authentication.getName())
.claim(AUTHORITIES_KEY, authorities)
.claim("memberId", memberId)
.setExpiration(accessTokenExpiresIn)
.signWith(key, SignatureAlgorithm.HS256)
.compact();
Expand All @@ -76,15 +75,14 @@ public TokenInfo generateToken(Authentication authentication, Long memberId) {
}

// ์ด๋ฉ”์ผ๋กœ ํ† ํฐ ์ƒ์„ฑ
public TokenInfo generateToken(String email, Long memberId) {
public TokenInfo generateToken(String email) {
long now = (new Date()).getTime();

// Access Token ์ƒ์„ฑ
Date accessTokenExpiresIn = new Date(now + ACCESS_TOKEN_EXPIRE_TIME);
String accessToken = Jwts.builder()
.setSubject(email)
.claim(AUTHORITIES_KEY, "ROLE_USER")
.claim("memberId", memberId)
.setExpiration(accessTokenExpiresIn)
.signWith(key, SignatureAlgorithm.HS256)
.compact();
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/codiary/backend/domain/MemberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.codiary.backend.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.codiary.backend.global.jwt.JwtTokenProvider;
import com.codiary.backend.global.jwt.TokenInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@SpringBootTest
@AutoConfigureMockMvc
public class MemberTest {

@Autowired
private MockMvc mockMvc;
@Autowired
private JwtTokenProvider jwtTokenProvider;

private String token;

@BeforeEach
public void setup() {
// ์กด์žฌํ•˜๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ† ํฐ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. DB์— ์žˆ๋Š” ์‚ฌ์šฉ์ž์˜ email์„ ์ž…๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•ด์š”.
TokenInfo token = jwtTokenProvider.generateToken("[email protected]");
this.token = token.getAccessToken();
}

@Test
public void test() throws Exception {
// Test ํ•˜๊ณ ์ž ํ•˜๋Š” url์„ ์ž…๋ ฅ
MvcResult result = mockMvc.perform(
get("/api/v2/member/info").header("Authorization", "Bearer " + token)
)
.andExpect(status().isOk())
.andReturn();

// ์‘๋‹ต ๋ถ„์„ (body๋“  ๋ญ๋“  ์ •๋ณด ๊บผ๋‚ด์„œ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.)
String responseBody = result.getResponse().getContentAsString();
assertThat(responseBody).contains("\"email\":\"[email protected]\"");
}
}

0 comments on commit f7dc214

Please sign in to comment.