-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSubmitPublicKeyAfterCreationNonGoogleAccountFlowTest.kt
More file actions
156 lines (140 loc) · 5.75 KB
/
SubmitPublicKeyAfterCreationNonGoogleAccountFlowTest.kt
File metadata and controls
156 lines (140 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com
* Contributors: denbond7
*/
package com.flowcrypt.email.ui
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.clearText
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.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.activityScenarioRule
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.api.email.IMAPStoreConnection
import com.flowcrypt.email.api.email.JavaEmailConstants
import com.flowcrypt.email.junit.annotations.DependsOnMailServer
import com.flowcrypt.email.junit.annotations.FlowCryptTestSettings
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.activity.MainActivity
import com.flowcrypt.email.ui.base.BaseSignTest
import com.flowcrypt.email.util.AccountDaoManager
import jakarta.mail.Flags
import jakarta.mail.Folder
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequest
import org.eclipse.angus.mail.imap.IMAPFolder
import org.junit.Assert.assertTrue
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
import java.util.UUID
import java.util.concurrent.TimeUnit
/**
* https://github.com/FlowCrypt/flowcrypt-android/issues/1984
*
* @author Denys Bondarenko
*/
@DependsOnMailServer
@MediumTest
@RunWith(AndroidJUnit4::class)
@FlowCryptTestSettings(useIntents = true, useCommonIdling = false)
class SubmitPublicKeyAfterCreationNonGoogleAccountFlowTest : BaseSignTest() {
override val activityScenarioRule = activityScenarioRule<MainActivity>()
private val userWithoutBackups = AccountDaoManager.getUserWithoutBackup()
private var isSubmitPubKeyCalled = false
val mockWebServerRule = FlowCryptMockWebServerRule(
TestConstants.MOCK_WEB_SERVER_PORT,
object : okhttp3.mockwebserver.Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
if (request.path?.startsWith("/attester/pub", ignoreCase = true) == true) {
val lastSegment = request.requestUrl?.pathSegments?.lastOrNull()
when {
userWithoutBackups.email.equals(lastSegment, true) -> {
isSubmitPubKeyCalled = true
return MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
}
}
}
return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
})
@get:Rule
var ruleChain: TestRule = RuleChain
.outerRule(RetryRule.DEFAULT)
.around(ClearAppSettingsRule())
.around(GrantPermissionRuleChooser.grant(android.Manifest.permission.POST_NOTIFICATIONS))
.around(mockWebServerRule)
.around(activityScenarioRule)
.around(ScreenshotTestRule())
@Test
fun testCallSubmitPubKeyAfterKeyCreation() {
try {
onView(withId(R.id.buttonOtherEmailProvider))
.check(matches(isDisplayed()))
.perform(click())
onView(withId(R.id.editTextEmail))
.perform(clearText(), typeText(userWithoutBackups.email), closeSoftKeyboard())
onView(withId(R.id.editTextPassword))
.perform(clearText(), typeText(userWithoutBackups.password), closeSoftKeyboard())
onView(withId(R.id.buttonTryToConnect))
.check(matches(isDisplayed()))
.perform(click())
waitForObjectWithText(
getResString(R.string.create_a_new_key).uppercase(),
TimeUnit.SECONDS.toMillis(5)
)
onView(withId(R.id.buttonCreateNewKey))
.check(matches(isDisplayed()))
.perform(click())
val passphrase = UUID.randomUUID().toString() + UUID.randomUUID().toString()
onView(withId(R.id.editTextKeyPassword))
.check(matches(isDisplayed()))
.perform(replaceText(passphrase))
Thread.sleep(TimeUnit.SECONDS.toMillis(1))
onView(withId(R.id.buttonSetPassPhrase))
.check(matches(isDisplayed()))
.perform(click())
onView(withId(R.id.editTextKeyPasswordSecond))
.check(matches(isDisplayed()))
.perform(replaceText(passphrase))
onView(withId(R.id.buttonConfirmPassPhrases))
.check(matches(isDisplayed()))
.perform(click())
//need to wait while a key is creating
waitForObjectWithText(JavaEmailConstants.FOLDER_INBOX, TimeUnit.SECONDS.toMillis(10))
assertTrue(isSubmitPubKeyCalled)
} finally {
runBlocking {
val imapStoreConnection = IMAPStoreConnection(getTargetContext(), userWithoutBackups)
imapStoreConnection.connect()
imapStoreConnection.store.use { store ->
store.getFolder("INBOX").use { folder ->
val imapFolder = (folder as IMAPFolder).apply { open(Folder.READ_WRITE) }
val msgs =
imapFolder.messages.filter { it.subject == "Your FlowCrypt Backup" }.toTypedArray()
if (msgs.isNotEmpty()) {
imapFolder.setFlags(msgs, Flags(Flags.Flag.DELETED), true)
}
folder.expunge()
}
}
}
}
}
}