Skip to content
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
250 changes: 126 additions & 124 deletions src/test/java/com/rabbitmqprac/service/AuthServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.never;
Expand All @@ -44,145 +43,148 @@ public class AuthServiceTest {
@InjectMocks
private AuthService authService;

private static User user = UserFixture.FIRST_USER.toEntity();
private static final User USER = UserFixture.FIRST_USER.toEntity();
private static final Jwts JWTS = mock(Jwts.class);

@Nested
@DisplayName("회원가입 성공 시나리오")
class SignUpSuccessScenarios {
@Test
@DisplayName("회원가입 성공")
void signUp() {
// given
final String nickname = "nickname";
final String username = "nickname";
final String password = "password_123";
final String confirmPassword = "password_123";
AuthSignUpReq req = new AuthSignUpReq(nickname, username, password, confirmPassword);
Jwts jwts = mock(Jwts.class);

given(userService.saveUserWithEncryptedPassword(any(UserCreateReq.class)))
.willReturn(user);
given(jwtHelper.createToken(user)).willReturn(jwts);

// when
Pair<Long, Jwts> result = authService.signUp(req);

// then
assertThat(result.getLeft()).isEqualTo(user.getId());
assertThat(result.getRight()).isEqualTo(jwts);
verify(userService).saveUserWithEncryptedPassword(any(UserCreateReq.class));
verify(jwtHelper).createToken(user);
@DisplayName("회원가입 시나리오")
class SignUpScenario {
private final String nickname = "nickname";
private final String username = "nickname";
private final String password = "password_123";
private final String confirmPassword = "password_123";
private final AuthSignUpReq req = new AuthSignUpReq(nickname, username, password, confirmPassword);

@Nested
@DisplayName("성공 시나리오")
class SuccessScenario {
@Test
@DisplayName("회원가입 성공")
void signUpSuccess() {
// given
given(userService.saveUserWithEncryptedPassword(any(UserCreateReq.class))).willReturn(USER);
given(jwtHelper.createToken(USER)).willReturn(JWTS);

// when
Pair<Long, Jwts> result = authService.signUp(req);

// then
assertThat(result.getLeft()).isEqualTo(USER.getId());
assertThat(result.getRight()).isEqualTo(JWTS);
verify(userService).saveUserWithEncryptedPassword(any(UserCreateReq.class));
verify(jwtHelper).createToken(USER);
}
}
}

@Nested
@DisplayName("회원가입 실패 시나리오")
class SignUpFailScenarios {
@Test
@DisplayName("password와 confirmPassword가 다른 경우 회원 가입에 실패")
void signUpWhenInvalidPassword() {
// given
final String nickname = "nickname";
final String username = "nickname";
final String password = "password";
final String confirmPassword = "invalid_password";
AuthSignUpReq req = new AuthSignUpReq(nickname, username, password, confirmPassword);

// when
AuthErrorException errorException = assertThrows(AuthErrorException.class, () -> authService.signUp(req));

// then
assertThat(errorException.getErrorCode()).isEqualTo(AuthErrorCode.PASSWORD_CONFIRM_MISMATCH);
@Nested
@DisplayName("실패 시나리오")
class FailScenario {
@Test
@DisplayName("password와 confirmPassword가 다르면 회원가입 실패")
void signUpFailWhenPasswordMismatch() {
// given
AuthSignUpReq invalidReq = new AuthSignUpReq(nickname, username, password, "wrong");

// when
AuthErrorException ex = assertThrows(AuthErrorException.class, () -> authService.signUp(invalidReq));

// then
assertThat(ex.getErrorCode()).isEqualTo(AuthErrorCode.PASSWORD_CONFIRM_MISMATCH);
}
}
}

@Nested
@DisplayName("로그인 성공 시나리오")
class SignInSuccessScenarios {
@Test
@DisplayName("로그인 성공")
void signIn() {
// given
final String username = "nickname";
final String password = "password";
AuthSignInReq req = new AuthSignInReq(username, password);
Jwts jwts = mock(Jwts.class);

given(userService.readUserByUsername(username)).willReturn(user);
given(jwtHelper.createToken(user)).willReturn(jwts);
given(bCryptPasswordEncoder.matches(password, user.getPassword())).willReturn(Boolean.TRUE);

// when
Pair<Long, Jwts> result = authService.signIn(req);

// then
assertThat(result.getLeft()).isEqualTo(user.getId());
assertThat(result.getRight()).isEqualTo(jwts);
verify(jwtHelper).createToken(user);
@DisplayName("로그인 시나리오")
class SignInScenario {
private final String username = "nickname";
private final String password = "password";
private final AuthSignInReq req = new AuthSignInReq(username, password);

@Nested
@DisplayName("성공 시나리오")
class SuccessScenario {
@Test
@DisplayName("로그인 성공")
void signInSuccess() {
// given
given(userService.readUserByUsername(username)).willReturn(USER);
given(jwtHelper.createToken(USER)).willReturn(JWTS);
given(bCryptPasswordEncoder.matches(password, USER.getPassword())).willReturn(true);

// when
Pair<Long, Jwts> result = authService.signIn(req);

// then
assertThat(result.getLeft()).isEqualTo(USER.getId());
assertThat(result.getRight()).isEqualTo(JWTS);
verify(jwtHelper).createToken(USER);
}
}
}

@Nested
@DisplayName("로그인 실패 시나리오")
class SignInFailScenarios {
@Test
@DisplayName("로그인 유저의 패스워드가 올바르지 않다면 로그인 실패")
void signInWhenInvalidPassword() {
// given
final String username = "nickname";
final String password = "password";
AuthSignInReq req = new AuthSignInReq(username, password);
given(userService.readUserByUsername(username)).willReturn(user);
given(bCryptPasswordEncoder.matches(password, user.getPassword())).willReturn(Boolean.FALSE);

// when
AuthErrorException errorException = assertThrows(AuthErrorException.class, () -> authService.signIn(req));

// then
assertThat(errorException.getErrorCode()).isEqualTo(AuthErrorCode.INVALID_PASSWORD);
verify(jwtHelper, never()).createToken(user);
@Nested
@DisplayName("실패 시나리오")
class FailScenario {
@Test
@DisplayName("패스워드가 올바르지 않으면 로그인 실패")
void signInFailWhenInvalidPassword() {
// given
given(userService.readUserByUsername(username)).willReturn(USER);
given(bCryptPasswordEncoder.matches(password, USER.getPassword())).willReturn(false);

// when
AuthErrorException ex = assertThrows(AuthErrorException.class, () -> authService.signIn(req));

// then
assertThat(ex.getErrorCode()).isEqualTo(AuthErrorCode.INVALID_PASSWORD);
verify(jwtHelper, never()).createToken(USER);
}
}
}

@Nested
@DisplayName("패스워드 변경 성공 시나리오")
class UpdatePasswordScenarios {
@Test
@DisplayName("패스워드 변경 성공")
void updatePasswordSuccess() {
// given
final Long userId = anyLong();
final String oldPassword = "oldPassword";
final String newPassword = "newPassword";
AuthUpdatePasswordReq req = new AuthUpdatePasswordReq(oldPassword, newPassword);

given(userService.readUser(userId)).willReturn(user);
given(bCryptPasswordEncoder.matches(req.oldPassword(), user.getPassword())).willReturn(true);
given(req.newPassword(bCryptPasswordEncoder)).willReturn(newPassword);

// when
authService.updatePassword(userId, req);

// then
assertThat(user.getPassword()).isEqualTo(newPassword);
@DisplayName("패스워드 변경 시나리오")
class UpdatePasswordScenario {
private final Long userId = 1L;
private final String oldPassword = "oldPassword";
private final String newPassword = "newPassword";
private final AuthUpdatePasswordReq req = new AuthUpdatePasswordReq(oldPassword, newPassword);

@Nested
@DisplayName("성공 시나리오")
class SuccessScenario {
@Test
@DisplayName("패스워드 변경 성공")
void updatePasswordSuccess() {
// given
given(userService.readUser(userId)).willReturn(USER);
given(bCryptPasswordEncoder.matches(oldPassword, USER.getPassword())).willReturn(true);
given(req.newPassword(bCryptPasswordEncoder)).willReturn(newPassword);

// when
authService.updatePassword(userId, req);

// then
assertThat(USER.getPassword()).isEqualTo(newPassword);
}
}

@Test
@DisplayName("기존 패스워드가 틀리면 변경 실패")
void updatePasswordFailWhenInvalidOldPassword() {
// given
final Long userId = anyLong();
final String oldPassword = "oldPassword";
final String newPassword = "newPassword";
AuthUpdatePasswordReq req = new AuthUpdatePasswordReq(oldPassword, newPassword);
given(userService.readUser(userId)).willReturn(user);
given(bCryptPasswordEncoder.matches(oldPassword, user.getPassword())).willReturn(false);

// when
AuthErrorException errorException = assertThrows(AuthErrorException.class, () -> authService.updatePassword(userId, req));

// then
assertThat(errorException.getErrorCode()).isEqualTo(AuthErrorCode.INVALID_PASSWORD);
@Nested
@DisplayName("실패 시나리오")
class FailScenario {
@Test
@DisplayName("기존 패스워드가 틀리면 변경 실패")
void updatePasswordFailWhenInvalidOldPassword() {
// given
given(userService.readUser(userId)).willReturn(USER);
given(bCryptPasswordEncoder.matches(oldPassword, USER.getPassword())).willReturn(false);

// when
AuthErrorException ex = assertThrows(AuthErrorException.class, () -> authService.updatePassword(userId, req));

// then
assertThat(ex.getErrorCode()).isEqualTo(AuthErrorCode.INVALID_PASSWORD);
}
}
}
}
67 changes: 32 additions & 35 deletions src/test/java/com/rabbitmqprac/service/ChatMessageServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,37 @@ public class ChatMessageServiceTest {
private static final ChatRoomFixture CHAT_ROOM_FIXTURE = ChatRoomFixture.FIRST_CHAT_ROOM;
private static final ChatMessageFixture CHAT_MESSAGE_FIXTURE = ChatMessageFixture.FIRST_CHAT_MESSAGE;

private User user = mock(User.class);
private ChatRoom chatRoom = mock(ChatRoom.class);
private ChatMessage chatMessage = mock(ChatMessage.class);
private static final User USER = mock(User.class);
private static final ChatRoom CHAT_ROOM = mock(ChatRoom.class);
private static final ChatMessage CHAT_MESSAGE = mock(ChatMessage.class);

@BeforeEach
void setUp() {
given(user.getId()).willReturn(USER_FIXTURE.getId());
given(chatRoom.getId()).willReturn(CHAT_ROOM_FIXTURE.getId());

given(chatMessage.getId()).willReturn(CHAT_MESSAGE_FIXTURE.getId());
given(chatMessage.getUser()).willReturn(user);
given(chatMessage.getChatRoom()).willReturn(chatRoom);
given(USER.getId()).willReturn(USER_FIXTURE.getId());
given(CHAT_ROOM.getId()).willReturn(CHAT_ROOM_FIXTURE.getId());
given(CHAT_MESSAGE.getId()).willReturn(CHAT_MESSAGE_FIXTURE.getId());
given(CHAT_MESSAGE.getUser()).willReturn(USER);
given(CHAT_MESSAGE.getChatRoom()).willReturn(CHAT_ROOM);
}

@Nested
@DisplayName("채팅 메시지 전송 시나리오")
class SendMessageScenario {
private final ChatMessageReq req = new ChatMessageReq("content");

@Nested
@DisplayName("채팅 메시지 전송 성공 시나리오")
class SendMessageSuccessScenario {
@DisplayName("성공 시나리오")
class SuccessScenario {
@Test
@DisplayName("성공")
@DisplayName("채팅 메시지 전송 성공")
void sendMessageSuccess() {
// given
ChatMessageReq req = new ChatMessageReq("content");
given(entityFacade.readUser(user.getId())).willReturn(user);
given(entityFacade.readChatRoom(chatRoom.getId())).willReturn(chatRoom);
given(chatMessageRepository.save(any(ChatMessage.class))).willReturn(chatMessage);
given(entityFacade.readUser(USER.getId())).willReturn(USER);
given(entityFacade.readChatRoom(CHAT_ROOM.getId())).willReturn(CHAT_ROOM);
given(chatMessageRepository.save(any(ChatMessage.class))).willReturn(CHAT_MESSAGE);

// when
chatMessageService.sendMessage(user.getId(), chatRoom.getId(), req);
chatMessageService.sendMessage(USER.getId(), CHAT_ROOM.getId(), req);

// then
verify(rabbitPublisher).publish(any(Long.class), any(ChatMessageRes.class));
Expand All @@ -96,24 +96,24 @@ void sendMessageSuccess() {
@Nested
@DisplayName("채팅 메시지 범위 조회 시나리오")
class ReadMessageBetweenScenario {
private final Long from = 2L;
private final Long to = 10L;

@Nested
@DisplayName("채팅 메시지 범위 조회 성공 시나리오")
class ReadMessageBetweenSuccessScenario {
@DisplayName("성공 시나리오")
class SuccessScenario {
@Test
@DisplayName("안 읽은 메시지가 존재하는 경우")
void readMessageBetweenSuccessWhenExistsUnReadMessage() {
// given
final Long from = 2L;
final Long to = 10L;
given(chatMessageRepository.findByChatRoomIdAndIdBetween(user.getId(), from, to))
.willReturn(List.of(chatMessage));

long lastMessageId = chatRoom.getId() - 1; // chatRoomId보다 작은 값
given(chatMessageStatusService.readLastReadMessageId(user.getId(), chatRoom.getId()))
given(chatMessageRepository.findByChatRoomIdAndIdBetween(CHAT_ROOM.getId(), from, to))
.willReturn(List.of(CHAT_MESSAGE));
long lastMessageId = CHAT_ROOM.getId() - 1;
given(chatMessageStatusService.readLastReadMessageId(USER.getId(), CHAT_ROOM.getId()))
.willReturn(lastMessageId);

// when
chatMessageService.readChatMessagesBetween(user.getId(), chatRoom.getId(), from, to);
chatMessageService.readChatMessagesBetween(USER.getId(), CHAT_ROOM.getId(), from, to);

// then
verify(chatMessageStatusService).saveLastReadMessageId(any(Long.class), any(Long.class), any(Long.class));
Expand All @@ -123,17 +123,14 @@ void readMessageBetweenSuccessWhenExistsUnReadMessage() {
@DisplayName("안 읽은 메시지가 존재하지 않는 경우")
void readMessageBetweenSuccessWhenNotExistsUnReadMessage() {
// given
final Long from = 2L;
final Long to = 10L;
given(chatMessageRepository.findByChatRoomIdAndIdBetween(user.getId(), from, to))
.willReturn(List.of(chatMessage));

long lastMessageId = chatRoom.getId() + 1; // chatRoomId보다 큰 값
given(chatMessageStatusService.readLastReadMessageId(user.getId(), chatRoom.getId()))
given(chatMessageRepository.findByChatRoomIdAndIdBetween(CHAT_ROOM.getId(), from, to))
.willReturn(List.of(CHAT_MESSAGE));
long lastMessageId = CHAT_ROOM.getId() + 1;
given(chatMessageStatusService.readLastReadMessageId(USER.getId(), CHAT_ROOM.getId()))
.willReturn(lastMessageId);

// when
chatMessageService.readChatMessagesBetween(user.getId(), chatRoom.getId(), from, to);
chatMessageService.readChatMessagesBetween(USER.getId(), CHAT_ROOM.getId(), from, to);

// then
verify(chatMessageStatusService, never()).saveLastReadMessageId(any(Long.class), any(Long.class), any(Long.class));
Expand Down
Loading
Loading