From 4c8af0a66cba34e7133bab34cfb91a17b5030827 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 31 Dec 2024 09:32:09 +0100 Subject: [PATCH] Add missing tests on DefaultUnifiedPushGatewayResolver --- .../DefaultUnifiedPushGatewayResolverTest.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt index afe0bd2489..190bdad4da 100644 --- a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt +++ b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt @@ -14,7 +14,11 @@ import io.element.android.libraries.pushproviders.unifiedpush.network.DiscoveryU import io.element.android.tests.testutils.testCoroutineDispatchers import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest +import okhttp3.ResponseBody.Companion.toResponseBody import org.junit.Test +import retrofit2.HttpException +import retrofit2.Response +import java.net.HttpURLConnection internal val matrixDiscoveryResponse = { DiscoveryResponse( @@ -98,6 +102,36 @@ class DefaultUnifiedPushGatewayResolverTest { assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.Error("http://custom.url/_matrix/push/v1/notify")) } + @Test + fun `when a custom url is not found (404), NoMatrixGateway is returned`() = runTest { + val unifiedPushApiFactory = FakeUnifiedPushApiFactory( + discoveryResponse = { + throw HttpException(Response.error(HttpURLConnection.HTTP_NOT_FOUND, "".toResponseBody())) + } + ) + val sut = createDefaultUnifiedPushGatewayResolver( + unifiedPushApiFactory = unifiedPushApiFactory + ) + val result = sut.getGateway("http://custom.url") + assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url") + assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.NoMatrixGateway) + } + + @Test + fun `when a custom url is forbidden (403), Error is returned`() = runTest { + val unifiedPushApiFactory = FakeUnifiedPushApiFactory( + discoveryResponse = { + throw HttpException(Response.error(HttpURLConnection.HTTP_FORBIDDEN, "".toResponseBody())) + } + ) + val sut = createDefaultUnifiedPushGatewayResolver( + unifiedPushApiFactory = unifiedPushApiFactory + ) + val result = sut.getGateway("http://custom.url") + assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url") + assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.Error("http://custom.url/_matrix/push/v1/notify")) + } + @Test fun `when a custom url is invalid, ErrorInvalidUrl is returned`() = runTest { val unifiedPushApiFactory = FakeUnifiedPushApiFactory(