Skip to content

Commit

Permalink
Added more tests. Refactored code.| #1201
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Aug 12, 2021
1 parent ac073eb commit 9ed061d
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 42 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import com.flowcrypt.email.rules.RetryRule
import com.flowcrypt.email.rules.ScreenshotTestRule
import com.flowcrypt.email.ui.activity.base.BaseCreateMessageActivityTest
import com.flowcrypt.email.ui.widget.CustomChipSpanChipCreator
import com.flowcrypt.email.util.TestGeneralUtil
import com.flowcrypt.email.util.UIUtil
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequest
import okio.Buffer
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand Down Expand Up @@ -74,7 +76,7 @@ class CreateMessageActivityWkdTest : BaseCreateMessageActivityTest() {
return handleDirectPolicyRequest()
}

if ("/.well-known/openpgpkey/.*/hu/.*\\?l=.*".toRegex().matches(request.path ?: "")) {
if ("/.well-known/openpgpkey/hu/.*\\?l=.*".toRegex().matches(request.path ?: "")) {
return handleDirectWkdRequest()
}

Expand All @@ -93,8 +95,116 @@ class CreateMessageActivityWkdTest : BaseCreateMessageActivityTest() {
.around(ScreenshotTestRule())

@Test
fun testWkdNoResult() {
val recipient = "wkd_no_result@localhost"
fun testWkdAdvancedNoResult() {
val recipient = "wkd_advanced_no_result@localhost"
fillInAllFields(recipient)
onView(withId(R.id.editTextRecipientTo))
.check(
matches(
withChipsBackgroundColor(
chipText = recipient,
backgroundColor = UIUtil.getColor(
context = getTargetContext(),
colorResourcesId = CustomChipSpanChipCreator.CHIP_COLOR_RES_ID_PGP_NOT_EXISTS
)
)
)
)
}

@Test
fun testWkdAdvancedPub() {
val recipient = "wkd_advanced_pub@localhost"
fillInAllFields(recipient)
onView(withId(R.id.editTextRecipientTo))
.check(
matches(
withChipsBackgroundColor(
chipText = recipient,
backgroundColor = UIUtil.getColor(
context = getTargetContext(),
colorResourcesId = CustomChipSpanChipCreator.CHIP_COLOR_RES_ID_PGP_EXISTS
)
)
)
)
}

@Test
fun testWkdAdvancedSkippedWkdDirectNoPolicyPub() {
val recipient = "wkd_direct_no_policy@localhost"
fillInAllFields(recipient)
onView(withId(R.id.editTextRecipientTo))
.check(
matches(
withChipsBackgroundColor(
chipText = recipient,
backgroundColor = UIUtil.getColor(
context = getTargetContext(),
colorResourcesId = CustomChipSpanChipCreator.CHIP_COLOR_RES_ID_PGP_NOT_EXISTS
)
)
)
)
}

@Test
fun testWkdAdvancedSkippedWkdDirectNoResult() {
val recipient = "wkd_direct_no_result@localhost"
fillInAllFields(recipient)
onView(withId(R.id.editTextRecipientTo))
.check(
matches(
withChipsBackgroundColor(
chipText = recipient,
backgroundColor = UIUtil.getColor(
context = getTargetContext(),
colorResourcesId = CustomChipSpanChipCreator.CHIP_COLOR_RES_ID_PGP_NOT_EXISTS
)
)
)
)
}

@Test
fun testWkdAdvancedSkippedWkdDirectPub() {
val recipient = "wkd_direct_pub@localhost"
fillInAllFields(recipient)
onView(withId(R.id.editTextRecipientTo))
.check(
matches(
withChipsBackgroundColor(
chipText = recipient,
backgroundColor = UIUtil.getColor(
context = getTargetContext(),
colorResourcesId = CustomChipSpanChipCreator.CHIP_COLOR_RES_ID_PGP_EXISTS
)
)
)
)
}

@Test
fun testWkdAdvancedTimeOutWkdDirectAvailable() {
val recipient = "wkd_direct_pub@localhost"
fillInAllFields(recipient)
onView(withId(R.id.editTextRecipientTo))
.check(
matches(
withChipsBackgroundColor(
chipText = recipient,
backgroundColor = UIUtil.getColor(
context = getTargetContext(),
colorResourcesId = CustomChipSpanChipCreator.CHIP_COLOR_RES_ID_PGP_EXISTS
)
)
)
)
}

@Test
fun testWkdAdvancedTimeOutWkdDirectTimeOut() {
val recipient = "wkd_advanced_direct_timeout@localhost"
fillInAllFields(recipient)
onView(withId(R.id.editTextRecipientTo))
.check(
Expand All @@ -111,24 +221,80 @@ class CreateMessageActivityWkdTest : BaseCreateMessageActivityTest() {
}

private fun handleAdvancedPolicyRequest(): MockResponse {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
return when (testNameRule.methodName) {
"testWkdAdvancedNoResult", "testWkdAdvancedPub" -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
}

"testWkdAdvancedTimeOutWkdDirectAvailable",
"testWkdAdvancedTimeOutWkdDirectTimeOut" -> {
Thread.sleep(5000)
MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}

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

private fun handleAdvancedWkdRequest(): MockResponse {
return when (testNameRule.methodName) {
"testWkdNoResult" -> {
"testWkdAdvancedNoResult" -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}

"testWkdAdvancedPub" -> {
MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
Buffer().write(
TestGeneralUtil.readFileFromAssetsAsByteArray(
"pgp/keys/wkd_advanced_pub@localhost_pub.asc"
)
)
)
}

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

private fun handleDirectPolicyRequest(): MockResponse {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
return when (testNameRule.methodName) {
"testWkdAdvancedSkippedWkdDirectNoResult",
"testWkdAdvancedSkippedWkdDirectPub",
"testWkdAdvancedTimeOutWkdDirectAvailable" -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
}

"testWkdAdvancedTimeOutWkdDirectTimeOut" -> {
Thread.sleep(5000)
MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}

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

private fun handleDirectWkdRequest(): MockResponse {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_ACCEPTABLE)
return when (testNameRule.methodName) {
"testWkdAdvancedSkippedWkdDirectNoResult" -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}

"testWkdAdvancedSkippedWkdDirectPub",
"testWkdAdvancedTimeOutWkdDirectAvailable" -> {
MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
Buffer().write(
TestGeneralUtil.readFileFromAssetsAsByteArray(
"pgp/keys/wkd_direct_pub@localhost_pub.asc"
)
)
)
}

else -> MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,12 @@ import com.flowcrypt.email.extensions.kotlin.isValidEmail

// https://en.wikipedia.org/wiki/Email_address#Internationalization_examples
class BetterInternetAddress(str: String, verifySpecialCharacters: Boolean = true) {

companion object {
const val alphanum = "\\p{L}\\u0900-\\u097F0-9"
const val validEmail = "(?:[${alphanum}!#\$%&'*+/=?^_`{|}~-]+(?:\\.[${alphanum}!#\$%&'*+/=?^" +
"_`{|}~-]+)*|\"(?:[\\x01-\\x08" +
"\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f" +
"])*\")@(?:(?:[${alphanum}](?:[${alphanum}-]*[${alphanum}])?\\.)+[${alphanum}](?:[" +
"${alphanum}-]*[${alphanum}])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:" +
"25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[${alphanum}-]*[${alphanum}]:(?:[\\x01-\\x08\\x0b" +
"\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])"
private const val validPersonalNameWithEmail =
"([$alphanum\\p{Punct}\\p{Space}]*)<($validEmail)>"

private val validEmailRegex = validEmail.toRegex()
private val validPersonalNameWithEmailRegex = validPersonalNameWithEmail.toRegex()
// if these appear in the display-name they must be double quoted
private val containsSpecialCharacterRegex = ".*[()<>\\[\\]:;@\\\\,.\"].*".toRegex()
// double quotes at ends only
private val doubleQuotedTextRegex = "\"[^\"]*\"".toRegex()
private val validLocalhostEmailRegex = Regex("([a-zA-z])([a-zA-z0-9])+@localhost")

fun isValidEmail(email: String): Boolean {
return validEmailRegex.matchEntire(email) != null
}

fun isValidLocalhostEmail(email: String): Boolean {
return validLocalhostEmailRegex.matchEntire(email) != null
}

fun areValidEmails(emails: Iterable<String>): Boolean {
return emails.all { it.isValidEmail() }
}
}

val personalName: String?
val emailAddress: String

init {
val personalNameWithEmailMatch = validPersonalNameWithEmailRegex.find(str)
val emailMatch = str.matches(validEmailRegex)
val emailMatch = str.matches(validEmailRegex) || str.matches(validLocalhostEmailRegex)
when {
personalNameWithEmailMatch != null -> {
val group = personalNameWithEmailMatch.groupValues
Expand All @@ -65,11 +31,49 @@ class BetterInternetAddress(str: String, verifySpecialCharacters: Boolean = true
)
}
}

emailMatch -> {
personalName = null
emailAddress = str
}

else -> throw IllegalArgumentException("Invalid email $str")
}
}

companion object {
private const val ALPHANUM = "\\p{L}\\u0900-\\u097F0-9"
private const val VALID_EMAIL =
"(?:[${ALPHANUM}!#\$%&'*+/=?^_`{|}~-]+(?:\\.[${ALPHANUM}!#\$%&'*+/=?^" +
"_`{|}~-]+)*|\"(?:[\\x01-\\x08" +
"\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f" +
"])*\")@(?:(?:[${ALPHANUM}](?:[${ALPHANUM}-]*[${ALPHANUM}])?\\.)+[${ALPHANUM}](?:[" +
"${ALPHANUM}-]*[${ALPHANUM}])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:" +
"25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[${ALPHANUM}-]*[${ALPHANUM}]:(?:[\\x01-\\x08\\x0b" +
"\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])"
private const val VALID_PERSONAL_NAME_WITH_EMAIL =
"([$ALPHANUM\\p{Punct}\\p{Space}]*)<($VALID_EMAIL)>"

private val validEmailRegex = VALID_EMAIL.toRegex()
private val validPersonalNameWithEmailRegex = VALID_PERSONAL_NAME_WITH_EMAIL.toRegex()

// if these appear in the display-name they must be double quoted
private val containsSpecialCharacterRegex = ".*[()<>\\[\\]:;@\\\\,.\"].*".toRegex()

// double quotes at ends only
private val doubleQuotedTextRegex = "\"[^\"]*\"".toRegex()
private val validLocalhostEmailRegex = Regex("([a-zA-z])([a-zA-z0-9])+@localhost")

fun isValidEmail(email: String): Boolean {
return validEmailRegex.matchEntire(email) != null
}

fun isValidLocalhostEmail(email: String): Boolean {
return validLocalhostEmailRegex.matchEntire(email) != null
}

fun areValidEmails(emails: Iterable<String>): Boolean {
return emails.all { it.isValidEmail() }
}
}
}

0 comments on commit 9ed061d

Please sign in to comment.