Skip to content

Commit

Permalink
#100 - 현재 유저가 어떠한 제제를 당했는지 확인할 수 있는 API를 만든다. (#101)
Browse files Browse the repository at this point in the history
* test: 테스트 수정

* style: 스타일 수정
  • Loading branch information
kpeel5839 authored Aug 24, 2024
1 parent d9fbafe commit 2f15956
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/src/main/kotlin/com/wespot/user/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import com.wespot.user.dto.request.ModifiedSettingRequest
import com.wespot.user.dto.request.UpdateProfileRequest
import com.wespot.user.dto.response.BackgroundListResponse
import com.wespot.user.dto.response.CharacterListResponse
import com.wespot.user.dto.response.CheckedRestrictionResponse
import com.wespot.user.dto.response.UserResponse
import com.wespot.user.dto.response.UserSettingResponse
import com.wespot.user.port.`in`.CheckedUserRestrictionUseCase
import com.wespot.user.port.`in`.UserSettingUseCase
import com.wespot.user.port.`in`.UserUseCase
import org.springframework.http.ResponseEntity
Expand All @@ -19,7 +21,8 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping("/api/v1/users")
class UserController(
private val userUseCase: UserUseCase,
private val userSettingUseCase: UserSettingUseCase
private val userSettingUseCase: UserSettingUseCase,
private val checkedUserRestrictionUseCase: CheckedUserRestrictionUseCase
) {

@GetMapping("/me")
Expand Down Expand Up @@ -73,4 +76,11 @@ class UserController(
.build()
}

@GetMapping("/restrictions/me")
fun checkMyRestriction(): ResponseEntity<CheckedRestrictionResponse> {
val response = checkedUserRestrictionUseCase.getUserRestriction()

return ResponseEntity.ok(response)
}

}
41 changes: 40 additions & 1 deletion app/src/test/kotlin/com/wespot/user/fixture/UserFixture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.wespot.user.User
import com.wespot.user.UserConsent
import com.wespot.user.UserIntroduction
import com.wespot.user.Gender
import com.wespot.user.RestrictionType
import com.wespot.user.dto.request.UpdateProfileRequest
import org.springframework.security.authentication.TestingAuthenticationToken
import org.springframework.security.core.context.SecurityContextHolder
Expand Down Expand Up @@ -254,7 +255,7 @@ object UserFixture {
)

fun createWithIdAndSchoolIdAndGradeAndClassNumber(
id:Long,
id: Long,
schoolId: Long,
grade: Int,
classNumber: Int
Expand Down Expand Up @@ -327,4 +328,42 @@ object UserFixture {
withdrawAt = LocalDateTime.now(),
)

fun createUserWithRestrictionTypeAndRestrictDay(restrictions: List<Pair<RestrictionType, Long>>): User {
var restriction = Restriction.createInitialState()
for (eachRestriction in restrictions) {
restriction = restriction.addRestrict(eachRestriction.first, eachRestriction.second)
}
return User(
id = 0,
email = "TestEmail@Kakako",
password = "TestPassword",
role = Role.USER,
name = "TestUser",
introduction = UserIntroduction.from("hello"),
gender = Gender.MALE,
schoolId = 1L,
grade = 1,
classNumber = 1,
setting = Setting(),
profile = Profile(0, "black", "image.png"),
fcm = FCM(0, "token", LocalDateTime.now()),
social = Social(
socialType = SocialType.KAKAO,
socialId = "1123123",
socialEmail = null,
socialRefreshToken = "refreshToken"
),
userConsent = UserConsent(
id = 0,
consentType = ConsentType.MARKETING,
consentValue = true,
consentedAt = LocalDateTime.now()
),
restriction = restriction,
createdAt = LocalDateTime.now(),
updatedAt = LocalDateTime.now(),
withdrawAt = LocalDateTime.now()
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.wespot.user.service

import com.wespot.common.service.ServiceTest
import com.wespot.user.RestrictionType
import com.wespot.user.fixture.UserFixture
import com.wespot.user.port.out.UserPort
import io.kotest.matchers.shouldBe
import org.springframework.beans.factory.annotation.Autowired
import java.time.LocalDate
import kotlin.test.Test

class CheckedUserRestrictionServiceTest @Autowired constructor(
private val userPort: UserPort,
private val checkedUserRestrictionService: CheckedUserRestrictionService
) : ServiceTest() {

@Test
fun `사용자가 제한을 당하지 않은 것을 확인한다`() {
// given
val user = UserFixture.createWithId(0)
val savedUser = userPort.save(user)
UserFixture.setSecurityContextUser(savedUser)

// when
val response = checkedUserRestrictionService.getUserRestriction()

// then
response.messageRestrictionType shouldBe RestrictionType.NONE
response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31)
response.voteRestrictionType shouldBe RestrictionType.NONE
response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31)
}

@Test
fun `사용자가 메시지로 인해 제한을 당한 것을 확인한다`() {
// given
val user =
UserFixture.createUserWithRestrictionTypeAndRestrictDay(
listOf(
Pair(
RestrictionType.PERMANENT_BAN_MESSAGE_REPORT,
Long.MAX_VALUE
)
)
)
val savedUser = userPort.save(user)
UserFixture.setSecurityContextUser(savedUser)

// when
val response = checkedUserRestrictionService.getUserRestriction()

// then
response.messageRestrictionType shouldBe RestrictionType.PERMANENT_BAN_MESSAGE_REPORT
response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31)
response.voteRestrictionType shouldBe RestrictionType.NONE
response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31)
}

