Skip to content

Commit

Permalink
#74 - 탈퇴 및 이용제한을 당한 유저를 필터링한다. (#111)
Browse files Browse the repository at this point in the history
* refactor: 탈퇴 및 이용제한 걸린 친구는 투표를 받을 수 없도록 수정

* refactor: 쿼리 일부 최적화

* refactor: 규제를 당한 유저는 해당 서비스 사용할 수 없도록 수정

* refactor: 제재를 당한 유저는 검색을 할 수 없도록 수정

* feat: Message 관련하여 탈퇴 혹은 규제를 당한 유저가 서비스를 사용할 수 없도록 수정

* refactor: loginUserId 추가

* style: 사용하지 않는 import 제거

* refactor: EventUtils 선언

* refactor: ApplicationEventPublisher Utils로 변경

* test: 쪽지 테스트 추가

* style: 개행 제거

* test: VoteTest 추가

* test: UserSearchTest 추가

* test: classmate 찾는 jpa method 테스트 추가

* test: Test 전부 통과하도록 수정

* test: Search User Service Test 추가
  • Loading branch information
kpeel5839 authored Sep 3, 2024
1 parent 9964b29 commit 2cdf300
Show file tree
Hide file tree
Showing 32 changed files with 892 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ class MessageScheduler(
fun scheduleMessageUpdate() {
schedulerMessageUseCase.sendScheduledMessages()
}

}
6 changes: 3 additions & 3 deletions app/src/main/kotlin/com/wespot/vote/VoteController.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wespot.vote

