From 264d1bc694b4e1c41d080772e22c0c618d01fae8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 4 Sep 2024 11:42:40 +0200 Subject: [PATCH] Let UnifiedPushProvider use UnifiedPushCurrentUserPushConfigProvider --- .../unifiedpush/UnifiedPushProvider.kt | 13 +- ...edPushCurrentUserPushConfigProviderTest.kt | 120 ++++++++++++++++++ .../unifiedpush/UnifiedPushProviderTest.kt | 87 ++----------- 3 files changed, 132 insertions(+), 88 deletions(-) create mode 100644 libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushCurrentUserPushConfigProviderTest.kt diff --git a/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt b/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt index 680b264f29..667be5ab5d 100644 --- a/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt +++ b/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt @@ -23,8 +23,6 @@ import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig import io.element.android.libraries.pushproviders.api.Distributor import io.element.android.libraries.pushproviders.api.PushProvider import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret -import io.element.android.services.appnavstate.api.AppNavigationStateService -import io.element.android.services.appnavstate.api.currentSessionId import javax.inject.Inject @ContributesMultibinding(AppScope::class) @@ -34,7 +32,7 @@ class UnifiedPushProvider @Inject constructor( private val unRegisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, private val pushClientSecret: PushClientSecret, private val unifiedPushStore: UnifiedPushStore, - private val appNavigationStateService: AppNavigationStateService, + private val unifiedPushCurrentUserPushConfigProvider: UnifiedPushCurrentUserPushConfigProvider, ) : PushProvider { override val index = UnifiedPushConfig.INDEX override val name = UnifiedPushConfig.NAME @@ -62,13 +60,6 @@ class UnifiedPushProvider @Inject constructor( } override suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig? { - val currentSession = appNavigationStateService.appNavigationState.value.navigationState.currentSessionId() ?: return null - val clientSecret = pushClientSecret.getSecretForUser(currentSession) - val url = unifiedPushStore.getPushGateway(clientSecret) ?: return null - val pushKey = unifiedPushStore.getEndpoint(clientSecret) ?: return null - return CurrentUserPushConfig( - url = url, - pushKey = pushKey, - ) + return unifiedPushCurrentUserPushConfigProvider.provide() } } diff --git a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushCurrentUserPushConfigProviderTest.kt b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushCurrentUserPushConfigProviderTest.kt new file mode 100644 index 0000000000..b115d193fe --- /dev/null +++ b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushCurrentUserPushConfigProviderTest.kt @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2024 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.element.android.libraries.pushproviders.unifiedpush + +import com.google.common.truth.Truth.assertThat +import io.element.android.libraries.matrix.test.A_SECRET +import io.element.android.libraries.matrix.test.A_SESSION_ID +import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig +import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret +import io.element.android.libraries.pushstore.test.userpushstore.clientsecret.FakePushClientSecret +import io.element.android.services.appnavstate.api.AppNavigationState +import io.element.android.services.appnavstate.api.AppNavigationStateService +import io.element.android.services.appnavstate.api.NavigationState +import io.element.android.services.appnavstate.test.FakeAppNavigationStateService +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.runTest +import org.junit.Test + +class DefaultUnifiedPushCurrentUserPushConfigProviderTest { + @Test + fun `getCurrentUserPushConfig no session`() = runTest { + val sut = createDefaultUnifiedPushCurrentUserPushConfigProvider() + val result = sut.provide() + assertThat(result).isNull() + } + + @Test + fun `getCurrentUserPushConfig no push gateway`() = runTest { + val sut = createDefaultUnifiedPushCurrentUserPushConfigProvider( + appNavigationStateService = FakeAppNavigationStateService( + appNavigationState = MutableStateFlow( + AppNavigationState( + navigationState = NavigationState.Session(owner = "owner", sessionId = A_SESSION_ID), + isInForeground = true + ) + ) + ), + pushClientSecret = FakePushClientSecret( + getSecretForUserResult = { A_SECRET } + ), + unifiedPushStore = FakeUnifiedPushStore( + getPushGatewayResult = { null } + ), + ) + val result = sut.provide() + assertThat(result).isNull() + } + + @Test + fun `getCurrentUserPushConfig no push key`() = runTest { + val sut = createDefaultUnifiedPushCurrentUserPushConfigProvider( + appNavigationStateService = FakeAppNavigationStateService( + appNavigationState = MutableStateFlow( + AppNavigationState( + navigationState = NavigationState.Session(owner = "owner", sessionId = A_SESSION_ID), + isInForeground = true + ) + ) + ), + pushClientSecret = FakePushClientSecret( + getSecretForUserResult = { A_SECRET } + ), + unifiedPushStore = FakeUnifiedPushStore( + getPushGatewayResult = { "aPushGateway" }, + getEndpointResult = { null } + ), + ) + val result = sut.provide() + assertThat(result).isNull() + } + + @Test + fun `getCurrentUserPushConfig ok`() = runTest { + val sut = createDefaultUnifiedPushCurrentUserPushConfigProvider( + appNavigationStateService = FakeAppNavigationStateService( + appNavigationState = MutableStateFlow( + AppNavigationState( + navigationState = NavigationState.Session(owner = "owner", sessionId = A_SESSION_ID), + isInForeground = true + ) + ) + ), + pushClientSecret = FakePushClientSecret( + getSecretForUserResult = { A_SECRET } + ), + unifiedPushStore = FakeUnifiedPushStore( + getPushGatewayResult = { "aPushGateway" }, + getEndpointResult = { "aEndpoint" } + ), + ) + val result = sut.provide() + assertThat(result).isEqualTo(CurrentUserPushConfig("aPushGateway", "aEndpoint")) + } + + private fun createDefaultUnifiedPushCurrentUserPushConfigProvider( + pushClientSecret: PushClientSecret = FakePushClientSecret(), + unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(), + appNavigationStateService: AppNavigationStateService = FakeAppNavigationStateService(), + ): DefaultUnifiedPushCurrentUserPushConfigProvider { + return DefaultUnifiedPushCurrentUserPushConfigProvider( + pushClientSecret = pushClientSecret, + unifiedPushStore = unifiedPushStore, + appNavigationStateService = appNavigationStateService, + ) + } +} diff --git a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProviderTest.kt b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProviderTest.kt index 0e3bf1655c..a7c0623a33 100644 --- a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProviderTest.kt +++ b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProviderTest.kt @@ -24,18 +24,14 @@ import io.element.android.libraries.matrix.test.AN_EXCEPTION import io.element.android.libraries.matrix.test.A_SECRET import io.element.android.libraries.matrix.test.A_SESSION_ID import io.element.android.libraries.matrix.test.FakeMatrixClient -import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig import io.element.android.libraries.pushproviders.api.Distributor +import io.element.android.libraries.pushproviders.test.aCurrentUserPushConfig +import io.element.android.libraries.pushproviders.unifiedpush.troubleshoot.FakeUnifiedPushCurrentUserPushConfigProvider import io.element.android.libraries.pushproviders.unifiedpush.troubleshoot.FakeUnifiedPushDistributorProvider import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret import io.element.android.libraries.pushstore.test.userpushstore.clientsecret.FakePushClientSecret -import io.element.android.services.appnavstate.api.AppNavigationState -import io.element.android.services.appnavstate.api.AppNavigationStateService -import io.element.android.services.appnavstate.api.NavigationState -import io.element.android.services.appnavstate.test.FakeAppNavigationStateService import io.element.android.tests.testutils.lambda.lambdaRecorder import io.element.android.tests.testutils.lambda.value -import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.runTest import org.junit.Test @@ -226,78 +222,15 @@ class UnifiedPushProviderTest { } @Test - fun `getCurrentUserPushConfig no session`() = runTest { - val unifiedPushProvider = createUnifiedPushProvider() - val result = unifiedPushProvider.getCurrentUserPushConfig() - assertThat(result).isNull() - } - - @Test - fun `getCurrentUserPushConfig no push gateway`() = runTest { + fun `getCurrentUserPushConfig invokes the provider methods`() = runTest { + val currentUserPushConfig = aCurrentUserPushConfig() val unifiedPushProvider = createUnifiedPushProvider( - appNavigationStateService = FakeAppNavigationStateService( - appNavigationState = MutableStateFlow( - AppNavigationState( - navigationState = NavigationState.Session(owner = "owner", sessionId = A_SESSION_ID), - isInForeground = true - ) - ) - ), - pushClientSecret = FakePushClientSecret( - getSecretForUserResult = { A_SECRET } - ), - unifiedPushStore = FakeUnifiedPushStore( - getPushGatewayResult = { null } - ), + unifiedPushCurrentUserPushConfigProvider = FakeUnifiedPushCurrentUserPushConfigProvider( + currentUserPushConfig = { currentUserPushConfig } + ) ) val result = unifiedPushProvider.getCurrentUserPushConfig() - assertThat(result).isNull() - } - - @Test - fun `getCurrentUserPushConfig no push key`() = runTest { - val unifiedPushProvider = createUnifiedPushProvider( - appNavigationStateService = FakeAppNavigationStateService( - appNavigationState = MutableStateFlow( - AppNavigationState( - navigationState = NavigationState.Session(owner = "owner", sessionId = A_SESSION_ID), - isInForeground = true - ) - ) - ), - pushClientSecret = FakePushClientSecret( - getSecretForUserResult = { A_SECRET } - ), - unifiedPushStore = FakeUnifiedPushStore( - getPushGatewayResult = { "aPushGateway" }, - getEndpointResult = { null } - ), - ) - val result = unifiedPushProvider.getCurrentUserPushConfig() - assertThat(result).isNull() - } - - @Test - fun `getCurrentUserPushConfig ok`() = runTest { - val unifiedPushProvider = createUnifiedPushProvider( - appNavigationStateService = FakeAppNavigationStateService( - appNavigationState = MutableStateFlow( - AppNavigationState( - navigationState = NavigationState.Session(owner = "owner", sessionId = A_SESSION_ID), - isInForeground = true - ) - ) - ), - pushClientSecret = FakePushClientSecret( - getSecretForUserResult = { A_SECRET } - ), - unifiedPushStore = FakeUnifiedPushStore( - getPushGatewayResult = { "aPushGateway" }, - getEndpointResult = { "aEndpoint" } - ), - ) - val result = unifiedPushProvider.getCurrentUserPushConfig() - assertThat(result).isEqualTo(CurrentUserPushConfig("aPushGateway", "aEndpoint")) + assertThat(result).isEqualTo(currentUserPushConfig) } private fun createUnifiedPushProvider( @@ -306,7 +239,7 @@ class UnifiedPushProviderTest { unRegisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase = FakeUnregisterUnifiedPushUseCase(), pushClientSecret: PushClientSecret = FakePushClientSecret(), unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(), - appNavigationStateService: AppNavigationStateService = FakeAppNavigationStateService(), + unifiedPushCurrentUserPushConfigProvider: UnifiedPushCurrentUserPushConfigProvider = FakeUnifiedPushCurrentUserPushConfigProvider(), ): UnifiedPushProvider { return UnifiedPushProvider( unifiedPushDistributorProvider = unifiedPushDistributorProvider, @@ -314,7 +247,7 @@ class UnifiedPushProviderTest { unRegisterUnifiedPushUseCase = unRegisterUnifiedPushUseCase, pushClientSecret = pushClientSecret, unifiedPushStore = unifiedPushStore, - appNavigationStateService = appNavigationStateService + unifiedPushCurrentUserPushConfigProvider = unifiedPushCurrentUserPushConfigProvider, ) } }