Minor cleanup of the MainViewModelTests and the AuthRepo (#1023)

This commit is contained in:
David Perez 2024-02-15 15:33:19 -06:00 committed by Álison Fernandes
parent 6e3c5930a1
commit 874ead8f3e
2 changed files with 49 additions and 67 deletions

View file

@ -233,8 +233,7 @@ class AuthRepositoryImpl(
private val yubiKeyResultChannel = Channel<YubiKeyResult>(capacity = Int.MAX_VALUE)
override val yubiKeyResultFlow: Flow<YubiKeyResult> = yubiKeyResultChannel.receiveAsFlow()
private val mutableSsoCallbackResultFlow =
bufferedMutableSharedFlow<SsoCallbackResult>()
private val mutableSsoCallbackResultFlow = bufferedMutableSharedFlow<SsoCallbackResult>()
override val ssoCallbackResultFlow: Flow<SsoCallbackResult> =
mutableSsoCallbackResultFlow.asSharedFlow()
@ -377,7 +376,8 @@ class AuthRepositoryImpl(
password: String?,
twoFactorData: TwoFactorDataModel,
captchaToken: String?,
): LoginResult = identityTokenAuthModel?.let {
): LoginResult = identityTokenAuthModel
?.let {
loginCommon(
email = email,
password = password,
@ -385,7 +385,8 @@ class AuthRepositoryImpl(
twoFactorData = twoFactorData,
captchaToken = captchaToken ?: twoFactorResponse?.captchaToken,
)
} ?: LoginResult.Error(errorMessage = null)
}
?: LoginResult.Error(errorMessage = null)
override suspend fun login(
email: String,
@ -588,17 +589,18 @@ class AuthRepositoryImpl(
}
override suspend fun resendVerificationCodeEmail(): ResendEmailResult =
resendEmailRequestJson?.let { jsonRequest ->
resendEmailRequestJson
?.let { jsonRequest ->
accountsService.resendVerificationCodeEmail(body = jsonRequest).fold(
onFailure = { ResendEmailResult.Error(message = it.message) },
onSuccess = { ResendEmailResult.Success },
)
} ?: ResendEmailResult.Error(message = null)
}
?: ResendEmailResult.Error(message = null)
@Suppress("ReturnCount")
override fun switchAccount(userId: String): SwitchAccountResult {
val currentUserState = authDiskSource.userState
?: return SwitchAccountResult.NoChange
val currentUserState = authDiskSource.userState ?: return SwitchAccountResult.NoChange
val previousActiveUserId = currentUserState.activeUserId
if (userId == previousActiveUserId) {
@ -699,9 +701,7 @@ class AuthRepositoryImpl(
}
}
},
onFailure = {
RegisterResult.Error(errorMessage = null)
},
onFailure = { RegisterResult.Error(errorMessage = null) },
)
}
@ -709,10 +709,7 @@ class AuthRepositoryImpl(
return accountsService.requestPasswordHint(email).fold(
onSuccess = {
when (it) {
is PasswordHintResponseJson.Error -> {
PasswordHintResult.Error(it.errorMessage)
}
is PasswordHintResponseJson.Error -> PasswordHintResult.Error(it.errorMessage)
PasswordHintResponseJson.Success -> PasswordHintResult.Success
}
},
@ -1133,14 +1130,8 @@ class AuthRepositoryImpl(
password = password,
)
.fold(
onSuccess = {
PasswordStrengthResult.Success(
passwordStrength = it,
)
},
onFailure = {
PasswordStrengthResult.Error
},
onSuccess = { PasswordStrengthResult.Success(passwordStrength = it) },
onFailure = { PasswordStrengthResult.Error },
)
@Suppress("ReturnCount")
@ -1188,7 +1179,6 @@ class AuthRepositoryImpl(
}
}
@Suppress("CyclomaticComplexMethod", "ReturnCount")
override suspend fun validatePasswordAgainstPolicies(
password: String,
): Boolean = passwordPolicies

View file

@ -4,8 +4,6 @@ import android.content.Intent
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.bitwarden.core.CipherView
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.auth.util.getPasswordlessRequestDataIntentOrNull
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManager
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManagerImpl
@ -17,30 +15,27 @@ import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManagerImpl
import com.x8bit.bitwarden.data.platform.manager.model.PasswordlessRequestData
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
class MainViewModelTest : BaseViewModelTest() {
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT)
private val mutableUserStateFlow = MutableStateFlow<UserState?>(DEFAULT_USER_STATE)
private val mutableScreenCaptureAllowedFlow = MutableStateFlow(true)
private val mutableCrashLoggingEnabledFlow = MutableStateFlow(true)
val authRepository = mockk<AuthRepository> {
every { userStateFlow } returns mutableUserStateFlow
every { activeUserId } returns USER_ID
}
private val settingsRepository = mockk<SettingsRepository> {
every { appTheme } returns AppTheme.DEFAULT
every { appThemeStateFlow } returns mutableAppThemeFlow
@ -52,6 +47,24 @@ class MainViewModelTest : BaseViewModelTest() {
}
private val savedStateHandle = SavedStateHandle()
@BeforeEach
fun setup() {
mockkStatic(
Intent::getPasswordlessRequestDataIntentOrNull,
Intent::getAutofillSaveItemOrNull,
Intent::getAutofillSelectionDataOrNull,
)
}
@AfterEach
fun tearDown() {
unmockkStatic(
Intent::getPasswordlessRequestDataIntentOrNull,
Intent::getAutofillSaveItemOrNull,
Intent::getAutofillSelectionDataOrNull,
)
}
@Suppress("MaxLineLength")
@Test
fun `initialization should set a saved SpecialCircumstance to the SpecialCircumstanceManager if present`() {
@ -360,27 +373,6 @@ class MainViewModelTest : BaseViewModelTest() {
set(SPECIAL_CIRCUMSTANCE_KEY, initialSpecialCircumstance)
},
)
}
companion object {
private const val SPECIAL_CIRCUMSTANCE_KEY = "special-circumstance"
private const val USER_ID = "userID"
private val DEFAULT_USER_STATE = UserState(
activeUserId = USER_ID,
accounts = listOf(
UserState.Account(
userId = USER_ID,
name = "Active User",
email = "active@bitwarden.com",
environment = Environment.Us,
avatarColorHex = "#aa00aa",
isPremium = true,
isLoggedIn = true,
isVaultUnlocked = true,
needsPasswordReset = false,
isBiometricsEnabled = false,
organizations = emptyList(),
),
),
)
}
}
private const val SPECIAL_CIRCUMSTANCE_KEY: String = "special-circumstance"