UnifiedPush: extract logic to resolve the gateway url and unit test it.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import javax.inject.Inject
|
||||
|
||||
interface UnifiedPushGatewayUrlResolver {
|
||||
fun resolve(
|
||||
gatewayResult: UnifiedPushGatewayResolverResult,
|
||||
instance: String,
|
||||
): String
|
||||
}
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultUnifiedPushGatewayUrlResolver @Inject constructor(
|
||||
private val unifiedPushStore: UnifiedPushStore,
|
||||
) : UnifiedPushGatewayUrlResolver {
|
||||
override fun resolve(
|
||||
gatewayResult: UnifiedPushGatewayResolverResult,
|
||||
instance: String,
|
||||
): String {
|
||||
return when (gatewayResult) {
|
||||
is UnifiedPushGatewayResolverResult.Error -> {
|
||||
// Use previous gateway if any, or the provided one
|
||||
unifiedPushStore.getPushGateway(instance) ?: gatewayResult.gateway
|
||||
}
|
||||
UnifiedPushGatewayResolverResult.ErrorInvalidUrl,
|
||||
UnifiedPushGatewayResolverResult.NoMatrixGateway -> {
|
||||
UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL
|
||||
}
|
||||
is UnifiedPushGatewayResolverResult.Success -> {
|
||||
gatewayResult.gateway
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
|
||||
@Inject lateinit var guardServiceStarter: GuardServiceStarter
|
||||
@Inject lateinit var unifiedPushStore: UnifiedPushStore
|
||||
@Inject lateinit var unifiedPushGatewayResolver: UnifiedPushGatewayResolver
|
||||
@Inject lateinit var unifiedPushGatewayUrlResolver: UnifiedPushGatewayUrlResolver
|
||||
@Inject lateinit var newGatewayHandler: UnifiedPushNewGatewayHandler
|
||||
@Inject lateinit var endpointRegistrationHandler: EndpointRegistrationHandler
|
||||
@Inject lateinit var coroutineScope: CoroutineScope
|
||||
@@ -65,19 +66,7 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
|
||||
coroutineScope.launch {
|
||||
val gateway = unifiedPushGatewayResolver.getGateway(endpoint)
|
||||
.let { gatewayResult ->
|
||||
when (gatewayResult) {
|
||||
is UnifiedPushGatewayResolverResult.Error -> {
|
||||
// Use previous gateway if any, or the provided one
|
||||
unifiedPushStore.getPushGateway(instance) ?: gatewayResult.gateway
|
||||
}
|
||||
UnifiedPushGatewayResolverResult.ErrorInvalidUrl,
|
||||
UnifiedPushGatewayResolverResult.NoMatrixGateway -> {
|
||||
UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL
|
||||
}
|
||||
is UnifiedPushGatewayResolverResult.Success -> {
|
||||
gatewayResult.gateway
|
||||
}
|
||||
}
|
||||
unifiedPushGatewayUrlResolver.resolve(gatewayResult, instance)
|
||||
}
|
||||
unifiedPushStore.storePushGateway(instance, gateway)
|
||||
val result = newGatewayHandler.handle(endpoint, gateway, instance)
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultUnifiedPushGatewayUrlResolverTest {
|
||||
@Test
|
||||
fun `resolve ErrorInvalidUrl returns the default gateway`() {
|
||||
val sut = createDefaultUnifiedPushGatewayUrlResolver()
|
||||
val result = sut.resolve(
|
||||
gatewayResult = UnifiedPushGatewayResolverResult.ErrorInvalidUrl,
|
||||
instance = "",
|
||||
)
|
||||
assertThat(result).isEqualTo(UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `resolve NoMatrixGateway returns the default gateway`() {
|
||||
val sut = createDefaultUnifiedPushGatewayUrlResolver()
|
||||
val result = sut.resolve(
|
||||
gatewayResult = UnifiedPushGatewayResolverResult.NoMatrixGateway,
|
||||
instance = "",
|
||||
)
|
||||
assertThat(result).isEqualTo(UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `resolve Success returns the url`() {
|
||||
val sut = createDefaultUnifiedPushGatewayUrlResolver()
|
||||
val result = sut.resolve(
|
||||
gatewayResult = UnifiedPushGatewayResolverResult.Success("aUrl"),
|
||||
instance = "",
|
||||
)
|
||||
assertThat(result).isEqualTo("aUrl")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `resolve Error returns the current url when available`() {
|
||||
val sut = createDefaultUnifiedPushGatewayUrlResolver(
|
||||
unifiedPushStore = FakeUnifiedPushStore(
|
||||
getPushGatewayResult = { instance ->
|
||||
assertThat(instance).isEqualTo("instance")
|
||||
"aCurrentUrl"
|
||||
},
|
||||
)
|
||||
)
|
||||
val result = sut.resolve(
|
||||
gatewayResult = UnifiedPushGatewayResolverResult.Error("aUrl"),
|
||||
instance = "instance",
|
||||
)
|
||||
assertThat(result).isEqualTo("aCurrentUrl")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `resolve Error returns the url if no current url is available`() {
|
||||
val sut = createDefaultUnifiedPushGatewayUrlResolver(
|
||||
unifiedPushStore = FakeUnifiedPushStore(
|
||||
getPushGatewayResult = { instance ->
|
||||
assertThat(instance).isEqualTo("instance")
|
||||
null
|
||||
},
|
||||
)
|
||||
)
|
||||
val result = sut.resolve(
|
||||
gatewayResult = UnifiedPushGatewayResolverResult.Error("aUrl"),
|
||||
instance = "instance",
|
||||
)
|
||||
assertThat(result).isEqualTo("aUrl")
|
||||
}
|
||||
|
||||
private fun createDefaultUnifiedPushGatewayUrlResolver(
|
||||
unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(),
|
||||
) = DefaultUnifiedPushGatewayUrlResolver(
|
||||
unifiedPushStore = unifiedPushStore,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeUnifiedPushGatewayUrlResolver(
|
||||
private val resolveResult: (UnifiedPushGatewayResolverResult, String) -> String = { _, _ -> lambdaError() },
|
||||
) : UnifiedPushGatewayUrlResolver {
|
||||
override fun resolve(gatewayResult: UnifiedPushGatewayResolverResult, instance: String): String {
|
||||
return resolveResult(gatewayResult, instance)
|
||||
}
|
||||
}
|
||||
@@ -118,6 +118,9 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
unifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(
|
||||
getGatewayResult = { UnifiedPushGatewayResolverResult.Success("aGateway") }
|
||||
),
|
||||
unifiedPushGatewayUrlResolver = FakeUnifiedPushGatewayUrlResolver(
|
||||
resolveResult = { _, _ -> "aGatewayUrl" }
|
||||
),
|
||||
endpointRegistrationHandler = endpointRegistrationHandler,
|
||||
unifiedPushNewGatewayHandler = unifiedPushNewGatewayHandler,
|
||||
)
|
||||
@@ -133,7 +136,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
}
|
||||
storePushGatewayResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value(A_SECRET), value("aGateway"))
|
||||
.with(value(A_SECRET), value("aGatewayUrl"))
|
||||
storeUpEndpointResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value(A_SECRET), value("anEndpoint"))
|
||||
@@ -158,6 +161,9 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
unifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(
|
||||
getGatewayResult = { UnifiedPushGatewayResolverResult.Success("aGateway") }
|
||||
),
|
||||
unifiedPushGatewayUrlResolver = FakeUnifiedPushGatewayUrlResolver(
|
||||
resolveResult = { _, _ -> "aGatewayUrl" }
|
||||
),
|
||||
endpointRegistrationHandler = endpointRegistrationHandler,
|
||||
unifiedPushNewGatewayHandler = unifiedPushNewGatewayHandler,
|
||||
)
|
||||
@@ -173,7 +179,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
}
|
||||
storePushGatewayResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value(A_SECRET), value("aGateway"))
|
||||
.with(value(A_SECRET), value("aGatewayUrl"))
|
||||
storeUpEndpointResult.assertions()
|
||||
.isNeverCalled()
|
||||
}
|
||||
@@ -182,6 +188,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
pushHandler: PushHandler = FakePushHandler(),
|
||||
unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(),
|
||||
unifiedPushGatewayResolver: UnifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(),
|
||||
unifiedPushGatewayUrlResolver: UnifiedPushGatewayUrlResolver = FakeUnifiedPushGatewayUrlResolver(),
|
||||
unifiedPushNewGatewayHandler: UnifiedPushNewGatewayHandler = FakeUnifiedPushNewGatewayHandler(),
|
||||
endpointRegistrationHandler: EndpointRegistrationHandler = EndpointRegistrationHandler(),
|
||||
): VectorUnifiedPushMessagingReceiver {
|
||||
@@ -191,6 +198,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
this.guardServiceStarter = NoopGuardServiceStarter()
|
||||
this.unifiedPushStore = unifiedPushStore
|
||||
this.unifiedPushGatewayResolver = unifiedPushGatewayResolver
|
||||
this.unifiedPushGatewayUrlResolver = unifiedPushGatewayUrlResolver
|
||||
this.newGatewayHandler = unifiedPushNewGatewayHandler
|
||||
this.endpointRegistrationHandler = endpointRegistrationHandler
|
||||
this.coroutineScope = this@createVectorUnifiedPushMessagingReceiver
|
||||
|
||||
Reference in New Issue
Block a user