Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
#2 로그인테스트코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy3831 committed Sep 18, 2021
1 parent 0caccc1 commit 60e9721
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
75 changes: 47 additions & 28 deletions src/test/java/com/photobook/service/impl/UserServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.time.LocalDate;
Expand All @@ -32,7 +34,7 @@ public UserDto userInfo() {
UserDto userInfo = new UserDto();
userInfo.setUserId(1);
userInfo.setId("admin");
userInfo.setPassword("1234");
userInfo.setPassword(passwordEncoder.encode("1234"));
userInfo.setName("관리자");
userInfo.setEmail("admin@gmail.com");
userInfo.setBirth(LocalDate.of(1993, 10, 28));
Expand All @@ -43,31 +45,48 @@ public UserDto userInfo() {
return userInfo;
}

// @Test
// @DisplayName("사용자 정보가 존재하면 성공")
// public void successGetUserInfoByIdAndPassword() {
// //given
// UserDto userInfo = userInfo();
// when(userMapper.getUserInfoByIdAndPassword("admin", "1234")).thenReturn(userInfo);
//
// //when
// UserDto result = userServiceImpl.getUserInfoByIdAndPassword("admin", "1234");
//
// //then
// assertEquals(result, userInfo);
// }
//
// @Test
// @DisplayName("사용자 정보가 존재하지 않으면 성공")
// public void failureGetUserInfoByIdAndPassword() {
// //given
// when(userMapper.getUserInfoByIdAndPassword("notExistId", "notExistPassword")).thenReturn(null);
//
// //then
// assertThrows(IllegalArgumentException.class, () -> {
// //when
// userServiceImpl.getUserInfoByIdAndPassword("notExistId", "notExistPassword");
// });
//
// }
@Test
@DisplayName("로그인 검증 성공")
public void successValidateLogin() {
//given
UserDto userInfo = userInfo();
when(userMapper.getUserInfoById("admin")).thenReturn(userInfo);
when(passwordEncoder.matches("1234", userInfo.getPassword())).thenReturn(true);

//when
UserDto result = userServiceImpl.validateLogin("admin", "1234");

//then
assertEquals(result, userInfo);
}

@Test
@DisplayName("로그인 검증 실패_존재하지 않는 아이디")
public void failureValidateLoginId() {
//given
when(userMapper.getUserInfoById("admin")).thenReturn(null);

//then
assertThrows(InternalAuthenticationServiceException.class, () -> {
//when
userServiceImpl.validateLogin("admin", "1234");
});

}

@Test
@DisplayName("로그인 검증 실패_비밀번호 불일치")
public void failureValidateLoginPwd() {
//given
UserDto userInfo = userInfo();
when(userMapper.getUserInfoById("admin")).thenReturn(userInfo);
when(passwordEncoder.matches("1234", userInfo.getPassword())).thenReturn(false);

//then
assertThrows(BadCredentialsException.class, () -> {
//when
userServiceImpl.validateLogin("admin", "1234");
});
}

}
1 change: 1 addition & 0 deletions src/test/java/com/photobook/utils/PasswordEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void passwordEncode() {

//when
String encodedPwd = passwordEncoder.encode(rawPwd);
System.out.println(encodedPwd);

//then
assertAll(
Expand Down

0 comments on commit 60e9721

Please sign in to comment.