Skip to content

Commit

Permalink
Added ComposeScreenNoKeyAvailableFlowTest.| #597
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Mar 27, 2024
1 parent c55b9ea commit 2bae042
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected]
* Contributors: DenBond7
*/

package com.flowcrypt.email.ui

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.action.ViewActions.replaceText
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.hasTextColor
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.flowcrypt.email.R
import com.flowcrypt.email.TestConstants
import com.flowcrypt.email.extensions.kotlin.asInternetAddress
import com.flowcrypt.email.junit.annotations.FlowCryptTestSettings
import com.flowcrypt.email.rules.AddPrivateKeyToDatabaseRule
import com.flowcrypt.email.rules.ClearAppSettingsRule
import com.flowcrypt.email.rules.FlowCryptMockWebServerRule
import com.flowcrypt.email.rules.GrantPermissionRuleChooser
import com.flowcrypt.email.rules.RetryRule
import com.flowcrypt.email.rules.ScreenshotTestRule
import com.flowcrypt.email.ui.base.BaseComposeScreenTest
import com.flowcrypt.email.util.PrivateKeysManager
import com.flowcrypt.email.util.TestGeneralUtil
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequest
import org.hamcrest.CoreMatchers.not
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import java.net.HttpURLConnection

/**
* @author Denys Bondarenko
*/
@MediumTest
@RunWith(AndroidJUnit4::class)
@FlowCryptTestSettings(useCommonIdling = false)
class ComposeScreenNoKeyAvailableFlowTest : BaseComposeScreenTest() {
private val addPrivateKeyToDatabaseRule = AddPrivateKeyToDatabaseRule(
keyPath = "pgp/[email protected]_prv_strong_primary.asc"
)

private val pgpKeyDetails =
PrivateKeysManager.getPgpKeyDetailsFromAssets("pgp/[email protected]_fisrtKey_prv_strong.asc")

@get:Rule
var ruleChain: TestRule = RuleChain
.outerRule(RetryRule.DEFAULT)
.around(ClearAppSettingsRule())
.around(GrantPermissionRuleChooser.grant(android.Manifest.permission.POST_NOTIFICATIONS))
.around(addAccountToDatabaseRule)
.around(addPrivateKeyToDatabaseRule)
.around(activeActivityRule)
.around(ScreenshotTestRule())

@Test
fun testImportKey() {
activeActivityRule?.launch(intent)
registerAllIdlingResources()
fillInAllFields(
to = setOf(
requireNotNull(TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER.asInternetAddress())
)
)

//check that editTextFrom has gray text color. It means a sender doesn't have a private key
onView(withId(R.id.editTextFrom))
.check(matches(isDisplayed()))
.check(matches(hasTextColor(R.color.gray)))

onView(withId(R.id.menuActionSend))
.check(matches(isDisplayed()))
.perform(click())

isDialogWithTextDisplayed(
decorView,
getResString(R.string.no_key_available, addAccountToDatabaseRule.account.email)
)

addTextToClipboard("private key", requireNotNull(pgpKeyDetails.privateKey))
onView(withText(R.string.import_private_key))
.check(matches(isDisplayed()))
.perform(click())

Thread.sleep(1000)
onView(withText(R.string.load_from_clipboard))
.check(matches(isDisplayed()))
.perform(click())

onView(withId(R.id.editTextKeyPassword))
.perform(
replaceText(TestConstants.DEFAULT_STRONG_PASSWORD),
closeSoftKeyboard()
)
onView(withId(R.id.buttonPositiveAction))
.perform(scrollTo(), click())

waitForObjectWithText(addAccountToDatabaseRule.account.email, 5000)

//check that editTextFrom doesn't have gray text color. It means a sender has a private key.
onView(withId(R.id.editTextFrom))
.check(matches(isDisplayed()))
.check(matches(not(hasTextColor(R.color.gray))))
}

companion object {
@get:ClassRule
@JvmStatic
val mockWebServerRule = FlowCryptMockWebServerRule(TestConstants.MOCK_WEB_SERVER_PORT,
object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
if (request.path?.startsWith("/attester/pub", ignoreCase = true) == true) {
val lastSegment = request.requestUrl?.pathSegments?.lastOrNull()

when {
TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER.equals(
lastSegment, true
) -> {
return MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(TestGeneralUtil.readResourceAsString("3.txt"))
}
}
}

return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,22 @@ class CreateMessageFragment : BaseFragment<FragmentCreateMessageBinding>(),

private fun setupPrivateKeysViewModel() {
KeysStorageImpl.getInstance(requireContext()).secretKeyRingsLiveData
.observe(viewLifecycleOwner) { updateFromAddressAdapter(it) }
.observe(viewLifecycleOwner) {
updateFromAddressAdapter(it)
if (composeMsgViewModel.msgEncryptionType == MessageEncryptionType.ENCRYPTED) {
val from = binding?.editTextFrom?.text?.toString() ?: return@observe
val position = fromAddressesAdapter?.getPosition(from) ?: return@observe
binding?.editTextFrom?.setTextColor(
if (fromAddressesAdapter?.isEnabled(position) == true) {
originalColor
} else {
UIUtil.getColor(requireContext(), R.color.gray)
}
)
} else {
binding?.editTextFrom?.setTextColor(originalColor)
}
}
}

private fun updateFromAddressAdapter(list: List<PGPSecretKeyRing>) {
Expand Down

0 comments on commit 2bae042

Please sign in to comment.