-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: include legalhold request pending when observing self user lega…
…lhold state [WPB-6464] (#2479)
- Loading branch information
Showing
7 changed files
with
185 additions
and
147 deletions.
There are no files selected for viewing
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
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
37 changes: 0 additions & 37 deletions
37
...Main/kotlin/com/wire/kalium/logic/feature/legalhold/ObserveLegalHoldForSelfUserUseCase.kt
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
...kotlin/com/wire/kalium/logic/feature/legalhold/ObserveLegalHoldStateForSelfUserUseCase.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,54 @@ | ||
/* | ||
* 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.legalhold | ||
|
||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.logic.feature.legalhold.ObserveLegalHoldRequestUseCase.Result.LegalHoldRequestAvailable | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.combine | ||
|
||
/** | ||
* Use case that allows to observe the legal hold state for the self user. | ||
*/ | ||
interface ObserveLegalHoldStateForSelfUserUseCase { | ||
suspend operator fun invoke(): Flow<LegalHoldStateForSelfUser> | ||
} | ||
|
||
internal class ObserveLegalHoldStateForSelfUserUseCaseImpl internal constructor( | ||
private val selfUserId: UserId, | ||
private val observeLegalHoldStateForUser: ObserveLegalHoldStateForUserUseCase, | ||
private val observeLegalHoldRequestUseCase: ObserveLegalHoldRequestUseCase, | ||
) : ObserveLegalHoldStateForSelfUserUseCase { | ||
override suspend fun invoke(): Flow<LegalHoldStateForSelfUser> = | ||
combine( | ||
observeLegalHoldStateForUser(selfUserId), | ||
observeLegalHoldRequestUseCase(), | ||
) { legalHoldState, legalHoldRequest -> | ||
when { | ||
legalHoldState is LegalHoldState.Enabled -> LegalHoldStateForSelfUser.Enabled | ||
legalHoldRequest is LegalHoldRequestAvailable -> LegalHoldStateForSelfUser.PendingRequest | ||
else -> LegalHoldStateForSelfUser.Disabled | ||
} | ||
} | ||
} | ||
|
||
sealed class LegalHoldStateForSelfUser { | ||
data object Enabled : LegalHoldStateForSelfUser() | ||
data object Disabled : LegalHoldStateForSelfUser() | ||
data object PendingRequest : LegalHoldStateForSelfUser() | ||
} |
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
98 changes: 0 additions & 98 deletions
98
.../kotlin/com/wire/kalium/logic/feature/legalhold/ObserveLegalHoldForSelfUserUseCaseTest.kt
This file was deleted.
Oops, something went wrong.
114 changes: 114 additions & 0 deletions
114
...in/com/wire/kalium/logic/feature/legalhold/ObserveLegalHoldStateForSelfUserUseCaseTest.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,114 @@ | ||
/* | ||
* 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.legalhold | ||
|
||
import com.wire.kalium.logic.framework.TestUser | ||
import io.ktor.utils.io.core.toByteArray | ||
import io.mockative.Mock | ||
import io.mockative.eq | ||
import io.mockative.given | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import io.mockative.verify | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ObserveLegalHoldStateForSelfUserUseCaseTest { | ||
|
||
private fun testLegalHoldStateForSelfUser( | ||
givenLegalHoldState: LegalHoldState, | ||
givenLegalHoldRequestState: ObserveLegalHoldRequestUseCase.Result, | ||
expected: LegalHoldStateForSelfUser | ||
) = runTest { | ||
// given | ||
val (arrangement, useCase) = Arrangement() | ||
.withLegalHoldState(givenLegalHoldState) | ||
.withLegalHoldRequestState(givenLegalHoldRequestState) | ||
.arrange() | ||
// when | ||
val result = useCase() | ||
// then | ||
assertEquals(expected, result.first()) | ||
verify(arrangement.observeLegalHoldStateForUser) | ||
.suspendFunction(arrangement.observeLegalHoldStateForUser::invoke) | ||
.with(eq(TestUser.SELF.id)) | ||
.wasInvoked(once) | ||
verify(arrangement.observeLegalHoldRequestUseCase) | ||
.function(arrangement.observeLegalHoldRequestUseCase::invoke) | ||
.wasInvoked(once) | ||
} | ||
|
||
@Test | ||
fun givenLegalHoldEnabled_whenStartingObservingForSelfUser_thenEmitEnabled() = | ||
testLegalHoldStateForSelfUser( | ||
givenLegalHoldState = LegalHoldState.Enabled, | ||
givenLegalHoldRequestState = ObserveLegalHoldRequestUseCase.Result.NoLegalHoldRequest, | ||
expected = LegalHoldStateForSelfUser.Enabled | ||
) | ||
|
||
@Test | ||
fun givenLegalHoldRequestAvailable_whenStartingObservingForSelfUser_thenEmitRequestPending() = | ||
testLegalHoldStateForSelfUser( | ||
givenLegalHoldState = LegalHoldState.Disabled, | ||
givenLegalHoldRequestState = ObserveLegalHoldRequestUseCase.Result.LegalHoldRequestAvailable("fingerprint".toByteArray()), | ||
expected = LegalHoldStateForSelfUser.PendingRequest | ||
) | ||
|
||
@Test | ||
fun givenLegalHoldDisabledAndNoRequestPending_whenStartingObservingForSelfUser_thenEmitDisabled() = | ||
testLegalHoldStateForSelfUser( | ||
givenLegalHoldState = LegalHoldState.Disabled, | ||
givenLegalHoldRequestState = ObserveLegalHoldRequestUseCase.Result.NoLegalHoldRequest, | ||
expected = LegalHoldStateForSelfUser.Disabled | ||
) | ||
|
||
class Arrangement { | ||
|
||
@Mock | ||
val observeLegalHoldStateForUser = mock(ObserveLegalHoldStateForUserUseCase::class) | ||
|
||
@Mock | ||
val observeLegalHoldRequestUseCase = mock(ObserveLegalHoldRequestUseCase::class) | ||
|
||
private val observeLegalHoldForSelfUser: ObserveLegalHoldStateForSelfUserUseCase = | ||
ObserveLegalHoldStateForSelfUserUseCaseImpl( | ||
selfUserId = TestUser.SELF.id, | ||
observeLegalHoldStateForUser = observeLegalHoldStateForUser, | ||
observeLegalHoldRequestUseCase = observeLegalHoldRequestUseCase, | ||
) | ||
|
||
fun arrange() = this to observeLegalHoldForSelfUser | ||
|
||
fun withLegalHoldState(result: LegalHoldState) = apply { | ||
given(observeLegalHoldStateForUser) | ||
.suspendFunction(observeLegalHoldStateForUser::invoke) | ||
.whenInvokedWith(eq(TestUser.SELF.id)) | ||
.then { flowOf(result) } | ||
} | ||
|
||
fun withLegalHoldRequestState(result: ObserveLegalHoldRequestUseCase.Result) = apply { | ||
given(observeLegalHoldRequestUseCase) | ||
.function(observeLegalHoldRequestUseCase::invoke) | ||
.whenInvoked() | ||
.then { flowOf(result) } | ||
} | ||
} | ||
} |