import com.wespot.vote.dto.request.VoteRequests
import com.wespot.vote.dto.response.SaveVoteResponse
import com.wespot.vote.dto.response.SavedVoteResponse
import com.wespot.vote.dto.response.VoteItems
import com.wespot.vote.dto.response.received.ReceivedVoteResponse
import com.wespot.vote.dto.response.received.ReceivedVotesResponses
Expand Down Expand Up @@ -41,8 +41,8 @@ class VoteController(
@PostMapping
fun createVote(
@RequestBody requests: VoteRequests
): ResponseEntity<SaveVoteResponse> {
val savedId: SaveVoteResponse = savedVoteUseCase.saveVote(requests)
): ResponseEntity<SavedVoteResponse> {
val savedId: SavedVoteResponse = savedVoteUseCase.saveVote(requests)
return ResponseEntity.status(HttpStatus.CREATED)
.body(savedId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import com.wespot.exception.CustomException
import com.wespot.exception.ExceptionView
import com.wespot.school.port.out.SchoolPort
import com.wespot.user.SocialType
import com.wespot.user.event.CreatedVoteEvent
import com.wespot.user.event.SignUpUserEvent
import com.wespot.user.event.WelcomeMessageEvent
import com.wespot.user.fixture.UserFixture
import com.wespot.user.port.out.FCMPort
import com.wespot.user.port.out.ProfilePort
Expand All @@ -36,7 +33,6 @@ import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.spyk
import org.springframework.context.ApplicationEventPublisher
import org.springframework.http.HttpStatus
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.core.Authentication
Expand All @@ -56,7 +52,6 @@ class AuthServiceTest : BehaviorSpec({
val authenticationManager = mockk<AuthenticationManager>()
val passwordEncoder = mockk<PasswordEncoder>()
val refreshTokenService = mockk<RefreshTokenService>()
val eventPublisher = mockk<ApplicationEventPublisher>()
val fcmPort = mockk<FCMPort>()
val restrictionPort = mockk<RestrictionPort>()
val personalInfoPort = mockk<PersonalInfoPort>()
Expand All @@ -77,7 +72,6 @@ class AuthServiceTest : BehaviorSpec({
authenticationManager = authenticationManager,
passwordEncoder = passwordEncoder,
refreshTokenService = refreshTokenService,
eventPublisher = eventPublisher,
fcmPort = fcmPort,
secretKey = secretKey,
restrictionPort = restrictionPort,
Expand Down Expand Up @@ -219,9 +213,6 @@ class AuthServiceTest : BehaviorSpec({
every { userPort.save(any()) } returns user
every { authService.saveRelatedEntities(user, signUpRequest, authData.fcmToken) } just Runs
every { authService.signIn(any()) } returns tokenAndUserDetailResponse
every { eventPublisher.publishEvent(SignUpUserEvent(user)) } returns Unit
every { eventPublisher.publishEvent(CreatedVoteEvent(user)) } returns Unit
every { eventPublisher.publishEvent(WelcomeMessageEvent(user)) } returns Unit

`when`("사용자가 signUp을 호출할 때") {
val response = authService.signUp(signUpRequest)
Expand Down
152 changes: 148 additions & 4 deletions app/src/test/kotlin/com/wespot/message/domain/MessageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.wespot.exception.CustomException
import com.wespot.message.Message
import com.wespot.message.MessageTimeValidator
import com.wespot.message.fixture.MessageFixture
import com.wespot.user.RestrictionType
import com.wespot.user.fixture.ProfileFixture
import com.wespot.user.fixture.UserFixture
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -63,8 +66,8 @@ class MessageTest : BehaviorSpec({
val shouldThrow = shouldThrow<CustomException> {
Message.sendMessage(
badWordsContent,
1,
2,
UserFixture.createWithId(1),
UserFixture.createWithId(2),
"senderName",
false
)
Expand All @@ -77,8 +80,8 @@ class MessageTest : BehaviorSpec({
val shouldThrow = shouldThrow<CustomException> {
Message.sendMessage(
emptyContent,
1,
2,
UserFixture.createWithId(1),
UserFixture.createWithId(2),
"senderName",
false
)
Expand All @@ -90,4 +93,145 @@ class MessageTest : BehaviorSpec({
unmockkStatic(LocalDateTime::class)
}

given("쪽지를 수정할 때") {
val restrictionUser =
UserFixture.createUserWithIdAndRestrictionTypeAndRestrictDay(
1,
listOf(Pair(RestrictionType.PERMANENT_BAN_MESSAGE_REPORT, Long.MAX_VALUE))
)
val withDrawUser = UserFixture.createWithId(2).withdraw().completeWithdraw(ProfileFixture.createWithId(1))
val restrictionUserMessage = MessageFixture.createMessage("Hello", 2, 1, "senderName")
val withDrawUserMessage = MessageFixture.createMessage("Hello", 1, 2, "senderName")
`when`("수정하는 자가 탈퇴 혹은 이용제재를 당한 사용자라면") {
val shouldThrow1 = shouldThrow<CustomException> {
restrictionUserMessage.updateMessage(
"Hello1",
restrictionUser,
withDrawUser.id,
restrictionUser.name
)
}
val shouldThrow2 = shouldThrow<CustomException> {
withDrawUserMessage.updateMessage(
"Hello2",
withDrawUser,
restrictionUser.id,
withDrawUser.name
)
}
then("예외가 발생한다.") {
shouldThrow1 shouldHaveMessage "이용제한을 당한 학생은 해당 서비스를 이용할 수 없습니다."
shouldThrow2 shouldHaveMessage "탈퇴한 학생은 해당 서비스를 이용할 수 없습니다."
}
}
}

given("쪽지를 보낼 때") {
val restrictionUser =
UserFixture.createUserWithIdAndRestrictionTypeAndRestrictDay(
1,
listOf(Pair(RestrictionType.PERMANENT_BAN_MESSAGE_REPORT, Long.MAX_VALUE))
)
val withDrawUser = UserFixture.createWithId(2).withdraw().completeWithdraw(ProfileFixture.createWithId(1))
`when`("송신자가 탈퇴 혹은 이용제재를 당한 사용자라면") {
val shouldThrow1 = shouldThrow<CustomException> {
Message.sendMessage(
"hello",
UserFixture.createWithId(3),
withDrawUser,
withDrawUser.name,
false,
)
}
val shouldThrow2 = shouldThrow<CustomException> {
Message.sendMessage(
"hello",
UserFixture.createWithId(3),
restrictionUser,
restrictionUser.name,
false
)
}

then("예외가 발생한다.") {
shouldThrow1 shouldHaveMessage "탈퇴한 학생은 해당 서비스를 이용할 수 없습니다."
shouldThrow2 shouldHaveMessage "이용제한을 당한 학생은 해당 서비스를 이용할 수 없습니다."
}
}

`when`("수신자가 탈퇴 혹은 이용제재를 당한 사용자라면") {
val shouldThrow1 = shouldThrow<CustomException> {
Message.sendMessage(
"hello",
withDrawUser,
UserFixture.createWithId(3),
"senderName",
false,
)
}
val shouldThrow2 = shouldThrow<CustomException> {
Message.sendMessage(
"hello",
restrictionUser,
UserFixture.createWithId(3),
"senderName",
false
)
}

then("예외가 발생한다.") {
shouldThrow1 shouldHaveMessage "탈퇴한 학생은 해당 서비스를 이용할 수 없습니다."
shouldThrow2 shouldHaveMessage "이용제한을 당한 학생은 해당 서비스를 이용할 수 없습니다."
}
}
}

given("받은 쪽지를 삭제할 때") {
val restrictionUser =
UserFixture.createUserWithIdAndRestrictionTypeAndRestrictDay(
1,
listOf(Pair(RestrictionType.PERMANENT_BAN_MESSAGE_REPORT, Long.MAX_VALUE))
)
val withDrawUser = UserFixture.createWithId(2).withdraw().completeWithdraw(ProfileFixture.createWithId(1))
val restrictionUserMessage = MessageFixture.createMessage("Hello", 2, 1, "senderName")
val withDrawUserMessage = MessageFixture.createMessage("Hello", 1, 2, "senderName")
`when`("수신자가 탈퇴 혹은 이용제재를 당한 사용자라면") {
val shouldThrow1 = shouldThrow<CustomException> {
withDrawUserMessage.receivedMessageSoftDelete(restrictionUser)
}
val shouldThrow2 = shouldThrow<CustomException> {
restrictionUserMessage.receivedMessageSoftDelete(withDrawUser)
}

then("예외가 발생한다.") {
shouldThrow1 shouldHaveMessage "이용제한을 당한 학생은 해당 서비스를 이용할 수 없습니다."
shouldThrow2 shouldHaveMessage "탈퇴한 학생은 해당 서비스를 이용할 수 없습니다."
}
}
}

given("보낸 쪽지를 삭제할 때") {
val restrictionUser =
UserFixture.createUserWithIdAndRestrictionTypeAndRestrictDay(
1,
listOf(Pair(RestrictionType.PERMANENT_BAN_MESSAGE_REPORT, Long.MAX_VALUE))
)
val withDrawUser = UserFixture.createWithId(2).withdraw().completeWithdraw(ProfileFixture.createWithId(1))
val restrictionUserMessage = MessageFixture.createMessage("Hello", 2, 1, "senderName")
val withDrawUserMessage = MessageFixture.createMessage("Hello", 1, 2, "senderName")
`when`("송신자가 탈퇴 혹은 이용제재를 당한 사용자라면") {
val shouldThrow1 = shouldThrow<CustomException> {
restrictionUserMessage.sendMessageSoftDelete(restrictionUser)
}
val shouldThrow2 = shouldThrow<CustomException> {
withDrawUserMessage.sendMessageSoftDelete(withDrawUser)
}

then("예외가 발생한다.") {
shouldThrow1 shouldHaveMessage "이용제한을 당한 학생은 해당 서비스를 이용할 수 없습니다."
shouldThrow2 shouldHaveMessage "탈퇴한 학생은 해당 서비스를 이용할 수 없습니다."
}
}
}

})
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.wespot.message.fixture
import com.wespot.message.Message
import com.wespot.message.MessageContent
import com.wespot.message.MessageType
import com.wespot.user.fixture.UserFixture
import java.time.LocalDateTime


Expand Down Expand Up @@ -89,8 +90,8 @@ object MessageFixture {
): Message {
return Message.sendMessage(
content = content,
receiverId = receiverId,
senderId = senderId,
receiver = UserFixture.createWithId(receiverId),
sender = UserFixture.createWithId(senderId),
senderName = senderName,
isAnonymous = false,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.wespot.message.MessageContent
import com.wespot.message.MessageTimeValidator
import com.wespot.message.dto.request.UpdateMessageRequest
import com.wespot.message.dto.response.UpdateMessageResponse
import com.wespot.message.event.ReadMessageByReceiverEvent
import com.wespot.message.fixture.MessageFixture
import com.wespot.message.port.out.MessagePort
import com.wespot.user.User
Expand All @@ -18,7 +17,6 @@ import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.springframework.context.ApplicationEventPublisher
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
Expand All @@ -27,11 +25,9 @@ class ModifyMessageServiceTest : BehaviorSpec({

val messagePort = mockk<MessagePort>()
val userPort = mockk<UserPort>()
val eventPublisher = mockk<ApplicationEventPublisher>()
val modifyMessageService = ModifyMessageService(
messagePort = messagePort,
userPort = userPort,
eventPublisher = eventPublisher
)

lateinit var sender: User
Expand Down Expand Up @@ -99,16 +95,6 @@ class ModifyMessageServiceTest : BehaviorSpec({
every { userPort.findById(1) } returns sender
every { messagePort.findById(messageId) } returns receivedMessage
every { messagePort.save(any()) } returns receivedMessage.copy(isReceiverRead = true)
every {
eventPublisher.publishEvent(
ReadMessageByReceiverEvent(
sender,
receiver,
messageId,
false
)
)
} returns Unit

then("메시지가 읽은 상태로 업데이트되어야 한다") {
modifyMessageService.readMessage(messageId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.wespot.message.Message
import com.wespot.message.MessageTimeValidator
import com.wespot.message.dto.request.SendMessageRequest
import com.wespot.message.dto.response.SendMessageResponse
import com.wespot.message.event.MessageLimitEvent
import com.wespot.message.event.ReceivedMessageEvent
import com.wespot.message.fixture.MessageFixture
import com.wespot.message.port.out.MessagePort
import com.wespot.user.User
Expand All @@ -19,7 +17,6 @@ import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.springframework.context.ApplicationEventPublisher
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
Expand All @@ -29,12 +26,10 @@ class SendMessageServiceTest : BehaviorSpec({
val messagePort = mockk<MessagePort>()
val userPort = mockk<UserPort>()
val blockedUserPort = mockk<BlockedUserPort>()
val eventPublisher = mockk<ApplicationEventPublisher>()
val sendMessageService = SendMessageService(
messagePort = messagePort,
userPort = userPort,
blockedUserPort = blockedUserPort,
eventPublisher = eventPublisher
)

lateinit var sender: User
Expand Down Expand Up @@ -75,8 +70,6 @@ class SendMessageServiceTest : BehaviorSpec({
every { messagePort.save(any()) } returns message
every { messagePort.sendMessageCount(sender.id) } returns 0
every { messagePort.hasSentMessageToday(sender.id, receiver.id) } returns false
every { eventPublisher.publishEvent(MessageLimitEvent(sender.id, 0)) } returns Unit
every { eventPublisher.publishEvent(ReceivedMessageEvent(receiver, 0)) } returns Unit
every { blockedUserPort.existsByBlockerIdAndBlockedId(1, 2) } returns false
every { blockedUserPort.existsByBlockerIdAndBlockedId(2, 1) } returns false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class RealSendMessageServiceTest @Autowired constructor(
val message = messagePort.save(
Message.sendMessage(
content = "content",
receiverId = receiver.id,
senderId = sender.id,
receiver = receiver,
sender = sender,
senderName = "senderName",
isAnonymous = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.wespot.common.service.ServiceTest
import com.wespot.message.Message
import com.wespot.message.MessageTimeValidator
import com.wespot.message.event.ReceivedMessageEvent
import com.wespot.message.fixture.MessageFixture
import com.wespot.message.port.out.MessagePort
import com.wespot.notification.NotificationType
import com.wespot.notification.port.out.NotificationPort
Expand Down Expand Up @@ -40,8 +39,8 @@ class NotificationEventListenerTest @Autowired constructor(
val message = messagePort.save(
Message.sendMessage(
content = "content",
receiverId = receiver.id,
senderId = sender.id,
receiver = receiver,
sender = sender,
senderName = sender.name,
isAnonymous = false
)
Expand Down
Loading

0 comments on commit 2cdf300

Please sign in to comment.