Skip to content

Commit

Permalink
Refactored code.| #1768
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jun 23, 2022
1 parent 4581077 commit 730d100
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ class RefreshKeysFromEkmFlowTest : BaseRefreshKeysFromEkmFlowTest() {
.around(activityScenarioRule)
.around(ScreenshotTestRule())

override fun handleEkmAPI(gson: Gson): MockResponse {
return when (testNameRule.methodName) {
"testUpdatePrvKeyFromEkmSuccessSilent", "testUpdatePrvKeyFromEkmShowFixMissingPassphrase" ->
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(EKM_RESPONSE_SUCCESS))

"testUpdatePrvKeyFromEkmShowApiError" ->
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(EKM_ERROR_RESPONSE))

else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
}

@Test
fun testUpdatePrvKeyFromEkmSuccessSilent() {
val keysStorage = KeysStorageImpl.getInstance(getTargetContext())
Expand Down Expand Up @@ -164,20 +178,6 @@ class RefreshKeysFromEkmFlowTest : BaseRefreshKeysFromEkmFlowTest() {
return existingPgpKeyDetailsBeforeUpdating
}

override fun handleEkmAPI(gson: Gson): MockResponse {
return when (testNameRule.methodName) {
"testUpdatePrvKeyFromEkmSuccessSilent", "testUpdatePrvKeyFromEkmShowFixMissingPassphrase" ->
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(EKM_RESPONSE_SUCCESS))

"testUpdatePrvKeyFromEkmShowApiError" ->
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(EKM_ERROR_RESPONSE))

else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
}

companion object {
private val EKM_KEY_WITH_EXTENDED_EXPIRATION = PrivateKeysManager.getPgpKeyDetailsFromAssets(
"pgp/[email protected]_prv_default.asc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import com.flowcrypt.email.TestConstants
import com.flowcrypt.email.api.retrofit.response.api.EkmPrivateKeysResponse
import com.flowcrypt.email.api.retrofit.response.model.Key
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.extensions.org.pgpainless.util.asString
import com.flowcrypt.email.model.KeyImportDetails
import com.flowcrypt.email.rules.AddPrivateKeyToDatabaseRule
import com.flowcrypt.email.rules.ClearAppSettingsRule
import com.flowcrypt.email.rules.RetryRule
import com.flowcrypt.email.rules.ScreenshotTestRule
import com.flowcrypt.email.security.KeysStorageImpl
import com.flowcrypt.email.security.model.PgpKeyDetails
import com.flowcrypt.email.security.pgp.PgpKey
import com.flowcrypt.email.ui.base.BaseRefreshKeysFromEkmFlowTest
import com.flowcrypt.email.util.PrivateKeysManager
Expand Down Expand Up @@ -90,7 +88,8 @@ class RefreshRevokedKeysFromEkmFlowTest : BaseRefreshKeysFromEkmFlowTest() {
)

//check existing key before updating
val existingPgpKeyDetailsBeforeUpdating = checkExistingKeyBeforeUpdating(keysStorage)
val existingPgpKeyDetailsBeforeUpdating = keysStorage.getPgpKeyDetailsList().first()
assertTrue(existingPgpKeyDetailsBeforeUpdating.isRevoked)

//we need to make a delay to wait while [KeysStorageImpl] will update internal data
Thread.sleep(2000)
Expand Down Expand Up @@ -120,7 +119,8 @@ class RefreshRevokedKeysFromEkmFlowTest : BaseRefreshKeysFromEkmFlowTest() {
)

//check existing key before updating
val firstPgpKeyDetailsBeforeUpdating = checkExistingKeyBeforeUpdating(keysStorage)
val firstPgpKeyDetailsBeforeUpdating = keysStorage.getPgpKeyDetailsList().first()
assertTrue(firstPgpKeyDetailsBeforeUpdating.isRevoked)

//we need to make a delay to wait while [KeysStorageImpl] will update internal data
Thread.sleep(2000)
Expand Down Expand Up @@ -152,16 +152,6 @@ class RefreshRevokedKeysFromEkmFlowTest : BaseRefreshKeysFromEkmFlowTest() {
.check(matches(isDisplayed()))
}

private fun checkExistingKeyBeforeUpdating(keysStorage: KeysStorageImpl): PgpKeyDetails {
val existingPgpKeyDetailsBeforeUpdating = keysStorage.getPgpKeyDetailsList().first()
assertTrue(existingPgpKeyDetailsBeforeUpdating.isRevoked)
assertEquals(
addPrivateKeyToDatabaseRule.passphrase,
keysStorage.getPassphraseByFingerprint(existingPgpKeyDetailsBeforeUpdating.fingerprint)?.asString
)
return existingPgpKeyDetailsBeforeUpdating
}

companion object {
private val EKM_KEY_WITH_MODIFICATION_DATE_AFTER_REVOKED =
PrivateKeysManager.getPgpKeyDetailsFromAssets(
Expand Down

0 comments on commit 730d100

Please sign in to comment.