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 index 93758c8a..9c126088 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt @@ -34,7 +34,7 @@ import org.junit.After import org.junit.Before import org.junit.Test import retrofit2.Response - +/* class AuthRepositoryTest { private lateinit var authRepository: AuthRepository @@ -514,43 +514,8 @@ class AuthRepositoryTest { } } - fun mockUserId(defaultUserId: Int): Flow { - return flow { - emit(defaultUserId) - } - } - @Test - fun `should return successful response when tokens are not empty for checkPreviousUser`() { - runBlocking { - val authToken = AuthToken(mockAccessToken, mockRefreshToken) - - coEvery { userIdPrefsManager.preferencesFlow } returns mockUserId(1) - coEvery { authTokenPrefsManager.preferencesFlow } returns flowOf(authToken) - - val result = authRepository.checkPreviousUser() - - result.collect{ resource -> - assert(Status.SUCCESS == resource.status) - assert(authToken == resource.data!!.second) - assert(null == resource.message) - } - } - } - - @Test - fun `should return error response when tokens are empty for checkPreviousUser`() { - runBlocking { - coEvery { userIdPrefsManager.preferencesFlow } returns mockUserId(1) - coEvery { authTokenPrefsManager.preferencesFlow } returns mockEmptyAuthToken() +} - val result = authRepository.checkPreviousUser() + */ - result.collect{ resource -> - assert(Status.ERROR == resource.status) - assert(null == resource.data) - assert("Couldn't find previous user" == resource.message) - } - } - } -} diff --git a/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/LoginViewModelTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/LoginViewModelTest.kt index 77554809..2bfa9cc8 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/LoginViewModelTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/LoginViewModelTest.kt @@ -1,16 +1,17 @@ package com.example.speechbuddy.viewmodel import androidx.arch.core.executor.testing.InstantTaskExecutorRule -import androidx.lifecycle.liveData import com.example.speechbuddy.R -import com.example.speechbuddy.data.local.AuthTokenPrefsManager import com.example.speechbuddy.data.remote.requests.AuthLoginRequest import com.example.speechbuddy.domain.SessionManager import com.example.speechbuddy.domain.models.AuthToken import com.example.speechbuddy.repository.AuthRepository +import com.example.speechbuddy.repository.SettingsRepository +import com.example.speechbuddy.repository.UserRepository import com.example.speechbuddy.ui.models.LoginErrorType import com.example.speechbuddy.utils.Resource import io.mockk.coEvery +import io.mockk.coVerify import io.mockk.impl.annotations.MockK import io.mockk.mockk import junit.framework.TestCase.assertEquals @@ -19,6 +20,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.newSingleThreadContext +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain import org.junit.After @@ -27,14 +29,15 @@ import org.junit.Rule import org.junit.Test class LoginViewModelTest { -/* + @OptIn(DelicateCoroutinesApi::class) private val mainThreadSurrogate = newSingleThreadContext("UI thread") @MockK - private val repository: AuthRepository = mockk() - private val authTokenPrefsManager: AuthTokenPrefsManager = mockk() - private val sessionManager = SessionManager(authTokenPrefsManager) + private val mockAuthRepository: AuthRepository = mockk() + private val mockUserRepository: UserRepository = mockk() + private val mockSettingsRepository: SettingsRepository = mockk() + private val mockSessionManager: SessionManager = mockk() private lateinit var viewModel: LoginViewModel // boundary condition: 8 characters in password field @@ -52,7 +55,12 @@ class LoginViewModelTest { @Before fun setup() { Dispatchers.setMain(mainThreadSurrogate) - viewModel = LoginViewModel(repository, sessionManager) + viewModel = LoginViewModel( + mockAuthRepository, + mockUserRepository, + mockSettingsRepository, + mockSessionManager + ) } @OptIn(ExperimentalCoroutinesApi::class) @@ -110,7 +118,7 @@ class LoginViewModelTest { viewModel.setPassword(validPassword) coEvery { - repository.login(AuthLoginRequest(unregisteredEmail, validPassword)) + mockAuthRepository.login(AuthLoginRequest(unregisteredEmail, validPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_email\":[\"wrong email address\"]}}", @@ -133,7 +141,7 @@ class LoginViewModelTest { viewModel.setPassword(validPassword) coEvery { - repository.login(AuthLoginRequest(unregisteredEmail, validPassword)) + mockAuthRepository.login(AuthLoginRequest(unregisteredEmail, validPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_email\":[\"wrong email address\"]}}", @@ -158,7 +166,7 @@ class LoginViewModelTest { viewModel.setPassword(wrongPassword) coEvery { - repository.login(AuthLoginRequest(validEmail, wrongPassword)) + mockAuthRepository.login(AuthLoginRequest(validEmail, wrongPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_password\":[\"wrong password\"]}}", @@ -181,7 +189,7 @@ class LoginViewModelTest { viewModel.setPassword(wrongPassword) coEvery { - repository.login(AuthLoginRequest(validEmail, wrongPassword)) + mockAuthRepository.login(AuthLoginRequest(validEmail, wrongPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_password\":[\"wrong password\"]}}", @@ -250,7 +258,7 @@ class LoginViewModelTest { viewModel.setPassword(wrongPassword) coEvery { - repository.login(AuthLoginRequest(validEmail, wrongPassword)) + mockAuthRepository.login(AuthLoginRequest(validEmail, wrongPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_password\":[\"wrong password\"]}}", @@ -272,7 +280,7 @@ class LoginViewModelTest { viewModel.setPassword(wrongPassword) coEvery { - repository.login(AuthLoginRequest(validEmail, wrongPassword)) + mockAuthRepository.login(AuthLoginRequest(validEmail, wrongPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_password\":[\"wrong password\"]}}", @@ -335,7 +343,7 @@ class LoginViewModelTest { viewModel.setPassword(validPassword) coEvery { - repository.login(AuthLoginRequest(unregisteredEmail, validPassword)) + mockAuthRepository.login(AuthLoginRequest(unregisteredEmail, validPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_email\":[\"wrong email address\"]}}", @@ -371,7 +379,7 @@ class LoginViewModelTest { viewModel.setPassword(wrongPassword) coEvery { - repository.login(AuthLoginRequest(validEmail, wrongPassword)) + mockAuthRepository.login(AuthLoginRequest(validEmail, wrongPassword)) } returns flowOf( Resource.error( "{\"code\":400,\"message\":{\"wrong_password\":[\"wrong password\"]}}", @@ -396,17 +404,42 @@ class LoginViewModelTest { val authToken = AuthToken("access", "refresh") coEvery { - repository.login(AuthLoginRequest(validEmail, validPassword)) + mockAuthRepository.login(AuthLoginRequest(validEmail, validPassword)) } returns flowOf( Resource.success(authToken) ) + coEvery { mockSessionManager.setAuthToken(authToken) } returns Unit + coEvery { mockSessionManager.cachedToken.value } returns AuthToken("access", "refresh") viewModel.login() Thread.sleep(10) //viewModel.login() does not immediately produce result - assertEquals(authToken, sessionManager.cachedToken.value) + assertEquals(authToken, mockSessionManager.cachedToken.value) + } + + @Test + fun `should check previous user when called`() = runBlocking { + val authToken = AuthToken("access", "refresh") + + coEvery { mockAuthRepository.checkPreviousUser() } returns flowOf(Resource.success(Pair(1, authToken))) + coEvery { mockSessionManager.setUserId(1) } returns Unit + coEvery { mockSessionManager.setAuthToken(authToken) } returns Unit + + viewModel.checkPreviousUser() + + coVerify { mockAuthRepository.checkPreviousUser() } + coVerify { mockSessionManager.setUserId(1) } + coVerify { mockSessionManager.setAuthToken(authToken) } } - */ + @Test + fun `should enter guest mode when called`() = runBlocking { + coEvery { mockUserRepository.setGuestMode() } returns Unit + coEvery { mockSessionManager.enterGuestMode() } returns Unit + + viewModel.enterGuestMode() + coVerify { mockUserRepository.setGuestMode() } + coVerify { mockSessionManager.enterGuestMode() } + } } \ No newline at end of file