Skip to content

#100 - 현재 유저가 어떠한 제제를 당했는지 확인할 수 있는 API를 만든다. #101

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

Merged
merged 2 commits into from
Aug 24, 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
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)
}

}
Loading