-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package net.mullvad.mullvadvpn.usecase | ||
|
||
import app.cash.turbine.test | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlin.test.assertEquals | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.test.runTest | ||
import net.mullvad.mullvadvpn.lib.ipc.Event | ||
import net.mullvad.mullvadvpn.lib.ipc.MessageHandler | ||
import net.mullvad.mullvadvpn.lib.ipc.events | ||
import net.mullvad.mullvadvpn.model.AccountExpiry | ||
import net.mullvad.mullvadvpn.model.TunnelState | ||
import net.mullvad.mullvadvpn.repository.AccountRepository | ||
import net.mullvad.talpid.tunnel.ErrorState | ||
import net.mullvad.talpid.tunnel.ErrorStateCause | ||
import org.joda.time.DateTime | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class OutOfTimeUseCaseTest { | ||
|
||
private val mockAccountRepository: AccountRepository = mockk() | ||
private val mockMessageHandler: MessageHandler = mockk() | ||
|
||
private val events = MutableSharedFlow<Event.TunnelStateChange>() | ||
private val expiry = MutableStateFlow<AccountExpiry>(AccountExpiry.Missing) | ||
|
||
lateinit var outOfTimeUseCase: OutOfTimeUseCase | ||
|
||
@Before | ||
fun setup() { | ||
every { mockAccountRepository.accountExpiryState } returns expiry | ||
every { mockMessageHandler.events<Event.TunnelStateChange>() } returns events | ||
outOfTimeUseCase = OutOfTimeUseCase(mockAccountRepository, mockMessageHandler) | ||
} | ||
|
||
@Test | ||
fun `No events should result in no expiry`() = runTest { | ||
// Arrange | ||
// Act, Assert | ||
outOfTimeUseCase.isOutOfTime().test { assertEquals(false, awaitItem()) } | ||
} | ||
|
||
@Test | ||
fun `Tunnel is blocking because out of time should emit true`() = runTest { | ||
// Arrange | ||
// Act, Assert | ||
val errorStateCause = ErrorStateCause.AuthFailed("[EXPIRED_ACCOUNT]") | ||
val tunnelStateError = TunnelState.Error(ErrorState(errorStateCause, true)) | ||
val errorChange = Event.TunnelStateChange(tunnelStateError) | ||
|
||
outOfTimeUseCase.isOutOfTime().test { | ||
assertEquals(false, awaitItem()) | ||
events.emit(errorChange) | ||
assertEquals(true, awaitItem()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Account expiry that has expired should emit true`() = runTest { | ||
// Arrange | ||
val expiredAccountExpiry = AccountExpiry.Available(DateTime.now().minusDays(1)) | ||
// Act, Assert | ||
outOfTimeUseCase.isOutOfTime().test { | ||
assertEquals(false, awaitItem()) | ||
expiry.emit(expiredAccountExpiry) | ||
assertEquals(true, awaitItem()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Account expiry that has not expired should emit nothing`() = runTest { | ||
// Arrange | ||
val expiredAccountExpiry = AccountExpiry.Available(DateTime.now().plusDays(1)) | ||
|
||
// Act, Assert | ||
outOfTimeUseCase.isOutOfTime().test { | ||
assertEquals(false, awaitItem()) | ||
expiry.emit(expiredAccountExpiry) | ||
expectNoEvents() | ||
} | ||
} | ||
|
||
@Test | ||
fun `Account that expires without new expiry event`() = runTest { | ||
// Arrange | ||
val expiredAccountExpiry = AccountExpiry.Available(DateTime.now().plusSeconds(2)) | ||
|
||
// Act, Assert | ||
outOfTimeUseCase.isOutOfTime().test { | ||
// Initial event that doesn't change | ||
assertEquals(false, awaitItem()) | ||
expiry.emit(expiredAccountExpiry) | ||
|
||
assertEquals(true, awaitItem()) | ||
} | ||
} | ||
} |