mirror of
https://github.com/bitwarden/android.git
synced 2025-02-18 13:00:01 +03:00
Minor cleanup of the MainViewModelTests and the AuthRepo (#1023)
This commit is contained in:
parent
6e3c5930a1
commit
874ead8f3e
2 changed files with 49 additions and 67 deletions
|
@ -233,8 +233,7 @@ class AuthRepositoryImpl(
|
||||||
private val yubiKeyResultChannel = Channel<YubiKeyResult>(capacity = Int.MAX_VALUE)
|
private val yubiKeyResultChannel = Channel<YubiKeyResult>(capacity = Int.MAX_VALUE)
|
||||||
override val yubiKeyResultFlow: Flow<YubiKeyResult> = yubiKeyResultChannel.receiveAsFlow()
|
override val yubiKeyResultFlow: Flow<YubiKeyResult> = yubiKeyResultChannel.receiveAsFlow()
|
||||||
|
|
||||||
private val mutableSsoCallbackResultFlow =
|
private val mutableSsoCallbackResultFlow = bufferedMutableSharedFlow<SsoCallbackResult>()
|
||||||
bufferedMutableSharedFlow<SsoCallbackResult>()
|
|
||||||
override val ssoCallbackResultFlow: Flow<SsoCallbackResult> =
|
override val ssoCallbackResultFlow: Flow<SsoCallbackResult> =
|
||||||
mutableSsoCallbackResultFlow.asSharedFlow()
|
mutableSsoCallbackResultFlow.asSharedFlow()
|
||||||
|
|
||||||
|
@ -377,15 +376,17 @@ class AuthRepositoryImpl(
|
||||||
password: String?,
|
password: String?,
|
||||||
twoFactorData: TwoFactorDataModel,
|
twoFactorData: TwoFactorDataModel,
|
||||||
captchaToken: String?,
|
captchaToken: String?,
|
||||||
): LoginResult = identityTokenAuthModel?.let {
|
): LoginResult = identityTokenAuthModel
|
||||||
loginCommon(
|
?.let {
|
||||||
email = email,
|
loginCommon(
|
||||||
password = password,
|
email = email,
|
||||||
authModel = it,
|
password = password,
|
||||||
twoFactorData = twoFactorData,
|
authModel = it,
|
||||||
captchaToken = captchaToken ?: twoFactorResponse?.captchaToken,
|
twoFactorData = twoFactorData,
|
||||||
)
|
captchaToken = captchaToken ?: twoFactorResponse?.captchaToken,
|
||||||
} ?: LoginResult.Error(errorMessage = null)
|
)
|
||||||
|
}
|
||||||
|
?: LoginResult.Error(errorMessage = null)
|
||||||
|
|
||||||
override suspend fun login(
|
override suspend fun login(
|
||||||
email: String,
|
email: String,
|
||||||
|
@ -588,17 +589,18 @@ class AuthRepositoryImpl(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun resendVerificationCodeEmail(): ResendEmailResult =
|
override suspend fun resendVerificationCodeEmail(): ResendEmailResult =
|
||||||
resendEmailRequestJson?.let { jsonRequest ->
|
resendEmailRequestJson
|
||||||
accountsService.resendVerificationCodeEmail(body = jsonRequest).fold(
|
?.let { jsonRequest ->
|
||||||
onFailure = { ResendEmailResult.Error(message = it.message) },
|
accountsService.resendVerificationCodeEmail(body = jsonRequest).fold(
|
||||||
onSuccess = { ResendEmailResult.Success },
|
onFailure = { ResendEmailResult.Error(message = it.message) },
|
||||||
)
|
onSuccess = { ResendEmailResult.Success },
|
||||||
} ?: ResendEmailResult.Error(message = null)
|
)
|
||||||
|
}
|
||||||
|
?: ResendEmailResult.Error(message = null)
|
||||||
|
|
||||||
@Suppress("ReturnCount")
|
@Suppress("ReturnCount")
|
||||||
override fun switchAccount(userId: String): SwitchAccountResult {
|
override fun switchAccount(userId: String): SwitchAccountResult {
|
||||||
val currentUserState = authDiskSource.userState
|
val currentUserState = authDiskSource.userState ?: return SwitchAccountResult.NoChange
|
||||||
?: return SwitchAccountResult.NoChange
|
|
||||||
val previousActiveUserId = currentUserState.activeUserId
|
val previousActiveUserId = currentUserState.activeUserId
|
||||||
|
|
||||||
if (userId == previousActiveUserId) {
|
if (userId == previousActiveUserId) {
|
||||||
|
@ -699,9 +701,7 @@ class AuthRepositoryImpl(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onFailure = {
|
onFailure = { RegisterResult.Error(errorMessage = null) },
|
||||||
RegisterResult.Error(errorMessage = null)
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -709,10 +709,7 @@ class AuthRepositoryImpl(
|
||||||
return accountsService.requestPasswordHint(email).fold(
|
return accountsService.requestPasswordHint(email).fold(
|
||||||
onSuccess = {
|
onSuccess = {
|
||||||
when (it) {
|
when (it) {
|
||||||
is PasswordHintResponseJson.Error -> {
|
is PasswordHintResponseJson.Error -> PasswordHintResult.Error(it.errorMessage)
|
||||||
PasswordHintResult.Error(it.errorMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
PasswordHintResponseJson.Success -> PasswordHintResult.Success
|
PasswordHintResponseJson.Success -> PasswordHintResult.Success
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -1133,14 +1130,8 @@ class AuthRepositoryImpl(
|
||||||
password = password,
|
password = password,
|
||||||
)
|
)
|
||||||
.fold(
|
.fold(
|
||||||
onSuccess = {
|
onSuccess = { PasswordStrengthResult.Success(passwordStrength = it) },
|
||||||
PasswordStrengthResult.Success(
|
onFailure = { PasswordStrengthResult.Error },
|
||||||
passwordStrength = it,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onFailure = {
|
|
||||||
PasswordStrengthResult.Error
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Suppress("ReturnCount")
|
@Suppress("ReturnCount")
|
||||||
|
@ -1188,7 +1179,6 @@ class AuthRepositoryImpl(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("CyclomaticComplexMethod", "ReturnCount")
|
|
||||||
override suspend fun validatePasswordAgainstPolicies(
|
override suspend fun validatePasswordAgainstPolicies(
|
||||||
password: String,
|
password: String,
|
||||||
): Boolean = passwordPolicies
|
): Boolean = passwordPolicies
|
||||||
|
|
|
@ -4,8 +4,6 @@ import android.content.Intent
|
||||||
import androidx.lifecycle.SavedStateHandle
|
import androidx.lifecycle.SavedStateHandle
|
||||||
import app.cash.turbine.test
|
import app.cash.turbine.test
|
||||||
import com.bitwarden.core.CipherView
|
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.auth.util.getPasswordlessRequestDataIntentOrNull
|
||||||
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManager
|
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManager
|
||||||
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManagerImpl
|
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.PasswordlessRequestData
|
||||||
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
|
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
|
||||||
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
|
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.base.BaseViewModelTest
|
||||||
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
|
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
|
||||||
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
|
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
|
import io.mockk.mockkStatic
|
||||||
|
import io.mockk.unmockkStatic
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.test.runTest
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.jupiter.api.AfterEach
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertNull
|
import org.junit.jupiter.api.Assertions.assertNull
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
class MainViewModelTest : BaseViewModelTest() {
|
class MainViewModelTest : BaseViewModelTest() {
|
||||||
|
|
||||||
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
|
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
|
||||||
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT)
|
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT)
|
||||||
private val mutableUserStateFlow = MutableStateFlow<UserState?>(DEFAULT_USER_STATE)
|
|
||||||
private val mutableScreenCaptureAllowedFlow = MutableStateFlow(true)
|
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> {
|
private val settingsRepository = mockk<SettingsRepository> {
|
||||||
every { appTheme } returns AppTheme.DEFAULT
|
every { appTheme } returns AppTheme.DEFAULT
|
||||||
every { appThemeStateFlow } returns mutableAppThemeFlow
|
every { appThemeStateFlow } returns mutableAppThemeFlow
|
||||||
|
@ -52,6 +47,24 @@ class MainViewModelTest : BaseViewModelTest() {
|
||||||
}
|
}
|
||||||
private val savedStateHandle = SavedStateHandle()
|
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")
|
@Suppress("MaxLineLength")
|
||||||
@Test
|
@Test
|
||||||
fun `initialization should set a saved SpecialCircumstance to the SpecialCircumstanceManager if present`() {
|
fun `initialization should set a saved SpecialCircumstance to the SpecialCircumstanceManager if present`() {
|
||||||
|
@ -360,27 +373,6 @@ class MainViewModelTest : BaseViewModelTest() {
|
||||||
set(SPECIAL_CIRCUMSTANCE_KEY, initialSpecialCircumstance)
|
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"
|
||||||
|
|
Loading…
Add table
Reference in a new issue