Refactor: change signature to always have clientSecret first, since it's acting as a key.
This commit is contained in:
@@ -28,9 +28,9 @@ import javax.inject.Inject
|
||||
|
||||
interface UnifiedPushStore {
|
||||
fun getEndpoint(clientSecret: String): String?
|
||||
fun storeUpEndpoint(endpoint: String?, clientSecret: String)
|
||||
fun storeUpEndpoint(clientSecret: String, endpoint: String?)
|
||||
fun getPushGateway(clientSecret: String): String?
|
||||
fun storePushGateway(gateway: String?, clientSecret: String)
|
||||
fun storePushGateway(clientSecret: String, gateway: String?)
|
||||
fun getDistributorValue(userId: UserId): String?
|
||||
fun setDistributorValue(userId: UserId, value: String)
|
||||
}
|
||||
@@ -53,10 +53,10 @@ class DefaultUnifiedPushStore @Inject constructor(
|
||||
/**
|
||||
* Store UnifiedPush Endpoint to the SharedPrefs.
|
||||
*
|
||||
* @param endpoint the endpoint to store
|
||||
* @param clientSecret the client secret, to identify the session
|
||||
* @param endpoint the endpoint to store
|
||||
*/
|
||||
override fun storeUpEndpoint(endpoint: String?, clientSecret: String) {
|
||||
override fun storeUpEndpoint(clientSecret: String, endpoint: String?) {
|
||||
defaultPrefs.edit {
|
||||
putString(PREFS_ENDPOINT_OR_TOKEN + clientSecret, endpoint)
|
||||
}
|
||||
@@ -75,10 +75,10 @@ class DefaultUnifiedPushStore @Inject constructor(
|
||||
/**
|
||||
* Store Push Gateway to the SharedPrefs.
|
||||
*
|
||||
* @param gateway the push gateway to store
|
||||
* @param clientSecret the client secret, to identify the session
|
||||
* @param gateway the push gateway to store
|
||||
*/
|
||||
override fun storePushGateway(gateway: String?, clientSecret: String) {
|
||||
override fun storePushGateway(clientSecret: String, gateway: String?) {
|
||||
defaultPrefs.edit {
|
||||
putString(PREFS_PUSH_GATEWAY + clientSecret, gateway)
|
||||
}
|
||||
|
||||
@@ -43,8 +43,8 @@ class DefaultUnregisterUnifiedPushUseCase @Inject constructor(
|
||||
}
|
||||
return pusherSubscriber.unregisterPusher(matrixClient, endpoint, gateway)
|
||||
.onSuccess {
|
||||
unifiedPushStore.storeUpEndpoint(null, clientSecret)
|
||||
unifiedPushStore.storePushGateway(null, clientSecret)
|
||||
unifiedPushStore.storeUpEndpoint(clientSecret, null)
|
||||
unifiedPushStore.storePushGateway(clientSecret, null)
|
||||
UnifiedPush.unregisterApp(context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,13 +73,13 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
|
||||
Timber.tag(loggerTag.value).i("onNewEndpoint: $endpoint")
|
||||
coroutineScope.launch {
|
||||
val gateway = unifiedPushGatewayResolver.getGateway(endpoint)
|
||||
unifiedPushStore.storePushGateway(gateway, instance)
|
||||
unifiedPushStore.storePushGateway(instance, gateway)
|
||||
val result = newGatewayHandler.handle(endpoint, gateway, instance)
|
||||
.onFailure {
|
||||
Timber.tag(loggerTag.value).e(it, "Failed to handle new gateway")
|
||||
}
|
||||
.onSuccess {
|
||||
unifiedPushStore.storeUpEndpoint(endpoint, instance)
|
||||
unifiedPushStore.storeUpEndpoint(instance, endpoint)
|
||||
}
|
||||
endpointRegistrationHandler.registrationDone(
|
||||
RegistrationResult(
|
||||
|
||||
@@ -36,8 +36,8 @@ class DefaultUnregisterUnifiedPushUseCaseTest {
|
||||
@Test
|
||||
fun `test un registration successful`() = runTest {
|
||||
val lambda = lambdaRecorder { _: MatrixClient, _: String, _: String -> Result.success(Unit) }
|
||||
val storeUpEndpointResult = lambdaRecorder { _: String?, _: String -> }
|
||||
val storePushGatewayResult = lambdaRecorder { _: String?, _: String -> }
|
||||
val storeUpEndpointResult = lambdaRecorder { _: String, _: String? -> }
|
||||
val storePushGatewayResult = lambdaRecorder { _: String, _: String? -> }
|
||||
val matrixClient = FakeMatrixClient()
|
||||
val useCase = createDefaultUnregisterUnifiedPushUseCase(
|
||||
unifiedPushStore = FakeUnifiedPushStore(
|
||||
@@ -57,10 +57,10 @@ class DefaultUnregisterUnifiedPushUseCaseTest {
|
||||
.with(value(matrixClient), value("aEndpoint"), value("aGateway"))
|
||||
storeUpEndpointResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value(null), value(A_SECRET))
|
||||
.with(value(A_SECRET), value(null))
|
||||
storePushGatewayResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value(null), value(A_SECRET))
|
||||
.with(value(A_SECRET), value(null))
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -20,9 +20,9 @@ import io.element.android.libraries.matrix.api.core.UserId
|
||||
|
||||
class FakeUnifiedPushStore(
|
||||
private val getEndpointResult: (String) -> String? = { TODO() },
|
||||
private val storeUpEndpointResult: (String?, String) -> Unit = { _, _ -> TODO() },
|
||||
private val storeUpEndpointResult: (String, String?) -> Unit = { _, _ -> TODO() },
|
||||
private val getPushGatewayResult: (String) -> String? = { TODO() },
|
||||
private val storePushGatewayResult: (String?, String) -> Unit = { _, _ -> TODO() },
|
||||
private val storePushGatewayResult: (String, String?) -> Unit = { _, _ -> TODO() },
|
||||
private val getDistributorValueResult: (UserId) -> String? = { TODO() },
|
||||
private val setDistributorValueResult: (UserId, String) -> Unit = { _, _ -> TODO() },
|
||||
) : UnifiedPushStore {
|
||||
@@ -30,16 +30,16 @@ class FakeUnifiedPushStore(
|
||||
return getEndpointResult(clientSecret)
|
||||
}
|
||||
|
||||
override fun storeUpEndpoint(endpoint: String?, clientSecret: String) {
|
||||
storeUpEndpointResult(endpoint, clientSecret)
|
||||
override fun storeUpEndpoint(clientSecret: String, endpoint: String?) {
|
||||
storeUpEndpointResult(clientSecret, endpoint)
|
||||
}
|
||||
|
||||
override fun getPushGateway(clientSecret: String): String? {
|
||||
return getPushGatewayResult(clientSecret)
|
||||
}
|
||||
|
||||
override fun storePushGateway(gateway: String?, clientSecret: String) {
|
||||
storePushGatewayResult(gateway, clientSecret)
|
||||
override fun storePushGateway(clientSecret: String, gateway: String?) {
|
||||
storePushGatewayResult(clientSecret, gateway)
|
||||
}
|
||||
|
||||
override fun getDistributorValue(userId: UserId): String? {
|
||||
|
||||
@@ -99,8 +99,8 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
@Test
|
||||
fun `onNewEndpoint run the expected tasks`() = runTest {
|
||||
val context = InstrumentationRegistry.getInstrumentation().context
|
||||
val storePushGatewayResult = lambdaRecorder<String?, String, Unit> { _, _ -> }
|
||||
val storeUpEndpointResult = lambdaRecorder<String?, String, Unit> { _, _ -> }
|
||||
val storePushGatewayResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
|
||||
val storeUpEndpointResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
|
||||
val unifiedPushStore = FakeUnifiedPushStore(
|
||||
storePushGatewayResult = storePushGatewayResult,
|
||||
storeUpEndpointResult = storeUpEndpointResult,
|
||||
@@ -130,17 +130,17 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
}
|
||||
storePushGatewayResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value("aGateway"), value(A_SECRET))
|
||||
.with(value(A_SECRET), value("aGateway"))
|
||||
storeUpEndpointResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value("anEndpoint"), value(A_SECRET))
|
||||
.with(value(A_SECRET), value("anEndpoint"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `onNewEndpoint, if registration fails, the endpoint should not be stored`() = runTest {
|
||||
val context = InstrumentationRegistry.getInstrumentation().context
|
||||
val storePushGatewayResult = lambdaRecorder<String?, String, Unit> { _, _ -> }
|
||||
val storeUpEndpointResult = lambdaRecorder<String?, String, Unit> { _, _ -> }
|
||||
val storePushGatewayResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
|
||||
val storeUpEndpointResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
|
||||
val unifiedPushStore = FakeUnifiedPushStore(
|
||||
storePushGatewayResult = storePushGatewayResult,
|
||||
storeUpEndpointResult = storeUpEndpointResult,
|
||||
@@ -170,7 +170,7 @@ class VectorUnifiedPushMessagingReceiverTest {
|
||||
}
|
||||
storePushGatewayResult.assertions()
|
||||
.isCalledOnce()
|
||||
.with(value("aGateway"), value(A_SECRET))
|
||||
.with(value(A_SECRET), value("aGateway"))
|
||||
storeUpEndpointResult.assertions()
|
||||
.isNeverCalled()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user