Skip to content

Commit

Permalink
Merge branch 'hotfix5.216.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
cmonfortep committed Oct 9, 2024
2 parents 8bf30a2 + e4d8ffe commit b3918b5
Show file tree
Hide file tree
Showing 107 changed files with 3,595 additions and 4,275 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-nightly-autofill.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
api-key: ${{ secrets.MOBILE_DEV_API_KEY }}
name: ${{ github.sha }}
app-file: apk/release.apk
android-api-level: 33
android-api-level: 30
workspace: .maestro
include-tags: autofillNoAuthTests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ import com.duckduckgo.app.usage.search.SearchCountDao
import com.duckduckgo.app.widget.ui.WidgetCapabilities
import com.duckduckgo.appbuildconfig.api.AppBuildConfig
import com.duckduckgo.autofill.api.AutofillCapabilityChecker
import com.duckduckgo.autofill.api.AutofillWebMessageRequest
import com.duckduckgo.autofill.api.domain.app.LoginCredentials
import com.duckduckgo.autofill.api.email.EmailManager
import com.duckduckgo.autofill.api.passwordgeneration.AutomaticSavedLoginsMonitor
Expand Down Expand Up @@ -3790,22 +3789,59 @@ class BrowserTabViewModelTest {
assertTrue(browserViewState().isEmailSignedIn)
}

@Test
fun whenEmailSignOutEventThenEmailSignEventCommandSent() = runTest {
emailStateFlow.emit(true)
emailStateFlow.emit(false)

assertCommandIssuedTimes<Command.EmailSignEvent>(2)
}

@Test
fun whenEmailIsSignedInThenEmailSignEventCommandSent() = runTest {
emailStateFlow.emit(true)

assertCommandIssued<Command.EmailSignEvent>()
}

@Test
fun whenConsumeAliasThenInjectAddressCommandSent() {
whenever(mockEmailManager.getAlias()).thenReturn("alias")

testee.usePrivateDuckAddress("", "alias")

assertCommandIssued<Command.InjectEmailAddress> {
assertEquals("alias", this.duckAddress)
}
}

@Test
fun whenUseAddressThenInjectAddressCommandSent() {
whenever(mockEmailManager.getEmailAddress()).thenReturn("address")

testee.usePersonalDuckAddress("", "address")

assertCommandIssued<Command.InjectEmailAddress> {
assertEquals("address", this.duckAddress)
}
}

@Test
fun whenShowEmailTooltipIfAddressExistsThenShowEmailTooltipCommandSent() {
whenever(mockEmailManager.getEmailAddress()).thenReturn("address")

testee.showEmailProtectionChooseEmailPrompt(urlRequest())
testee.showEmailProtectionChooseEmailPrompt()

assertCommandIssued<Command.ShowEmailProtectionChooseEmailPrompt> {
assertEquals("address", this.duckAddress)
assertEquals("address", this.address)
}
}

@Test
fun whenShowEmailTooltipIfAddressDoesNotExistThenCommandNotSent() {
whenever(mockEmailManager.getEmailAddress()).thenReturn(null)

testee.showEmailProtectionChooseEmailPrompt(urlRequest())
testee.showEmailProtectionChooseEmailPrompt()

assertCommandNotIssued<Command.ShowEmailProtectionChooseEmailPrompt>()
}
Expand Down Expand Up @@ -4406,6 +4442,16 @@ class BrowserTabViewModelTest {
assertShowHistoryCommandSent(expectedStackSize = 10)
}

@Test
fun whenReturnNoCredentialsWithPageThenEmitCancelIncomingAutofillRequestCommand() = runTest {
val url = "originalurl.com"
testee.returnNoCredentialsWithPage(url)

assertCommandIssued<Command.CancelIncomingAutofillRequest> {
assertEquals(url, this.url)
}
}

@Test
fun whenOnAutoconsentResultReceivedThenSiteUpdated() {
updateUrl("http://www.example.com/", "http://twitter.com/explore", true)
Expand Down Expand Up @@ -5992,8 +6038,6 @@ class BrowserTabViewModelTest {
}
}

private fun urlRequest() = AutofillWebMessageRequest("", "", "")

private fun givenLoginDetected(domain: String) = LoginDetected(authLoginDomain = "", forwardedToDomain = domain)

