#24 simple sns: 회원가입, 로그인 테스트코드 수정
This commit is contained in:
@@ -15,6 +15,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import static com.example.sns.exception.ErrorCode.*;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
@@ -53,7 +54,7 @@ public class UserControllerTest {
|
|||||||
String username = "username";
|
String username = "username";
|
||||||
String password = "password";
|
String password = "password";
|
||||||
|
|
||||||
when(userService.join(username, password)).thenThrow(new SnsApplicationException(ErrorCode.DUPLICATED_USER_NAME, ""));
|
when(userService.join(username, password)).thenThrow(new SnsApplicationException(DUPLICATED_USER_NAME));
|
||||||
|
|
||||||
mockMvc.perform(post("/api/v1/users/join")
|
mockMvc.perform(post("/api/v1/users/join")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
@@ -81,7 +82,7 @@ public class UserControllerTest {
|
|||||||
String username = "username";
|
String username = "username";
|
||||||
String password = "password";
|
String password = "password";
|
||||||
|
|
||||||
when(userService.login(username, password)).thenThrow(new SnsApplicationException());
|
when(userService.login(username, password)).thenThrow(new SnsApplicationException(USER_NOT_FOUND));
|
||||||
|
|
||||||
mockMvc.perform(post("/api/v1/users/login")
|
mockMvc.perform(post("/api/v1/users/login")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
@@ -95,7 +96,7 @@ public class UserControllerTest {
|
|||||||
String username = "username";
|
String username = "username";
|
||||||
String password = "password";
|
String password = "password";
|
||||||
|
|
||||||
when(userService.login(username, password)).thenThrow(new SnsApplicationException());
|
when(userService.login(username, password)).thenThrow(new SnsApplicationException(INVALID_PASSWORD));
|
||||||
|
|
||||||
mockMvc.perform(post("/api/v1/users/login")
|
mockMvc.perform(post("/api/v1/users/login")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static com.example.sns.exception.ErrorCode.*;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@@ -36,7 +36,7 @@ class UserServiceTest {
|
|||||||
|
|
||||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
||||||
when(encoder.encode(password)).thenReturn("encrypt_password");
|
when(encoder.encode(password)).thenReturn("encrypt_password");
|
||||||
when(userEntityRepository.save(any())).thenReturn(Optional.of(UserEntityFixture.get(username, password)));
|
when(userEntityRepository.save(any())).thenReturn(UserEntityFixture.get(username, password));
|
||||||
|
|
||||||
assertDoesNotThrow(() -> userService.join(username, password));
|
assertDoesNotThrow(() -> userService.join(username, password));
|
||||||
}
|
}
|
||||||
@@ -52,10 +52,12 @@ class UserServiceTest {
|
|||||||
when(encoder.encode(password)).thenReturn("encrypt_password");
|
when(encoder.encode(password)).thenReturn("encrypt_password");
|
||||||
when(userEntityRepository.save(any())).thenReturn(Optional.of(fixture));
|
when(userEntityRepository.save(any())).thenReturn(Optional.of(fixture));
|
||||||
|
|
||||||
assertThrows(
|
SnsApplicationException e = assertThrows(
|
||||||
SnsApplicationException.class,
|
SnsApplicationException.class,
|
||||||
() -> userService.join(username, password)
|
() -> userService.join(username, password)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assertEquals(DUPLICATED_USER_NAME, e.getErrorCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -66,6 +68,7 @@ class UserServiceTest {
|
|||||||
UserEntity fixture = UserEntityFixture.get(username, password);
|
UserEntity fixture = UserEntityFixture.get(username, password);
|
||||||
|
|
||||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
||||||
|
when(encoder.matches(password, fixture.getPassword())).thenReturn(true);
|
||||||
|
|
||||||
assertDoesNotThrow(() -> userService.login(username, password));
|
assertDoesNotThrow(() -> userService.login(username, password));
|
||||||
}
|
}
|
||||||
@@ -77,10 +80,12 @@ class UserServiceTest {
|
|||||||
|
|
||||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
assertThrows(
|
SnsApplicationException e = assertThrows(
|
||||||
SnsApplicationException.class,
|
SnsApplicationException.class,
|
||||||
() -> userService.login(username, password)
|
() -> userService.login(username, password)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assertEquals(USER_NOT_FOUND, e.getErrorCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -93,9 +98,11 @@ class UserServiceTest {
|
|||||||
|
|
||||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
||||||
|
|
||||||
assertThrows(
|
SnsApplicationException e = assertThrows(
|
||||||
SnsApplicationException.class,
|
SnsApplicationException.class,
|
||||||
() -> userService.login(username, wrongPassword)
|
() -> userService.login(username, wrongPassword)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assertEquals(INVALID_PASSWORD, e.getErrorCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user