- Input
<hide/>
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LoginMemberInput {
private String email;
private String password;
}
- Impl
<hide/>
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<Member> optionalMember = memberRepository.findByEmail(username);
if (!optionalMember.isPresent()) {
throw new RuntimeException("회원 정보가 존재하지 않습니다.");
}
Member member = optionalMember.get();
return new User(member.getEmail(), member.getPassword(), AuthorityUtils.NO_AUTHORITIES);
}
@Override
public void login(LoginMemberInput input) {
UserDetails userDetails = loadUserByUsername(input.getEmail());
Optional<Member> optionalMember = memberRepository.findByEmail(input.getEmail());
Member member = optionalMember.get();
if (!bCryptPasswordEncoder.matches(input.getPassword(), member.getPassword())) {
throw new RuntimeException("회원 비밀번호를 잘못 입력했습니다.");
}
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
userDetails, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(token);
}
- Ctrl
<hide/>
@PostMapping("/signin")
public ResponseEntity<?> signin(@RequestBody LoginMemberInput input) {
memberService.login(input);
String message = "정상적으로 로그인 완료했습니다.";
return new ResponseEntity(message, HttpStatus.OK);
}
- Test
<hide/>
@Test
@DisplayName("로그인 - 성공")
void login() {
// given
Member member = Member.builder()
.email("egg@naver.com")
.build();
String encPassword = bCryptPasswordEncoder.encode("abc!@#12");
member.setPassword(encPassword);
given(memberRepository.findByEmail(member.getEmail())).willReturn(
Optional.of(member)
);
// when
LoginMemberInput input = LoginMemberInput.builder()
.email("egg@naver.com")
.password("abc!@#12")
.build();
// then
memberService.login(input);
}
@Test
@DisplayName("로그인(아이디 오류) - 실패")
void login_fail_id() {
// given
LoginMemberInput input = LoginMemberInput.builder()
.email("egg@naver.com")
.password("abc!@#12")
.build();
// when
// 가짜 데이터 널로 받아야되는경우
given(memberRepository.findByEmail(anyString())).willReturn(
Optional.empty()
);
// then
RuntimeException error = assertThrows(RuntimeException.class,
() -> memberService.login(input));
assertEquals(error.getMessage(), "회원 정보가 존재하지 않습니다.");
}
@Test
@DisplayName("로그인(비밀번호 오류) - 실패")
void login_fail_pwd() {
// given
Member member = Member.builder()
.email("egg@naver.com")
.build();
String encPassword = bCryptPasswordEncoder.encode("abc!@#12");
member.setPassword(encPassword);
given(memberRepository.findByEmail(member.getEmail())).willReturn(
Optional.of(member)
);
// when
LoginMemberInput input = LoginMemberInput.builder()
.email("egg@naver.com")
.password("abc!@#1200")
.build();
// then
RuntimeException exception = assertThrows(RuntimeException.class,
() -> memberService.login(input));
assertEquals(exception.getMessage(), "회원 비밀번호를 잘못 입력했습니다.");
}
- 오류: PotentialStubbingProblem
- 원인: 스터빙 오류
- 해결: any()를 넣으면 처리 가능하다.
'Spring Projcect > [팀플] In & Out 가계부' 카테고리의 다른 글
회원 가입 API (0) | 2022.10.27 |
---|---|
회원 로그아웃 API (0) | 2022.10.26 |
회원 예외 처리 MemberErrorCode, ExceptionHandler (0) | 2022.10.25 |
회원 정보 조회 및 수정 API (0) | 2022.10.21 |
회원 아이디 & 비밀번호 찾기 (0) | 2022.10.19 |