diff --git a/frontend/app/build.gradle.kts b/frontend/app/build.gradle.kts index 8039ad4c..c72a6cec 100644 --- a/frontend/app/build.gradle.kts +++ b/frontend/app/build.gradle.kts @@ -63,6 +63,7 @@ dependencies { implementation("androidx.compose.material3:material3") implementation("androidx.test:monitor:1.6.1") testImplementation("junit:junit:4.13.2") + testImplementation("org.json:json:20210307") testImplementation("org.mockito:mockito-core:5.5.0") testImplementation("org.junit.jupiter:junit-jupiter:5.8.2") testImplementation("androidx.arch.core:core-testing:2.2.0") @@ -73,8 +74,6 @@ dependencies { debugImplementation("androidx.compose.ui:ui-tooling") debugImplementation("androidx.compose.ui:ui-test-manifest") - testImplementation("io.mockk:mockk:1.13.8") - // Lifecycle val lifecycleVersion = "2.6.2" implementation("androidx.lifecycle:lifecycle-common:$lifecycleVersion") @@ -89,7 +88,6 @@ dependencies { val coroutinesVersion = "1.7.1" implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion") - testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion") // Preference diff --git a/frontend/app/src/main/java/com/example/speechbuddy/data/remote/models/ErrorResponseMapper.kt b/frontend/app/src/main/java/com/example/speechbuddy/data/remote/models/ErrorResponseMapper.kt new file mode 100644 index 00000000..6f3f9c74 --- /dev/null +++ b/frontend/app/src/main/java/com/example/speechbuddy/data/remote/models/ErrorResponseMapper.kt @@ -0,0 +1,50 @@ +package com.example.speechbuddy.data.remote.models + +import android.util.Log +import com.example.speechbuddy.domain.models.ErrorResponse +import okhttp3.ResponseBody +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +class ErrorResponseMapper { + fun mapToDomainModel(response: ResponseBody): ErrorResponse { + try { + val errorJson = JSONObject(response.charStream().readText()).optJSONObject("error") + + if (errorJson != null) { + val code = errorJson.optInt("code", -1) + val messageJson = errorJson.opt("message") + + // if "message" in the ResponseBody is a list + if (messageJson is JSONArray) { + val firstMessage = messageJson.optJSONObject(0) + if (firstMessage != null) { + val keys = firstMessage.keys() + if (keys.hasNext()) { + val key = keys.next() + val description = firstMessage.optJSONArray(key).optString(0) + return ErrorResponse(code, key, description) + } + } + } + // if "message" in the ResponseBody is a single json + else if (messageJson is JSONObject) { + val keys = messageJson.keys() + if (keys.hasNext()) { + val key = keys.next() + val description = messageJson.optString(key) + return ErrorResponse(code, key, description) + } + } + } + + // Return a default ErrorResponse if the responseBody structure doesn't match predefined cases + return ErrorResponse() + } catch (e: JSONException) { + // Handle the JSON parsing error + Log.e("ErrorResponseMapper", "JSON parsing error: ${e.message}") + return ErrorResponse() // Return a default ErrorResponse in case of JSON parsing error + } + } +} diff --git a/frontend/app/src/main/java/com/example/speechbuddy/di/NetworkModule.kt b/frontend/app/src/main/java/com/example/speechbuddy/di/NetworkModule.kt index a32944dc..f635021d 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/di/NetworkModule.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/di/NetworkModule.kt @@ -2,6 +2,7 @@ package com.example.speechbuddy.di import com.example.speechbuddy.MainApplication import com.example.speechbuddy.data.remote.models.AuthTokenDtoMapper +import com.example.speechbuddy.data.remote.models.ErrorResponseMapper import com.example.speechbuddy.service.AuthService import com.example.speechbuddy.utils.Constants import com.squareup.moshi.Moshi @@ -49,6 +50,12 @@ class NetworkModule { return AuthTokenDtoMapper() } + @Singleton + @Provides + fun provideErrorResponseMapper(): ErrorResponseMapper { + return ErrorResponseMapper() + } + } class AuthInterceptor: Interceptor { diff --git a/frontend/app/src/main/java/com/example/speechbuddy/domain/models/ErrorResponse.kt b/frontend/app/src/main/java/com/example/speechbuddy/domain/models/ErrorResponse.kt new file mode 100644 index 00000000..16dd7700 --- /dev/null +++ b/frontend/app/src/main/java/com/example/speechbuddy/domain/models/ErrorResponse.kt @@ -0,0 +1,11 @@ +package com.example.speechbuddy.domain.models + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize +data class ErrorResponse( + val code: Int = -1, + val key: String = "Unknown Error With Error Response", + val description: String = "Unknown Error With Error Response" +) : Parcelable \ No newline at end of file diff --git a/frontend/app/src/main/java/com/example/speechbuddy/repository/AuthRepository.kt b/frontend/app/src/main/java/com/example/speechbuddy/repository/AuthRepository.kt index 63946827..656e0af1 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/repository/AuthRepository.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/repository/AuthRepository.kt @@ -2,6 +2,7 @@ package com.example.speechbuddy.repository import com.example.speechbuddy.data.remote.AuthTokenRemoteSource import com.example.speechbuddy.data.remote.models.AuthTokenDtoMapper +import com.example.speechbuddy.data.remote.models.ErrorResponseMapper import com.example.speechbuddy.data.remote.requests.AuthLoginRequest import com.example.speechbuddy.data.remote.requests.AuthResetPasswordRequest import com.example.speechbuddy.data.remote.requests.AuthSendCodeRequest @@ -13,7 +14,6 @@ import com.example.speechbuddy.utils.Resource import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map -import org.json.JSONObject import retrofit2.Response import javax.inject.Inject import javax.inject.Singleton @@ -22,74 +22,75 @@ import javax.inject.Singleton class AuthRepository @Inject constructor( private val authService: AuthService, private val authTokenRemoteSource: AuthTokenRemoteSource, - private val authTokenDtoMapper: AuthTokenDtoMapper + private val authTokenDtoMapper: AuthTokenDtoMapper, + private val errorResponseMapper: ErrorResponseMapper, ) { - suspend fun signup(authSignupRequest: AuthSignupRequest): Flow> = - flow { - val result = authService.signup(authSignupRequest) - emit(result) - } + suspend fun signup(authSignupRequest: AuthSignupRequest): Flow> = flow { + val result = authService.signup(authSignupRequest) + emit(result) + } suspend fun login(authLoginRequest: AuthLoginRequest): Flow> { - return authTokenRemoteSource.loginAuthToken(authLoginRequest) - .map { response -> + return authTokenRemoteSource.loginAuthToken(authLoginRequest).map { response -> if (response.isSuccessful && response.code() == 200) { response.body()?.let { authTokenDto -> authTokenDto.let { - Resource.success(authTokenDtoMapper.mapToDomainModel(authTokenDto)) + Resource.success( + authTokenDtoMapper.mapToDomainModel( + authTokenDto + ) + ) } } ?: returnUnknownError() } else { response.errorBody()?.let { responseBody -> - val errorMessage = - JSONObject(responseBody.charStream().readText()).getString("error") + val errorMsgKey = errorResponseMapper.mapToDomainModel(responseBody).key Resource.error( - errorMessage, - null + errorMsgKey, null ) } ?: returnUnknownError() } } } - suspend fun sendCodeForSignup(authVerifyEmailSendRequest: AuthSendCodeRequest): Flow> = + suspend fun sendCodeForSignup(authSendCodeRequest: AuthSendCodeRequest): Flow> = flow { - val result = authService.sendCodeForSignup(authVerifyEmailSendRequest) + val result = authService.sendCodeForSignup(authSendCodeRequest) emit(result) } - suspend fun sendCodeForResetPassword(authVerifyEmailSendRequest: AuthSendCodeRequest): Flow> = + suspend fun sendCodeForResetPassword(authSendCodeRequest: AuthSendCodeRequest): Flow> = flow { - val result = authService.sendCodeForResetPassword(authVerifyEmailSendRequest) + val result = authService.sendCodeForResetPassword(authSendCodeRequest) emit(result) } - suspend fun verifyEmailForSignup(authVerifyEmailAcceptRequest: AuthVerifyEmailRequest): Flow> = + suspend fun verifyEmailForSignup(authVerifyEmailRequest: AuthVerifyEmailRequest): Flow> = flow { - val result = authService.verifyEmailForSignup(authVerifyEmailAcceptRequest) + val result = authService.verifyEmailForSignup(authVerifyEmailRequest) emit(result) } - suspend fun verifyEmailForResetPassword(authVerifyEmailAcceptRequest: AuthVerifyEmailRequest): Flow> { + suspend fun verifyEmailForResetPassword(authVerifyEmailRequest: AuthVerifyEmailRequest): Flow> { return authTokenRemoteSource.verifyEmailForResetPasswordAuthToken( - authVerifyEmailAcceptRequest - ) - .map { response -> + authVerifyEmailRequest + ).map { response -> if (response.isSuccessful && response.code() == 200) { response.body()?.let { authTokenDto -> authTokenDto.let { - Resource.success(authTokenDtoMapper.mapToDomainModel(authTokenDto)) + Resource.success( + authTokenDtoMapper.mapToDomainModel( + authTokenDto + ) + ) } } ?: returnUnknownError() } else { response.errorBody()?.let { responseBody -> - val errorJson = JSONObject(responseBody.charStream().readText()) - val messageJson = errorJson.getJSONObject("error").getJSONObject("message") - val firstKeyOfMessage = messageJson.keys().next().toString() + val errorMsgKey = errorResponseMapper.mapToDomainModel(responseBody).key Resource.error( - firstKeyOfMessage, - null + errorMsgKey, null ) } ?: returnUnknownError() } @@ -104,8 +105,7 @@ class AuthRepository @Inject constructor( private fun returnUnknownError(): Resource { return Resource.error( - "Unknown error", - null + "Unknown error", null ) } diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/EmailVerificationViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/EmailVerificationViewModel.kt index 11f9b55d..740e2c1b 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/EmailVerificationViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/EmailVerificationViewModel.kt @@ -8,6 +8,7 @@ import androidx.lifecycle.viewModelScope import androidx.navigation.NavHostController import com.example.speechbuddy.MainApplication.Companion.token_prefs import com.example.speechbuddy.R +import com.example.speechbuddy.data.remote.models.ErrorResponseMapper import com.example.speechbuddy.data.remote.requests.AuthSendCodeRequest import com.example.speechbuddy.data.remote.requests.AuthVerifyEmailRequest import com.example.speechbuddy.domain.models.AuthToken @@ -33,6 +34,7 @@ class EmailVerificationViewModel @Inject internal constructor( private val _uiState = MutableStateFlow(EmailVerificationUiState()) val uiState: StateFlow = _uiState.asStateFlow() + private val errorResponseMapper = ErrorResponseMapper() var emailInput by mutableStateOf("") private set @@ -77,8 +79,6 @@ class EmailVerificationViewModel @Inject internal constructor( } fun verifySend(source: String?) { - /* TODO: 나중에 고쳐야 함 */ - // What function(ultimately, API call) to use val sendCode = if (source == "signup") { repository::sendCodeForSignup @@ -116,7 +116,8 @@ class EmailVerificationViewModel @Inject internal constructor( } 400 -> { - val messageId = when (result.message()) { + val errorKey = errorResponseMapper.mapToDomainModel(result.errorBody()!!).key + val messageId = when (errorKey) { "email" -> R.string.false_email "already_taken" -> R.string.email_already_taken "no_user" -> R.string.no_such_user diff --git a/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt new file mode 100644 index 00000000..2a6fc1ff --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt @@ -0,0 +1,369 @@ +package com.example.speechbuddy.repository + +import com.example.speechbuddy.data.remote.AuthTokenRemoteSource +import com.example.speechbuddy.data.remote.models.AuthTokenDto +import com.example.speechbuddy.data.remote.models.AuthTokenDtoMapper +import com.example.speechbuddy.data.remote.models.ErrorResponseMapper +import com.example.speechbuddy.data.remote.requests.AuthLoginRequest +import com.example.speechbuddy.data.remote.requests.AuthResetPasswordRequest +import com.example.speechbuddy.data.remote.requests.AuthSendCodeRequest +import com.example.speechbuddy.data.remote.requests.AuthSignupRequest +import com.example.speechbuddy.data.remote.requests.AuthVerifyEmailRequest +import com.example.speechbuddy.domain.models.AuthToken +import com.example.speechbuddy.service.AuthService +import com.example.speechbuddy.utils.Status +import io.mockk.clearAllMocks +import io.mockk.coEvery +import io.mockk.impl.annotations.MockK +import io.mockk.mockk +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.runBlocking +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.ResponseBody.Companion.toResponseBody +import org.junit.After +import org.junit.Before +import org.junit.Test +import retrofit2.Response + +class AuthRepositoryTest { + + private lateinit var authRepository: AuthRepository + + @MockK + private val authService = mockk() + private val authTokenRemoteSource = mockk() + private val authTokenDtoMapper = AuthTokenDtoMapper() + private val errorResponseMapper = ErrorResponseMapper() + + private val mockEmail = "test@example.com" + private val mockPassword = "password123" + private val mockNickname = "TestUser" + private val mockCode = "123456" + private val mockAccessToken = "testAccessToken" + private val mockRefreshToken = "testRefreshToken" + + private val mockErrorJson = """ + { + "error": { + "code": 000, + "message": { + "key of message": [ + "error description" + ] + } + } + } + """ + private val mockErrorResponseBody = + mockErrorJson.toResponseBody("application/json".toMediaType()) + + @Before + fun setup() { + authRepository = AuthRepository(authService, authTokenRemoteSource, authTokenDtoMapper, errorResponseMapper) + } + + @After + fun tearDown(){ + clearAllMocks() + } + + @Test + fun `should return successful response when signup request is valid`() { + runBlocking { + val authSignupRequest = AuthSignupRequest( + email = mockEmail, + password = mockPassword, + nickname = mockNickname + ) + // 성공 케이스의 Response를 정의 + val successResponse = Response.success(201, null) + + coEvery { authService.signup(authSignupRequest) } returns successResponse + + val result = authRepository.signup(authSignupRequest) + // 아래 resource는 Response 타입 + result.collect { resource -> + assert(resource.isSuccessful) + assert(resource.code()==201) + assert(resource.body()==null) + } + } + } + + @Test + fun `should return error response when signup request is invalid`() { + runBlocking{ + val authSignupRequest = AuthSignupRequest( + email = mockEmail, + password = "invalid", + nickname = mockNickname + ) + // 에러 케이스의 Response를 정의 + val errorResponse = Response.error(400, mockErrorResponseBody) + + coEvery { authService.signup(authSignupRequest) } returns errorResponse + + val result = authRepository.signup(authSignupRequest) + result.collect { resource -> + assert(!resource.isSuccessful) + assert(resource.code()==400) + } + } + } + + @Test + fun `should return SUCCESS Resource when login request is valid`(){ + runBlocking { + val authLoginRequest = AuthLoginRequest( + email = mockEmail, + password = mockPassword + ) + val authTokenDto = AuthTokenDto( + accessToken = mockAccessToken, + refreshToken = mockRefreshToken + ) + val expectedAuthToken = AuthToken( + accessToken = mockAccessToken, + refreshToken = mockRefreshToken + ) + // 성공 케이스의 Response를 정의 + val successResponse = Response.success(200, authTokenDto) + + coEvery { authTokenRemoteSource.loginAuthToken(authLoginRequest) } returns flowOf(successResponse) + + val result = authRepository.login(authLoginRequest) + + // 아래 resource는 Resource 타입 + result.collect { resource -> + assert(resource.status == Status.SUCCESS) + assert(resource.data == expectedAuthToken) + assert(resource.message == null) + } + } + } + + @Test + fun `should return ERROR Resource when login request is invalid`() { + runBlocking{ + val authLoginRequest = AuthLoginRequest( + email = mockEmail, + password = "invalid" + ) + val errorResponse = Response.error(400, mockErrorResponseBody) + + coEvery { authTokenRemoteSource.loginAuthToken(authLoginRequest) } returns flowOf(errorResponse) + + val result = authRepository.login(authLoginRequest) + result.collect { resource -> + assert(resource.status == Status.ERROR) + assert(resource.data == null) + assert(resource.message == "key of message") + } + } + } + + @Test + fun `should return successful response when sendCodeForSignup request is valid`() { + runBlocking { + val authSendCodeRequest = AuthSendCodeRequest( + email = mockEmail + ) + val successResponse = Response.success(200, null) + + coEvery { authService.sendCodeForSignup(authSendCodeRequest) } returns successResponse + + val result = authRepository.sendCodeForSignup(authSendCodeRequest) + // 아래 resource는 Response 타입 + result.collect { resource -> + assert(resource.isSuccessful) + assert(resource.code()==200) + assert(resource.body()==null) + } + } + } + + @Test + fun `should return error response when sendCodeForSignup request is invalid`() { + runBlocking{ + val authSendCodeRequest = AuthSendCodeRequest( + email = "invalid@example.com" + ) + val errorResponse = Response.error(400, mockErrorResponseBody) + + coEvery { authService.sendCodeForSignup(authSendCodeRequest) } returns errorResponse + + val result = authRepository.sendCodeForSignup(authSendCodeRequest) + result.collect { resource -> + assert(!resource.isSuccessful) + assert(resource.code()==400) + } + } + } + + @Test + fun `should return successful response when sendCodeForResetPassword request is valid`() { + runBlocking { + val authSendCodeRequest = AuthSendCodeRequest( + email = mockEmail + ) + val successResponse = Response.success(200, null) + + coEvery { authService.sendCodeForResetPassword(authSendCodeRequest) } returns successResponse + + val result = authRepository.sendCodeForResetPassword(authSendCodeRequest) + // 아래 resource는 Response 타입 + result.collect { resource -> + assert(resource.isSuccessful) + assert(resource.code()==200) + assert(resource.body()==null) + } + } + } + + @Test + fun `should return error response when sendCodeForResetPassword request is invalid`() { + runBlocking{ + val authSendCodeRequest = AuthSendCodeRequest( + email = "invalid@example.com" + ) + val errorResponse = Response.error(400, mockErrorResponseBody) + + coEvery { authService.sendCodeForResetPassword(authSendCodeRequest) } returns errorResponse + + val result = authRepository.sendCodeForResetPassword(authSendCodeRequest) + result.collect { resource -> + assert(!resource.isSuccessful) + assert(resource.code()==400) + } + } + } + + @Test + fun `should return successful response when verifyEmailForSignup request is valid`() { + runBlocking { + val authVerifyEmailRequest = AuthVerifyEmailRequest( + email = mockEmail, + code = mockCode + ) + val successResponse = Response.success(200, null) + + coEvery { authService.verifyEmailForSignup(authVerifyEmailRequest) } returns successResponse + + val result = authRepository.verifyEmailForSignup(authVerifyEmailRequest) + // 아래 resource는 Response 타입 + result.collect { resource -> + assert(resource.isSuccessful) + assert(resource.code()==200) + assert(resource.body()==null) + } + } + } + + @Test + fun `should return error response when verifyEmailForSignup request is invalid`() { + runBlocking{ + val authVerifyEmailRequest = AuthVerifyEmailRequest( + email = mockEmail, + code = "invalid" + ) + val errorResponse = Response.error(400, mockErrorResponseBody) + + coEvery { authService.verifyEmailForSignup(authVerifyEmailRequest) } returns errorResponse + + val result = authRepository.verifyEmailForSignup(authVerifyEmailRequest) + result.collect { resource -> + assert(!resource.isSuccessful) + assert(resource.code()==400) + } + } + } + + @Test + fun `should return SUCCESS Resource when verifyEmailForResetPassword request is valid`(){ + runBlocking { + val authVerifyEmailRequest = AuthVerifyEmailRequest( + email = mockEmail, + code = mockCode + ) + val authTokenDto = AuthTokenDto( + accessToken = mockAccessToken, + refreshToken = null + ) + val expectedAuthToken = AuthToken( + accessToken = mockAccessToken, + refreshToken = null + ) + val successResponse = Response.success(200, authTokenDto) + + coEvery { authTokenRemoteSource.verifyEmailForResetPasswordAuthToken(authVerifyEmailRequest) } returns flowOf(successResponse) + + val result = authRepository.verifyEmailForResetPassword(authVerifyEmailRequest) + + // 아래 resource는 Resource 타입 + result.collect { resource -> + assert(resource.status == Status.SUCCESS) + assert(resource.data == expectedAuthToken) + assert(resource.message == null) + } + } + } + + @Test + fun `should return ERROR Resource when verifyEmailForResetPassword request is invalid`() { + runBlocking{ + val authVerifyEmailRequest = AuthVerifyEmailRequest( + email = mockEmail, + code = "invalid" + ) + val errorResponse = Response.error(400, mockErrorResponseBody) + + coEvery { authTokenRemoteSource.verifyEmailForResetPasswordAuthToken(authVerifyEmailRequest) } returns flowOf(errorResponse) + + val result = authRepository.verifyEmailForResetPassword(authVerifyEmailRequest) + result.collect { resource -> + assert(resource.status == Status.ERROR) + assert(resource.data == null) + assert(resource.message == "key of message") + } + } + } + + @Test + fun `should return successful response when resetPassword request is valid`() { + runBlocking { + val authResetPasswordRequest = AuthResetPasswordRequest( + password = "newPassword" + ) + val successResponse = Response.success(200, null) + + coEvery { authService.resetPassword(authResetPasswordRequest) } returns successResponse + + val result = authRepository.resetPassword(authResetPasswordRequest) + // 아래 resource는 Response 타입 + result.collect { resource -> + assert(resource.isSuccessful) + assert(resource.code()==200) + assert(resource.body()==null) + } + } + } + + @Test + fun `should return error response when resetPassword request is invalid`() { + runBlocking{ + val authResetPasswordRequest = AuthResetPasswordRequest( + password = "invalid" + ) + val errorResponse = Response.error(400, mockErrorResponseBody) + + coEvery { authService.resetPassword(authResetPasswordRequest) } returns errorResponse + + val result = authRepository.resetPassword(authResetPasswordRequest) + result.collect { resource -> + assert(!resource.isSuccessful) + assert(resource.code()==400) + } + } + } + +} \ No newline at end of file diff --git a/frontend/app/src/test/java/com/example/speechbuddy/service/AuthServiceTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/service/AuthServiceTest.kt index 9c4f2002..c8469a8b 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/service/AuthServiceTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/service/AuthServiceTest.kt @@ -4,8 +4,8 @@ import com.example.speechbuddy.data.remote.models.AuthTokenDto import com.example.speechbuddy.data.remote.requests.AuthLoginRequest import com.example.speechbuddy.data.remote.requests.AuthResetPasswordRequest import com.example.speechbuddy.data.remote.requests.AuthSignupRequest -import com.example.speechbuddy.data.remote.requests.AuthVerifyEmailAcceptRequest -import com.example.speechbuddy.data.remote.requests.AuthVerifyEmailSendRequest +import com.example.speechbuddy.data.remote.requests.AuthVerifyEmailRequest +import com.example.speechbuddy.data.remote.requests.AuthSendCodeRequest import io.mockk.coEvery import io.mockk.coVerify import io.mockk.mockk @@ -62,52 +62,52 @@ class AuthServiceTest { @Test fun `should return response success when request email send is valid for signup`() = runBlocking { - val verifyEmailSendRequest = AuthVerifyEmailSendRequest(mockEmail) - coEvery { authService.verifySendSignup(verifyEmailSendRequest) } returns Response.success( + val verifyEmailSendRequest = AuthSendCodeRequest(mockEmail) + coEvery { authService.sendCodeForSignup(verifyEmailSendRequest) } returns Response.success( null ) - val response = authService.verifySendSignup(verifyEmailSendRequest) + val response = authService.sendCodeForSignup(verifyEmailSendRequest) - coVerify(exactly = 1) { authService.verifySendSignup(verifyEmailSendRequest) } + coVerify(exactly = 1) { authService.sendCodeForSignup(verifyEmailSendRequest) } assertTrue(response.isSuccessful) } @Test fun `should return response success when request email send is valid for password reset`() = runBlocking { - val verifyEmailSendRequest = AuthVerifyEmailSendRequest(mockEmail) - coEvery { authService.verifySendPW(verifyEmailSendRequest) } returns Response.success(null) + val verifyEmailSendRequest = AuthSendCodeRequest(mockEmail) + coEvery { authService.sendCodeForResetPassword(verifyEmailSendRequest) } returns Response.success(null) - val response = authService.verifySendPW(verifyEmailSendRequest) + val response = authService.sendCodeForResetPassword(verifyEmailSendRequest) - coVerify(exactly = 1) { authService.verifySendPW(verifyEmailSendRequest) } + coVerify(exactly = 1) { authService.sendCodeForResetPassword(verifyEmailSendRequest) } assertTrue(response.isSuccessful) } @Test fun `should return response with success when request code is valid for signup`() = runBlocking { - val verifyEmailAcceptRequest = AuthVerifyEmailAcceptRequest(mockEmail, mockCode) - coEvery { authService.verifyAcceptSignup(verifyEmailAcceptRequest) } returns Response.success( + val verifyEmailAcceptRequest = AuthVerifyEmailRequest(mockEmail, mockCode) + coEvery { authService.verifyEmailForSignup(verifyEmailAcceptRequest) } returns Response.success( null ) - val response = authService.verifyAcceptSignup(verifyEmailAcceptRequest) + val response = authService.verifyEmailForSignup(verifyEmailAcceptRequest) - coVerify(exactly = 1) { authService.verifyAcceptSignup(verifyEmailAcceptRequest) } + coVerify(exactly = 1) { authService.verifyEmailForSignup(verifyEmailAcceptRequest) } assertTrue(response.isSuccessful) } @Test fun `should return response with auth token dto when request code is valid for password reset`() = runBlocking { - val verifyEmailAcceptRequest = AuthVerifyEmailAcceptRequest(mockEmail, mockCode) + val verifyEmailAcceptRequest = AuthVerifyEmailRequest(mockEmail, mockCode) val authTokenDto = AuthTokenDto("AccessToken", "RefreshToken") - coEvery { authService.verifyAcceptPW(verifyEmailAcceptRequest) } returns Response.success( + coEvery { authService.verifyEmailForResetPassword(verifyEmailAcceptRequest) } returns Response.success( authTokenDto ) - val response = authService.verifyAcceptPW(verifyEmailAcceptRequest) + val response = authService.verifyEmailForResetPassword(verifyEmailAcceptRequest) - coVerify(exactly = 1) { authService.verifyAcceptPW(verifyEmailAcceptRequest) } + coVerify(exactly = 1) { authService.verifyEmailForResetPassword(verifyEmailAcceptRequest) } assertTrue(response.isSuccessful) assertTrue(response.body()?.accessToken == "AccessToken") assertTrue(response.body()?.refreshToken == "RefreshToken") @@ -150,57 +150,57 @@ class AuthServiceTest { @Test fun `should return response with error when request email send is invalid for signup`() = runBlocking { - val verifyEmailSendRequest = AuthVerifyEmailSendRequest("invalid_email") - coEvery { authService.verifySendSignup(verifyEmailSendRequest) } returns Response.error( + val verifyEmailSendRequest = AuthSendCodeRequest("invalid_email") + coEvery { authService.sendCodeForSignup(verifyEmailSendRequest) } returns Response.error( 400, errorResponseBody ) - val response = authService.verifySendSignup(verifyEmailSendRequest) + val response = authService.sendCodeForSignup(verifyEmailSendRequest) - coVerify(exactly = 1) { authService.verifySendSignup(verifyEmailSendRequest) } + coVerify(exactly = 1) { authService.sendCodeForSignup(verifyEmailSendRequest) } assertFalse(response.isSuccessful) } @Test fun `should return response with error when request email send is invalid for password reset`() = runBlocking { - val verifyEmailSendRequest = AuthVerifyEmailSendRequest("invalid_email") - coEvery { authService.verifySendPW(verifyEmailSendRequest) } returns Response.error( + val verifyEmailSendRequest = AuthSendCodeRequest("invalid_email") + coEvery { authService.sendCodeForResetPassword(verifyEmailSendRequest) } returns Response.error( 400, errorResponseBody ) - val response = authService.verifySendPW(verifyEmailSendRequest) + val response = authService.sendCodeForResetPassword(verifyEmailSendRequest) - coVerify(exactly = 1) { authService.verifySendPW(verifyEmailSendRequest) } + coVerify(exactly = 1) { authService.sendCodeForResetPassword(verifyEmailSendRequest) } assertFalse(response.isSuccessful) } @Test fun `should return response with error when request code is invalid for signup`() = runBlocking { - val verifyEmailAcceptRequest = AuthVerifyEmailAcceptRequest(mockEmail, "wrong_code") - coEvery { authService.verifyAcceptSignup(verifyEmailAcceptRequest) } returns Response.error( + val verifyEmailAcceptRequest = AuthVerifyEmailRequest(mockEmail, "wrong_code") + coEvery { authService.verifyEmailForSignup(verifyEmailAcceptRequest) } returns Response.error( 400, errorResponseBody ) - val response = authService.verifyAcceptSignup(verifyEmailAcceptRequest) + val response = authService.verifyEmailForSignup(verifyEmailAcceptRequest) - coVerify(exactly = 1) { authService.verifyAcceptSignup(verifyEmailAcceptRequest) } + coVerify(exactly = 1) { authService.verifyEmailForSignup(verifyEmailAcceptRequest) } assertFalse(response.isSuccessful) } @Test fun `should return response with error when request code is invalid for password reset`() = runBlocking { - val verifyEmailAcceptRequest = AuthVerifyEmailAcceptRequest(mockEmail, "wrong_code") - coEvery { authService.verifyAcceptPW(verifyEmailAcceptRequest) } returns Response.error( + val verifyEmailAcceptRequest = AuthVerifyEmailRequest(mockEmail, "wrong_code") + coEvery { authService.verifyEmailForResetPassword(verifyEmailAcceptRequest) } returns Response.error( 400, errorResponseBody ) - val response = authService.verifyAcceptPW(verifyEmailAcceptRequest) + val response = authService.verifyEmailForResetPassword(verifyEmailAcceptRequest) - coVerify(exactly = 1) { authService.verifyAcceptPW(verifyEmailAcceptRequest) } + coVerify(exactly = 1) { authService.verifyEmailForResetPassword(verifyEmailAcceptRequest) } assertFalse(response.isSuccessful) }