Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadJaara committed Jul 5, 2024
1 parent a582f86 commit ad43d0d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.wire.android.ui.home.conversations.sendmessage

import androidx.lifecycle.SavedStateHandle
import com.wire.android.config.TestDispatcherProvider
import com.wire.android.config.mockUri
import com.wire.android.framework.FakeKaliumFileSystem
Expand Down Expand Up @@ -72,9 +71,6 @@ internal class SendMessageViewModelArrangement {
coEvery { observeConversationUnderLegalHoldNotified(any()) } returns flowOf(true)
}

@MockK
private lateinit var savedStateHandle: SavedStateHandle

@MockK
lateinit var sendTextMessage: SendTextMessageUseCase

Expand Down Expand Up @@ -165,7 +161,7 @@ internal class SendMessageViewModelArrangement {
}
}

fun withSuccessfulSendAttachmentMessage() = apply {
fun withSendAttachmentMessageResult(result: ScheduleNewAssetMessageResult) = apply {
coEvery {
sendAssetMessage(
any(),
Expand All @@ -177,7 +173,7 @@ internal class SendMessageViewModelArrangement {
any(),
any()
)
} returns ScheduleNewAssetMessageResult.Success("some-message-id")
} returns result
}

fun withSuccessfulSendTextMessage() = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.failure.LegalHoldEnabledForConversationFailure
import com.wire.kalium.logic.feature.asset.ScheduleNewAssetMessageResult
import io.mockk.coVerify
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -65,7 +66,7 @@ class SendMessageViewModelTest {
)
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withSuccessfulSendAttachmentMessage()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Success("some-message-id"))
.withHandleUriAsset(HandleUriAssetUseCase.Result.Success(mockedAttachment))
.arrange()

Expand Down Expand Up @@ -102,7 +103,7 @@ class SendMessageViewModelTest {
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withStoredAsset(assetPath, assetContent)
.withSuccessfulSendAttachmentMessage()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Success("some-message-id"))
.withHandleUriAsset(HandleUriAssetUseCase.Result.Success(mockedAttachment))
.arrange()

Expand Down Expand Up @@ -130,7 +131,7 @@ class SendMessageViewModelTest {
// Given
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withSuccessfulSendAttachmentMessage()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Success("some-message-id"))
.arrange()
val mockedAttachment = null

Expand Down Expand Up @@ -166,7 +167,7 @@ class SendMessageViewModelTest {
)
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withSuccessfulSendAttachmentMessage()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Success("some-message-id"))
.withHandleUriAsset(HandleUriAssetUseCase.Result.Failure.AssetTooLarge(mockedAttachment, 25))
.arrange()
val mockedMessageBundle = ComposableMessageBundle.AttachmentPickedBundle(
Expand Down Expand Up @@ -208,7 +209,7 @@ class SendMessageViewModelTest {
)
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withSuccessfulSendAttachmentMessage()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Success("some-message-id"))
.withHandleUriAsset(HandleUriAssetUseCase.Result.Failure.AssetTooLarge(mockedAttachment, limit))
.arrange()
val mockedMessageBundle = ComposableMessageBundle.AttachmentPickedBundle(
Expand Down Expand Up @@ -241,7 +242,7 @@ class SendMessageViewModelTest {
// Given
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withSuccessfulSendAttachmentMessage()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Success("some-message-id"))
.withHandleUriAsset(HandleUriAssetUseCase.Result.Failure.Unknown)
.arrange()
val mockedMessageBundle = ComposableMessageBundle.AttachmentPickedBundle(
Expand Down Expand Up @@ -303,7 +304,7 @@ class SendMessageViewModelTest {
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withStoredAsset(assetPath, assetContent)
.withSuccessfulSendAttachmentMessage()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Success("some-message-id"))
.withHandleUriAsset(HandleUriAssetUseCase.Result.Success(mockedAttachment))
.arrange()

Expand Down Expand Up @@ -564,6 +565,90 @@ class SendMessageViewModelTest {
assertEquals(SureAboutMessagingDialogState.Hidden, viewModel.sureAboutMessagingDialogState)
}

@Test
fun `given mimeType is DisabledByTeam, when trying to send, then show message to user`() =
runTest {
// Given
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Failure.DisabledByTeam)
.withHandleUriAsset(HandleUriAssetUseCase.Result.Failure.Unknown)
.arrange()

val mockedAttachment = AssetBundle(
"key",
"application/pdf",
"some-data-path".toPath(),
1L,
"mocked_file.pdf",
AttachmentType.GENERIC_FILE
)

// When
viewModel.infoMessage.test {
viewModel.sendAttachment(mockedAttachment, conversationId)

// Then
coVerify(exactly = 1) {
arrangement.sendAssetMessage.invoke(
any(),
any(),
any(),
any(),
any(),
any(),
any(),
any()
)
}
assertEquals(ConversationSnackbarMessages.ErrorAssetRestriction, awaitItem())
}
}

@Test
fun `given mimeType is RestrictedFileType, when trying to send, then show message to user`() =
runTest {
val assetPath = "mocked-asset-data-path".toPath()
val assetContent = "some-dummy-audio".toByteArray()

val mockedAttachment = AssetBundle(
"key",
"application/pdf",
"some-data-path".toPath(),
1L,
"mocked_file.pdf",
AttachmentType.GENERIC_FILE
)

// Given
val (arrangement, viewModel) = SendMessageViewModelArrangement()
.withSuccessfulViewModelInit()
.withStoredAsset(assetPath, assetContent)
.withHandleUriAsset(HandleUriAssetUseCase.Result.Success(mockedAttachment))
.withSendAttachmentMessageResult(ScheduleNewAssetMessageResult.Failure.RestrictedFileType)
.arrange()

// When
viewModel.infoMessage.test {
viewModel.sendAttachment(mockedAttachment, conversationId)

// Then
coVerify(exactly = 1) {
arrangement.sendAssetMessage.invoke(
any(),
any(),
any(),
any(),
any(),
any(),
any(),
any()
)
}
assertEquals(ConversationSnackbarMessages.ErrorAssetRestriction, awaitItem())
}
}

companion object {
val conversationId: ConversationId = ConversationId("some-dummy-value", "some.dummy.domain")
}
Expand Down

0 comments on commit ad43d0d

Please sign in to comment.