mirror of
https://github.com/bitwarden/android.git
synced 2025-02-17 04:19:54 +03:00
Make CipherView.toAutofillCipherProvider public and add tests (#835)
This commit is contained in:
parent
5b854c17b7
commit
8d81b160f9
3 changed files with 144 additions and 37 deletions
|
@ -5,16 +5,14 @@ import android.content.Intent
|
|||
import com.bitwarden.core.CipherView
|
||||
import com.x8bit.bitwarden.data.autofill.builder.FilledDataBuilder
|
||||
import com.x8bit.bitwarden.data.autofill.builder.FilledDataBuilderImpl
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillCipher
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillRequest
|
||||
import com.x8bit.bitwarden.data.autofill.parser.AutofillParser
|
||||
import com.x8bit.bitwarden.data.autofill.provider.AutofillCipherProvider
|
||||
import com.x8bit.bitwarden.data.autofill.util.buildDataset
|
||||
import com.x8bit.bitwarden.data.autofill.util.createAutofillSelectionResultIntent
|
||||
import com.x8bit.bitwarden.data.autofill.util.getAutofillAssistStructureOrNull
|
||||
import com.x8bit.bitwarden.data.autofill.util.toAutofillAppInfo
|
||||
import com.x8bit.bitwarden.data.autofill.util.toAutofillCipherProvider
|
||||
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
|
||||
import com.x8bit.bitwarden.data.platform.util.subtitle
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
|
@ -87,37 +85,3 @@ private fun Activity.setResultAndFinish(resultIntent: Intent) {
|
|||
this.setResult(Activity.RESULT_OK, resultIntent)
|
||||
this.finish()
|
||||
}
|
||||
|
||||
private fun CipherView.toAutofillCipherProvider(): AutofillCipherProvider =
|
||||
object : AutofillCipherProvider {
|
||||
override suspend fun isVaultLocked(): Boolean = true
|
||||
|
||||
override suspend fun getCardAutofillCiphers(): List<AutofillCipher.Card> {
|
||||
val card = this@toAutofillCipherProvider.card ?: return emptyList()
|
||||
return listOf(
|
||||
AutofillCipher.Card(
|
||||
name = name,
|
||||
subtitle = subtitle.orEmpty(),
|
||||
cardholderName = card.cardholderName.orEmpty(),
|
||||
code = card.code.orEmpty(),
|
||||
expirationMonth = card.expMonth.orEmpty(),
|
||||
expirationYear = card.expYear.orEmpty(),
|
||||
number = card.number.orEmpty(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun getLoginAutofillCiphers(
|
||||
uri: String,
|
||||
): List<AutofillCipher.Login> {
|
||||
val login = this@toAutofillCipherProvider.login ?: return emptyList()
|
||||
return listOf(
|
||||
AutofillCipher.Login(
|
||||
name = name,
|
||||
password = login.password.orEmpty(),
|
||||
subtitle = subtitle.orEmpty(),
|
||||
username = login.username.orEmpty(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package com.x8bit.bitwarden.data.autofill.util
|
||||
|
||||
import com.bitwarden.core.CipherView
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillCipher
|
||||
import com.x8bit.bitwarden.data.autofill.provider.AutofillCipherProvider
|
||||
import com.x8bit.bitwarden.data.platform.util.subtitle
|
||||
|
||||
/**
|
||||
* Creates a single-item [AutofillCipherProvider] based on the given [CipherView].
|
||||
*/
|
||||
fun CipherView.toAutofillCipherProvider(): AutofillCipherProvider =
|
||||
object : AutofillCipherProvider {
|
||||
override suspend fun isVaultLocked(): Boolean = false
|
||||
|
||||
override suspend fun getCardAutofillCiphers(): List<AutofillCipher.Card> {
|
||||
val card = this@toAutofillCipherProvider.card ?: return emptyList()
|
||||
return listOf(
|
||||
AutofillCipher.Card(
|
||||
name = name,
|
||||
subtitle = subtitle.orEmpty(),
|
||||
cardholderName = card.cardholderName.orEmpty(),
|
||||
code = card.code.orEmpty(),
|
||||
expirationMonth = card.expMonth.orEmpty(),
|
||||
expirationYear = card.expYear.orEmpty(),
|
||||
number = card.number.orEmpty(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun getLoginAutofillCiphers(
|
||||
uri: String,
|
||||
): List<AutofillCipher.Login> {
|
||||
val login = this@toAutofillCipherProvider.login ?: return emptyList()
|
||||
return listOf(
|
||||
AutofillCipher.Login(
|
||||
name = name,
|
||||
password = login.password.orEmpty(),
|
||||
subtitle = subtitle.orEmpty(),
|
||||
username = login.username.orEmpty(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
package com.x8bit.bitwarden.data.autofill.util
|
||||
|
||||
import com.bitwarden.core.CipherType
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillCipher
|
||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class CipherViewExtensionsTest {
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `toAutofillCipherProvider should return a provider with the correct data for a Login type`() =
|
||||
runTest {
|
||||
val cipherView = createMockCipherView(
|
||||
number = 1,
|
||||
cipherType = CipherType.LOGIN,
|
||||
)
|
||||
|
||||
val autofillCipherProvider = cipherView.toAutofillCipherProvider()
|
||||
|
||||
assertFalse(autofillCipherProvider.isVaultLocked())
|
||||
assertEquals(
|
||||
emptyList<AutofillCipher.Card>(),
|
||||
autofillCipherProvider.getCardAutofillCiphers(),
|
||||
)
|
||||
assertEquals(
|
||||
listOf(
|
||||
AutofillCipher.Login(
|
||||
name = "mockName-1",
|
||||
subtitle = "mockUsername-1",
|
||||
password = "mockPassword-1",
|
||||
username = "mockUsername-1",
|
||||
),
|
||||
),
|
||||
autofillCipherProvider.getLoginAutofillCiphers(uri = "uri"),
|
||||
)
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `toAutofillCipherProvider should return a provider with the correct data for a Card type`() =
|
||||
runTest {
|
||||
val cipherView = createMockCipherView(
|
||||
number = 1,
|
||||
cipherType = CipherType.CARD,
|
||||
)
|
||||
|
||||
val autofillCipherProvider = cipherView.toAutofillCipherProvider()
|
||||
|
||||
assertFalse(autofillCipherProvider.isVaultLocked())
|
||||
assertEquals(
|
||||
emptyList<AutofillCipher.Login>(),
|
||||
autofillCipherProvider.getLoginAutofillCiphers(uri = "uri"),
|
||||
)
|
||||
assertEquals(
|
||||
listOf(
|
||||
AutofillCipher.Card(
|
||||
name = "mockName-1",
|
||||
subtitle = "mockBrand-1, *er-1",
|
||||
cardholderName = "mockCardholderName-1",
|
||||
code = "mockCode-1",
|
||||
expirationMonth = "mockExpMonth-1",
|
||||
expirationYear = "mockExpirationYear-1",
|
||||
number = "mockNumber-1",
|
||||
),
|
||||
),
|
||||
autofillCipherProvider.getCardAutofillCiphers(),
|
||||
)
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `toAutofillCipherProvider should return a provider with the correct data for any other type`() =
|
||||
runTest {
|
||||
CipherType
|
||||
.entries
|
||||
.filterNot { it == CipherType.CARD }
|
||||
.filterNot { it == CipherType.LOGIN }
|
||||
.forEach { cipherType ->
|
||||
val autofillCipherProvider = createMockCipherView(
|
||||
number = 1,
|
||||
cipherType = cipherType,
|
||||
)
|
||||
.toAutofillCipherProvider()
|
||||
|
||||
assertFalse(autofillCipherProvider.isVaultLocked())
|
||||
assertEquals(
|
||||
emptyList<AutofillCipher.Card>(),
|
||||
autofillCipherProvider.getCardAutofillCiphers(),
|
||||
)
|
||||
assertEquals(
|
||||
emptyList<AutofillCipher.Login>(),
|
||||
autofillCipherProvider.getLoginAutofillCiphers(uri = "uri"),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue