-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
14 changed files
with
296 additions
and
10 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...-application/src/main/kotlin/team/comit/simtong/domain/user/usecase/AdminSignInUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...ication/src/test/kotlin/team/comit/simtong/domain/user/usecase/AdminSignInUseCaseTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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, | ||
|
@@ -116,4 +132,16 @@ class SignInUseCaseTests { | |
} | ||
} | ||
|
||
@Test | ||
fun `관리자 계정`() { | ||
// given | ||
given(queryUserPort.queryUserByEmployeeNumber(employeeNumber)) | ||
.willReturn(adminStub) | ||
|
||
// when & then | ||
assertThrows<DifferentPermissionAccountException> { | ||
signInUseCase.execute(requestStub) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...in/kotlin/team/comit/simtong/domain/user/exception/DifferentPermissionAccountException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.