UnifiedPush: extract logic to resolve the gateway url and unit test it.
This commit is contained in:
parent
9ada3791d7
commit
b85d4bac1d
5 changed files with 156 additions and 15 deletions
|
|
@ -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 guardServiceStarter: GuardServiceStarter
|
||||||
@Inject lateinit var unifiedPushStore: UnifiedPushStore
|
@Inject lateinit var unifiedPushStore: UnifiedPushStore
|
||||||
@Inject lateinit var unifiedPushGatewayResolver: UnifiedPushGatewayResolver
|
@Inject lateinit var unifiedPushGatewayResolver: UnifiedPushGatewayResolver
|
||||||
|
@Inject lateinit var unifiedPushGatewayUrlResolver: UnifiedPushGatewayUrlResolver
|
||||||
@Inject lateinit var newGatewayHandler: UnifiedPushNewGatewayHandler
|
@Inject lateinit var newGatewayHandler: UnifiedPushNewGatewayHandler
|
||||||
@Inject lateinit var endpointRegistrationHandler: EndpointRegistrationHandler
|
@Inject lateinit var endpointRegistrationHandler: EndpointRegistrationHandler
|
||||||
@Inject lateinit var coroutineScope: CoroutineScope
|
@Inject lateinit var coroutineScope: CoroutineScope
|
||||||
|
|
@ -65,19 +66,7 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
val gateway = unifiedPushGatewayResolver.getGateway(endpoint)
|
val gateway = unifiedPushGatewayResolver.getGateway(endpoint)
|
||||||
.let { gatewayResult ->
|
.let { gatewayResult ->
|
||||||
when (gatewayResult) {
|
unifiedPushGatewayUrlResolver.resolve(gatewayResult, instance)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
unifiedPushStore.storePushGateway(instance, gateway)
|
unifiedPushStore.storePushGateway(instance, gateway)
|
||||||
val result = newGatewayHandler.handle(endpoint, gateway, instance)
|
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(
|
unifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(
|
||||||
getGatewayResult = { UnifiedPushGatewayResolverResult.Success("aGateway") }
|
getGatewayResult = { UnifiedPushGatewayResolverResult.Success("aGateway") }
|
||||||
),
|
),
|
||||||
|
unifiedPushGatewayUrlResolver = FakeUnifiedPushGatewayUrlResolver(
|
||||||
|
resolveResult = { _, _ -> "aGatewayUrl" }
|
||||||
|
),
|
||||||
endpointRegistrationHandler = endpointRegistrationHandler,
|
endpointRegistrationHandler = endpointRegistrationHandler,
|
||||||
unifiedPushNewGatewayHandler = unifiedPushNewGatewayHandler,
|
unifiedPushNewGatewayHandler = unifiedPushNewGatewayHandler,
|
||||||
)
|
)
|
||||||
|
|
@ -133,7 +136,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||||
}
|
}
|
||||||
storePushGatewayResult.assertions()
|
storePushGatewayResult.assertions()
|
||||||
.isCalledOnce()
|
.isCalledOnce()
|
||||||
.with(value(A_SECRET), value("aGateway"))
|
.with(value(A_SECRET), value("aGatewayUrl"))
|
||||||
storeUpEndpointResult.assertions()
|
storeUpEndpointResult.assertions()
|
||||||
.isCalledOnce()
|
.isCalledOnce()
|
||||||
.with(value(A_SECRET), value("anEndpoint"))
|
.with(value(A_SECRET), value("anEndpoint"))
|
||||||
|
|
@ -158,6 +161,9 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||||
unifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(
|
unifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(
|
||||||
getGatewayResult = { UnifiedPushGatewayResolverResult.Success("aGateway") }
|
getGatewayResult = { UnifiedPushGatewayResolverResult.Success("aGateway") }
|
||||||
),
|
),
|
||||||
|
unifiedPushGatewayUrlResolver = FakeUnifiedPushGatewayUrlResolver(
|
||||||
|
resolveResult = { _, _ -> "aGatewayUrl" }
|
||||||
|
),
|
||||||
endpointRegistrationHandler = endpointRegistrationHandler,
|
endpointRegistrationHandler = endpointRegistrationHandler,
|
||||||
unifiedPushNewGatewayHandler = unifiedPushNewGatewayHandler,
|
unifiedPushNewGatewayHandler = unifiedPushNewGatewayHandler,
|
||||||
)
|
)
|
||||||
|
|
@ -173,7 +179,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||||
}
|
}
|
||||||
storePushGatewayResult.assertions()
|
storePushGatewayResult.assertions()
|
||||||
.isCalledOnce()
|
.isCalledOnce()
|
||||||
.with(value(A_SECRET), value("aGateway"))
|
.with(value(A_SECRET), value("aGatewayUrl"))
|
||||||
storeUpEndpointResult.assertions()
|
storeUpEndpointResult.assertions()
|
||||||
.isNeverCalled()
|
.isNeverCalled()
|
||||||
}
|
}
|
||||||
|
|
@ -182,6 +188,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||||
pushHandler: PushHandler = FakePushHandler(),
|
pushHandler: PushHandler = FakePushHandler(),
|
||||||
unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(),
|
unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(),
|
||||||
unifiedPushGatewayResolver: UnifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(),
|
unifiedPushGatewayResolver: UnifiedPushGatewayResolver = FakeUnifiedPushGatewayResolver(),
|
||||||
|
unifiedPushGatewayUrlResolver: UnifiedPushGatewayUrlResolver = FakeUnifiedPushGatewayUrlResolver(),
|
||||||
unifiedPushNewGatewayHandler: UnifiedPushNewGatewayHandler = FakeUnifiedPushNewGatewayHandler(),
|
unifiedPushNewGatewayHandler: UnifiedPushNewGatewayHandler = FakeUnifiedPushNewGatewayHandler(),
|
||||||
endpointRegistrationHandler: EndpointRegistrationHandler = EndpointRegistrationHandler(),
|
endpointRegistrationHandler: EndpointRegistrationHandler = EndpointRegistrationHandler(),
|
||||||
): VectorUnifiedPushMessagingReceiver {
|
): VectorUnifiedPushMessagingReceiver {
|
||||||
|
|
@ -191,6 +198,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||||
this.guardServiceStarter = NoopGuardServiceStarter()
|
this.guardServiceStarter = NoopGuardServiceStarter()
|
||||||
this.unifiedPushStore = unifiedPushStore
|
this.unifiedPushStore = unifiedPushStore
|
||||||
this.unifiedPushGatewayResolver = unifiedPushGatewayResolver
|
this.unifiedPushGatewayResolver = unifiedPushGatewayResolver
|
||||||
|
this.unifiedPushGatewayUrlResolver = unifiedPushGatewayUrlResolver
|
||||||
this.newGatewayHandler = unifiedPushNewGatewayHandler
|
this.newGatewayHandler = unifiedPushNewGatewayHandler
|
||||||
this.endpointRegistrationHandler = endpointRegistrationHandler
|
this.endpointRegistrationHandler = endpointRegistrationHandler
|
||||||
this.coroutineScope = this@createVectorUnifiedPushMessagingReceiver
|
this.coroutineScope = this@createVectorUnifiedPushMessagingReceiver
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue