From 13fd2785a0fe0ae2c60c36ff951b6d7eb3c19c55 Mon Sep 17 00:00:00 2001 From: mohamadjaara Date: Wed, 11 Sep 2024 17:27:47 +0200 Subject: [PATCH] fix: images form iOS are blocked when restrictions are applied --- .../receiver/asset/AssetMessageHandler.kt | 23 +++++-------- .../receiver/asset/AssetMessageHandlerTest.kt | 33 ++++++++++--------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandler.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandler.kt index 991f9e019b4..e165a19c4e1 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandler.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandler.kt @@ -59,23 +59,18 @@ internal class AssetMessageHandlerImpl( // asset data then we can not decide now if it is allowed or not // it is safe to continue and the code later will check the original // asset message and decide if it is allowed or not - if ( - message.content.value.name.isNullOrEmpty() && - message.content.value.isAssetDataComplete - ) { - kaliumLogger.e("The asset message trying to be processed has invalid data looking locally") - AssetRestrictionContinuationStrategy.RestrictIfThereIsNotOldMessageWithTheSameAssetID - } else { - validateAssetMimeTypeUseCase( + if (validateAssetMimeTypeUseCase( fileName = messageContent.value.name, mimeType = messageContent.value.mimeType, allowedExtension = it.state.allowedType - ).let { validateResult -> - if (validateResult) { - AssetRestrictionContinuationStrategy.Continue - } else { - AssetRestrictionContinuationStrategy.Restrict - } + ) + ) { + AssetRestrictionContinuationStrategy.Continue + } else { + if (messageContent.value.name.isNullOrEmpty() && messageContent.value.isAssetDataComplete) { + AssetRestrictionContinuationStrategy.RestrictIfThereIsNotOldMessageWithTheSameAssetID + } else { + AssetRestrictionContinuationStrategy.Restrict } } } diff --git a/logic/src/jvmTest/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandlerTest.kt b/logic/src/jvmTest/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandlerTest.kt index 77c719e5f23..30e12eb7b26 100644 --- a/logic/src/jvmTest/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandlerTest.kt +++ b/logic/src/jvmTest/kotlin/com/wire/kalium/logic/sync/receiver/asset/AssetMessageHandlerTest.kt @@ -35,6 +35,7 @@ import com.wire.kalium.logic.sync.receiver.conversation.message.hasValidData import com.wire.kalium.logic.sync.receiver.conversation.message.hasValidRemoteData import io.mockative.Mock import io.mockative.any +import io.mockative.anything import io.mockative.classOf import io.mockative.eq import io.mockative.given @@ -261,7 +262,7 @@ class AssetMessageHandlerTest { val isFileSharingEnabled = FileSharingStatus.Value.EnabledSome(listOf("txt", "png", "zip")) val (arrangement, assetMessageHandler) = Arrangement() .withSuccessfulFileSharingFlag(isFileSharingEnabled) - .withValidateAssetMime(true) + .withValidateAssetFileType(true) .withSuccessfulStoredMessage(previewAssetMessage) .withSuccessfulPersistMessageUseCase(updateAssetMessage) .arrange() @@ -286,8 +287,8 @@ class AssetMessageHandlerTest { .with(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id)) .wasInvoked(exactly = once) - verify(arrangement.validateAssetMimeType) - .suspendFunction(arrangement.validateAssetMimeType::invoke) + verify(arrangement.validateAssetFileTypeUseCase) + .suspendFunction(arrangement.validateAssetFileTypeUseCase::invoke) .with(eq(COMPLETE_ASSET_CONTENT.value.name), eq("application/zip"), eq(isFileSharingEnabled.allowedType)) .wasInvoked(exactly = once) } @@ -300,7 +301,7 @@ class AssetMessageHandlerTest { val isFileSharingEnabled = FileSharingStatus.Value.EnabledSome(listOf("txt", "png")) val (arrangement, assetMessageHandler) = Arrangement() .withSuccessfulFileSharingFlag(isFileSharingEnabled) - .withValidateAssetMime(true) + .withValidateAssetFileType(true) .withSuccessfulStoredMessage(previewAssetMessage) .withSuccessfulPersistMessageUseCase(updateAssetMessage) .arrange() @@ -325,8 +326,8 @@ class AssetMessageHandlerTest { .with(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id)) .wasInvoked(exactly = once) - verify(arrangement.validateAssetMimeType) - .suspendFunction(arrangement.validateAssetMimeType::invoke) + verify(arrangement.validateAssetFileTypeUseCase) + .suspendFunction(arrangement.validateAssetFileTypeUseCase::invoke) .with(eq(COMPLETE_ASSET_CONTENT.value.name), eq("application/zip"), eq(isFileSharingEnabled.allowedType)) .wasInvoked(exactly = once) } @@ -339,7 +340,7 @@ class AssetMessageHandlerTest { val isFileSharingEnabled = FileSharingStatus.Value.Disabled val (arrangement, assetMessageHandler) = Arrangement() .withSuccessfulFileSharingFlag(isFileSharingEnabled) - .withValidateAssetMime(true) + .withValidateAssetFileType(true) .withSuccessfulStoredMessage(previewAssetMessage) .withSuccessfulPersistMessageUseCase(updateAssetMessage) .arrange() @@ -364,8 +365,8 @@ class AssetMessageHandlerTest { .with(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id)) .wasNotInvoked() - verify(arrangement.validateAssetMimeType) - .suspendFunction(arrangement.validateAssetMimeType::invoke) + verify(arrangement.validateAssetFileTypeUseCase) + .suspendFunction(arrangement.validateAssetFileTypeUseCase::invoke) .with(any(), any>()) .wasNotInvoked() } @@ -403,6 +404,7 @@ class AssetMessageHandlerTest { val (arrangement, assetMessageHandler) = Arrangement() .withSuccessfulFileSharingFlag(isFileSharingEnabled) .withSuccessfulStoredMessage(previewAssetMessage) + .withValidateAssetFileType(true) .arrange() // When @@ -450,6 +452,7 @@ class AssetMessageHandlerTest { .withSuccessfulFileSharingFlag(isFileSharingEnabled) .withSuccessfulStoredMessage(null) .withSuccessfulPersistMessageUseCase(storedMessage) + .withValidateAssetFileType(true) .arrange() // When @@ -479,15 +482,15 @@ class AssetMessageHandlerTest { val userConfigRepository = mock(classOf()) @Mock - val validateAssetMimeType = mock(classOf()) + val validateAssetFileTypeUseCase = mock(classOf()) private val assetMessageHandlerImpl = - AssetMessageHandlerImpl(messageRepository, persistMessage, userConfigRepository, validateAssetMimeType) + AssetMessageHandlerImpl(messageRepository, persistMessage, userConfigRepository, validateAssetFileTypeUseCase) - fun withValidateAssetMime(result: Boolean) = apply { - given(validateAssetMimeType) - .function(validateAssetMimeType::invoke) - .whenInvokedWith(any(), any()) + fun withValidateAssetFileType(result: Boolean) = apply { + given(validateAssetFileTypeUseCase) + .function(validateAssetFileTypeUseCase::invoke) + .whenInvokedWith(anything(), any(), any()) .thenReturn(result) }