AutofillSaveTest.kt

3.4 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    }3233    @Test34    fun planAutofillSave_addsNewWebLogin() {35        val draft = autofillSaveDraft(36            ParsedStructure(37                emailValue = "[email protected]",38                passwordValue = "new-secret",39                webDomain = "login.example.com",40            ),41        )!!4243        val plan = planAutofillSave(emptyList(), draft)4445        assertTrue(plan is AutofillSavePlan.Add)46        val entry = (plan as AutofillSavePlan.Add).entry47        assertEquals("login.example.com", entry.title)48        assertEquals("[email protected]", entry.email)49        assertEquals("new-secret", entry.password)50        assertEquals("https://login.example.com", entry.url)51    }5253    @Test54    fun planAutofillSave_updatesMatchingWebOriginAndIdentity() {55        val existing = entry("Example", "https://example.com/login", username = "june")56        val draft = AutofillSaveDraft(57            origin = "app.example.com",58            isWebOrigin = true,59            identity = "june",60            identityIsEmail = false,61            password = "new",62            totp = null,63        )6465        val plan = planAutofillSave(listOf(existing), draft)6667        assertTrue(plan is AutofillSavePlan.Update)68        val updated = (plan as AutofillSavePlan.Update).entry69        assertEquals(existing.id, updated.id)70        assertEquals("new", updated.password)71        assertEquals("june", updated.username)72    }7374    @Test75    fun planAutofillSave_doesNotUpdateSubstringLookalikeOrigin() {76        val existing = entry("GitHub", "https://github.com/login", username = "june")77        val draft = AutofillSaveDraft(78            origin = "notgithub.com",79            isWebOrigin = true,80            identity = "june",81            identityIsEmail = false,82            password = "phished",83            totp = null,84        )8586        val plan = planAutofillSave(listOf(existing), draft)8788        assertTrue(plan is AutofillSavePlan.Add)89    }9091    @Test92    fun planAutofillSave_updatesNativePackageOnlyOnExactOrigin() {93        val existing = entry("Native", "com.example.app", username = "june")94        val draft = AutofillSaveDraft(95            origin = "com.example.app",96            isWebOrigin = false,97            identity = "june",98            identityIsEmail = false,99            password = "new-native",100            totp = "123456",101        )102103        val plan = planAutofillSave(listOf(existing), draft)104105        assertTrue(plan is AutofillSavePlan.Update)106        val updated = (plan as AutofillSavePlan.Update).entry107        assertEquals("new-native", updated.password)108        assertEquals("123456", updated.totp)109    }110}