private fun givenCurrentSite(domain: String): Site {
Expand Down Expand Up @@ -6146,6 +6190,10 @@ class BrowserTabViewModelTest {
fun anyUri(): Uri = any()

class FakeCapabilityChecker(var enabled: Boolean) : AutofillCapabilityChecker {
override suspend fun canAccessCredentialManagementScreen(): Boolean = enabled
override suspend fun isAutofillEnabledByConfiguration(url: String) = enabled
override suspend fun canInjectCredentialsToWebView(url: String) = enabled
override suspend fun canSaveCredentialsFromWebView(url: String) = enabled
override suspend fun canGeneratePasswordFromWebView(url: String) = enabled
override suspend fun canAccessCredentialManagementScreen() = enabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.duckduckgo.app.pixels.AppPixelName
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.app.statistics.pixels.Pixel.PixelParameter.LOADING_BAR_EXPERIMENT
import com.duckduckgo.autoconsent.api.Autoconsent
import com.duckduckgo.autofill.api.BrowserAutofill
import com.duckduckgo.autofill.api.InternalTestUserChecker
import com.duckduckgo.browser.api.JsInjectorPlugin
import com.duckduckgo.browser.api.WebViewVersionProvider
Expand Down Expand Up @@ -117,6 +118,7 @@ class BrowserWebViewClientTest {
private val trustedCertificateStore: TrustedCertificateStore = mock()
private val webViewHttpAuthStore: WebViewHttpAuthStore = mock()
private val thirdPartyCookieManager: ThirdPartyCookieManager = mock()
private val browserAutofillConfigurator: BrowserAutofill.Configurator = mock()
private val webResourceRequest: WebResourceRequest = mock()
private val webResourceError: WebResourceError = mock()
private val ampLinks: AmpLinks = mock()
Expand Down Expand Up @@ -154,6 +156,7 @@ class BrowserWebViewClientTest {
thirdPartyCookieManager,
TestScope(),
coroutinesTestRule.testDispatcherProvider,
browserAutofillConfigurator,
ampLinks,
printInjector,
internalTestUserChecker,
Expand Down Expand Up @@ -367,6 +370,13 @@ class BrowserWebViewClientTest {
verify(cookieManager).flush()
}

@UiThreadTest
@Test
fun whenOnPageStartedCalledThenInjectEmailAutofillJsCalled() {
testee.onPageStarted(webView, null, null)
verify(browserAutofillConfigurator).configureAutofillForCurrentPage(webView, null)
}

@UiThreadTest
@Test
fun whenShouldOverrideThrowsExceptionThenRecordException() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* Copyright (c) 2022 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.email

import android.annotation.SuppressLint
import android.webkit.WebView
import androidx.test.annotation.UiThreadTest
import androidx.test.filters.SdkSuppress
import androidx.test.platform.app.InstrumentationRegistry
import com.duckduckgo.app.autofill.DefaultEmailProtectionJavascriptInjector
import com.duckduckgo.app.autofill.EmailProtectionJavascriptInjector
import com.duckduckgo.app.browser.DuckDuckGoUrlDetectorImpl
import com.duckduckgo.app.browser.R
import com.duckduckgo.autofill.api.Autofill
import com.duckduckgo.autofill.api.AutofillFeature
import com.duckduckgo.autofill.api.email.EmailManager
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.feature.toggles.api.FakeFeatureToggleFactory
import com.duckduckgo.feature.toggles.api.Toggle
import java.io.BufferedReader
import org.junit.Before
import org.junit.Test
import org.mockito.kotlin.*

class EmailInjectorJsTest {

private val mockEmailManager: EmailManager = mock()
private val mockDispatcherProvider: DispatcherProvider = mock()
private val autofillFeature = FakeFeatureToggleFactory.create(AutofillFeature::class.java)
private val mockAutofill: Autofill = mock()
private val javascriptInjector: EmailProtectionJavascriptInjector = DefaultEmailProtectionJavascriptInjector()

lateinit var testee: EmailInjectorJs

@Before
fun setup() {
testee =
EmailInjectorJs(
mockEmailManager,
DuckDuckGoUrlDetectorImpl(),
mockDispatcherProvider,
autofillFeature,
javascriptInjector,
mockAutofill,
)
whenever(mockAutofill.isAnException(any())).thenReturn(false)
}

@SuppressLint("DenyListedApi")
@UiThreadTest
@Test
@SdkSuppress(minSdkVersion = 24)
fun whenInjectAddressThenInjectJsCodeReplacingTheAlias() {
val address = "address"
val jsToEvaluate = getAliasJsToEvaluate().replace("%s", address)
val webView = spy(WebView(InstrumentationRegistry.getInstrumentation().targetContext))
autofillFeature.self().setRawStoredState(Toggle.State(enable = true))

testee.injectAddressInEmailField(webView, address, "https://example.com")

verify(webView).evaluateJavascript(jsToEvaluate, null)
}

@SuppressLint("DenyListedApi")
@UiThreadTest
@Test
@SdkSuppress(minSdkVersion = 24)
fun whenInjectAddressAndFeatureIsDisabledThenJsCodeNotInjected() {
autofillFeature.self().setRawStoredState(Toggle.State(enable = true))

val address = "address"
val webView = spy(WebView(InstrumentationRegistry.getInstrumentation().targetContext))

testee.injectAddressInEmailField(webView, address, "https://example.com")

verify(webView, never()).evaluateJavascript(any(), any())
}

@UiThreadTest
@Test
@SdkSuppress(minSdkVersion = 24)
fun whenInjectAddressAndUrlIsAnExceptionThenJsCodeNotInjected() {
whenever(mockAutofill.isAnException(any())).thenReturn(true)

val address = "address"
val webView = spy(WebView(InstrumentationRegistry.getInstrumentation().targetContext))

testee.injectAddressInEmailField(webView, address, "https://example.com")

verify(webView, never()).evaluateJavascript(any(), any())
}

@UiThreadTest
@Test
@SdkSuppress(minSdkVersion = 24)
fun whenNotifyWebAppSignEventAndUrlIsNotFromDuckDuckGoAndEmailIsSignedInThenDoNotEvaluateJsCode() {
whenever(mockEmailManager.isSignedIn()).thenReturn(true)
val jsToEvaluate = getNotifySignOutJsToEvaluate()
val webView = spy(WebView(InstrumentationRegistry.getInstrumentation().targetContext))

testee.notifyWebAppSignEvent(webView, "https://example.com")

verify(webView, never()).evaluateJavascript(jsToEvaluate, null)
}

@UiThreadTest
@Test
@SdkSuppress(minSdkVersion = 24)
fun whenNotifyWebAppSignEventAndUrlIsNotFromDuckDuckGoAndEmailIsNotSignedInThenDoNotEvaluateJsCode() {
whenever(mockEmailManager.isSignedIn()).thenReturn(false)
val jsToEvaluate = getNotifySignOutJsToEvaluate()
val webView = spy(WebView(InstrumentationRegistry.getInstrumentation().targetContext))

testee.notifyWebAppSignEvent(webView, "https://example.com")

verify(webView, never()).evaluateJavascript(jsToEvaluate, null)
}

@SuppressLint("DenyListedApi")
@UiThreadTest
@Test
@SdkSuppress(minSdkVersion = 24)
fun whenNotifyWebAppSignEventAndUrlIsFromDuckDuckGoAndFeatureIsDisabledAndEmailIsNotSignedInThenDoNotEvaluateJsCode() {
whenever(mockEmailManager.isSignedIn()).thenReturn(false)
autofillFeature.self().setRawStoredState(Toggle.State(enable = false))

val jsToEvaluate = getNotifySignOutJsToEvaluate()
val webView = spy(WebView(InstrumentationRegistry.getInstrumentation().targetContext))

testee.notifyWebAppSignEvent(webView, "https://duckduckgo.com/email")

verify(webView, never()).evaluateJavascript(jsToEvaluate, null)
}

@SuppressLint("DenyListedApi")
@UiThreadTest
@Test
@SdkSuppress(minSdkVersion = 24)
fun whenNotifyWebAppSignEventAndUrlIsFromDuckDuckGoAndFeatureIsEnabledAndEmailIsNotSignedInThenEvaluateJsCode() {
whenever(mockEmailManager.isSignedIn()).thenReturn(false)
autofillFeature.self().setRawStoredState(Toggle.State(enable = true))

val jsToEvaluate = getNotifySignOutJsToEvaluate()
val webView = spy(WebView(InstrumentationRegistry.getInstrumentation().targetContext))

testee.notifyWebAppSignEvent(webView, "https://duckduckgo.com/email")

verify(webView).evaluateJavascript(jsToEvaluate, null)
}

private fun getAliasJsToEvaluate(): String {
val js = InstrumentationRegistry.getInstrumentation().targetContext.resources.openRawResource(R.raw.inject_alias)
.bufferedReader()
.use { it.readText() }
return "javascript:$js"
}

private fun getNotifySignOutJsToEvaluate(): String {
val js =
InstrumentationRegistry.getInstrumentation().targetContext.resources.openRawResource(R.raw.signout_autofill)
.bufferedReader()
.use { it.readText() }
return "javascript:$js"
}

private fun readResource(resourceName: String): BufferedReader? {
return javaClass.classLoader?.getResource(resourceName)?.openStream()?.bufferedReader()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.autofill

import android.content.Context
import com.duckduckgo.app.browser.R
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import dagger.SingleInstanceIn
import javax.inject.Inject

@SingleInstanceIn(AppScope::class)
@ContributesBinding(AppScope::class)
class DefaultEmailProtectionJavascriptInjector @Inject constructor() : EmailProtectionJavascriptInjector {
private lateinit var aliasFunctions: String
private lateinit var signOutFunctions: String

override fun getAliasFunctions(
context: Context,
alias: String?,
): String {
if (!this::aliasFunctions.isInitialized) {
aliasFunctions = context.resources.openRawResource(R.raw.inject_alias).bufferedReader().use { it.readText() }
}
return aliasFunctions.replace("%s", alias.orEmpty())
}

override fun getSignOutFunctions(
context: Context,
): String {
if (!this::signOutFunctions.isInitialized) {
signOutFunctions = context.resources.openRawResource(R.raw.signout_autofill).bufferedReader().use { it.readText() }
}
return signOutFunctions
}
}
Loading

0 comments on commit b3918b5

Please sign in to comment.