Skip to content

Commit

Permalink
fix: images form iOS are blocked when restrictions are applied [WPB-1…
Browse files Browse the repository at this point in the history
…0830] 🍒 (#3010)

* fix: images form iOS are blocked when restrictions are applied (#3006)

* cherry pick and resolve conflicts

---------

Co-authored-by: Mohamad Jaara <[email protected]>
  • Loading branch information
github-actions[bot] and MohamadJaara authored Sep 16, 2024
1 parent 4d062f6 commit 6037016
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,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 (
messageContent.value.name.isNullOrEmpty() &&
messageContent.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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.wire.kalium.logic.functional.Either
import com.wire.kalium.util.time.UNIX_FIRST_DATE
import io.mockative.Mock
import io.mockative.any
import io.mockative.classOf
import io.mockative.coEvery
import io.mockative.coVerify
import io.mockative.eq
Expand Down Expand Up @@ -251,7 +252,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()
Expand All @@ -276,11 +277,10 @@ class AssetMessageHandlerTest {
eq(previewAssetMessage.conversationId),
eq(previewAssetMessage.id)
)
}
.wasInvoked(exactly = once)
}.wasInvoked(exactly = once)

coVerify {
arrangement.validateAssetMimeType(
arrangement.validateAssetFileTypeUseCase(
fileName = eq(COMPLETE_ASSET_CONTENT.value.name),
mimeType = eq("application/zip"),
allowedExtension = eq(isFileSharingEnabled.allowedType)
Expand All @@ -296,7 +296,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()
Expand All @@ -323,7 +323,7 @@ class AssetMessageHandlerTest {
}.wasInvoked(exactly = once)

coVerify {
arrangement.validateAssetMimeType(
arrangement.validateAssetFileTypeUseCase(
fileName = eq(COMPLETE_ASSET_CONTENT.value.name),
mimeType = eq("application/zip"),
allowedExtension = eq(isFileSharingEnabled.allowedType)
Expand All @@ -339,7 +339,7 @@ class AssetMessageHandlerTest {
val isFileSharingEnabled = FileSharingStatus.Value.Disabled
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withValidateAssetMime(true)
.withValidateAssetFileType(true)
.withSuccessfulStoredMessage(previewAssetMessage)
.withSuccessfulPersistMessageUseCase(updateAssetMessage)
.arrange()
Expand All @@ -361,8 +361,7 @@ class AssetMessageHandlerTest {
coVerify { arrangement.messageRepository.getMessageById(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id)) }
.wasNotInvoked()

coVerify { arrangement.validateAssetMimeType(any<String>(), any<String>(), any<List<String>>()) }
.wasNotInvoked()
coVerify { arrangement.validateAssetFileTypeUseCase(any<String>(), any<String>(), any<List<String>>()) }
}

@Test
Expand Down Expand Up @@ -396,6 +395,7 @@ class AssetMessageHandlerTest {
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withSuccessfulStoredMessage(previewAssetMessage)
.withValidateAssetFileType(true)
.arrange()

// When
Expand Down Expand Up @@ -440,6 +440,7 @@ class AssetMessageHandlerTest {
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withSuccessfulStoredMessage(null)
.withSuccessfulPersistMessageUseCase(storedMessage)
.withValidateAssetFileType(true)
.arrange()

// When
Expand All @@ -465,14 +466,14 @@ class AssetMessageHandlerTest {
val userConfigRepository = mock(UserConfigRepository::class)

@Mock
val validateAssetMimeType = mock(ValidateAssetFileTypeUseCase::class)
val validateAssetFileTypeUseCase = mock(classOf<ValidateAssetFileTypeUseCase>())

private val assetMessageHandlerImpl =
AssetMessageHandlerImpl(messageRepository, persistMessage, userConfigRepository, validateAssetMimeType)
AssetMessageHandlerImpl(messageRepository, persistMessage, userConfigRepository, validateAssetFileTypeUseCase)

fun withValidateAssetMime(result: Boolean) = apply {
fun withValidateAssetFileType(result: Boolean) = apply {
every {
validateAssetMimeType.invoke(any(), any(), any())
validateAssetFileTypeUseCase.invoke(any(), any(), any())
}.returns(result)
}

Expand Down

0 comments on commit 6037016

Please sign in to comment.