Skip to content

Commit

Permalink
Merge pull request #21 from kko-bugi/feature/18-userTest
Browse files Browse the repository at this point in the history
[feature/18-userTest] user 관련 테스트
  • Loading branch information
JoongHyun-Kim authored Jan 24, 2024
2 parents 215883a + 4ce5970 commit 7f6313e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public enum BaseResponseStatus {
* 2000: Request 오류
*/
// users(2000-2099)
INVALID_USER_IDX(false, 2000, "잘못된 user Idx 입니다."),
INVALID_USER_IDX(false, 2000, "잘못된 user Idx입니다."),
DUPLICATED_LOGIN_ID(false, 2001, "이미 사용중인 아이디입니다."),
DUPLICATED_NICKNAME(false, 2002, "이미 사용중인 닉네임입니다."),
UNMATCHED_PASSWORD(false, 2003, "비밀번호가 일치하지 않습니다."),
INVALIID_PASSWORD(false, 2004, "비민번호가 잘못되었습니다."),
INVALID_PASSWORD(false, 2004, "비민번호가 잘못되었습니다."),
INVALID_LOGIN_ID(false, 2005, "잘못된 아이디입니다."),

// produce(2100-2199)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Service
@RequiredArgsConstructor
public class AuthService {
@Value("${jwt.token-validity-in-seconds}")
@Value("${jwt.token-validity-in-millis}")
private int expireTime;
@Value("${jwt.secret-key}")
private String secretKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean checkNickname(String nickname) {
public LoginResponse login(LoginRequest loginRequest) throws BaseException {
try {
User user = userRepository.findByLoginId(loginRequest.loginId()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
if(!encoder.matches(loginRequest.password(), user.getPassword())) throw new BaseException(INVALIID_PASSWORD);
if(!encoder.matches(loginRequest.password(), user.getPassword())) throw new BaseException(INVALID_PASSWORD);

String accessToken = authService.generateAccessToken(user);
user.updateAccessToken(accessToken);
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/kkobugi/puremarket/user/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.kkobugi.puremarket.user;

import com.kkobugi.puremarket.common.BaseException;
import com.kkobugi.puremarket.user.application.UserService;
import com.kkobugi.puremarket.user.domain.dto.LoginRequest;
import com.kkobugi.puremarket.user.domain.dto.LoginResponse;
import com.kkobugi.puremarket.user.domain.dto.SignupRequest;
import com.kkobugi.puremarket.user.domain.entity.User;
import com.kkobugi.puremarket.user.repository.UserRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import static com.kkobugi.puremarket.common.enums.BaseResponseStatus.INVALID_LOGIN_ID;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class UserServiceTest {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Autowired
private BCryptPasswordEncoder encoder;

@Test
@DisplayName("회원가입")
public void signup() throws BaseException {

// given
SignupRequest signupRequest = new SignupRequest("nickname", "id", "pw", "pw", "01012345678");

// when
userService.signup(signupRequest);

// then
User user = userRepository.findByLoginId("id").orElseThrow(() -> new BaseException(INVALID_LOGIN_ID));
assertEquals("nickname", user.getNickname());
assertTrue(encoder.matches("pw", user.getPassword()));
assertEquals("01012345678", user.getContact());
}

@Test
@DisplayName("로그인")
public void login() throws BaseException {
// given
LoginRequest loginRequest = new LoginRequest("id", "pw");

// when
LoginResponse loginResponse = userService.login(loginRequest);

// then
User user = userRepository.findByLoginId("id").orElseThrow(() -> new BaseException(INVALID_LOGIN_ID));
assertNotNull(loginResponse.accessToken());
assertTrue(encoder.matches("pw", user.getPassword()));
assertEquals("id", user.getLoginId());
}
}

0 comments on commit 7f6313e

Please sign in to comment.