diff --git a/app/src/main/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxy.kt b/app/src/main/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxy.kt index 2ff3e7d57..1ad6819f7 100644 --- a/app/src/main/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxy.kt +++ b/app/src/main/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxy.kt @@ -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) @@ -50,6 +51,7 @@ class SwitchToMainBackendIfOnProxy @Inject constructor( SwitchFailure } } + } sealed class Result object AlreadyUsingMainBackend : Result() diff --git a/app/src/test/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxyTest.kt b/app/src/test/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxyTest.kt index 903b5c95b..0bea670f2 100644 --- a/app/src/test/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxyTest.kt +++ b/app/src/test/java/ch/protonmail/android/api/SwitchToMainBackendIfOnProxyTest.kt @@ -41,6 +41,7 @@ internal class SwitchToMainBackendIfOnProxyTest { private val legacyUserMock = mockk() private val userManagerMock = mockk { every { requireCurrentLegacyUser() } returns legacyUserMock + every { currentLegacyUser } returns legacyUserMock } private val switchToMainBackendIfAvailableMock = mockk() private val sharedPrefsMock = mockk() @@ -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