Skip to content

Commit

Permalink
Updated tests.| #2162
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Mar 6, 2023
1 parent 0fd42bf commit 12aecdc
Show file tree
Hide file tree
Showing 8 changed files with 720 additions and 193 deletions.
1 change: 1 addition & 0 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ global_job_config:
- echo "#added by flowcrypt" | sudo tee -a /etc/dnsmasq.conf
- echo "listen-address=127.0.0.1" | sudo tee -a /etc/dnsmasq.conf
- echo "address=/flowcrypt.test/127.0.0.1" | sudo tee -a /etc/dnsmasq.conf
- echo "address=/wrongssl.test/127.0.0.1" | sudo tee -a /etc/dnsmasq.conf
- echo "address=/localhost/127.0.0.1" | sudo tee -a /etc/dnsmasq.conf
- sudo systemctl restart dnsmasq
# print some debug info
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* © 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.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
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.api.retrofit.response.api.FesServerResponse
import com.flowcrypt.email.api.retrofit.response.base.ApiError
import com.flowcrypt.email.rules.ClearAppSettingsRule
import com.flowcrypt.email.rules.GrantPermissionRuleChooser
import com.flowcrypt.email.rules.RetryRule
import com.flowcrypt.email.rules.ScreenshotTestRule
import com.flowcrypt.email.ui.base.BaseFesDuringSetupFlowTest
import com.flowcrypt.email.util.exception.ApiException
import com.google.gson.Gson
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequest
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)
class FesDuringSetupCommonFlowTest : BaseFesDuringSetupFlowTest() {

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

override fun handleAPI(request: RecordedRequest, gson: Gson): MockResponse {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}

override fun handleCheckIfFesIsAvailableAtCustomerFesUrl(gson: Gson): MockResponse {
return when (testNameRule.methodName) {
"testFesFailedApiError" -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
gson.toJson(
FesServerResponse(
ApiError(
code = HttpURLConnection.HTTP_FORBIDDEN,
msg = ERROR_TEXT
)
)
)
)
}

"testFesAvailableExternalServiceAlias" -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(FES_SUCCESS_RESPONSE.copy(service = "external-service")))
}

"testFesAvailableEnterpriseServerAlias" -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(FES_SUCCESS_RESPONSE.copy(service = "enterprise-server")))
}

else -> {
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(gson.toJson(FES_SUCCESS_RESPONSE))
}
}
}

override fun handleClientConfigurationAPI(gson: Gson): MockResponse {
val responseCode = when (testNameRule.methodName) {
"testFesAvailableExternalServiceAlias" -> HttpURLConnection.HTTP_NOT_ACCEPTABLE
"testFesAvailableEnterpriseServerAlias" -> HttpURLConnection.HTTP_CONFLICT
else -> HttpURLConnection.HTTP_OK
}

return MockResponse().setResponseCode(responseCode)
}

override fun handleClientConfigurationAPIForSharedTenantFes(
account: String?,
gson: Gson
): MockResponse {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}

@Test
fun testFesAvailableExternalServiceAlias() {
setupAndClickSignInButton(genMockGoogleSignInAccountJson(EMAIL_FES_SERVER_EXTERNAL_SERVICE))
//we simulate error for https://fes.$domain/api/v1/client-configuration?domain=$domain
//to check that external-service was accepted and we called getClientConfigurationFromFes()

isDialogWithTextDisplayed(
decorView,
"ApiException:" + ApiException(
ApiError(
code = HttpURLConnection.HTTP_NOT_ACCEPTABLE,
msg = ""
)
).message!!
)
}

@Test
fun testFesAvailableEnterpriseServerAlias() {
setupAndClickSignInButton(genMockGoogleSignInAccountJson(EMAIL_FES_SERVER_ENTERPRISE_SERVER))
//we simulate error for https://fes.$domain/api/v1/client-configuration?domain=$domain
//to check that enterprise-service was accepted and we called getClientConfigurationFromFes()
isDialogWithTextDisplayed(
decorView,
"ApiException:" + ApiException(
ApiError(
code = HttpURLConnection.HTTP_CONFLICT,
msg = ""
)
).message!!
)
}

@Test
fun testFesFailedApiError() {
setupAndClickSignInButton(genMockGoogleSignInAccountJson(EMAIL_FES_API_ERROR))
isDialogWithTextDisplayed(decorView, ERROR_TEXT)
onView(withText(R.string.retry))
.check(matches(isDisplayed()))
}

@Test
fun testFesFailedNoConnection() {
try {
changeConnectionState(false)
setupAndClickSignInButton(genMockGoogleSignInAccountJson(EMAIL_FES_NO_CONNECTION))
isDialogWithTextDisplayed(
decorView = decorView,
message = getResString(R.string.no_connection_or_server_is_not_reachable)
)
} finally {
changeConnectionState(true)
}
}

companion object {
private const val EMAIL_FES_NO_CONNECTION = "[email protected]"
private const val EMAIL_FES_SERVER_EXTERNAL_SERVICE =
"[email protected]"
private const val EMAIL_FES_SERVER_ENTERPRISE_SERVER =
"[email protected]"
private const val EMAIL_FES_API_ERROR = "[email protected]"
private const val ERROR_TEXT = "ERROR_TEXT"

private val FES_SUCCESS_RESPONSE = FesServerResponse(
apiError = null,
vendor = "FlowCrypt",
service = "enterprise-server",
orgId = "localhost",
version = "2021",
endUserApiVersion = "v1",
adminApiVersion = "v1"
)
}
}
Loading

0 comments on commit 12aecdc

Please sign in to comment.