Let UnifiedPushProvider use UnifiedPushCurrentUserPushConfigProvider
This commit is contained in:
parent
3e74fa05d7
commit
264d1bc694
3 changed files with 132 additions and 88 deletions
|
|
@ -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.Distributor
|
||||||
import io.element.android.libraries.pushproviders.api.PushProvider
|
import io.element.android.libraries.pushproviders.api.PushProvider
|
||||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
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
|
import javax.inject.Inject
|
||||||
|
|
||||||
@ContributesMultibinding(AppScope::class)
|
@ContributesMultibinding(AppScope::class)
|
||||||
|
|
@ -34,7 +32,7 @@ class UnifiedPushProvider @Inject constructor(
|
||||||
private val unRegisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
|
private val unRegisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
|
||||||
private val pushClientSecret: PushClientSecret,
|
private val pushClientSecret: PushClientSecret,
|
||||||
private val unifiedPushStore: UnifiedPushStore,
|
private val unifiedPushStore: UnifiedPushStore,
|
||||||
private val appNavigationStateService: AppNavigationStateService,
|
private val unifiedPushCurrentUserPushConfigProvider: UnifiedPushCurrentUserPushConfigProvider,
|
||||||
) : PushProvider {
|
) : PushProvider {
|
||||||
override val index = UnifiedPushConfig.INDEX
|
override val index = UnifiedPushConfig.INDEX
|
||||||
override val name = UnifiedPushConfig.NAME
|
override val name = UnifiedPushConfig.NAME
|
||||||
|
|
@ -62,13 +60,6 @@ class UnifiedPushProvider @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig? {
|
override suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig? {
|
||||||
val currentSession = appNavigationStateService.appNavigationState.value.navigationState.currentSessionId() ?: return null
|
return unifiedPushCurrentUserPushConfigProvider.provide()
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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_SECRET
|
||||||
import io.element.android.libraries.matrix.test.A_SESSION_ID
|
import io.element.android.libraries.matrix.test.A_SESSION_ID
|
||||||
import io.element.android.libraries.matrix.test.FakeMatrixClient
|
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.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.pushproviders.unifiedpush.troubleshoot.FakeUnifiedPushDistributorProvider
|
||||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
||||||
import io.element.android.libraries.pushstore.test.userpushstore.clientsecret.FakePushClientSecret
|
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.lambdaRecorder
|
||||||
import io.element.android.tests.testutils.lambda.value
|
import io.element.android.tests.testutils.lambda.value
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.test.runTest
|
import kotlinx.coroutines.test.runTest
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
|
|
@ -226,78 +222,15 @@ class UnifiedPushProviderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `getCurrentUserPushConfig no session`() = runTest {
|
fun `getCurrentUserPushConfig invokes the provider methods`() = runTest {
|
||||||
val unifiedPushProvider = createUnifiedPushProvider()
|
val currentUserPushConfig = aCurrentUserPushConfig()
|
||||||
val result = unifiedPushProvider.getCurrentUserPushConfig()
|
|
||||||
assertThat(result).isNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `getCurrentUserPushConfig no push gateway`() = runTest {
|
|
||||||
val unifiedPushProvider = createUnifiedPushProvider(
|
val unifiedPushProvider = createUnifiedPushProvider(
|
||||||
appNavigationStateService = FakeAppNavigationStateService(
|
unifiedPushCurrentUserPushConfigProvider = FakeUnifiedPushCurrentUserPushConfigProvider(
|
||||||
appNavigationState = MutableStateFlow(
|
currentUserPushConfig = { currentUserPushConfig }
|
||||||
AppNavigationState(
|
)
|
||||||
navigationState = NavigationState.Session(owner = "owner", sessionId = A_SESSION_ID),
|
|
||||||
isInForeground = true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
),
|
|
||||||
pushClientSecret = FakePushClientSecret(
|
|
||||||
getSecretForUserResult = { A_SECRET }
|
|
||||||
),
|
|
||||||
unifiedPushStore = FakeUnifiedPushStore(
|
|
||||||
getPushGatewayResult = { null }
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
val result = unifiedPushProvider.getCurrentUserPushConfig()
|
val result = unifiedPushProvider.getCurrentUserPushConfig()
|
||||||
assertThat(result).isNull()
|
assertThat(result).isEqualTo(currentUserPushConfig)
|
||||||
}
|
|
||||||
|
|
||||||
@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"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createUnifiedPushProvider(
|
private fun createUnifiedPushProvider(
|
||||||
|
|
@ -306,7 +239,7 @@ class UnifiedPushProviderTest {
|
||||||
unRegisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase = FakeUnregisterUnifiedPushUseCase(),
|
unRegisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase = FakeUnregisterUnifiedPushUseCase(),
|
||||||
pushClientSecret: PushClientSecret = FakePushClientSecret(),
|
pushClientSecret: PushClientSecret = FakePushClientSecret(),
|
||||||
unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(),
|
unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(),
|
||||||
appNavigationStateService: AppNavigationStateService = FakeAppNavigationStateService(),
|
unifiedPushCurrentUserPushConfigProvider: UnifiedPushCurrentUserPushConfigProvider = FakeUnifiedPushCurrentUserPushConfigProvider(),
|
||||||
): UnifiedPushProvider {
|
): UnifiedPushProvider {
|
||||||
return UnifiedPushProvider(
|
return UnifiedPushProvider(
|
||||||
unifiedPushDistributorProvider = unifiedPushDistributorProvider,
|
unifiedPushDistributorProvider = unifiedPushDistributorProvider,
|
||||||
|
|
@ -314,7 +247,7 @@ class UnifiedPushProviderTest {
|
||||||
unRegisterUnifiedPushUseCase = unRegisterUnifiedPushUseCase,
|
unRegisterUnifiedPushUseCase = unRegisterUnifiedPushUseCase,
|
||||||
pushClientSecret = pushClientSecret,
|
pushClientSecret = pushClientSecret,
|
||||||
unifiedPushStore = unifiedPushStore,
|
unifiedPushStore = unifiedPushStore,
|
||||||
appNavigationStateService = appNavigationStateService
|
unifiedPushCurrentUserPushConfigProvider = unifiedPushCurrentUserPushConfigProvider,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue