Skip to content

Commit

Permalink
Fixed some tests.| #372
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed May 26, 2021
1 parent 2401ad6 commit 4a5c256
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.flowcrypt.email.api.email.model.AuthCredentials
import com.flowcrypt.email.api.email.model.SecurityType
import com.flowcrypt.email.base.BaseTest
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.junit.annotations.DependsOnMailServer
import com.flowcrypt.email.junit.annotations.NotReadyForCI
import com.flowcrypt.email.matchers.CustomMatchers.Companion.withSecurityTypeOption
Expand Down Expand Up @@ -337,18 +338,19 @@ class AddOtherAccountFragmentTest : BaseTest() {
@Test
@DependsOnMailServer
fun testWhenNoAccountsAndHasBackup() {
val prvKey = PrivateKeysManager.getNodeKeyDetailsFromAssets(
"pgp/[email protected]_fisrtKey_prv_default.asc")
val prvKey = PrivateKeysManager
.getNodeKeyDetailsFromAssets("pgp/[email protected]_fisrtKey_prv_default.asc")
.copy(passphraseType = KeyEntity.PassphraseType.DATABASE)

intending(hasComponent(ComponentName(getTargetContext(), CheckKeysActivity::class.java)))
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, Intent().apply {
putExtra(CheckKeysActivity.KEY_EXTRA_UNLOCKED_PRIVATE_KEYS, ArrayList(listOf(prvKey)))
}))
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, Intent().apply {
putExtra(CheckKeysActivity.KEY_EXTRA_UNLOCKED_PRIVATE_KEYS, ArrayList(listOf(prvKey)))
}))

val creds = AuthCredentialsManager.getAuthCredentials()

onView(withId(R.id.editTextEmail))
.perform(clearText(), typeText(creds.email), closeSoftKeyboard())
.perform(clearText(), typeText(creds.email), closeSoftKeyboard())
onView(withId(R.id.editTextPassword))
.perform(clearText(), typeText(creds.password), closeSoftKeyboard())
onView(withId(R.id.buttonTryToConnect))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.test.filters.MediumTest
import com.flowcrypt.email.R
import com.flowcrypt.email.TestConstants
import com.flowcrypt.email.base.BaseTest
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.junit.annotations.DependsOnMailServer
import com.flowcrypt.email.junit.annotations.NotReadyForCI
import com.flowcrypt.email.rules.AddAccountToDatabaseRule
Expand Down Expand Up @@ -165,6 +166,7 @@ class ImportPrivateKeyActivityFromSettingsTest : BaseTest() {
@JvmStatic
fun createResources() {
keyDetails.tempPassphrase = TestConstants.DEFAULT_STRONG_PASSWORD.toCharArray()
keyDetails.passphraseType = KeyEntity.PassphraseType.DATABASE
privateKey = keyDetails.privateKey!!
fileWithPrivateKey = TestGeneralUtil.createFileAndFillWithContent(
TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER + "_sec.asc", privateKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ class KeysSettingsActivityTest : BaseTest() {
.check(matches(withText(getHtmlString(getResString(R.string.template_fingerprint,
GeneralUtil.doSectionsInText(" ", details.fingerprint, 4)!!)))))

onView(withId(R.id.textViewFingerprint)).check(
matches(withText(getResString(R.string.template_fingerprint_3, details.fingerprint))))

onView(withId(R.id.textViewDate))
.check(matches(withText(getHtmlString(getResString(R.string.template_date,
DateFormat.getMediumDateFormat(getTargetContext()).format(Date(details.created)))))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SearchMessagesActivityTest : BaseEmailListActivityTest() {
//todo-denbond7 Need to improve this code
Thread.sleep(2000)
onView(withId(R.id.rVMsgs))
.check(matches(withRecyclerViewItemCount(4))).check(matches(isDisplayed()))
.check(matches(withRecyclerViewItemCount(3))).check(matches(isDisplayed()))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,70 @@ import java.util.*
*/
class PrivateKeysManager {
companion object {
fun saveKeyFromAssetsToDatabase(accountEntity: AccountEntity, keyPath: String,
passphrase: String, sourceType: KeyImportDetails.SourceType) {
fun saveKeyFromAssetsToDatabase(
accountEntity: AccountEntity, keyPath: String,
passphrase: String, sourceType: KeyImportDetails.SourceType
) {
val nodeKeyDetails = getNodeKeyDetailsFromAssets(keyPath)
saveKeyToDatabase(accountEntity, nodeKeyDetails, passphrase, sourceType)
}

fun saveKeyToDatabase(accountEntity: AccountEntity, pgpKeyDetails: PgpKeyDetails,
passphrase: String, sourceType: KeyImportDetails.SourceType) {
fun saveKeyToDatabase(
accountEntity: AccountEntity, pgpKeyDetails: PgpKeyDetails,
passphrase: String, sourceType: KeyImportDetails.SourceType
) {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val roomDatabase = FlowCryptRoomDatabase.getDatabase(context)
val keyEntity = pgpKeyDetails.copy(passphraseType = KeyEntity.PassphraseType.DATABASE)
.toKeyEntity(accountEntity)
.copy(
source = sourceType.toString(),
privateKey = KeyStoreCryptoManager.encrypt(pgpKeyDetails.privateKey).toByteArray(),
storedPassphrase = KeyStoreCryptoManager.encrypt(passphrase))
val existedKey = roomDatabase.keysDao().getKeyByAccountAndFingerprint(accountEntity.email,
pgpKeyDetails.fingerprint)
existedKey?.let { roomDatabase.keysDao().delete(it) }
val keyEntity = pgpKeyDetails
.copy(passphraseType = KeyEntity.PassphraseType.DATABASE)
.toKeyEntity(accountEntity)
.copy(
source = sourceType.toString(),
privateKey = KeyStoreCryptoManager.encrypt(pgpKeyDetails.privateKey).toByteArray(),
storedPassphrase = KeyStoreCryptoManager.encrypt(passphrase)
)
val existingKey = roomDatabase.keysDao().getKeyByAccountAndFingerprint(
accountEntity.email,
pgpKeyDetails.fingerprint
)
existingKey?.let { roomDatabase.keysDao().delete(it) }
roomDatabase.keysDao().insert(keyEntity)
// Added timeout for a better sync between threads.
Thread.sleep(3000)
}

fun getNodeKeyDetailsFromAssets(assetsPath: String, onlyPrivate: Boolean = false): PgpKeyDetails {
fun getNodeKeyDetailsFromAssets(
assetsPath: String,
onlyPrivate: Boolean = false
): PgpKeyDetails {
return getNodeKeyDetailsListFromAssets(assetsPath, onlyPrivate).first()
}

fun getNodeKeyDetailsListFromAssets(assetsPath: String, onlyPrivate: Boolean = false): List<PgpKeyDetails> {
fun getNodeKeyDetailsListFromAssets(
assetsPath: String,
onlyPrivate: Boolean = false
): List<PgpKeyDetails> {
val parsedCollections =
PgpKey.parseKeys(TestGeneralUtil.readFileFromAssetsAsStream(assetsPath))
PgpKey.parseKeys(TestGeneralUtil.readFileFromAssetsAsStream(assetsPath))

if (onlyPrivate) {
val onlyPrivateKeysCollection = PgpKey.ParseKeyResult(
PGPKeyRingCollection(parsedCollections.pgpKeyRingCollection
.pgpSecretKeyRingCollection.keyRings.asSequence().toList(), false))
PGPKeyRingCollection(
parsedCollections.pgpKeyRingCollection
.pgpSecretKeyRingCollection.keyRings.asSequence().toList(), false
)
)

return onlyPrivateKeysCollection.toPgpKeyDetailsList()
} else {
return parsedCollections.toPgpKeyDetailsList()
}
}

fun getKeysFromAssets(keysPaths: Array<String>, onlyPrivate: Boolean = false): ArrayList<PgpKeyDetails> {
fun getKeysFromAssets(
keysPaths: Array<String>,
onlyPrivate: Boolean = false
): ArrayList<PgpKeyDetails> {
val privateKeys = ArrayList<PgpKeyDetails>()
keysPaths.forEach { path ->
privateKeys.addAll(getNodeKeyDetailsListFromAssets(path, onlyPrivate))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ class PrivateKeysViewModel(application: Application) : BaseNodeApiViewModel(appl
}
}

fun createPrivateKey(accountEntity: AccountEntity, passphrase: String) {
fun createPrivateKey(
accountEntity: AccountEntity, passphrase: String,
passphraseType: KeyEntity.PassphraseType
) {
viewModelScope.launch {
createPrivateKeyLiveData.value = Result.loading()
var pgpKeyDetails: PgpKeyDetails? = null
Expand All @@ -317,7 +320,7 @@ class PrivateKeysViewModel(application: Application) : BaseNodeApiViewModel(appl
accountEntity.displayName
?: accountEntity.email, accountEntity.email
), passphrase
).toPgpKeyDetails()
).toPgpKeyDetails().copy(passphraseType = passphraseType)

val existedAccount =
roomDatabase.accountDao().getAccountSuspend(accountEntity.email.toLowerCase(Locale.US))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.activity.viewModels
import com.flowcrypt.email.R
import com.flowcrypt.email.api.retrofit.response.base.Result
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.extensions.decrementSafely
import com.flowcrypt.email.extensions.incrementSafely
import com.flowcrypt.email.jetpack.viewmodel.PrivateKeysViewModel
Expand Down Expand Up @@ -44,7 +45,13 @@ class CreatePrivateKeyActivity : BasePassPhraseManagerActivity() {
get() = findViewById(R.id.layoutContent)

override fun onConfirmPassPhraseSuccess() {
tempAccount?.let { privateKeysViewModel.createPrivateKey(it, editTextKeyPassword.text.toString()) }
tempAccount?.let {
privateKeysViewModel.createPrivateKey(
accountEntity = it,
passphrase = editTextKeyPassword.text.toString(),
passphraseType = KeyEntity.PassphraseType.DATABASE
)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -57,7 +64,8 @@ class CreatePrivateKeyActivity : BasePassPhraseManagerActivity() {
}

if (savedInstanceState != null) {
this.createdPrivateKeyFingerprint = savedInstanceState.getString(KEY_CREATED_PRIVATE_KEY_FINGERPRINT)
this.createdPrivateKeyFingerprint =
savedInstanceState.getString(KEY_CREATED_PRIVATE_KEY_FINGERPRINT)
}

setupPrivateKeyViewModel()
Expand All @@ -72,7 +80,8 @@ class CreatePrivateKeyActivity : BasePassPhraseManagerActivity() {
finish()
}
} else {
Toast.makeText(this, R.string.please_wait_while_key_will_be_created, Toast.LENGTH_SHORT).show()
Toast.makeText(this, R.string.please_wait_while_key_will_be_created, Toast.LENGTH_SHORT)
.show()
}
}

Expand Down Expand Up @@ -137,8 +146,10 @@ class CreatePrivateKeyActivity : BasePassPhraseManagerActivity() {

it.exception?.let { exception ->
if (exception is ApiException) {
showSnackbar(rootView, exception.apiError?.msg ?: it.javaClass.simpleName,
getString(R.string.retry), Snackbar.LENGTH_LONG) {
showSnackbar(
rootView, exception.apiError?.msg ?: it.javaClass.simpleName,
getString(R.string.retry), Snackbar.LENGTH_LONG
) {
onConfirmPassPhraseSuccess()
}
} else {
Expand All @@ -154,9 +165,15 @@ class CreatePrivateKeyActivity : BasePassPhraseManagerActivity() {
}

companion object {
val KEY_EXTRA_ACCOUNT = GeneralUtil.generateUniqueExtraKey("KEY_EXTRA_ACCOUNT", BasePassPhraseManagerActivity::class.java)
val KEY_EXTRA_ACCOUNT = GeneralUtil.generateUniqueExtraKey(
"KEY_EXTRA_ACCOUNT",
BasePassPhraseManagerActivity::class.java
)
val KEY_CREATED_PRIVATE_KEY_FINGERPRINT =
GeneralUtil.generateUniqueExtraKey("KEY_CREATED_PRIVATE_KEY_FINGERPRINT", CreatePrivateKeyActivity::class.java)
GeneralUtil.generateUniqueExtraKey(
"KEY_CREATED_PRIVATE_KEY_FINGERPRINT",
CreatePrivateKeyActivity::class.java
)

fun newIntent(context: Context, account: AccountEntity?): Intent {
val intent = Intent(context, CreatePrivateKeyActivity::class.java)
Expand Down

0 comments on commit 4a5c256

Please sign in to comment.