Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”€ :: ๋กœ๊ทธ์ธ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ถ”๊ฐ€ #217

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.juicycool.backend.domain.auth.presentation.dto.request;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class SignInRequestDto {
@NotBlank
private String email;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TokenResponse execute(SignInRequestDto dto) {
User user = userRepository.findByEmail(dto.getEmail())
.orElseThrow(NotFoundUserException::new);

if (passwordEncoder.matches(user.getPassword(), dto.getPassword()))
if (!passwordEncoder.matches(dto.getPassword(), user.getPassword()))
throw new NotMatchPasswordException();

TokenResponse tokenResponse = jwtProvider.generateTokenDto(user.getEmail());
Expand Down
105 changes: 105 additions & 0 deletions src/test/java/com/juicycool/backend/auth/SignInServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.juicycool.backend.auth;

import com.juicycool.backend.domain.auth.RefreshToken;
import com.juicycool.backend.domain.auth.exception.NotMatchPasswordException;
import com.juicycool.backend.domain.auth.presentation.dto.request.SignInRequestDto;
import com.juicycool.backend.domain.auth.presentation.dto.response.TokenResponse;
import com.juicycool.backend.domain.auth.repository.RefreshTokenRepository;
import com.juicycool.backend.domain.auth.service.impl.SignInServiceImpl;
import com.juicycool.backend.domain.user.User;
import com.juicycool.backend.domain.user.exception.NotFoundUserException;
import com.juicycool.backend.domain.user.repository.UserRepository;
import com.juicycool.backend.global.security.jwt.JwtProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class SignInServiceImplTest {

@Mock
private UserRepository userRepository;

@Mock
private RefreshTokenRepository refreshTokenRepository;

@Mock
private PasswordEncoder passwordEncoder;

@Mock
private JwtProvider jwtProvider;

@InjectMocks
private SignInServiceImpl signInService;

private User mockUser;
private SignInRequestDto dto;
private TokenResponse tokenDto;
private RefreshToken mockRefreshToken;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

mockUser = User.builder()
.email("[email protected]")
.password(passwordEncoder.encode("12345678!"))
.build();

dto = new SignInRequestDto("[email protected]", "12345678!");

tokenDto = TokenResponse.builder()
.accessToken("1231232")
.refreshToken("123213")
.build();

mockRefreshToken = RefreshToken.builder()
.email("[email protected]")
.token("123213")
.build();

when(jwtProvider.generateTokenDto(any())).thenReturn(tokenDto);
when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(mockUser));
when(refreshTokenRepository.save(any())).thenReturn(mockRefreshToken);
}

@Test
@DisplayName("๋งŒ์•ฝ email๋กœ ์œ ์ €๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์„ ๊ฒฝ์šฐ")
void If_cant_find_user_by_email() {
dto = new SignInRequestDto("[email protected]", "12345698!");

when(userRepository.findByEmail(dto.getEmail())).thenReturn(Optional.empty());

assertThrows(NotFoundUserException.class, () -> signInService.execute(dto));
}

@Test
@DisplayName("๋งŒ์•ฝ password๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ")
void If_password_not_match() {
dto = new SignInRequestDto("[email protected]", "12345679!");

when(userRepository.findByEmail(dto.getEmail())).thenReturn(Optional.of(mockUser));
when(passwordEncoder.matches(dto.getPassword(), mockUser.getPassword())).thenReturn(false);

assertThrows(NotMatchPasswordException.class, () -> signInService.execute(dto));
}

@Test
@DisplayName("๋งŒ์•ฝ ์ •์ƒ์ ์œผ๋กœ ๋กœ๊ทธ์ธ์„ ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ")
void If_successfully_sign_in() {
when(passwordEncoder.matches(dto.getPassword(), mockUser.getPassword())).thenReturn(true);

signInService.execute(dto);

verify(refreshTokenRepository, times(1)).save(any());
}

}
Loading