-
Notifications
You must be signed in to change notification settings - Fork 934
Add comprehensive tests for Unlock feature #6426
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
Merged
+774
โ0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6bb3566
Add comprehensive tests for Unlock feature
SaintPatrck 93b06c1
Refactor UnlockViewModelTest to use complete state assertions
SaintPatrck e3d99c4
Fix missing import in UnlockViewModelTest
SaintPatrck 105efc1
Address review comments
SaintPatrck c3b18e3
Address review comments
SaintPatrck 246c394
Comments
SaintPatrck 4eb60f9
Remove unused variable initialization in UnlockScreenTest
SaintPatrck e92447d
Merge branch 'main' into test/critical/unlock
SaintPatrck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
286 changes: 286 additions & 0 deletions
286
authenticator/src/test/kotlin/com/bitwarden/authenticator/ui/auth/unlock/UnlockScreenTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| package com.bitwarden.authenticator.ui.auth.unlock | ||
|
|
||
| import androidx.compose.ui.test.assert | ||
| import androidx.compose.ui.test.assertIsDisplayed | ||
| import androidx.compose.ui.test.hasAnyAncestor | ||
| import androidx.compose.ui.test.isDialog | ||
| import androidx.compose.ui.test.onNodeWithContentDescription | ||
| import androidx.compose.ui.test.onNodeWithTag | ||
| import androidx.compose.ui.test.onNodeWithText | ||
| import androidx.compose.ui.test.performClick | ||
| import com.bitwarden.authenticator.ui.platform.base.AuthenticatorComposeTest | ||
| import com.bitwarden.authenticator.ui.platform.manager.biometrics.BiometricsManager | ||
| import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow | ||
| import com.bitwarden.ui.platform.resource.BitwardenString | ||
| import com.bitwarden.ui.util.asText | ||
| import io.mockk.every | ||
| import io.mockk.just | ||
| import io.mockk.mockk | ||
| import io.mockk.runs | ||
| import io.mockk.slot | ||
| import io.mockk.verify | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import javax.crypto.Cipher | ||
|
|
||
| class UnlockScreenTest : AuthenticatorComposeTest() { | ||
|
|
||
| private var onUnlockedCalled = false | ||
|
|
||
| private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE) | ||
| private val mutableEventFlow = bufferedMutableSharedFlow<UnlockEvent>() | ||
|
|
||
| private val mockViewModel: UnlockViewModel = mockk(relaxed = true) { | ||
| every { stateFlow } returns mutableStateFlow | ||
| every { eventFlow } returns mutableEventFlow | ||
| every { trySendAction(any()) } just runs | ||
| } | ||
|
|
||
| private val mockBiometricsManager: BiometricsManager = mockk(relaxed = true) | ||
| private val mockCipher: Cipher = mockk() | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| setContent( | ||
| biometricsManager = mockBiometricsManager, | ||
| ) { | ||
| UnlockScreen( | ||
| viewModel = mockViewModel, | ||
| onUnlocked = { onUnlockedCalled = true }, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `unlock button should be displayed`() { | ||
| composeTestRule | ||
| .onNodeWithText("Unlock") | ||
| .assertIsDisplayed() | ||
| } | ||
|
|
||
| @Test | ||
| fun `logo should be displayed`() { | ||
| composeTestRule | ||
| .onNodeWithContentDescription("Bitwarden Authenticator") | ||
| .assertIsDisplayed() | ||
| } | ||
|
|
||
| @Test | ||
| fun `unlock button click should send BiometricsUnlockClick action`() { | ||
| composeTestRule | ||
| .onNodeWithText("Unlock") | ||
| .performClick() | ||
|
|
||
| verify { | ||
| mockViewModel.trySendAction(UnlockAction.BiometricsUnlockClick) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `NavigateToItemListing event should call onUnlocked callback`() { | ||
| mutableEventFlow.tryEmit(UnlockEvent.NavigateToItemListing) | ||
|
|
||
| assertTrue(onUnlockedCalled) | ||
| } | ||
|
|
||
| @Test | ||
| fun `PromptForBiometrics event should call biometricsManager`() { | ||
| val onSuccessSlot = slot<(Cipher) -> Unit>() | ||
| val onCancelSlot = slot<() -> Unit>() | ||
| val onErrorSlot = slot<() -> Unit>() | ||
| val onLockOutSlot = slot<() -> Unit>() | ||
|
|
||
| every { | ||
| mockBiometricsManager.promptBiometrics( | ||
| onSuccess = capture(onSuccessSlot), | ||
| onCancel = capture(onCancelSlot), | ||
| onError = capture(onErrorSlot), | ||
| onLockOut = capture(onLockOutSlot), | ||
| cipher = mockCipher, | ||
| ) | ||
| } just runs | ||
|
|
||
| mutableEventFlow.tryEmit(UnlockEvent.PromptForBiometrics(mockCipher)) | ||
|
|
||
| verify { | ||
| mockBiometricsManager.promptBiometrics( | ||
| onSuccess = any(), | ||
| onCancel = any(), | ||
| onError = any(), | ||
| onLockOut = any(), | ||
| cipher = mockCipher, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `biometric success callback should send BiometricsUnlockSuccess action`() { | ||
| val onSuccessSlot = slot<(Cipher) -> Unit>() | ||
|
|
||
| every { | ||
| mockBiometricsManager.promptBiometrics( | ||
| onSuccess = capture(onSuccessSlot), | ||
| onCancel = any(), | ||
| onError = any(), | ||
| onLockOut = any(), | ||
| cipher = mockCipher, | ||
| ) | ||
| } just runs | ||
|
|
||
| mutableEventFlow.tryEmit(UnlockEvent.PromptForBiometrics(mockCipher)) | ||
|
|
||
| // Invoke the captured success callback | ||
| onSuccessSlot.captured.invoke(mockCipher) | ||
|
|
||
| verify { | ||
| mockViewModel.trySendAction(UnlockAction.BiometricsUnlockSuccess(mockCipher)) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `biometric lockout callback should send BiometricsLockout action`() { | ||
| val onLockOutSlot = slot<() -> Unit>() | ||
|
|
||
| every { | ||
| mockBiometricsManager.promptBiometrics( | ||
| onSuccess = any(), | ||
| onCancel = any(), | ||
| onError = any(), | ||
| onLockOut = capture(onLockOutSlot), | ||
| cipher = mockCipher, | ||
| ) | ||
| } just runs | ||
|
|
||
| mutableEventFlow.tryEmit(UnlockEvent.PromptForBiometrics(mockCipher)) | ||
|
|
||
| // Invoke the captured lockout callback | ||
| onLockOutSlot.captured.invoke() | ||
|
|
||
| verify { | ||
| mockViewModel.trySendAction(UnlockAction.BiometricsLockout) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `error dialog should display when state has Error dialog`() { | ||
| mutableStateFlow.value = DEFAULT_STATE.copy( | ||
| dialog = UnlockState.Dialog.Error( | ||
| title = "Error Title".asText(), | ||
| message = "Error Message".asText(), | ||
| ), | ||
| ) | ||
|
|
||
| composeTestRule | ||
| .onNodeWithText("Error Title") | ||
| .assert(hasAnyAncestor(isDialog())) | ||
| .assertIsDisplayed() | ||
|
|
||
| composeTestRule | ||
| .onNodeWithText("Error Message") | ||
| .assert(hasAnyAncestor(isDialog())) | ||
| .assertIsDisplayed() | ||
| } | ||
|
|
||
| @Test | ||
| fun `error dialog dismiss should send DismissDialog action`() { | ||
| mutableStateFlow.value = DEFAULT_STATE.copy( | ||
| dialog = UnlockState.Dialog.Error( | ||
| title = BitwardenString.an_error_has_occurred.asText(), | ||
| message = BitwardenString.generic_error_message.asText(), | ||
| ), | ||
| ) | ||
|
|
||
| composeTestRule | ||
| .onNodeWithTag("AcceptAlertButton") | ||
| .assert(hasAnyAncestor(isDialog())) | ||
| .performClick() | ||
|
|
||
| verify { | ||
| mockViewModel.trySendAction(UnlockAction.DismissDialog) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `loading dialog should display when state has Loading dialog`() { | ||
| mutableStateFlow.value = DEFAULT_STATE.copy( | ||
| dialog = UnlockState.Dialog.Loading, | ||
| ) | ||
|
|
||
| composeTestRule | ||
| .onNodeWithText("Loading") | ||
| .assert(hasAnyAncestor(isDialog())) | ||
| .assertIsDisplayed() | ||
| } | ||
|
|
||
| @Test | ||
| fun `no dialog should be displayed when state dialog is null`() { | ||
| mutableStateFlow.value = DEFAULT_STATE.copy(dialog = null) | ||
|
|
||
| composeTestRule | ||
| .onNodeWithText("Loading") | ||
| .assertDoesNotExist() | ||
|
|
||
| composeTestRule | ||
| .onNodeWithText("Ok") | ||
| .assertDoesNotExist() | ||
| } | ||
|
|
||
| @Test | ||
| fun `biometric cancel callback should not crash`() { | ||
| val onCancelSlot = slot<() -> Unit>() | ||
|
|
||
| every { | ||
| mockBiometricsManager.promptBiometrics( | ||
| onSuccess = any(), | ||
| onCancel = capture(onCancelSlot), | ||
| onError = any(), | ||
| onLockOut = any(), | ||
| cipher = mockCipher, | ||
| ) | ||
| } just runs | ||
|
|
||
| mutableEventFlow.tryEmit(UnlockEvent.PromptForBiometrics(mockCipher)) | ||
|
|
||
| // Invoke the captured cancel callback - should not crash | ||
| onCancelSlot.captured.invoke() | ||
|
|
||
| // Verify no action was sent (it's a no-op) | ||
| verify(exactly = 0) { | ||
| mockViewModel.trySendAction(any()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `biometric error callback should not crash`() { | ||
| val onErrorSlot = slot<() -> Unit>() | ||
|
|
||
| every { | ||
| mockBiometricsManager.promptBiometrics( | ||
| onSuccess = any(), | ||
| onCancel = any(), | ||
| onError = capture(onErrorSlot), | ||
| onLockOut = any(), | ||
| cipher = mockCipher, | ||
| ) | ||
| } just runs | ||
|
|
||
| mutableEventFlow.tryEmit(UnlockEvent.PromptForBiometrics(mockCipher)) | ||
|
|
||
| // Invoke the captured error callback - should not crash | ||
| onErrorSlot.captured.invoke() | ||
|
|
||
| // Verify no action was sent (it's a no-op) | ||
| verify(exactly = 0) { | ||
| mockViewModel.trySendAction(any()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val DEFAULT_STATE = UnlockState( | ||
| isBiometricsEnabled = true, | ||
| isBiometricsValid = true, | ||
| showBiometricInvalidatedMessage = false, | ||
| dialog = null, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.