adding tests around the firebase registrar

This commit is contained in:
Adam Brown 2022-11-03 15:57:55 +00:00
parent b417b83fdf
commit d28ea9c67f
5 changed files with 127 additions and 3 deletions

View File

@ -13,5 +13,6 @@ dependencies {
kotlinTest(it)
androidImportFixturesWorkaround(project, project(":core"))
androidImportFixturesWorkaround(project, project(":matrix:common"))
androidImportFixturesWorkaround(project, project(":domains:android:stub"))
}

View File

@ -0,0 +1,79 @@
package app.dapk.st.push.messaging
import app.dapk.st.firebase.messaging.Messaging
import app.dapk.st.push.PushTokenPayload
import app.dapk.st.push.unifiedpush.FakePushHandler
import fake.FakeErrorTracker
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import test.delegateReturn
import test.runExpectTest
private const val A_TOKEN = "a-token"
private const val SYGNAL_GATEWAY = "https://sygnal.dapk.app/_matrix/push/v1/notify"
private val AN_ERROR = RuntimeException()
class MessagingPushTokenRegistrarTest {
private val fakePushHandler = FakePushHandler()
private val fakeErrorTracker = FakeErrorTracker()
private val fakeMessaging = FakeMessaging()
private val registrar = MessagingPushTokenRegistrar(
fakeErrorTracker,
fakePushHandler,
fakeMessaging.instance,
)
@Test
fun `when checking isAvailable, then delegates`() = runExpectTest {
fakeMessaging.givenIsAvailable().returns(true)
val result = registrar.isAvailable()
result shouldBeEqualTo true
}
@Test
fun `when registering current token, then enables and forwards current token to handler`() = runExpectTest {
fakeMessaging.instance.expect { it.enable() }
fakePushHandler.expect { it.onNewToken(PushTokenPayload(A_TOKEN, SYGNAL_GATEWAY)) }
fakeMessaging.givenToken().returns(A_TOKEN)
registrar.registerCurrentToken()
verifyExpects()
}
@Test
fun `given fails to register, when registering current token, then tracks error`() = runExpectTest {
fakeMessaging.instance.expect { it.enable() }
fakeMessaging.givenToken().throws(AN_ERROR)
fakeErrorTracker.expect { it.track(AN_ERROR) }
registrar.registerCurrentToken()
verifyExpects()
}
@Test
fun `when unregistering, then deletes token and disables`() = runExpectTest {
fakeMessaging.instance.expect { it.deleteToken() }
fakeMessaging.instance.expect { it.disable() }
registrar.unregister()
verifyExpects()
}
}
class FakeMessaging {
val instance = mockk<Messaging>()
fun givenIsAvailable() = every { instance.isAvailable() }.delegateReturn()
fun givenToken() = coEvery { instance.token() }.delegateReturn()
}

View File

@ -0,0 +1,41 @@
package app.dapk.st.push.messaging
import app.dapk.st.push.PushTokenPayload
import app.dapk.st.push.unifiedpush.FakePushHandler
import fixture.aRoomId
import fixture.anEventId
import org.junit.Test
import test.runExpectTest
private const val A_TOKEN = "a-push-token"
private const val SYGNAL_GATEWAY = "https://sygnal.dapk.app/_matrix/push/v1/notify"
private val A_ROOM_ID = aRoomId()
private val AN_EVENT_ID = anEventId()
class MessagingServiceAdapterTest {
private val fakePushHandler = FakePushHandler()
private val messagingServiceAdapter = MessagingServiceAdapter(fakePushHandler)
@Test
fun `onNewToken, then delegates to push handler`() = runExpectTest {
fakePushHandler.expect {
it.onNewToken(PushTokenPayload(token = A_TOKEN, gatewayUrl = SYGNAL_GATEWAY))
}
messagingServiceAdapter.onNewToken(A_TOKEN)
verifyExpects()
}
@Test
fun `onMessageReceived, then delegates to push handler`() = runExpectTest {
fakePushHandler.expect {
it.onMessageReceived(AN_EVENT_ID, A_ROOM_ID)
}
messagingServiceAdapter.onMessageReceived(AN_EVENT_ID, A_ROOM_ID)
verifyExpects()
}
}

View File

@ -0,0 +1,6 @@
package app.dapk.st.push.unifiedpush
import app.dapk.st.push.PushHandler
import io.mockk.mockk
class FakePushHandler : PushHandler by mockk()

View File

@ -2,7 +2,6 @@ package app.dapk.st.push.unifiedpush
import app.dapk.st.matrix.common.EventId
import app.dapk.st.matrix.common.RoomId
import app.dapk.st.push.PushHandler
import app.dapk.st.push.PushModule
import app.dapk.st.push.PushTokenPayload
import fake.FakeContext
@ -89,8 +88,6 @@ class FakePushModule {
fun givenPushHandler() = every { instance.pushHandler() }.delegateReturn()
}
class FakePushHandler : PushHandler by mockk()
class FakeEndpointReader : suspend (URL) -> String by mockk() {
fun given(url: String) = coEvery { this@FakeEndpointReader.invoke(URL(url)) }.delegateReturn()