-
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: create observer for team app lock config (WPB-4476) (#2175)
* feat: use case for observing team app lock config * chore: rename Applock data class
- Loading branch information
Showing
10 changed files
with
210 additions
and
12 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
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
25 changes: 25 additions & 0 deletions
25
logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/applock/AppLockTeamConfig.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,25 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.applock | ||
|
||
import kotlin.time.Duration | ||
|
||
data class AppLockTeamConfig( | ||
val isEnabled: Boolean, | ||
val timeout: Duration | ||
) |
54 changes: 54 additions & 0 deletions
54
...mmonMain/kotlin/com/wire/kalium/logic/feature/applock/AppLockTeamFeatureConfigObserver.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) 2023 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.applock | ||
|
||
import com.wire.kalium.logic.configuration.UserConfigRepository | ||
import com.wire.kalium.logic.functional.fold | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** | ||
* observe app lock feature flag of the team | ||
*/ | ||
interface AppLockTeamFeatureConfigObserver { | ||
operator fun invoke(): Flow<AppLockTeamConfig> | ||
} | ||
|
||
class AppLockTeamFeatureConfigObserverImpl( | ||
private val userConfigRepository: UserConfigRepository | ||
) : AppLockTeamFeatureConfigObserver { | ||
override fun invoke(): Flow<AppLockTeamConfig> = | ||
userConfigRepository.observeAppLockStatus().map { | ||
it.fold({ | ||
AppLockTeamConfig( | ||
isEnabled = false, | ||
timeout = DEFAULT_TIMEOUT | ||
) | ||
}, { appLockModel -> | ||
AppLockTeamConfig( | ||
isEnabled = appLockModel.enforceAppLock, | ||
timeout = appLockModel.inactivityTimeoutSecs.seconds | ||
) | ||
}) | ||
} | ||
|
||
companion object { | ||
val DEFAULT_TIMEOUT = 60.seconds | ||
} | ||
} |
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
103 changes: 103 additions & 0 deletions
103
...Test/kotlin/com/wire/kalium/logic/feature/applock/AppLockTeamFeatureConfigObserverTest.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,103 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.applock | ||
|
||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.configuration.UserConfigRepository | ||
import com.wire.kalium.logic.data.featureConfig.AppLockConfigModel | ||
import com.wire.kalium.logic.functional.Either | ||
import io.mockative.Mock | ||
import io.mockative.classOf | ||
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 | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class AppLockTeamFeatureConfigObserverTest { | ||
|
||
@Test | ||
fun givenRepositoryFailure_whenObservingAppLock_thenEmitAppLockConfigWithDisabledStatus() = | ||
runTest { | ||
val expectedAppLockValue = AppLockTeamConfig( | ||
false, | ||
AppLockTeamFeatureConfigObserverImpl.DEFAULT_TIMEOUT | ||
) | ||
val (arrangement, observer) = Arrangement() | ||
.withFailure() | ||
.arrange() | ||
|
||
val result = observer.invoke() | ||
|
||
verify(arrangement.userConfigRepository) | ||
.function(arrangement.userConfigRepository::observeAppLockStatus) | ||
.wasInvoked(exactly = once) | ||
assertEquals(expectedAppLockValue, result.first()) | ||
} | ||
|
||
@Test | ||
fun givenRepositorySuccess_whenObservingAppLock_thenEmitAppLockConfigWithValueFromRepository() { | ||
runTest { | ||
val expectedAppLockValue = AppLockTeamConfig( | ||
appLockConfigModel.enforceAppLock, | ||
appLockConfigModel.inactivityTimeoutSecs.seconds | ||
) | ||
val (arrangement, observer) = Arrangement() | ||
.withSuccess() | ||
.arrange() | ||
|
||
val result = observer.invoke() | ||
|
||
verify(arrangement.userConfigRepository) | ||
.function(arrangement.userConfigRepository::observeAppLockStatus) | ||
.wasInvoked(exactly = once) | ||
assertEquals(expectedAppLockValue, result.first()) | ||
} | ||
} | ||
|
||
private class Arrangement { | ||
|
||
@Mock | ||
val userConfigRepository = mock(classOf<UserConfigRepository>()) | ||
|
||
fun withFailure(): Arrangement = apply { | ||
given(userConfigRepository) | ||
.function(userConfigRepository::observeAppLockStatus) | ||
.whenInvoked() | ||
.thenReturn(flowOf(Either.Left(StorageFailure.DataNotFound))) | ||
} | ||
|
||
fun withSuccess(): Arrangement = apply { | ||
given(userConfigRepository) | ||
.function(userConfigRepository::observeAppLockStatus) | ||
.whenInvoked() | ||
.thenReturn(flowOf(Either.Right(appLockConfigModel))) | ||
} | ||
|
||
fun arrange() = this to AppLockTeamFeatureConfigObserverImpl(userConfigRepository) | ||
} | ||
|
||
companion object { | ||
val appLockConfigModel = AppLockConfigModel(true, 60) | ||
} | ||
} |
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
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