Skip to content

Commit

Permalink
fix: Showing E2EI dialog when feature disabled (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
borichellow authored Nov 3, 2023
1 parent d5616b2 commit 821e139
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ class UserSessionScope internal constructor(

val isMLSEnabled: IsMLSEnabledUseCase get() = IsMLSEnabledUseCaseImpl(featureSupport, userConfigRepository)

val observeE2EIRequired: ObserveE2EIRequiredUseCase get() = ObserveE2EIRequiredUseCaseImpl(userConfigRepository)
val observeE2EIRequired: ObserveE2EIRequiredUseCase get() = ObserveE2EIRequiredUseCaseImpl(userConfigRepository, featureSupport)
val markE2EIRequiredAsNotified: MarkEnablingE2EIAsNotifiedUseCase
get() = MarkEnablingE2EIAsNotifiedUseCaseImpl(userConfigRepository)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.wire.kalium.logic.feature.user

import com.wire.kalium.logic.configuration.E2EISettings
import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.featureFlags.FeatureSupport
import com.wire.kalium.logic.functional.getOrNull
import com.wire.kalium.logic.functional.onlyRight
import com.wire.kalium.util.DateTimeUtil
Expand Down Expand Up @@ -48,30 +49,35 @@ interface ObserveE2EIRequiredUseCase {

internal class ObserveE2EIRequiredUseCaseImpl(
private val userConfigRepository: UserConfigRepository,
private val featureSupport: FeatureSupport,
private val dispatcher: CoroutineDispatcher = KaliumDispatcherImpl.io
) : ObserveE2EIRequiredUseCase {

@OptIn(ExperimentalCoroutinesApi::class)
override fun invoke(): Flow<E2EIRequiredResult> = userConfigRepository
.observeE2EINotificationTime()
.map { it.getOrNull() }
.filterNotNull()
.delayUntilNotifyTime()
.flatMapLatest {
observeE2EISettings().flatMapLatest { setting ->
if (!setting.isRequired)
flowOf(E2EIRequiredResult.NotRequired)
else
observeCurrentE2EICertificate().map { currentCertificate ->
// TODO check here if current certificate needs to be renewed (soon, or now)
override fun invoke(): Flow<E2EIRequiredResult> {
if (!featureSupport.isMLSSupported) return flowOf(E2EIRequiredResult.NotRequired)

if (setting.gracePeriodEnd == null || setting.gracePeriodEnd <= DateTimeUtil.currentInstant())
E2EIRequiredResult.NoGracePeriod.Create
else E2EIRequiredResult.WithGracePeriod.Create(setting.gracePeriodEnd.minus(DateTimeUtil.currentInstant()))
}
return userConfigRepository
.observeE2EINotificationTime()
.map { it.getOrNull() }
.filterNotNull()
.delayUntilNotifyTime()
.flatMapLatest {
observeE2EISettings().flatMapLatest { setting ->
if (!setting.isRequired)
flowOf(E2EIRequiredResult.NotRequired)
else
observeCurrentE2EICertificate().map { currentCertificate ->
// TODO check here if current certificate needs to be renewed (soon, or now)

if (setting.gracePeriodEnd == null || setting.gracePeriodEnd <= DateTimeUtil.currentInstant())
E2EIRequiredResult.NoGracePeriod.Create
else E2EIRequiredResult.WithGracePeriod.Create(setting.gracePeriodEnd.minus(DateTimeUtil.currentInstant()))
}
}
}
}
.flowOn(dispatcher)
.flowOn(dispatcher)
}

private fun observeE2EISettings() = userConfigRepository.observeE2EISettings().onlyRight().flowOn(dispatcher)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.feature.user.E2EIRequiredResult
import com.wire.kalium.logic.feature.user.ObserveE2EIRequiredUseCase
import com.wire.kalium.logic.feature.user.ObserveE2EIRequiredUseCaseImpl
import com.wire.kalium.logic.featureFlags.FeatureSupport
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.test_util.TestKaliumDispatcher
import com.wire.kalium.util.DateTimeUtil
import io.mockative.Mock
import io.mockative.any
import io.mockative.given
import io.mockative.mock
import io.mockative.verify
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
Expand All @@ -50,6 +53,7 @@ class ObserveE2EIRequiredUseCaseTest {
val (_, useCase) = Arrangement()
.withMLSE2EISetting(MLS_E2EI_SETTING)
.withE2EINotificationTime(null)
.withIsMLSSupported(true)
.arrange()

useCase().test {
Expand All @@ -66,6 +70,7 @@ class ObserveE2EIRequiredUseCaseTest {
val (_, useCase) = Arrangement()
.withMLSE2EISetting(setting)
.withE2EINotificationTime(DateTimeUtil.currentInstant())
.withIsMLSSupported(true)
.arrange()

useCase().test {
Expand All @@ -82,6 +87,7 @@ class ObserveE2EIRequiredUseCaseTest {
val (_, useCase) = Arrangement()
.withMLSE2EISetting(setting)
.withE2EINotificationTime(DateTimeUtil.currentInstant())
.withIsMLSSupported(true)
.arrange()

useCase().test {
Expand All @@ -99,6 +105,7 @@ class ObserveE2EIRequiredUseCaseTest {
val (_, useCase) = Arrangement(TestKaliumDispatcher.io)
.withMLSE2EISetting(setting)
.withE2EINotificationTime(DateTimeUtil.currentInstant().plus(delayDuration))
.withIsMLSSupported(true)
.arrange()

useCase().test {
Expand All @@ -119,6 +126,7 @@ class ObserveE2EIRequiredUseCaseTest {
val (_, useCase) = Arrangement(TestKaliumDispatcher.io)
.withMLSE2EISetting(setting)
.withE2EINotificationTime(DateTimeUtil.currentInstant())
.withIsMLSSupported(true)
.arrange()

useCase().test {
Expand All @@ -133,6 +141,7 @@ class ObserveE2EIRequiredUseCaseTest {
val (_, useCase) = Arrangement()
.withMLSE2EISetting(MLS_E2EI_SETTING)
.withE2EINotificationTime(null)
.withIsMLSSupported(true)
.arrange()

useCase().test {
Expand All @@ -146,6 +155,7 @@ class ObserveE2EIRequiredUseCaseTest {
val (_, useCase) = Arrangement()
.withMLSE2EISetting(setting)
.withE2EINotificationTime(DateTimeUtil.currentInstant())
.withIsMLSSupported(true)
.arrange()

useCase().test {
Expand All @@ -154,26 +164,59 @@ class ObserveE2EIRequiredUseCaseTest {
}
}

@Test
fun givenMLSFeatureIsDisabled_thenNotRequiredIsEmitted() = runTest {
val delayDuration = 10.minutes
val setting = MLS_E2EI_SETTING.copy(
gracePeriodEnd = DateTimeUtil.currentInstant()
)
val (arrangement, useCase) = Arrangement(TestKaliumDispatcher.io)
.withMLSE2EISetting(setting)
.withE2EINotificationTime(DateTimeUtil.currentInstant().plus(delayDuration))
.withIsMLSSupported(false)
.arrange()

useCase().test {
advanceUntilIdle()
assertTrue { awaitItem() == E2EIRequiredResult.NotRequired }
awaitComplete()
}

verify(arrangement.userConfigRepository)
.function(arrangement.userConfigRepository::observeE2EINotificationTime)
.wasNotInvoked()
}

private class Arrangement(testDispatcher: CoroutineDispatcher = UnconfinedTestDispatcher()) {
@Mock
val userConfigRepository = mock(UserConfigRepository::class)

@Mock
val featureSupport = mock(FeatureSupport::class)

private var observeMLSEnabledUseCase: ObserveE2EIRequiredUseCase =
ObserveE2EIRequiredUseCaseImpl(userConfigRepository, testDispatcher)
ObserveE2EIRequiredUseCaseImpl(userConfigRepository, featureSupport, testDispatcher)

fun withMLSE2EISetting(setting: E2EISettings) = apply {
given(userConfigRepository)
.function(userConfigRepository::observeE2EISettings)
.whenInvoked()
.then { flowOf(Either.Right(setting)) }
}

fun withE2EINotificationTime(instant: Instant?) = apply {
given(userConfigRepository)
.function(userConfigRepository::observeE2EINotificationTime)
.whenInvoked()
.then { flowOf(Either.Right(instant)) }
}

fun withIsMLSSupported(supported: Boolean) = apply {
given(featureSupport)
.invocation { featureSupport.isMLSSupported }
.thenReturn(supported)
}

fun arrange() = this to observeMLSEnabledUseCase
}

Expand Down

0 comments on commit 821e139

Please sign in to comment.