Skip to content

Commit

Permalink
Merge pull request #235 from GSM-MSG/feat/test_sign_in
Browse files Browse the repository at this point in the history
๐Ÿ”€ :: ๋กœ๊ทธ์ธ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ
  • Loading branch information
Huuuunee authored Sep 27, 2023
2 parents 195d927 + 50e671d commit bac64e1
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ subprojects {

// test
implementation(Dependencies.SPRING_TEST)
implementation(Dependencies.MOCKK)
implementation(Dependencies.MOCKITO)

testImplementation(Dependencies.KOTEST_JUNIT5)
testImplementation(Dependencies.KOTEST_ASSERTIONS_CORE)
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Dependencies {

// test
const val SPRING_TEST = "org.springframework.boot:spring-boot-starter-test:${PluginVersions.SPRING_BOOT_VERSION}"
const val MOCKK = "io.mockk:mockk:${DependencyVersions.MOCKK_VERSION}"
const val MOCKITO = "org.mockito.kotlin:mockito-kotlin:${DependencyVersions.MOCKITO_VERSION}"

// kotest
const val KOTEST_JUNIT5 = "io.kotest:kotest-runner-junit5:${DependencyVersions.KOTEST_VERSION}"
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/DependencyVersions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object DependencyVersions {
const val JWT_VERSION = "0.9.1"
const val REDIS_VERSION = "2.7.2"
const val MYSQL = "8.0.28"
const val MOCKK_VERSION = "1.13.2"
const val MOCKITO_VERSION = "4.0.0"
const val AWS_VERSION = "2.2.6.RELEASE"
const val SERVLET_VERSION = "4.0.1"
const val GAUTH_VERSION = "2.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package team.msg.sms.domain.auth.core.usecase

import gauth.GAuthToken
import gauth.GAuthUserInfo
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.BDDMockito.given
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.any
import team.msg.sms.common.spi.GAuthPort
import team.msg.sms.domain.auth.dto.req.SignInRequestData
import team.msg.sms.domain.auth.dto.res.SignInResponseData
import team.msg.sms.domain.auth.dto.res.TokenResponseData
import team.msg.sms.domain.auth.model.RefreshToken
import team.msg.sms.domain.auth.model.Role
import team.msg.sms.domain.auth.spi.JwtPort
import team.msg.sms.domain.auth.spi.RefreshTokenPort
import team.msg.sms.domain.auth.usecase.SignInUseCase
import team.msg.sms.domain.student.service.StudentService
import team.msg.sms.domain.user.model.User
import team.msg.sms.domain.user.service.UserService
import team.msg.sms.global.annotation.SmsTest
import java.time.LocalDateTime
import java.util.UUID

@SmsTest
class GAuthSignInUseCaseTest {
@Mock
private lateinit var gAuthPort: GAuthPort

@Mock
private lateinit var userService: UserService

@Mock
private lateinit var jwtPort: JwtPort

@Mock
private lateinit var refreshTokenPort: RefreshTokenPort

@Mock
private lateinit var studentService: StudentService

private lateinit var signInUseCase: SignInUseCase

private val email = "test@gsm.hs.kr"
private val name = "test"
private val grade = 2
private val classNum = 4
private val number = 6
private val role = Role.ROLE_STUDENT

private val code = "test_gauth_code"

private val accessTokenExp = LocalDateTime.now()
private val refreshTokenExp = LocalDateTime.now()

private val saveUserStub: User by lazy {
User(
id = UUID.randomUUID(),
email = email,
name = name,
stuNum = "$grade$classNum+0+$number",
roles = mutableListOf(role)
)
}

private val requestStub: SignInRequestData by lazy {
SignInRequestData(code)
}

private val tokenResponseStub: TokenResponseData by lazy {
TokenResponseData(
accessToken = "access_token",
refreshToken = "refreshToken",
accessTokenExp = accessTokenExp,
refreshTokenExp = refreshTokenExp,
)
}

private val responseStub: SignInResponseData by lazy {
SignInResponseData(
accessToken = "access_token",
refreshToken = "refreshToken",
accessTokenExp =accessTokenExp,
refreshTokenExp = refreshTokenExp,
isExist = false,
role = role
)
}

private val refreshTokenStub: RefreshToken by lazy {
RefreshToken(
userId = saveUserStub.id,
token = "refreshToken"
)
}

private val gAuthTokenStub: GAuthToken by lazy {
GAuthToken(
mapOf(
"accessToken" to "gauth_access_token",
"refreshToken" to "gauth_refresh_token"
)
)
}
private val gAuthUserInfoStub: GAuthUserInfo by lazy {
GAuthUserInfo(
mapOf(
"email" to email,
"name" to name,
"grade" to grade,
"classNum" to classNum,
"num" to number,
"gender" to "MALE",
"profileUrl" to "profileImageUrl",
"role" to "ROLE_STUDENT"
)
)
}

@BeforeEach
fun setUp(){
MockitoAnnotations.openMocks(this)

signInUseCase = SignInUseCase(
gAuthPort = gAuthPort,
jwtPort = jwtPort,
refreshTokenPort = refreshTokenPort,
userService = userService,
studentService = studentService
)
}

@Test
fun `ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต`() {

// given
given(gAuthPort.receiveGAuthToken(requestStub.code))
.willReturn(gAuthTokenStub)

given(gAuthPort.receiveUserInfo(gAuthTokenStub.accessToken))
.willReturn(gAuthUserInfoStub)

given(userService.getRoleByGAuthInfo(gAuthUserInfoStub.email, gAuthUserInfoStub.role))
.willReturn(role)

given(userService.checkUserExistByEmail(gAuthUserInfoStub.email))
.willReturn(false)

given(userService.createUserWhenNotExistUser(any(), any()))
.willReturn(saveUserStub)

given(jwtPort.receiveToken(saveUserStub.id, saveUserStub.roles.first()))
.willReturn(tokenResponseStub)

given(refreshTokenPort.saveRefreshToken(any()))
.willReturn(refreshTokenStub)

given(studentService.checkNewStudent(saveUserStub, role.name))
.willReturn(false)

// when
val result = signInUseCase.execute(requestStub)

// then
assertThat(result).isEqualTo(responseStub)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package team.msg.sms.global.annotation

import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.junit.jupiter.MockitoExtension

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@ExtendWith(MockitoExtension::class)
annotation class SmsTest()

0 comments on commit bac64e1

Please sign in to comment.