@Test
fun `사용자가 투표로 인해 제한을 당한 것을 확인한다`() {
// given
val user =
UserFixture.createUserWithRestrictionTypeAndRestrictDay(
listOf(
Pair(
RestrictionType.PERMANENT_BAN_VOTE_REPORT,
Long.MAX_VALUE
)
)
)
val savedUser = userPort.save(user)
UserFixture.setSecurityContextUser(savedUser)

// when
val response = checkedUserRestrictionService.getUserRestriction()

// then
response.messageRestrictionType shouldBe RestrictionType.NONE
response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31)
response.voteRestrictionType shouldBe RestrictionType.PERMANENT_BAN_VOTE_REPORT
response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31)
}

@Test
fun `사용자가 투표, 쪽지로 인해 제한을 당한 것을 확인한다`() {
// given
val user =
UserFixture.createUserWithRestrictionTypeAndRestrictDay(
listOf(
Pair(
RestrictionType.PERMANENT_BAN_VOTE_REPORT,
Long.MAX_VALUE
),
Pair(
RestrictionType.PERMANENT_BAN_MESSAGE_REPORT,
Long.MAX_VALUE
)
)
)
val savedUser = userPort.save(user)
UserFixture.setSecurityContextUser(savedUser)

// when
val response = checkedUserRestrictionService.getUserRestriction()

// then
response.messageRestrictionType shouldBe RestrictionType.PERMANENT_BAN_MESSAGE_REPORT
response.messageReleaseDate shouldBe LocalDate.of(9999, 12, 31)
response.voteRestrictionType shouldBe RestrictionType.PERMANENT_BAN_VOTE_REPORT
response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31)
}

@Test
fun `쪽지 90일 제한을 받은 사용자의 제한을 확인한다`() {
// given
val now = LocalDate.now()
val user =
UserFixture.createUserWithRestrictionTypeAndRestrictDay(
listOf(
Pair(
RestrictionType.TEMPORARY_BAN_MESSAGE_REPORT,
90
)
)
)
val savedUser = userPort.save(user)
UserFixture.setSecurityContextUser(savedUser)

// when
val response = checkedUserRestrictionService.getUserRestriction()

// then
response.messageRestrictionType shouldBe RestrictionType.TEMPORARY_BAN_MESSAGE_REPORT
response.messageReleaseDate shouldBe now.plusDays(90)
response.voteRestrictionType shouldBe RestrictionType.NONE
response.voteReleaseDate shouldBe LocalDate.of(9999, 12, 31)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.wespot.user.port.out.UserPort
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

class UserSettingServiceTest @Autowired constructor(
private val userSettingService: UserSettingService,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.wespot.user.dto.response

import com.wespot.user.RestrictionType
import com.wespot.user.User
import java.time.LocalDate

data class CheckedRestrictionResponse(
val messageRestrictionType: RestrictionType,
val messageReleaseDate: LocalDate,
val voteRestrictionType: RestrictionType,
val voteReleaseDate: LocalDate,
) {

companion object {

fun from(user: User): CheckedRestrictionResponse {
return CheckedRestrictionResponse(
user.restriction.messageRestriction.restrictionType,
user.restriction.messageRestriction.releaseDate,
user.restriction.voteRestriction.restrictionType,
user.restriction.voteRestriction.releaseDate
)
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wespot.user.port.`in`

import com.wespot.user.dto.response.CheckedRestrictionResponse

interface CheckedUserRestrictionUseCase {

fun getUserRestriction(): CheckedRestrictionResponse

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wespot.user.service

import com.wespot.auth.service.SecurityUtils
import com.wespot.user.dto.response.CheckedRestrictionResponse
import com.wespot.user.port.`in`.CheckedUserRestrictionUseCase
import com.wespot.user.port.out.UserPort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class CheckedUserRestrictionService(
private val userPort: UserPort
) : CheckedUserRestrictionUseCase {

@Transactional(readOnly = true)
override fun getUserRestriction(): CheckedRestrictionResponse {
val loginUser = SecurityUtils.getLoginUser(userPort)

return CheckedRestrictionResponse.from(loginUser)
}

}

0 comments on commit 2f15956

Please sign in to comment.