AutofillSaveTest.kt

3.6 KB · raw

1package com.peachpasswords.autofill23import com.peachpasswords.vault.VaultEntry4import org.junit.Assert.assertEquals5import org.junit.Assert.assertNull6import org.junit.Assert.assertTrue7import org.junit.Test89class AutofillSaveTest {1011    private fun entry(12        title: String,13        url: String,14        username: String? = "june",15        password: String = "old",16    ) = VaultEntry(17        id = title,18        type = "login",19        title = title,20        username = username,21        password = password,22        url = url,23        createdAt = 1,24        modifiedAt = 1,25    )2627    @Test28    fun autofillSaveDraft_requiresOriginAndPassword() {29        assertNull(autofillSaveDraft(ParsedStructure(webDomain = "example.com")))30        assertNull(autofillSaveDraft(ParsedStructure(passwordValue = "pw")))31        assertNull(32            autofillSaveDraft(33                ParsedStructure(34                    webDomain = "example.com",35                    passwordValue = "pw",36                    credentialReleaseAllowed = false,37                ),38            ),39        )40    }4142    @Test43    fun planAutofillSave_addsNewWebLogin() {44        val draft = autofillSaveDraft(45            ParsedStructure(46                emailValue = "[email protected]",47                passwordValue = "new-secret",48                webDomain = "login.example.com",49            ),50        )!!5152        val plan = planAutofillSave(emptyList(), draft)5354        assertTrue(plan is AutofillSavePlan.Add)55        val entry = (plan as AutofillSavePlan.Add).entry56        assertEquals("login.example.com", entry.title)57        assertEquals("[email protected]", entry.email)58        assertEquals("new-secret", entry.password)59        assertEquals("https://login.example.com", entry.url)60    }6162    @Test63    fun planAutofillSave_updatesMatchingWebOriginAndIdentity() {64        val existing = entry("Example", "https://example.com/login", username = "june")65        val draft = AutofillSaveDraft(66            origin = "app.example.com",67            isWebOrigin = true,68            identity = "june",69            identityIsEmail = false,70            password = "new",71            totp = null,72        )7374        val plan = planAutofillSave(listOf(existing), draft)7576        assertTrue(plan is AutofillSavePlan.Update)77        val updated = (plan as AutofillSavePlan.Update).entry78        assertEquals(existing.id, updated.id)79        assertEquals("new", updated.password)80        assertEquals("june", updated.username)81    }8283    @Test84    fun planAutofillSave_doesNotUpdateSubstringLookalikeOrigin() {85        val existing = entry("GitHub", "https://github.com/login", username = "june")86        val draft = AutofillSaveDraft(87            origin = "notgithub.com",88            isWebOrigin = true,89            identity = "june",90            identityIsEmail = false,91            password = "phished",92            totp = null,93        )9495        val plan = planAutofillSave(listOf(existing), draft)9697        assertTrue(plan is AutofillSavePlan.Add)98    }99100    @Test101    fun planAutofillSave_updatesNativePackageOnlyOnExactOrigin() {102        val existing = entry("Native", "com.example.app", username = "june")103        val draft = AutofillSaveDraft(104            origin = "com.example.app",105            isWebOrigin = false,106            identity = "june",107            identityIsEmail = false,108            password = "new-native",109            totp = "123456",110        )111112        val plan = planAutofillSave(listOf(existing), draft)113114        assertTrue(plan is AutofillSavePlan.Update)115        val updated = (plan as AutofillSavePlan.Update).entry116        assertEquals("new-native", updated.password)117        assertEquals("123456", updated.totp)118    }119}