diff options
| author | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2025-08-08 16:44:24 +0200 |
|---|---|---|
| committer | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2025-09-30 14:19:34 +0200 |
| commit | 2c3f0c3c91b4e750c557f7e89015dd493a7cd4bf (patch) | |
| tree | 86b8264577274b2503e1209a1f5cdfd486cc16b3 /android/lib/shared/src/test | |
| parent | 7221f569af139c76d0848af2eb064ef3859cb94b (diff) | |
| download | mullvadvpn-2c3f0c3c91b4e750c557f7e89015dd493a7cd4bf.tar.xz mullvadvpn-2c3f0c3c91b4e750c557f7e89015dd493a7cd4bf.zip | |
Improve account data fetching
Add a new account data fetch every time a user enters the connect screen
This is limited to at a maximum one fetch every minute.
Add a check that the user is still logged in to the same account
before updating the account data cache.
Change account fetching behavior in the account screen to
fetch on every enter instead of init.
Diffstat (limited to 'android/lib/shared/src/test')
| -rw-r--r-- | android/lib/shared/src/test/kotlin/net/mullvad/mullvadvpn/lib/shared/AccountRepositoryTest.kt | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/android/lib/shared/src/test/kotlin/net/mullvad/mullvadvpn/lib/shared/AccountRepositoryTest.kt b/android/lib/shared/src/test/kotlin/net/mullvad/mullvadvpn/lib/shared/AccountRepositoryTest.kt new file mode 100644 index 0000000000..ff2ecb88b2 --- /dev/null +++ b/android/lib/shared/src/test/kotlin/net/mullvad/mullvadvpn/lib/shared/AccountRepositoryTest.kt @@ -0,0 +1,78 @@ +package net.mullvad.mullvadvpn.lib.shared + +import arrow.core.right +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.runTest +import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule +import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService +import net.mullvad.mullvadvpn.lib.model.AccountData +import net.mullvad.mullvadvpn.lib.model.AccountNumber +import net.mullvad.mullvadvpn.lib.model.DeviceState +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@OptIn(ExperimentalCoroutinesApi::class) +@ExtendWith(TestCoroutineRule::class) +class AccountRepositoryTest { + + private val mockManagementService: ManagementService = mockk() + private val mockDeviceRepository: DeviceRepository = mockk() + + private val mockDeviceStateFlow = MutableStateFlow<DeviceState>(DeviceState.LoggedOut) + + private lateinit var accountRepository: AccountRepository + + @BeforeEach + fun setup() { + every { mockDeviceRepository.deviceState } returns mockDeviceStateFlow + every { mockManagementService.deviceState } returns mockDeviceStateFlow + + accountRepository = + AccountRepository( + managementService = mockManagementService, + deviceRepository = mockDeviceRepository, + scope = MainScope(), + ) + } + + @Test + fun `given force is true should always call managementService getAccountData`() = runTest { + // Arrange + val accountData: AccountData = mockk() + val accountNumber = AccountNumber("1234567890") + every { accountData.accountNumber } returns accountNumber + coEvery { mockManagementService.getAccountData(accountNumber) } returns accountData.right() + + // Act + mockDeviceStateFlow.emit(DeviceState.LoggedIn(accountNumber, mockk(relaxed = true))) + accountRepository.refreshAccountData(ignoreTimeout = true) + + // Assert + coVerify { mockManagementService.getAccountData(accountNumber) } + } + + @Test + fun `given last latestAccountDataFetch null should always call managementService getAccountData`() = + runTest { + // Arrange + val accountData: AccountData = mockk() + val accountNumber = AccountNumber("1234567890") + every { accountData.accountNumber } returns accountNumber + coEvery { mockManagementService.getAccountData(accountNumber) } returns + accountData.right() + + // Act + mockDeviceStateFlow.emit(DeviceState.LoggedIn(accountNumber, mockk(relaxed = true))) + accountRepository.refreshAccountData(ignoreTimeout = false) + + // Assert + coVerify { mockManagementService.getAccountData(accountNumber) } + } +} |
