Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Merge branch 'fix/dont-try-to-switch-to-main-be-if-legacy-user-null' …
Browse files Browse the repository at this point in the history
…into 'develop'

Does not try to switch to main BE when current legacy user is null

See merge request android/mail/proton-mail-android!1172
  • Loading branch information
Maciej Surmacz committed Sep 15, 2022
2 parents 3b914bc + 2a7d59b commit e238027
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class SwitchToMainBackendIfOnProxy @Inject constructor(
@DefaultSharedPreferences private val sharedPreferences: SharedPreferences
) {

suspend operator fun invoke(): Result =
if (userManager.requireCurrentLegacyUser().usingDefaultApi) {
suspend operator fun invoke(): Result {
val currentLegacyUser = userManager.currentLegacyUser
return if (currentLegacyUser == null || currentLegacyUser.usingDefaultApi) {
AlreadyUsingMainBackend
} else {
val proxyBeforeSwitch = Proxies.getInstance(proxyList = null, sharedPreferences)
Expand All @@ -50,6 +51,7 @@ class SwitchToMainBackendIfOnProxy @Inject constructor(
SwitchFailure
}
}
}

sealed class Result
object AlreadyUsingMainBackend : Result()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal class SwitchToMainBackendIfOnProxyTest {
private val legacyUserMock = mockk<User>()
private val userManagerMock = mockk<UserManager> {
every { requireCurrentLegacyUser() } returns legacyUserMock
every { currentLegacyUser } returns legacyUserMock
}
private val switchToMainBackendIfAvailableMock = mockk<SwitchToMainBackendIfAvailable>()
private val sharedPrefsMock = mockk<SharedPreferences>()
Expand Down Expand Up @@ -74,6 +75,20 @@ internal class SwitchToMainBackendIfOnProxyTest {
coVerify(exactly = 0) { switchToMainBackendIfAvailableMock() }
}

@Test
fun `should assume main BE is used without trying to switch to main BE when user is null`() = runBlockingTest {
// given
val expectedResult = SwitchToMainBackendIfOnProxy.AlreadyUsingMainBackend
every { userManagerMock.currentLegacyUser } returns null

// when
val actualResult = switchToMainBackendIfOnProxy()

// then
assertEquals(expectedResult, actualResult)
coVerify(exactly = 0) { switchToMainBackendIfAvailableMock() }
}

@Test
fun `should return failure result when not using main BE and switching to main BE failed`() = runBlockingTest {
// given
Expand Down

0 comments on commit e238027

Please sign in to comment.