Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Login viewmodel #93

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -514,43 +514,8 @@ class AuthRepositoryTest {
}
}

fun mockUserId(defaultUserId: Int): Flow<Int> {
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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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\"]}}",
Expand All @@ -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\"]}}",
Expand All @@ -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\"]}}",
Expand All @@ -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\"]}}",
Expand Down Expand Up @@ -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\"]}}",
Expand All @@ -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\"]}}",
Expand Down Expand Up @@ -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\"]}}",
Expand Down Expand Up @@ -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\"]}}",
Expand All @@ -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() }
}
}