GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit 1497e488 authored by ekager's avatar ekager Committed by Emily Kager

For #13084 - Update SavedLoginsStorageControllerTest

parent 9066d0bf
......@@ -4,35 +4,38 @@
package org.mozilla.fenix.settings.logins
import android.os.Looper
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import mozilla.components.concept.storage.Login
import mozilla.components.service.sync.logins.SyncableLoginsStorage
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.ext.directionsEq
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.settings.logins.controller.SavedLoginsStorageController
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.LooperMode
import org.mozilla.fenix.settings.logins.fragment.EditLoginFragmentDirections
@ExperimentalCoroutinesApi
@LooperMode(LooperMode.Mode.PAUSED)
@RunWith(FenixRobolectricTestRunner::class)
class SavedLoginsStorageControllerTest {
private lateinit var components: Components
@get:Rule
val coroutinesTestRule = MainCoroutineRule(TestCoroutineDispatcher())
private val passwordsStorage: SyncableLoginsStorage = mockk(relaxed = true)
private lateinit var controller: SavedLoginsStorageController
private val navController: NavController = mockk(relaxed = true)
......@@ -47,11 +50,10 @@ class SavedLoginsStorageControllerTest {
}
coEvery { passwordsStorage.get(any()) } returns loginMock
every { loginsFragmentStore.dispatch(any()) } returns mockk()
components = mockk(relaxed = true)
controller = SavedLoginsStorageController(
passwordsStorage = passwordsStorage,
viewLifecycleScope = MainScope(),
viewLifecycleScope = scope,
navController = navController,
loginsFragmentStore = loginsFragmentStore
)
......@@ -65,35 +67,44 @@ class SavedLoginsStorageControllerTest {
@Test
fun `WHEN a login is deleted, THEN navigate back to the previous page`() = runBlocking {
val loginId = "id"
// mock for deleteLoginJob: Deferred<Boolean>?
coEvery { passwordsStorage.delete(any()) } returns true
controller.delete(loginId)
shadow()
coVerify { passwordsStorage.delete(loginId) }
}
private fun shadow() {
// solves issue with Roboelectric v4.3 and SDK 28
// https://github.com/robolectric/robolectric/issues/5356
shadowOf(Looper.getMainLooper()).idle()
coVerify {
passwordsStorage.delete(loginId)
navController.popBackStack(R.id.savedLoginsFragment, false)
}
}
@Test
fun `WHEN fetching the login list, THEN update the state in the store`() {
val loginId = "id"
// for deferredLogin: Deferred<List<Login>>?
coEvery { passwordsStorage.list() } returns listOf()
val login = Login(
guid = "id",
origin = "https://www.test.co.gov.org",
username = "user123",
password = "securePassword1",
httpRealm = "httpRealm",
formActionOrigin = ""
)
coEvery { passwordsStorage.list() } returns listOf(login)
controller.fetchLoginDetails(loginId)
controller.fetchLoginDetails(login.guid!!)
coVerify { passwordsStorage.list() }
val expectedLogin = login.mapToSavedLogin()
coVerify {
passwordsStorage.list()
loginsFragmentStore.dispatch(
LoginsAction.UpdateCurrentLogin(
expectedLogin
)
)
}
}
@Test
fun `WHEN saving an update to an item, THEN navigate to login detail view`() {
val login = Login(
val oldLogin = Login(
guid = "id",
origin = "https://www.test.co.gov.org",
username = "user123",
......@@ -101,11 +112,38 @@ class SavedLoginsStorageControllerTest {
httpRealm = "httpRealm",
formActionOrigin = ""
)
coEvery { passwordsStorage.get(any()) } returns loginMock
controller.save(login.guid!!, login.username, login.password)
coEvery { passwordsStorage.get(any()) } returns oldLogin
coEvery { passwordsStorage.update(any()) } just Runs
controller.save(oldLogin.guid!!, "newUsername", "newPassword")
val directions =
EditLoginFragmentDirections.actionEditLoginFragmentToLoginDetailFragment(
oldLogin.guid!!
)
val newLogin = Login(
guid = "id",
origin = "https://www.test.co.gov.org",
username = "newUsername",
password = "newPassword",
httpRealm = "httpRealm",
formActionOrigin = ""
)
val expectedNewList = listOf(newLogin.mapToSavedLogin())
coVerify { passwordsStorage.get(any()) }
coVerify {
passwordsStorage.get(oldLogin.guid!!)
passwordsStorage.update(newLogin)
loginsFragmentStore.dispatch(
LoginsAction.UpdateLoginsList(
expectedNewList
)
)
navController.navigate(directionsEq(directions))
}
}
@Test
......@@ -119,19 +157,34 @@ class SavedLoginsStorageControllerTest {
formActionOrigin = ""
)
val login2 = Login(
guid = "id2",
origin = "https://www.test.co.gov.org",
username = "user1234",
password = "securePassword1",
httpRealm = "httpRealm",
formActionOrigin = ""
)
coEvery { passwordsStorage.get(any()) } returns login
// for deferredLogin: Deferred<List<Login>>?
val dupeList = listOf(login2)
coEvery {
passwordsStorage.getPotentialDupesIgnoringUsername(any())
} returns listOf()
} returns dupeList
controller.findPotentialDuplicates(login.guid!!)
shadow()
val expectedDupeList = dupeList.map { it.mapToSavedLogin() }
coVerify {
passwordsStorage.getPotentialDupesIgnoringUsername(login)
loginsFragmentStore.dispatch(
LoginsAction.ListOfDupes(
dupeList = expectedDupeList
)
)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment