2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-19 09:25:34 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/19797 - New FenixAccountManager#getAccountState()

This new method allows a single source of truth for getting the up-to-date
account status without clients interesting in this needing to know what to
check for.
This commit is contained in:
Mugurell 2021-06-11 18:51:57 +03:00
parent db5fa44113
commit efebcc2bc8
2 changed files with 76 additions and 0 deletions

View File

@ -21,6 +21,20 @@ open class FenixAccountManager(context: Context) {
val accountProfileEmail val accountProfileEmail
get() = accountManager.accountProfile()?.email get() = accountManager.accountProfile()?.email
/**
* The current state of the Firefox Account. See [AccountState].
*/
val accountState: AccountState
get() = if (accountManager.authenticatedAccount() == null) {
AccountState.NO_ACCOUNT
} else {
if (accountManager.accountNeedsReauth()) {
AccountState.NEEDS_REAUTHENTICATION
} else {
AccountState.AUTHENTICATED
}
}
/** /**
* Check if the current account is signed in and authenticated. * Check if the current account is signed in and authenticated.
*/ */
@ -31,3 +45,23 @@ open class FenixAccountManager(context: Context) {
return account != null && !needsReauth return account != null && !needsReauth
} }
} }
/**
* General states as an overview of the current Firefox Account.
*/
enum class AccountState {
/**
* There is no known Firefox Account.
*/
NO_ACCOUNT,
/**
* A Firefox Account exists but needs to be re-authenticated.
*/
NEEDS_REAUTHENTICATION,
/**
* A Firefox Account exists and the user is currently signed into it.
*/
AUTHENTICATED,
}

View File

@ -12,6 +12,7 @@ import mozilla.components.concept.sync.Profile
import mozilla.components.service.fxa.manager.FxaAccountManager import mozilla.components.service.fxa.manager.FxaAccountManager
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -98,4 +99,45 @@ class FenixAccountManagerTest {
val signedIn = fenixFxaManager.signedInToFxa() val signedIn = fenixFxaManager.signedInToFxa()
assertFalse(signedIn) assertFalse(signedIn)
} }
@Test
fun `GIVEN no account exists WHEN accountState is called THEN it returns AccountState#NO_ACCOUNT`() {
every { context.components.backgroundServices.accountManager } returns accountManagerComponent
every { accountManagerComponent.authenticatedAccount() } returns null
fenixFxaManager = FenixAccountManager(context)
assertSame(AccountState.NO_ACCOUNT, fenixFxaManager.accountState)
// No account but signed in should not be possible. Test protecting against such a regression.
every { accountManagerComponent.accountNeedsReauth() } returns false
assertSame(AccountState.NO_ACCOUNT, fenixFxaManager.accountState)
// No account and signed out still means no account. Test protecting against such a regression.
every { accountManagerComponent.accountNeedsReauth() } returns true
assertSame(AccountState.NO_ACCOUNT, fenixFxaManager.accountState)
}
@Test
fun `GIVEN an account exists but needs to be re-authenticated WHEN accountState is called THEN it returns AccountState#NEEDS_REAUTHENTICATION`() {
every { context.components.backgroundServices.accountManager } returns accountManagerComponent
every { accountManagerComponent.authenticatedAccount() } returns mockk()
every { accountManagerComponent.accountNeedsReauth() } returns true
fenixFxaManager = FenixAccountManager(context)
val result = fenixFxaManager.accountState
assertSame(AccountState.NEEDS_REAUTHENTICATION, result)
}
@Test
fun `GIVEN an account exists and doesn't need to be re-authenticated WHEN accountState is called THEN it returns AccountState#AUTHENTICATED`() {
every { context.components.backgroundServices.accountManager } returns accountManagerComponent
every { accountManagerComponent.authenticatedAccount() } returns mockk()
every { accountManagerComponent.accountNeedsReauth() } returns false
fenixFxaManager = FenixAccountManager(context)
val result = fenixFxaManager.accountState
assertSame(AccountState.AUTHENTICATED, result)
}
} }