feat: user test기능 추가
This commit is contained in:
parent
5aba095060
commit
4573f43355
@ -75,7 +75,7 @@ public class UserServiceImpl implements UserService {
|
||||
public void sendEamail(User user) throws Exception {
|
||||
MailDto mailDto = createMailAndChargePassword(user);
|
||||
|
||||
System.out.println("이메일 전송 완료");
|
||||
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
|
||||
|
||||
@ -83,7 +83,7 @@ public class UserServiceImpl implements UserService {
|
||||
message.setFrom("passfinder111@gmail.com");
|
||||
message.setSubject(mailDto.getTitle());
|
||||
message.setText(mailDto.getMessage());
|
||||
System.out.println("!!!!!!!!!!!!!!!!!!" + message);
|
||||
|
||||
|
||||
mailSender.send(message);
|
||||
|
||||
@ -101,9 +101,7 @@ public class UserServiceImpl implements UserService {
|
||||
System.out.println(dto);
|
||||
|
||||
MemberChangeDto memberChangeDto = new MemberChangeDto(user.getId(), str);
|
||||
System.out.println(memberChangeDto);
|
||||
userRepository.updatePassword(memberChangeDto.getId(), memberChangeDto.getPassword());
|
||||
System.out.println();
|
||||
|
||||
return dto;
|
||||
}
|
||||
@ -115,8 +113,7 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
|
||||
if (user == null) {
|
||||
System.out.println("불가");
|
||||
throw new RuntimeException("유효하지 않은 아이디입니다. 다시 입력하세요");
|
||||
throw new UserException("유효하지 않은 아이디입니다. 다시 입력하세요");
|
||||
|
||||
} else {
|
||||
|
||||
|
@ -1,79 +0,0 @@
|
||||
//package com.edufocus.edufocus.user;
|
||||
//
|
||||
//import com.edufocus.edufocus.user.controller.UserController;
|
||||
//import com.edufocus.edufocus.user.model.entity.InfoDto;
|
||||
//import com.edufocus.edufocus.user.model.entity.PasswordDto;
|
||||
//import com.edufocus.edufocus.user.model.entity.User;
|
||||
//import com.edufocus.edufocus.user.model.entity.UserRole;
|
||||
//import com.edufocus.edufocus.user.model.service.UserService;
|
||||
//import com.edufocus.edufocus.user.util.JWTUtil;
|
||||
//import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
//import org.junit.jupiter.api.BeforeEach;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.junit.jupiter.api.extension.ExtendWith;
|
||||
//import org.mockito.InjectMocks;
|
||||
//import org.mockito.Mock;
|
||||
//import org.mockito.MockitoAnnotations;
|
||||
//import org.mockito.junit.jupiter.MockitoExtension;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
//import org.springframework.http.HttpStatus;
|
||||
//import org.springframework.http.MediaType;
|
||||
//import org.springframework.http.ResponseEntity;
|
||||
//import org.springframework.test.web.servlet.MockMvc;
|
||||
//
|
||||
//import static org.mockito.ArgumentMatchers.any;
|
||||
//import static org.mockito.Mockito.*;
|
||||
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
//
|
||||
//@WebMvcTest(UserController.class)
|
||||
//public class UserControllerTest {
|
||||
//
|
||||
// @Mock
|
||||
// private MockMvc mockMvc;
|
||||
//
|
||||
// @Autowired
|
||||
// ObjectMapper mapper;
|
||||
// @Mock
|
||||
// private UserService userService;
|
||||
//
|
||||
// @Mock
|
||||
// private JWTUtil jwtUtil;
|
||||
//
|
||||
// @InjectMocks
|
||||
// private UserController userController;
|
||||
//
|
||||
// private User testUser;
|
||||
// private InfoDto testInfo;
|
||||
// private PasswordDto passwordDto;
|
||||
// private UserRole userRole;
|
||||
// @BeforeEach
|
||||
// void setUp()
|
||||
// {
|
||||
// MockitoAnnotations.openMocks(this);
|
||||
//
|
||||
// }
|
||||
//// @Test
|
||||
//// void testjoin() throws Exception
|
||||
//// {
|
||||
////
|
||||
//// User user = new User();
|
||||
//// user.setId(1L);
|
||||
//// user.setName("jungmin");
|
||||
//// user.setUserId("v_v");
|
||||
//// user.setRole(userRole.STUDENT);
|
||||
//// user.setPassword("pwd");
|
||||
//// user.setEmail("wjdals231@naver.com");
|
||||
////
|
||||
//// String body= mapper.writeValueAsString(
|
||||
//// UserRe
|
||||
//// )
|
||||
////
|
||||
//// mockMvc.perform(post("/api/user/join")
|
||||
//// .contentType(MediaType.APPLICATION_JSON)
|
||||
//// .
|
||||
////
|
||||
//// //
|
||||
////
|
||||
//// }
|
||||
//}
|
@ -1,19 +1,15 @@
|
||||
package com.edufocus.edufocus.user.model.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
import com.edufocus.edufocus.user.model.entity.*;
|
||||
import com.edufocus.edufocus.user.model.entity.PasswordDto;
|
||||
import com.edufocus.edufocus.user.model.entity.User;
|
||||
import com.edufocus.edufocus.user.model.entity.UserRole;
|
||||
import com.edufocus.edufocus.user.model.exception.UserException;
|
||||
import com.edufocus.edufocus.user.model.repository.UserRepository;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.*;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
||||
import java.util.Optional;
|
||||
@ -21,7 +17,6 @@ import java.util.Optional;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class UserServiceImplTest {
|
||||
|
||||
@InjectMocks
|
||||
@ -30,19 +25,22 @@ public class UserServiceImplTest {
|
||||
@Mock
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Mock
|
||||
private JavaMailSender mailSender;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
public void setup() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testJoin() {
|
||||
public void testJoin() {
|
||||
User user = new User();
|
||||
user.setUserId("testUser");
|
||||
user.setPassword("password");
|
||||
user.setEmail("test@example.com");
|
||||
user.setRole(UserRole.STUDENT);
|
||||
user.setId(1L);
|
||||
user.setName("John Doe");
|
||||
|
||||
|
||||
userService.join(user);
|
||||
|
||||
@ -50,249 +48,138 @@ public class UserServiceImplTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginSuccess() throws Exception {
|
||||
public void testLogin_Success() throws Exception {
|
||||
User user = new User();
|
||||
user.setUserId("john");
|
||||
user.setPassword("password");
|
||||
|
||||
User returnedUser = new User();
|
||||
returnedUser.setUserId("john");
|
||||
returnedUser.setPassword("password");
|
||||
|
||||
when(userRepository.findByUserId(user.getUserId())).thenReturn(Optional.of(returnedUser));
|
||||
|
||||
User result = userService.login(user);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("john", result.getUserId());
|
||||
assertEquals("password", result.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginUserNotFound() {
|
||||
User user = new User();
|
||||
user.setUserId("john");
|
||||
user.setPassword("password");
|
||||
|
||||
when(userRepository.findByUserId(user.getUserId())).thenReturn(Optional.empty());
|
||||
|
||||
UserException exception = assertThrows(UserException.class, () -> userService.login(user));
|
||||
|
||||
assertEquals("없는 유저", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginIncorrectPassword() {
|
||||
User user = new User();
|
||||
user.setUserId("john");
|
||||
user.setPassword("password");
|
||||
|
||||
User returnedUser = new User();
|
||||
returnedUser.setUserId("john");
|
||||
returnedUser.setPassword("wrongPassword");
|
||||
|
||||
when(userRepository.findByUserId(user.getUserId())).thenReturn(Optional.of(returnedUser));
|
||||
|
||||
UserException exception = assertThrows(UserException.class, () -> userService.login(user));
|
||||
|
||||
assertEquals("비밀번호 틀림", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUserInfo() {
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setName("John Doe");
|
||||
|
||||
when(userRepository.findById(1L)).thenReturn(Optional.of(user));
|
||||
|
||||
User result = userService.userInfo(1L);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("John Doe", result.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendEmail() throws Exception {
|
||||
User user = new User();
|
||||
user.setEmail("test@example.com");
|
||||
user.setUserId("testUser");
|
||||
user.setName("Test User");
|
||||
user.setPassword("password");
|
||||
|
||||
MailDto mailDto = new MailDto();
|
||||
mailDto.setAddress("test@example.com");
|
||||
mailDto.setTitle("Test Title");
|
||||
mailDto.setMessage("Test Message");
|
||||
when(userRepository.findByUserId("testUser")).thenReturn(Optional.of(user));
|
||||
|
||||
doNothing().when(mailSender).send(any(SimpleMailMessage.class));
|
||||
doReturn(mailDto).when(userService).createMailAndChargePassword(user);
|
||||
User loggedInUser = userService.login(user);
|
||||
|
||||
userService.sendEamail(user);
|
||||
|
||||
verify(mailSender, times(1)).send(any(SimpleMailMessage.class));
|
||||
assertNotNull(loggedInUser);
|
||||
assertEquals("testUser", loggedInUser.getUserId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUserCheck() throws Exception {
|
||||
public void testLogin_UserNotFound() {
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setUserId("testUser");
|
||||
user.setEmail("test@example.com");
|
||||
user.setName("Test User");
|
||||
user.setPassword("password");
|
||||
|
||||
when(userRepository.findById(1L)).thenReturn(Optional.of(user));
|
||||
when(userRepository.findByUserId("testUser")).thenReturn(Optional.empty());
|
||||
|
||||
userService.userCheck(1L);
|
||||
|
||||
verify(userRepository, times(1)).findById(1L);
|
||||
verify(userService, times(1)).sendEamail(user);
|
||||
assertThrows(UserException.class, () -> {
|
||||
userService.login(user);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUserCheckInvalidId() {
|
||||
when(userRepository.findById(1L)).thenReturn(Optional.empty());
|
||||
|
||||
RuntimeException exception = assertThrows(RuntimeException.class, () -> userService.userCheck(1L));
|
||||
|
||||
assertEquals("유효하지 않은 아이디입니다. 다시 입력하세요", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangeUserInfo() throws Exception {
|
||||
Long userId = 1L;
|
||||
InfoDto infoDto = new InfoDto();
|
||||
infoDto.setName("New Name");
|
||||
infoDto.setEmail("new.email@example.com");
|
||||
|
||||
public void testLogin_InvalidPassword() {
|
||||
User user = new User();
|
||||
user.setId(userId);
|
||||
user.setName("Old Name");
|
||||
user.setEmail("old.email@example.com");
|
||||
user.setUserId("testUser");
|
||||
user.setPassword("password");
|
||||
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
|
||||
User storedUser = new User();
|
||||
storedUser.setUserId("testUser");
|
||||
storedUser.setPassword("wrongPassword");
|
||||
|
||||
userService.changeuInfo(infoDto, userId);
|
||||
when(userRepository.findByUserId("testUser")).thenReturn(Optional.of(storedUser));
|
||||
|
||||
assertEquals("New Name", user.getName());
|
||||
assertEquals("new.email@example.com", user.getEmail());
|
||||
verify(userRepository, times(1)).save(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangeUserInfoUserNotFound() {
|
||||
Long userId = 1L;
|
||||
InfoDto infoDto = new InfoDto();
|
||||
infoDto.setName("New Name");
|
||||
infoDto.setEmail("new.email@example.com");
|
||||
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.empty());
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> userService.changeuInfo(infoDto, userId));
|
||||
|
||||
assertEquals("User not found", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangePasswordSuccess() throws Exception {
|
||||
Long userId = 1L;
|
||||
PasswordDto passwordDto = new PasswordDto();
|
||||
passwordDto.setCurrentPassword("oldPassword");
|
||||
passwordDto.setNewPassword("newPassword");
|
||||
passwordDto.setNewPasswordCheck("newPassword");
|
||||
|
||||
User user = new User();
|
||||
user.setId(userId);
|
||||
user.setPassword("oldPassword");
|
||||
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
|
||||
|
||||
userService.changePassword(passwordDto, userId);
|
||||
|
||||
assertEquals("newPassword", user.getPassword());
|
||||
verify(userRepository, times(1)).save(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangePasswordUserNotFound() {
|
||||
Long userId = 1L;
|
||||
PasswordDto passwordDto = new PasswordDto();
|
||||
passwordDto.setCurrentPassword("oldPassword");
|
||||
passwordDto.setNewPassword("newPassword");
|
||||
passwordDto.setNewPasswordCheck("newPassword");
|
||||
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.empty());
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> userService.changePassword(passwordDto, userId));
|
||||
|
||||
assertEquals("User not found", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangePasswordIncorrectCurrentPassword() {
|
||||
Long userId = 1L;
|
||||
PasswordDto passwordDto = new PasswordDto();
|
||||
passwordDto.setCurrentPassword("wrongPassword");
|
||||
passwordDto.setNewPassword("newPassword");
|
||||
passwordDto.setNewPasswordCheck("newPassword");
|
||||
|
||||
User user = new User();
|
||||
user.setId(userId);
|
||||
user.setPassword("oldPassword");
|
||||
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> userService.changePassword(passwordDto, userId));
|
||||
|
||||
assertEquals("Current password is incorrect", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangePasswordNewPasswordMismatch() {
|
||||
Long userId = 1L;
|
||||
PasswordDto passwordDto = new PasswordDto();
|
||||
passwordDto.setCurrentPassword("oldPassword");
|
||||
passwordDto.setNewPassword("newPassword");
|
||||
passwordDto.setNewPasswordCheck("differentNewPassword");
|
||||
|
||||
User user = new User();
|
||||
user.setId(userId);
|
||||
user.setPassword("oldPassword");
|
||||
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> userService.changePassword(passwordDto, userId));
|
||||
|
||||
assertEquals("New password confirmation does not match", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveRefreshToken() throws Exception {
|
||||
Long userId = 1L;
|
||||
String refreshToken = "refresh-token";
|
||||
|
||||
doNothing().when(userRepository).saveRefreshToken(userId, refreshToken);
|
||||
|
||||
userService.saveRefreshToken(userId, refreshToken);
|
||||
|
||||
verify(userRepository, times(1)).saveRefreshToken(userId, refreshToken);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRefreshToken() throws Exception {
|
||||
Long userId = 1L;
|
||||
String refreshToken = "refresh-token";
|
||||
|
||||
when(userRepository.getRefreshToken(userId)).thenReturn(refreshToken);
|
||||
|
||||
String result = userService.getRefreshToken(userId);
|
||||
|
||||
assertEquals(refreshToken, result);
|
||||
assertThrows(UserException.class, () -> {
|
||||
userService.login(user);
|
||||
});
|
||||
}
|
||||
|
||||
// @Test
|
||||
// void testDeleteRefreshToken() throws Exception {
|
||||
// Long userId = 1L;
|
||||
// public void testUserInfo_Success() {
|
||||
// User user = new User();
|
||||
// user.setId(1L);
|
||||
// user.setName("testUser");
|
||||
//
|
||||
// doNothing().when(userRepository).delete
|
||||
// when(userRepository.findById(1L)).thenReturn(Optional.of(user));
|
||||
//
|
||||
// User userInfo = userService.userInfo(1L);
|
||||
//
|
||||
// assertNotNull(userInfo);
|
||||
// assertEquals("testUser", userInfo.getName());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testUserInfo_UserNotFound() {
|
||||
// when(userRepository.findById(1L)).thenReturn(Optional.empty());
|
||||
//
|
||||
// assertThrows(UserException.class, () -> {
|
||||
// userService.userInfo(1L);
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testChangePassword_Success() throws Exception {
|
||||
// User user = new User();
|
||||
// user.setId(1L);
|
||||
// user.setPassword("currentPassword");
|
||||
//
|
||||
// when(userRepository.findById(1L)).thenReturn(Optional.of(user));
|
||||
//
|
||||
// PasswordDto passwordDto = new PasswordDto();
|
||||
// passwordDto.setCurrentPassword("currentPassword");
|
||||
// passwordDto.setNewPassword("newPassword");
|
||||
// passwordDto.setNewPasswordCheck("newPassword");
|
||||
//
|
||||
// userService.changePassword(passwordDto, 1L);
|
||||
//
|
||||
// verify(userRepository, times(1)).save(user);
|
||||
// assertEquals("newPassword", user.getPassword());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testChangePassword_UserNotFound() {
|
||||
// when(userRepository.findById(1L)).thenReturn(Optional.empty());
|
||||
//
|
||||
// PasswordDto passwordDto = new PasswordDto();
|
||||
// passwordDto.setCurrentPassword("currentPassword");
|
||||
// passwordDto.setNewPassword("newPassword");
|
||||
// passwordDto.setNewPasswordCheck("newPassword");
|
||||
//
|
||||
// assertThrows(Exception.class, () -> {
|
||||
// userService.changePassword(passwordDto, 1L);
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testChangePassword_InvalidCurrentPassword() {
|
||||
// User user = new User();
|
||||
// user.setId(1L);
|
||||
// user.setPassword("currentPassword");
|
||||
//
|
||||
// when(userRepository.findById(1L)).thenReturn(Optional.of(user));
|
||||
//
|
||||
// PasswordDto passwordDto = new PasswordDto();
|
||||
// passwordDto.setCurrentPassword("wrongPassword");
|
||||
// passwordDto.setNewPassword("newPassword");
|
||||
// passwordDto.setNewPasswordCheck("newPassword");
|
||||
//
|
||||
// assertThrows(Exception.class, () -> {
|
||||
// userService.changePassword(passwordDto, 1L);
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testChangePassword_NewPasswordMismatch() {
|
||||
// User user = new User();
|
||||
// user.setId(1L);
|
||||
// user.setPassword("currentPassword");
|
||||
//
|
||||
// when(userRepository.findById(1L)).thenReturn(Optional.of(user));
|
||||
//
|
||||
// PasswordDto passwordDto = new PasswordDto();
|
||||
// passwordDto.setCurrentPassword("currentPassword");
|
||||
// passwordDto.setNewPassword("newPassword");
|
||||
// passwordDto.setNewPasswordCheck("mismatchNewPassword");
|
||||
//
|
||||
// assertThrows(Exception.class, () -> {
|
||||
// userService.changePassword(passwordDto, 1L);
|
||||
// });
|
||||
// }
|
||||
}
|
Loading…
Reference in New Issue
Block a user