Refactor: change signature to always have clientSecret first, since it's acting as a key.

This commit is contained in:
Benoit Marty 2024-05-23 00:14:00 +02:00
parent f0863a10c9
commit 0d445cdeb3
6 changed files with 27 additions and 27 deletions

View file

@ -28,9 +28,9 @@ import javax.inject.Inject
interface UnifiedPushStore { interface UnifiedPushStore {
fun getEndpoint(clientSecret: String): String? fun getEndpoint(clientSecret: String): String?
fun storeUpEndpoint(endpoint: String?, clientSecret: String) fun storeUpEndpoint(clientSecret: String, endpoint: String?)
fun getPushGateway(clientSecret: String): String? fun getPushGateway(clientSecret: String): String?
fun storePushGateway(gateway: String?, clientSecret: String) fun storePushGateway(clientSecret: String, gateway: String?)
fun getDistributorValue(userId: UserId): String? fun getDistributorValue(userId: UserId): String?
fun setDistributorValue(userId: UserId, value: String) fun setDistributorValue(userId: UserId, value: String)
} }
@ -53,10 +53,10 @@ class DefaultUnifiedPushStore @Inject constructor(
/** /**
* Store UnifiedPush Endpoint to the SharedPrefs. * Store UnifiedPush Endpoint to the SharedPrefs.
* *
* @param endpoint the endpoint to store
* @param clientSecret the client secret, to identify the session * @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 { defaultPrefs.edit {
putString(PREFS_ENDPOINT_OR_TOKEN + clientSecret, endpoint) putString(PREFS_ENDPOINT_OR_TOKEN + clientSecret, endpoint)
} }
@ -75,10 +75,10 @@ class DefaultUnifiedPushStore @Inject constructor(
/** /**
* Store Push Gateway to the SharedPrefs. * Store Push Gateway to the SharedPrefs.
* *
* @param gateway the push gateway to store
* @param clientSecret the client secret, to identify the session * @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 { defaultPrefs.edit {
putString(PREFS_PUSH_GATEWAY + clientSecret, gateway) putString(PREFS_PUSH_GATEWAY + clientSecret, gateway)
} }

View file

@ -43,8 +43,8 @@ class DefaultUnregisterUnifiedPushUseCase @Inject constructor(
} }
return pusherSubscriber.unregisterPusher(matrixClient, endpoint, gateway) return pusherSubscriber.unregisterPusher(matrixClient, endpoint, gateway)
.onSuccess { .onSuccess {
unifiedPushStore.storeUpEndpoint(null, clientSecret) unifiedPushStore.storeUpEndpoint(clientSecret, null)
unifiedPushStore.storePushGateway(null, clientSecret) unifiedPushStore.storePushGateway(clientSecret, null)
UnifiedPush.unregisterApp(context) UnifiedPush.unregisterApp(context)
} }
} }

View file

@ -73,13 +73,13 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
Timber.tag(loggerTag.value).i("onNewEndpoint: $endpoint") Timber.tag(loggerTag.value).i("onNewEndpoint: $endpoint")
coroutineScope.launch { coroutineScope.launch {
val gateway = unifiedPushGatewayResolver.getGateway(endpoint) val gateway = unifiedPushGatewayResolver.getGateway(endpoint)
unifiedPushStore.storePushGateway(gateway, instance) unifiedPushStore.storePushGateway(instance, gateway)
val result = newGatewayHandler.handle(endpoint, gateway, instance) val result = newGatewayHandler.handle(endpoint, gateway, instance)
.onFailure { .onFailure {
Timber.tag(loggerTag.value).e(it, "Failed to handle new gateway") Timber.tag(loggerTag.value).e(it, "Failed to handle new gateway")
} }
.onSuccess { .onSuccess {
unifiedPushStore.storeUpEndpoint(endpoint, instance) unifiedPushStore.storeUpEndpoint(instance, endpoint)
} }
endpointRegistrationHandler.registrationDone( endpointRegistrationHandler.registrationDone(
RegistrationResult( RegistrationResult(

View file

@ -36,8 +36,8 @@ class DefaultUnregisterUnifiedPushUseCaseTest {
@Test @Test
fun `test un registration successful`() = runTest { fun `test un registration successful`() = runTest {
val lambda = lambdaRecorder { _: MatrixClient, _: String, _: String -> Result.success(Unit) } val lambda = lambdaRecorder { _: MatrixClient, _: String, _: String -> Result.success(Unit) }
val storeUpEndpointResult = lambdaRecorder { _: String?, _: String -> } val storeUpEndpointResult = lambdaRecorder { _: String, _: String? -> }
val storePushGatewayResult = lambdaRecorder { _: String?, _: String -> } val storePushGatewayResult = lambdaRecorder { _: String, _: String? -> }
val matrixClient = FakeMatrixClient() val matrixClient = FakeMatrixClient()
val useCase = createDefaultUnregisterUnifiedPushUseCase( val useCase = createDefaultUnregisterUnifiedPushUseCase(
unifiedPushStore = FakeUnifiedPushStore( unifiedPushStore = FakeUnifiedPushStore(
@ -57,10 +57,10 @@ class DefaultUnregisterUnifiedPushUseCaseTest {
.with(value(matrixClient), value("aEndpoint"), value("aGateway")) .with(value(matrixClient), value("aEndpoint"), value("aGateway"))
storeUpEndpointResult.assertions() storeUpEndpointResult.assertions()
.isCalledOnce() .isCalledOnce()
.with(value(null), value(A_SECRET)) .with(value(A_SECRET), value(null))
storePushGatewayResult.assertions() storePushGatewayResult.assertions()
.isCalledOnce() .isCalledOnce()
.with(value(null), value(A_SECRET)) .with(value(A_SECRET), value(null))
} }
@Test @Test

View file

@ -20,9 +20,9 @@ import io.element.android.libraries.matrix.api.core.UserId
class FakeUnifiedPushStore( class FakeUnifiedPushStore(
private val getEndpointResult: (String) -> String? = { TODO() }, 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 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 getDistributorValueResult: (UserId) -> String? = { TODO() },
private val setDistributorValueResult: (UserId, String) -> Unit = { _, _ -> TODO() }, private val setDistributorValueResult: (UserId, String) -> Unit = { _, _ -> TODO() },
) : UnifiedPushStore { ) : UnifiedPushStore {
@ -30,16 +30,16 @@ class FakeUnifiedPushStore(
return getEndpointResult(clientSecret) return getEndpointResult(clientSecret)
} }
override fun storeUpEndpoint(endpoint: String?, clientSecret: String) { override fun storeUpEndpoint(clientSecret: String, endpoint: String?) {
storeUpEndpointResult(endpoint, clientSecret) storeUpEndpointResult(clientSecret, endpoint)
} }
override fun getPushGateway(clientSecret: String): String? { override fun getPushGateway(clientSecret: String): String? {
return getPushGatewayResult(clientSecret) return getPushGatewayResult(clientSecret)
} }
override fun storePushGateway(gateway: String?, clientSecret: String) { override fun storePushGateway(clientSecret: String, gateway: String?) {
storePushGatewayResult(gateway, clientSecret) storePushGatewayResult(clientSecret, gateway)
} }
override fun getDistributorValue(userId: UserId): String? { override fun getDistributorValue(userId: UserId): String? {

View file

@ -99,8 +99,8 @@ class VectorUnifiedPushMessagingReceiverTest {
@Test @Test
fun `onNewEndpoint run the expected tasks`() = runTest { fun `onNewEndpoint run the expected tasks`() = runTest {
val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context
val storePushGatewayResult = lambdaRecorder<String?, String, Unit> { _, _ -> } val storePushGatewayResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
val storeUpEndpointResult = lambdaRecorder<String?, String, Unit> { _, _ -> } val storeUpEndpointResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
val unifiedPushStore = FakeUnifiedPushStore( val unifiedPushStore = FakeUnifiedPushStore(
storePushGatewayResult = storePushGatewayResult, storePushGatewayResult = storePushGatewayResult,
storeUpEndpointResult = storeUpEndpointResult, storeUpEndpointResult = storeUpEndpointResult,
@ -130,17 +130,17 @@ class VectorUnifiedPushMessagingReceiverTest {
} }
storePushGatewayResult.assertions() storePushGatewayResult.assertions()
.isCalledOnce() .isCalledOnce()
.with(value("aGateway"), value(A_SECRET)) .with(value(A_SECRET), value("aGateway"))
storeUpEndpointResult.assertions() storeUpEndpointResult.assertions()
.isCalledOnce() .isCalledOnce()
.with(value("anEndpoint"), value(A_SECRET)) .with(value(A_SECRET), value("anEndpoint"))
} }
@Test @Test
fun `onNewEndpoint, if registration fails, the endpoint should not be stored`() = runTest { fun `onNewEndpoint, if registration fails, the endpoint should not be stored`() = runTest {
val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context
val storePushGatewayResult = lambdaRecorder<String?, String, Unit> { _, _ -> } val storePushGatewayResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
val storeUpEndpointResult = lambdaRecorder<String?, String, Unit> { _, _ -> } val storeUpEndpointResult = lambdaRecorder<String, String?, Unit> { _, _ -> }
val unifiedPushStore = FakeUnifiedPushStore( val unifiedPushStore = FakeUnifiedPushStore(
storePushGatewayResult = storePushGatewayResult, storePushGatewayResult = storePushGatewayResult,
storeUpEndpointResult = storeUpEndpointResult, storeUpEndpointResult = storeUpEndpointResult,
@ -170,7 +170,7 @@ class VectorUnifiedPushMessagingReceiverTest {
} }
storePushGatewayResult.assertions() storePushGatewayResult.assertions()
.isCalledOnce() .isCalledOnce()
.with(value("aGateway"), value(A_SECRET)) .with(value(A_SECRET), value("aGateway"))
storeUpEndpointResult.assertions() storeUpEndpointResult.assertions()
.isNeverCalled() .isNeverCalled()
} }