Skip to content

Commit

Permalink
merge: (#101) Admin 로그인 기능 설계 (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 authored Oct 12, 2022
2 parents 59b8b3c + 6f39b0e commit 3690ed7
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package team.comit.simtong.domain.user.usecase

import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.user.dto.SignInRequest
import team.comit.simtong.domain.user.exception.DifferentPasswordException
import team.comit.simtong.domain.user.exception.DifferentPermissionAccountException
import team.comit.simtong.domain.user.exception.UserNotFoundException
import team.comit.simtong.domain.user.model.Authority
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.domain.user.spi.UserJwtPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import team.comit.simtong.global.annotation.UseCase

/**
*
* Admin의 로그인 기능을 담당하는 AdminSignInUseCase
*
* @author Chokyunghyeon
* @date 2022/10/04
* @version 1.0.0
**/
@UseCase
class AdminSignInUseCase(
private val queryUserPort: QueryUserPort,
private val userJwtPort: UserJwtPort,
private val userSecurityPort: UserSecurityPort
) {

fun execute(request: SignInRequest): TokenResponse {
val admin = queryUserPort.queryUserByEmployeeNumber(request.employeeNumber)
?: throw UserNotFoundException.EXCEPTION

if (Authority.ROLE_ADMIN != admin.authority) {
throw DifferentPermissionAccountException.EXCEPTION
}

if (!userSecurityPort.compare(request.password, admin.password)) {
throw DifferentPasswordException.EXCEPTION
}

return userJwtPort.receiveToken(
userId = admin.id,
authority = admin.authority
)
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package team.comit.simtong.domain.user.usecase

import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.user.dto.SignInRequest
import team.comit.simtong.domain.user.exception.DifferentPasswordException
import team.comit.simtong.domain.user.exception.DifferentPermissionAccountException
import team.comit.simtong.domain.user.exception.UserNotFoundException
import team.comit.simtong.domain.user.model.Authority
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.domain.user.spi.UserJwtPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import team.comit.simtong.domain.user.dto.SignInRequest
import team.comit.simtong.global.annotation.UseCase

/**
Expand All @@ -29,6 +30,10 @@ class SignInUseCase(
val user = queryUserPort.queryUserByEmployeeNumber(request.employeeNumber)
?: throw UserNotFoundException.EXCEPTION

if (Authority.ROLE_COMMON != user.authority) {
throw DifferentPermissionAccountException.EXCEPTION
}

if (!userSecurityPort.compare(request.password, user.password)) {
throw DifferentPasswordException.EXCEPTION
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package team.comit.simtong.domain.user.usecase

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.user.dto.SignInRequest
import team.comit.simtong.domain.user.exception.DifferentPasswordException
import team.comit.simtong.domain.user.exception.DifferentPermissionAccountException
import team.comit.simtong.domain.user.exception.UserNotFoundException
import team.comit.simtong.domain.user.model.Authority
import team.comit.simtong.domain.user.model.User
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.domain.user.spi.UserJwtPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import java.util.*

@ExtendWith(SpringExtension::class)
class AdminSignInUseCaseTests {

@MockBean
private lateinit var queryUserPort: QueryUserPort

@MockBean
private lateinit var userSecurityPort: UserSecurityPort

@MockBean
private lateinit var userJwtPort: UserJwtPort

private lateinit var adminSignInUseCase: AdminSignInUseCase

private val employeeNumber: Int = 1234567891

private val adminStub: User by lazy {
User(
id = UUID.randomUUID(),
nickname = "test nickname",
name = "test name",
email = "[email protected]",
password = "test password",
employeeNumber = employeeNumber,
authority = Authority.ROLE_ADMIN,
spotId = UUID.randomUUID(),
teamId = UUID.randomUUID(),
profileImagePath = "test path"
)
}

private val userStub: User by lazy {
User(
id = UUID.randomUUID(),
nickname = "test nickname",
name = "test name",
email = "[email protected]",
password = "test password",
employeeNumber = employeeNumber,
authority = Authority.ROLE_COMMON,
spotId = UUID.randomUUID(),
teamId = UUID.randomUUID(),
profileImagePath = "test path"
)
}

private val requestStub: SignInRequest by lazy {
SignInRequest(
employeeNumber = employeeNumber,
password = "test password"
)
}

private val responseStub: TokenResponse by lazy {
TokenResponse(
accessToken = "test access token",
accessTokenExp = Date(),
refreshToken = "test refresh token"
)
}

@BeforeEach
fun setUp() {
adminSignInUseCase = AdminSignInUseCase(
queryUserPort,
userJwtPort,
userSecurityPort
)
}

@Test
fun `로그인 성공`() {
// given
given(queryUserPort.queryUserByEmployeeNumber(employeeNumber))
.willReturn(adminStub)

given(userSecurityPort.compare(requestStub.password, adminStub.password))
.willReturn(true)

given(userJwtPort.receiveToken(adminStub.id, adminStub.authority))
.willReturn(responseStub)

// when
val response = adminSignInUseCase.execute(requestStub)

// then
assertThat(response).isEqualTo(responseStub)
}

@Test
fun `비밀번호 불일치`() {
// given
given(queryUserPort.queryUserByEmployeeNumber(employeeNumber))
.willReturn(adminStub)

given(userSecurityPort.compare(requestStub.password, adminStub.password))
.willReturn(false)

// when & then
assertThrows<DifferentPasswordException> {
adminSignInUseCase.execute(requestStub)
}
}

@Test
fun `계정 찾기 실패`() {
// given
given(queryUserPort.queryUserByEmployeeNumber(employeeNumber))
.willReturn(null)

// when & then
assertThrows<UserNotFoundException> {
adminSignInUseCase.execute(requestStub)
}
}

@Test
fun `유저 계정`() {
// given
given(queryUserPort.queryUserByEmployeeNumber(employeeNumber))
.willReturn(userStub)

// when & then
assertThrows<DifferentPermissionAccountException> {
adminSignInUseCase.execute(requestStub)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.user.dto.SignInRequest
import team.comit.simtong.domain.user.exception.DifferentPasswordException
import team.comit.simtong.domain.user.exception.DifferentPermissionAccountException
import team.comit.simtong.domain.user.exception.UserNotFoundException
import team.comit.simtong.domain.user.model.Authority
import team.comit.simtong.domain.user.model.User
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.domain.user.spi.UserJwtPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import team.comit.simtong.domain.user.dto.SignInRequest
import java.util.*

@ExtendWith(SpringExtension::class)
Expand Down Expand Up @@ -50,6 +51,21 @@ class SignInUseCaseTests {
)
}

private val adminStub: User by lazy {
User(
id = UUID.randomUUID(),
nickname = "test nickname",
name = "test name",
email = "[email protected]",
password = "test password",
employeeNumber = employeeNumber,
authority = Authority.ROLE_ADMIN,
spotId = UUID.randomUUID(),
teamId = UUID.randomUUID(),
profileImagePath = "test path"
)
}

private val requestStub: SignInRequest by lazy {
SignInRequest(
employeeNumber = employeeNumber,
Expand Down Expand Up @@ -116,4 +132,16 @@ class SignInUseCaseTests {
}
}

@Test
fun `관리자 계정`() {
// given
given(queryUserPort.queryUserByEmployeeNumber(employeeNumber))
.willReturn(adminStub)

// when & then
assertThrows<DifferentPermissionAccountException> {
signInUseCase.execute(requestStub)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class UserInfoUseCaseTests {
authority = Authority.ROLE_COMMON,
spotId = id,
teamId = id,
adminCode = null,
profileImagePath = profileImagePath
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum class UserErrorCode(

// 401
DIFFERENT_PASSWORD(401, "비밀번호가 일치하지 않음"),
DIFFERENT_PERMISSION_ACCOUNT(401, "다른 권한의 계정"),

// 404
USER_NOT_FOUND(404, "유저를 찾을 수 없음");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package team.comit.simtong.domain.user.exception

import team.comit.simtong.domain.user.error.UserErrorCode
import team.comit.simtong.global.error.BusinessException

/**
*
* 다른 권한의 계정 에러를 발생시키는 DifferentPermissionAccountException
*
* @author Chokyunghyeon
* @date 2022/10/12
* @version 1.0.0
**/
class DifferentPermissionAccountException private constructor() : BusinessException(UserErrorCode.DIFFERENT_PERMISSION_ACCOUNT) {

companion object {
@JvmField
val EXCEPTION = DifferentPermissionAccountException()
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package team.comit.simtong.domain.user.model

import team.comit.simtong.global.annotation.Aggregate
import java.util.*
import java.util.UUID

/**
*
Expand Down Expand Up @@ -32,8 +32,6 @@ data class User(

val teamId: UUID,

val adminCode: String? = null,

val profileImagePath: String
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class SecurityConfig(
.antMatchers(HttpMethod.POST, "/files").permitAll()
.antMatchers(HttpMethod.POST, "/files/list").permitAll()

// admins
.antMatchers(HttpMethod.POST, "/admins/tokens").permitAll()

.anyRequest().authenticated()

http
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class UserJpaEntity(
@Column(columnDefinition = "CHAR(60)")
val password: String,

@Column(columnDefinition = "CHAR(60)")
val adminCode: String?,

@field:NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "spot_id", columnDefinition = "BINARY(16)")
Expand Down
Loading

0 comments on commit 3690ed7

Please sign in to comment.