Benoit Marty
2 weeks ago
10 changed files with 279 additions and 33 deletions
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
/* |
||||
* 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.test |
||||
|
||||
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig |
||||
|
||||
fun aCurrentUserPushConfig( |
||||
url: String = "aUrl", |
||||
pushKey: String = "aPushKey", |
||||
) = CurrentUserPushConfig( |
||||
url = url, |
||||
pushKey = pushKey, |
||||
) |
@ -0,0 +1,90 @@
@@ -0,0 +1,90 @@
|
||||
/* |
||||
* 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 |
||||
* |
||||
* http://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.troubleshoot |
||||
|
||||
import com.squareup.anvil.annotations.ContributesMultibinding |
||||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers |
||||
import io.element.android.libraries.di.AppScope |
||||
import io.element.android.libraries.pushproviders.api.PushProvider |
||||
import io.element.android.libraries.pushproviders.unifiedpush.UnifiedPushApiFactory |
||||
import io.element.android.libraries.pushproviders.unifiedpush.UnifiedPushConfig |
||||
import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTest |
||||
import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestDelegate |
||||
import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState |
||||
import io.element.android.libraries.troubleshoot.api.test.TestFilterData |
||||
import kotlinx.coroutines.CoroutineScope |
||||
import kotlinx.coroutines.flow.StateFlow |
||||
import kotlinx.coroutines.launch |
||||
import javax.inject.Inject |
||||
|
||||
@ContributesMultibinding(AppScope::class) |
||||
class UnifiedPushMatrixGatewayTest @Inject constructor( |
||||
private val unifiedPushApiFactory: UnifiedPushApiFactory, |
||||
private val coroutineDispatchers: CoroutineDispatchers, |
||||
private val pushProvider: PushProvider, |
||||
) : NotificationTroubleshootTest { |
||||
override val order = 450 |
||||
private val delegate = NotificationTroubleshootTestDelegate( |
||||
defaultName = "Test push gateway", |
||||
defaultDescription = "Ensure that the push gateway is valid.", |
||||
visibleWhenIdle = false, |
||||
fakeDelay = NotificationTroubleshootTestDelegate.SHORT_DELAY, |
||||
) |
||||
override val state: StateFlow<NotificationTroubleshootTestState> = delegate.state |
||||
|
||||
override fun isRelevant(data: TestFilterData): Boolean { |
||||
return data.currentPushProviderName == UnifiedPushConfig.NAME |
||||
} |
||||
|
||||
override suspend fun run(coroutineScope: CoroutineScope) { |
||||
delegate.start() |
||||
val config = pushProvider.getCurrentUserPushConfig() |
||||
if (config == null) { |
||||
delegate.updateState( |
||||
description = "No current push provider", |
||||
status = NotificationTroubleshootTestState.Status.Failure(false) |
||||
) |
||||
} else { |
||||
val gatewayBaseUrl = config.url.removeSuffix("/${UnifiedPushConfig.PUSH_GATEWAY_PATH}") |
||||
// Checking if the gateway is a Matrix gateway |
||||
coroutineScope.launch(coroutineDispatchers.io) { |
||||
val api = unifiedPushApiFactory.create(gatewayBaseUrl) |
||||
try { |
||||
val discoveryResponse = api.discover() |
||||
if (discoveryResponse.unifiedpush.gateway == "matrix") { |
||||
delegate.updateState( |
||||
description = "${config.url} is a Matrix gateway.", |
||||
status = NotificationTroubleshootTestState.Status.Success |
||||
) |
||||
} else { |
||||
delegate.updateState( |
||||
description = "${config.url} is not a Matrix gateway.", |
||||
status = NotificationTroubleshootTestState.Status.Failure(false) |
||||
) |
||||
} |
||||
} catch (throwable: Throwable) { |
||||
delegate.updateState( |
||||
description = "Fail to check the gateway ${config.url}: ${throwable.localizedMessage}", |
||||
status = NotificationTroubleshootTestState.Status.Failure(false) |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
override suspend fun reset() = delegate.reset() |
||||
} |
@ -0,0 +1,126 @@
@@ -0,0 +1,126 @@
|
||||
/* |
||||
* 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 |
||||
* |
||||
* http://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.troubleshoot |
||||
|
||||
import app.cash.turbine.test |
||||
import com.google.common.truth.Truth.assertThat |
||||
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig |
||||
import io.element.android.libraries.pushproviders.test.FakePushProvider |
||||
import io.element.android.libraries.pushproviders.test.aCurrentUserPushConfig |
||||
import io.element.android.libraries.pushproviders.unifiedpush.FakeUnifiedPushApiFactory |
||||
import io.element.android.libraries.pushproviders.unifiedpush.UnifiedPushConfig |
||||
import io.element.android.libraries.pushproviders.unifiedpush.invalidDiscoveryResponse |
||||
import io.element.android.libraries.pushproviders.unifiedpush.matrixDiscoveryResponse |
||||
import io.element.android.libraries.pushproviders.unifiedpush.network.DiscoveryResponse |
||||
import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState |
||||
import io.element.android.libraries.troubleshoot.api.test.TestFilterData |
||||
import io.element.android.tests.testutils.testCoroutineDispatchers |
||||
import kotlinx.coroutines.launch |
||||
import kotlinx.coroutines.test.TestScope |
||||
import kotlinx.coroutines.test.runTest |
||||
import org.junit.Test |
||||
|
||||
class UnifiedPushMatrixGatewayTestTest { |
||||
@Test |
||||
fun `test UnifiedPushMatrixGatewayTest success`() = runTest { |
||||
val sut = createUnifiedPushMatrixGatewayTest( |
||||
currentUserPushConfig = aCurrentUserPushConfig(), |
||||
discoveryResponse = matrixDiscoveryResponse, |
||||
) |
||||
launch { |
||||
sut.run(this) |
||||
} |
||||
sut.state.test { |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) |
||||
val lastItem = awaitItem() |
||||
assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Success) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun `test UnifiedPushMatrixGatewayTest no config found`() = runTest { |
||||
val sut = createUnifiedPushMatrixGatewayTest( |
||||
currentUserPushConfig = null, |
||||
discoveryResponse = matrixDiscoveryResponse, |
||||
) |
||||
launch { |
||||
sut.run(this) |
||||
} |
||||
sut.state.test { |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) |
||||
val lastItem = awaitItem() |
||||
assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Failure(false)) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun `test UnifiedPushMatrixGatewayTest not valid gateway`() = runTest { |
||||
val sut = createUnifiedPushMatrixGatewayTest( |
||||
currentUserPushConfig = aCurrentUserPushConfig(), |
||||
discoveryResponse = invalidDiscoveryResponse, |
||||
) |
||||
launch { |
||||
sut.run(this) |
||||
} |
||||
sut.state.test { |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) |
||||
val lastItem = awaitItem() |
||||
assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Failure(false)) |
||||
// Reset the error |
||||
sut.reset() |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun `test UnifiedPushMatrixGatewayTest network error`() = runTest { |
||||
val sut = createUnifiedPushMatrixGatewayTest( |
||||
currentUserPushConfig = aCurrentUserPushConfig(), |
||||
discoveryResponse = { throw RuntimeException("Network error") }, |
||||
) |
||||
launch { |
||||
sut.run(this) |
||||
} |
||||
sut.state.test { |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) |
||||
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) |
||||
val lastItem = awaitItem() |
||||
assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Failure(false)) |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
fun `test isRelevant`() = runTest { |
||||
val sut = createUnifiedPushMatrixGatewayTest() |
||||
assertThat(sut.isRelevant(TestFilterData(currentPushProviderName = UnifiedPushConfig.NAME))).isTrue() |
||||
assertThat(sut.isRelevant(TestFilterData(currentPushProviderName = "other"))).isFalse() |
||||
} |
||||
|
||||
private fun TestScope.createUnifiedPushMatrixGatewayTest( |
||||
currentUserPushConfig: CurrentUserPushConfig? = null, |
||||
discoveryResponse: () -> DiscoveryResponse = matrixDiscoveryResponse, |
||||
): UnifiedPushMatrixGatewayTest { |
||||
return UnifiedPushMatrixGatewayTest( |
||||
unifiedPushApiFactory = FakeUnifiedPushApiFactory(discoveryResponse), |
||||
coroutineDispatchers = testCoroutineDispatchers(), |
||||
pushProvider = FakePushProvider(currentUserPushConfig = currentUserPushConfig), |
||||
) |
||||
} |
||||
} |
Loading…
Reference in new issue