mirror of
https://github.com/bitwarden/android.git
synced 2026-05-12 14:51:15 -05:00
[PM-6702] Tests code clean up
This commit is contained in:
@@ -157,8 +157,6 @@ class FakeAuthDiskSource : AuthDiskSource {
|
||||
storedOrganizationKeys[userId] = organizationKeys
|
||||
}
|
||||
|
||||
|
||||
|
||||
override fun getOrganizations(
|
||||
userId: String,
|
||||
): List<SyncResponseJson.Profile.Organization>? = storedOrganizations[userId]
|
||||
|
||||
@@ -13,7 +13,6 @@ import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.compose.ui.test.performScrollTo
|
||||
import androidx.compose.ui.test.performTextInput
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
|
||||
import com.x8bit.bitwarden.ui.auth.feature.completeregistration.CompleteRegistrationAction.CheckDataBreachesToggle
|
||||
import com.x8bit.bitwarden.ui.auth.feature.completeregistration.CompleteRegistrationAction.CloseClick
|
||||
|
||||
@@ -18,7 +18,6 @@ import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManager
|
||||
import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManagerImpl
|
||||
import com.x8bit.bitwarden.data.platform.manager.model.CompleteRegistrationData
|
||||
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.FakeEnvironmentRepository
|
||||
import com.x8bit.bitwarden.ui.auth.feature.completeregistration.CompleteRegistrationAction.CloseClick
|
||||
import com.x8bit.bitwarden.ui.auth.feature.completeregistration.CompleteRegistrationAction.ConfirmPasswordInputChange
|
||||
@@ -56,7 +55,8 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val fakeEnvironmentRepository = FakeEnvironmentRepository()
|
||||
|
||||
private val specialCircumstanceManager: SpecialCircumstanceManager = SpecialCircumstanceManagerImpl()
|
||||
private val specialCircumstanceManager: SpecialCircumstanceManager =
|
||||
SpecialCircumstanceManagerImpl()
|
||||
|
||||
private var viewmodelVerifyEmailCalled = false
|
||||
|
||||
@@ -78,7 +78,7 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
|
||||
@Test
|
||||
fun `onCleared should erase specialCircumstance`() = runTest {
|
||||
specialCircumstanceManager.specialCircumstance = SpecialCircumstance.CompleteRegistration(
|
||||
specialCircumstanceManager.specialCircumstance = SpecialCircumstance.CompleteRegistration(
|
||||
completeRegistrationData = CompleteRegistrationData(
|
||||
email = EMAIL,
|
||||
verificationToken = TOKEN,
|
||||
@@ -87,7 +87,7 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
System.currentTimeMillis()
|
||||
)
|
||||
|
||||
val viewModel = CompleteRegistrationViewModel(
|
||||
val viewModel = CompleteRegistrationViewModel(
|
||||
savedStateHandle = SavedStateHandle(mapOf("state" to DEFAULT_STATE)),
|
||||
authRepository = mockAuthRepository,
|
||||
environmentRepository = fakeEnvironmentRepository,
|
||||
@@ -98,51 +98,53 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `CreateAccountClick with password below 12 chars should show password length dialog`() = runTest {
|
||||
val input = "abcdefghikl"
|
||||
coEvery {
|
||||
mockAuthRepository.getPasswordStrength("test@test.com", input)
|
||||
} returns PasswordStrengthResult.Error
|
||||
val viewModel = createCompleteRegistrationViewModel()
|
||||
viewModel.trySendAction(PasswordInputChange(input))
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
passwordInput = input,
|
||||
dialog = CompleteRegistrationDialog.Error(
|
||||
BasicDialogState.Shown(
|
||||
title = R.string.an_error_has_occurred.asText(),
|
||||
message = R.string.master_password_length_val_message_x.asText(12),
|
||||
fun `CreateAccountClick with password below 12 chars should show password length dialog`() =
|
||||
runTest {
|
||||
val input = "abcdefghikl"
|
||||
coEvery {
|
||||
mockAuthRepository.getPasswordStrength("test@test.com", input)
|
||||
} returns PasswordStrengthResult.Error
|
||||
val viewModel = createCompleteRegistrationViewModel()
|
||||
viewModel.trySendAction(PasswordInputChange(input))
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
passwordInput = input,
|
||||
dialog = CompleteRegistrationDialog.Error(
|
||||
BasicDialogState.Shown(
|
||||
title = R.string.an_error_has_occurred.asText(),
|
||||
message = R.string.master_password_length_val_message_x.asText(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(expectedState, awaitItem())
|
||||
)
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(expectedState, awaitItem())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `CreateAccountClick with passwords not matching should show password match dialog`() = runTest {
|
||||
val input = "testtesttesttest"
|
||||
coEvery {
|
||||
mockAuthRepository.getPasswordStrength(EMAIL, input)
|
||||
} returns PasswordStrengthResult.Error
|
||||
val viewModel = createCompleteRegistrationViewModel()
|
||||
viewModel.trySendAction(PasswordInputChange(input))
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
userEmail = EMAIL,
|
||||
passwordInput = input,
|
||||
dialog = CompleteRegistrationDialog.Error(
|
||||
BasicDialogState.Shown(
|
||||
title = R.string.an_error_has_occurred.asText(),
|
||||
message = R.string.master_password_confirmation_val_message.asText(),
|
||||
fun `CreateAccountClick with passwords not matching should show password match dialog`() =
|
||||
runTest {
|
||||
val input = "testtesttesttest"
|
||||
coEvery {
|
||||
mockAuthRepository.getPasswordStrength(EMAIL, input)
|
||||
} returns PasswordStrengthResult.Error
|
||||
val viewModel = createCompleteRegistrationViewModel()
|
||||
viewModel.trySendAction(PasswordInputChange(input))
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
userEmail = EMAIL,
|
||||
passwordInput = input,
|
||||
dialog = CompleteRegistrationDialog.Error(
|
||||
BasicDialogState.Shown(
|
||||
title = R.string.an_error_has_occurred.asText(),
|
||||
message = R.string.master_password_confirmation_val_message.asText(),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(expectedState, awaitItem())
|
||||
)
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(expectedState, awaitItem())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `CreateAccountClick with all inputs valid should show and hide loading dialog`() = runTest {
|
||||
@@ -218,34 +220,35 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `CreateAccountClick register returns CaptchaRequired should emit NavigateToCaptcha`() = runTest {
|
||||
val mockkUri = mockk<Uri>()
|
||||
every {
|
||||
generateUriForCaptcha(captchaId = "mock_captcha_id")
|
||||
} returns mockkUri
|
||||
val repo = mockk<AuthRepository> {
|
||||
every { captchaTokenResultFlow } returns flowOf()
|
||||
coEvery {
|
||||
register(
|
||||
email = EMAIL,
|
||||
masterPassword = PASSWORD,
|
||||
masterPasswordHint = null,
|
||||
emailVerificationToken = TOKEN,
|
||||
captchaToken = null,
|
||||
shouldCheckDataBreaches = false,
|
||||
isMasterPasswordStrong = true,
|
||||
fun `CreateAccountClick register returns CaptchaRequired should emit NavigateToCaptcha`() =
|
||||
runTest {
|
||||
val mockkUri = mockk<Uri>()
|
||||
every {
|
||||
generateUriForCaptcha(captchaId = "mock_captcha_id")
|
||||
} returns mockkUri
|
||||
val repo = mockk<AuthRepository> {
|
||||
every { captchaTokenResultFlow } returns flowOf()
|
||||
coEvery {
|
||||
register(
|
||||
email = EMAIL,
|
||||
masterPassword = PASSWORD,
|
||||
masterPasswordHint = null,
|
||||
emailVerificationToken = TOKEN,
|
||||
captchaToken = null,
|
||||
shouldCheckDataBreaches = false,
|
||||
isMasterPasswordStrong = true,
|
||||
)
|
||||
} returns RegisterResult.CaptchaRequired(captchaId = "mock_captcha_id")
|
||||
}
|
||||
val viewModel = createCompleteRegistrationViewModel(VALID_INPUT_STATE, repo)
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
assertEquals(
|
||||
CompleteRegistrationEvent.NavigateToCaptcha(uri = mockkUri),
|
||||
awaitItem(),
|
||||
)
|
||||
} returns RegisterResult.CaptchaRequired(captchaId = "mock_captcha_id")
|
||||
}
|
||||
}
|
||||
val viewModel = createCompleteRegistrationViewModel(VALID_INPUT_STATE, repo)
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
assertEquals(
|
||||
CompleteRegistrationEvent.NavigateToCaptcha(uri = mockkUri),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `CreateAccountClick register returns Success should emit NavigateToLogin`() = runTest {
|
||||
@@ -369,7 +372,8 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
isCheckDataBreachesToggled = true,
|
||||
)
|
||||
|
||||
val viewModel = createCompleteRegistrationViewModel(completeRegistrationState = initialState)
|
||||
val viewModel =
|
||||
createCompleteRegistrationViewModel(completeRegistrationState = initialState)
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(
|
||||
@@ -402,7 +406,8 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
passwordStrengthState = PasswordStrengthState.WEAK_1,
|
||||
isCheckDataBreachesToggled = true,
|
||||
)
|
||||
val viewModel = createCompleteRegistrationViewModel(completeRegistrationState = initialState)
|
||||
val viewModel =
|
||||
createCompleteRegistrationViewModel(completeRegistrationState = initialState)
|
||||
viewModel.trySendAction(CompleteRegistrationAction.CreateAccountClick)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(
|
||||
@@ -432,7 +437,10 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
|
||||
DEFAULT_STATE.copy(fromEmail = true)
|
||||
)
|
||||
viewModel.eventFlow.test {
|
||||
assertEquals(CompleteRegistrationEvent.ShowToast(R.string.email_verified.asText()), awaitItem())
|
||||
assertEquals(
|
||||
CompleteRegistrationEvent.ShowToast(R.string.email_verified.asText()),
|
||||
awaitItem()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.x8bit.bitwarden.ui.auth.feature.startregistration
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.filterToOne
|
||||
import androidx.compose.ui.test.hasAnyAncestor
|
||||
@@ -55,7 +54,9 @@ class StartRegistrationScreenTest : BaseComposeTest() {
|
||||
composeTestRule.setContent {
|
||||
StartRegistrationScreen(
|
||||
onNavigateBack = { onNavigateBackCalled = true },
|
||||
onNavigateToCompleteRegistration = { _, _ -> onNavigateToCompleteRegistrationCalled = true },
|
||||
onNavigateToCompleteRegistration = { _, _ ->
|
||||
onNavigateToCompleteRegistrationCalled = true
|
||||
},
|
||||
onNavigateToCheckEmail = { _ -> onNavigateToCheckEmailCalled = true },
|
||||
onNavigateToEnvironment = { onNavigateToEnvironmentCalled = true },
|
||||
intentManager = intentManager,
|
||||
@@ -78,18 +79,22 @@ class StartRegistrationScreenTest : BaseComposeTest() {
|
||||
|
||||
@Test
|
||||
fun `onNavigateToCompleteRegistration event should invoke navigate to complete registration`() {
|
||||
mutableEventFlow.tryEmit(StartRegistrationEvent.NavigateToCompleteRegistration(
|
||||
email = "email",
|
||||
verificationToken = "verificationToken"
|
||||
))
|
||||
mutableEventFlow.tryEmit(
|
||||
StartRegistrationEvent.NavigateToCompleteRegistration(
|
||||
email = "email",
|
||||
verificationToken = "verificationToken"
|
||||
)
|
||||
)
|
||||
assertTrue(onNavigateToCompleteRegistrationCalled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToCheckEmail event should invoke navigate to check email`() {
|
||||
mutableEventFlow.tryEmit(StartRegistrationEvent.NavigateToCheckEmail(
|
||||
email = "email",
|
||||
))
|
||||
mutableEventFlow.tryEmit(
|
||||
StartRegistrationEvent.NavigateToCheckEmail(
|
||||
email = "email",
|
||||
)
|
||||
)
|
||||
assertTrue(onNavigateToCheckEmailCalled)
|
||||
}
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ class StartRegistrationViewModelTest : BaseViewModelTest() {
|
||||
coEvery {
|
||||
sendVerificationEmail(
|
||||
email = EMAIL,
|
||||
name= NAME,
|
||||
name = NAME,
|
||||
receiveMarketingEmails = true,
|
||||
)
|
||||
} returns SendVerificationEmailResult.Success(
|
||||
@@ -175,7 +175,7 @@ class StartRegistrationViewModelTest : BaseViewModelTest() {
|
||||
coEvery {
|
||||
sendVerificationEmail(
|
||||
email = EMAIL,
|
||||
name= NAME,
|
||||
name = NAME,
|
||||
receiveMarketingEmails = true,
|
||||
)
|
||||
} returns SendVerificationEmailResult.Error(errorMessage = "mock_error")
|
||||
@@ -217,10 +217,10 @@ class StartRegistrationViewModelTest : BaseViewModelTest() {
|
||||
coEvery {
|
||||
sendVerificationEmail(
|
||||
email = EMAIL,
|
||||
name= NAME,
|
||||
name = NAME,
|
||||
receiveMarketingEmails = true,
|
||||
)
|
||||
} returns SendVerificationEmailResult.Success(emailVerificationToken = "verification_token",)
|
||||
} returns SendVerificationEmailResult.Success(emailVerificationToken = "verification_token")
|
||||
}
|
||||
val viewModel = StartRegistrationViewModel(
|
||||
savedStateHandle = validInputHandle,
|
||||
@@ -298,7 +298,11 @@ class StartRegistrationViewModelTest : BaseViewModelTest() {
|
||||
)
|
||||
viewModel.stateFlow.test {
|
||||
awaitItem()
|
||||
viewModel.trySendAction(StartRegistrationAction.EnvironmentTypeSelect(inputEnvironmentType))
|
||||
viewModel.trySendAction(
|
||||
StartRegistrationAction.EnvironmentTypeSelect(
|
||||
inputEnvironmentType
|
||||
)
|
||||
)
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(selectedEnvironmentType = Environment.Type.EU),
|
||||
awaitItem(),
|
||||
@@ -315,7 +319,11 @@ class StartRegistrationViewModelTest : BaseViewModelTest() {
|
||||
environmentRepository = fakeEnvironmentRepository
|
||||
)
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(StartRegistrationAction.EnvironmentTypeSelect(inputEnvironmentType))
|
||||
viewModel.trySendAction(
|
||||
StartRegistrationAction.EnvironmentTypeSelect(
|
||||
inputEnvironmentType
|
||||
)
|
||||
)
|
||||
assertEquals(
|
||||
StartRegistrationEvent.NavigateToEnvironment,
|
||||
awaitItem(),
|
||||
@@ -332,10 +340,12 @@ class StartRegistrationViewModelTest : BaseViewModelTest() {
|
||||
)
|
||||
viewModel.trySendAction(EmailInputChange("input"))
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(DEFAULT_STATE.copy(
|
||||
emailInput = "input",
|
||||
isContinueButtonEnabled = true,
|
||||
), awaitItem())
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
emailInput = "input",
|
||||
isContinueButtonEnabled = true,
|
||||
), awaitItem()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -141,7 +141,7 @@ class VaultViewModelTest : BaseViewModelTest() {
|
||||
val updatedUserId = "lockedUserId"
|
||||
viewModel.trySendAction(
|
||||
VaultAction.SwitchAccountClick(
|
||||
accountSummary = mockk() {
|
||||
accountSummary = mockk {
|
||||
every { userId } returns updatedUserId
|
||||
},
|
||||
),
|
||||
@@ -933,7 +933,7 @@ class VaultViewModelTest : BaseViewModelTest() {
|
||||
val updatedUserId = "lockedUserId"
|
||||
viewModel.trySendAction(
|
||||
VaultAction.SwitchAccountClick(
|
||||
accountSummary = mockk() {
|
||||
accountSummary = mockk {
|
||||
every { userId } returns updatedUserId
|
||||
},
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user