Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Consider team settings when registering mls device #WPB-10119 #3079

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,11 @@ class UserSessionScope internal constructor(

@OptIn(DelicateKaliumApi::class)
private val isAllowedToRegisterMLSClient: IsAllowedToRegisterMLSClientUseCase
get() = IsAllowedToRegisterMLSClientUseCaseImpl(featureSupport, mlsPublicKeysRepository)
get() = IsAllowedToRegisterMLSClientUseCaseImpl(
featureSupport,
mlsPublicKeysRepository,
userConfigRepository
)

private val syncFeatureConfigsUseCase: SyncFeatureConfigsUseCase
get() = SyncFeatureConfigsUseCaseImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

package com.wire.kalium.logic.feature.client

import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.data.mlspublickeys.MLSPublicKeysRepository
import com.wire.kalium.logic.featureFlags.FeatureSupport
import com.wire.kalium.logic.functional.fold
import com.wire.kalium.logic.functional.isRight
import com.wire.kalium.util.DelicateKaliumApi

Expand All @@ -39,8 +41,12 @@ interface IsAllowedToRegisterMLSClientUseCase {
internal class IsAllowedToRegisterMLSClientUseCaseImpl(
private val featureSupport: FeatureSupport,
private val mlsPublicKeysRepository: MLSPublicKeysRepository,
private val userConfigRepository: UserConfigRepository
) : IsAllowedToRegisterMLSClientUseCase {

override suspend operator fun invoke(): Boolean =
featureSupport.isMLSSupported && mlsPublicKeysRepository.getKeys().isRight()
override suspend operator fun invoke(): Boolean {
return featureSupport.isMLSSupported &&
mlsPublicKeysRepository.getKeys().isRight() &&
userConfigRepository.isMLSEnabled().fold({ false }, { isEnabled -> isEnabled })
}
}
MohamadJaara marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.feature.client

import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.data.mls.MLSPublicKeys
import com.wire.kalium.logic.data.mlspublickeys.MLSPublicKeysRepository
import com.wire.kalium.logic.featureFlags.FeatureSupport
import com.wire.kalium.logic.functional.Either
import io.mockative.Mock
import io.mockative.coEvery
import io.mockative.every
import io.mockative.mock
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals

class IsAllowedToRegisterMLSClientUseCaseTest {

@Test
fun givenAllMlsConditionsAreMet_whenUseCaseInvoked_returnsTrue() = runTest {
// given
val (_, isAllowedToRegisterMLSClientUseCase) = Arrangement()
.withMlsFeatureFlag(true)
.withUserConfigMlsEnabled(true)
.withGetPublicKeysSuccessful()
.arrange()

// when
val result = isAllowedToRegisterMLSClientUseCase()

// then
assertEquals(true, result)
}

@Test
fun givenMlsFeatureFlagDisabled_whenUseCaseInvoked_returnsFalse() = runTest {
// given
val (_, isAllowedToRegisterMLSClientUseCase) = Arrangement()
.withMlsFeatureFlag(false)
.withUserConfigMlsEnabled(true)
.withGetPublicKeysSuccessful()
.arrange()

// when
val result = isAllowedToRegisterMLSClientUseCase()

// then
assertEquals(false, result)
}

@Test
fun givenUserConfigMlsDisabled_whenUseCaseInvoked_returnsFalse() = runTest {
// given
val (_, isAllowedToRegisterMLSClientUseCase) = Arrangement()
.withMlsFeatureFlag(true)
.withUserConfigMlsEnabled(false)
.withGetPublicKeysSuccessful()
.arrange()

// when
val result = isAllowedToRegisterMLSClientUseCase()

// then
assertEquals(false, result)
}

@Test
fun givenPublicKeysFailure_whenUseCaseInvoked_returnsFalse() = runTest {
// given
val (_, isAllowedToRegisterMLSClientUseCase) = Arrangement()
.withMlsFeatureFlag(true)
.withUserConfigMlsEnabled(true)
.withGetPublicKeysFailed()
.arrange()

// when
val result = isAllowedToRegisterMLSClientUseCase()

// then
assertEquals(false, result)
}

@Test
fun givenUserConfigDataNotFound_whenUseCaseInvoked_returnsFalse() = runTest {
// given
val (_, isAllowedToRegisterMLSClientUseCase) = Arrangement()
.withMlsFeatureFlag(true)
.withUserConfigDataNotFound()
.withGetPublicKeysFailed()
.arrange()

// when
val result = isAllowedToRegisterMLSClientUseCase()

// then
assertEquals(false, result)
}


private class Arrangement {
@Mock
val featureSupport = mock(FeatureSupport::class)

@Mock
val mlsPublicKeysRepository = mock(MLSPublicKeysRepository::class)

@Mock
val userConfigRepository = mock(UserConfigRepository::class)

fun withMlsFeatureFlag(enabled: Boolean) = apply {
every {
featureSupport.isMLSSupported
}.returns(enabled)
}

fun withUserConfigMlsEnabled(enabled: Boolean) = apply {
every {
userConfigRepository.isMLSEnabled()
}.returns(Either.Right(enabled))
}

fun withUserConfigDataNotFound() = apply {
every {
userConfigRepository.isMLSEnabled()
}.returns(Either.Left(StorageFailure.DataNotFound))
}

suspend fun withGetPublicKeysSuccessful() = apply {
coEvery {
mlsPublicKeysRepository.getKeys()
}.returns(Either.Right(MLS_PUBLIC_KEY))
}

suspend fun withGetPublicKeysFailed() = apply {
coEvery {
mlsPublicKeysRepository.getKeys()
}.returns(Either.Left(CoreFailure.Unknown(Throwable("an error"))))
}

fun arrange() = this to IsAllowedToRegisterMLSClientUseCaseImpl(
featureSupport = featureSupport,
mlsPublicKeysRepository = mlsPublicKeysRepository,
userConfigRepository = userConfigRepository
)

companion object {
val MLS_PUBLIC_KEY = MLSPublicKeys(
removal = mapOf(
"ed25519" to "gRNvFYReriXbzsGu7zXiPtS8kaTvhU1gUJEV9rdFHVw="
)
)
}
}
}
Loading