Add test on DefaultTestPush

This commit is contained in:
Benoit Marty 2024-05-22 15:22:03 +02:00
parent 97530e752f
commit 3bde744d76
4 changed files with 98 additions and 8 deletions

View file

@ -15,14 +15,14 @@
*/
package io.element.android.libraries.push.impl.pushgateway
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.push.api.gateway.PushGatewayFailure
import javax.inject.Inject
class PushGatewayNotifyRequest @Inject constructor(
private val pushGatewayApiFactory: PushGatewayApiFactory,
) {
interface PushGatewayNotifyRequest {
data class Params(
val url: String,
val appId: String,
@ -31,7 +31,15 @@ class PushGatewayNotifyRequest @Inject constructor(
val roomId: RoomId,
)
suspend fun execute(params: Params) {
suspend fun execute(params: Params)
}
@ContributesBinding(AppScope::class)
class DefaultPushGatewayNotifyRequest @Inject constructor(
private val pushGatewayApiFactory: PushGatewayApiFactory,
) : PushGatewayNotifyRequest {
override suspend fun execute(params: PushGatewayNotifyRequest.Params) {
val pushGatewayApi = pushGatewayApiFactory.create(
params.url.substringBefore(PushGatewayConfig.URI_PUSH_GATEWAY_PREFIX_PATH)
)

View file

@ -23,7 +23,7 @@ import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertThrows
import org.junit.Test
class PushGatewayNotifyRequestTest {
class DefaultPushGatewayNotifyRequestTest {
@Test
fun `notify success`() = runTest {
val factory = FakePushGatewayApiFactory(
@ -33,7 +33,7 @@ class PushGatewayNotifyRequestTest {
)
}
)
val pushGatewayNotifyRequest = PushGatewayNotifyRequest(
val pushGatewayNotifyRequest = DefaultPushGatewayNotifyRequest(
pushGatewayApiFactory = factory,
)
pushGatewayNotifyRequest.execute(
@ -57,7 +57,7 @@ class PushGatewayNotifyRequestTest {
)
}
)
val pushGatewayNotifyRequest = PushGatewayNotifyRequest(
val pushGatewayNotifyRequest = DefaultPushGatewayNotifyRequest(
pushGatewayApiFactory = factory,
)
pushGatewayNotifyRequest.execute(
@ -81,7 +81,7 @@ class PushGatewayNotifyRequestTest {
)
}
)
val pushGatewayNotifyRequest = PushGatewayNotifyRequest(
val pushGatewayNotifyRequest = DefaultPushGatewayNotifyRequest(
pushGatewayApiFactory = factory,
)
assertThrows(PushGatewayFailure.PusherRejected::class.java) {

View file

@ -0,0 +1,55 @@
/*
* 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.push.impl.test
import io.element.android.appconfig.PushConfig
import io.element.android.libraries.push.impl.pushgateway.PushGatewayNotifyRequest
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig
import io.element.android.tests.testutils.lambda.lambdaRecorder
import io.element.android.tests.testutils.lambda.value
import kotlinx.coroutines.test.runTest
import org.junit.Test
class DefaultTestPushTest {
@Test
fun `test DefaultTestPush`() = runTest {
val executeResult = lambdaRecorder<PushGatewayNotifyRequest.Params, Unit> { }
val defaultTestPush = DefaultTestPush(
pushGatewayNotifyRequest = FakePushGatewayNotifyRequest(
executeResult = executeResult,
)
)
val aConfig = CurrentUserPushConfig(
url = "aUrl",
pushKey = "aPushKey",
)
defaultTestPush.execute(aConfig)
executeResult.assertions()
.isCalledOnce()
.with(
value(
PushGatewayNotifyRequest.Params(
url = aConfig.url,
appId = PushConfig.PUSHER_APP_ID,
pushKey = aConfig.pushKey,
eventId = DefaultTestPush.TEST_EVENT_ID,
roomId = DefaultTestPush.TEST_ROOM_ID,
)
)
)
}
}

View file

@ -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
*
* 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.push.impl.test
import io.element.android.libraries.push.impl.pushgateway.PushGatewayNotifyRequest
class FakePushGatewayNotifyRequest(
private val executeResult: (PushGatewayNotifyRequest.Params) -> Unit = { TODO() }
) : PushGatewayNotifyRequest {
override suspend fun execute(params: PushGatewayNotifyRequest.Params) {
executeResult(params)
}
}