forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue mozilla-mobile#9838: Introduce CreditCardValidationDelegate and…
… implement onCreditCardSave in GeckoCreditCardsAddressesStorageDelegate
- Loading branch information
1 parent
5aad028
commit d1f4baa
Showing
6 changed files
with
282 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...main/java/mozilla/components/service/sync/autofill/DefaultCreditCardValidationDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.sync.autofill | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import mozilla.components.concept.storage.CreditCard | ||
import mozilla.components.concept.storage.CreditCardValidationDelegate | ||
import mozilla.components.concept.storage.CreditCardValidationDelegate.Result | ||
import mozilla.components.concept.storage.CreditCardsAddressesStorage | ||
|
||
/** | ||
* A delegate that will check against the [CreditCardsAddressesStorage] to determine if a given | ||
* [CreditCard] can be persisted and returns information about why it can or cannot. | ||
*/ | ||
class DefaultCreditCardValidationDelegate( | ||
private val storage: Lazy<CreditCardsAddressesStorage>, | ||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO) | ||
) : CreditCardValidationDelegate { | ||
|
||
override fun validate(newCreditCard: CreditCard): Deferred<Result> { | ||
return scope.async { | ||
val creditCards = storage.value.getAllCreditCards() | ||
|
||
val foundCreditCard = if (creditCards.isEmpty()) { | ||
// No credit cards exist in the storage, create a new credit card to the storage. | ||
null | ||
} else { | ||
// Attempt to find a credit card with a matching guid or card number with the | ||
// new credit card. If an existing credit card is found, update it with the new | ||
// credit card entry. Otherwise, create a new credit card entry in the storage. | ||
creditCards.find { it.guid == newCreditCard.guid || it.cardNumber == newCreditCard.cardNumber } | ||
} | ||
|
||
if (foundCreditCard == null) Result.CanBeCreated else Result.CanBeUpdated( | ||
foundCreditCard | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
.../java/mozilla/components/service/sync/autofill/DefaultCreditCardValidationDelegateTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.sync.autofill | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.test.TestCoroutineScope | ||
import mozilla.components.concept.storage.CreditCard | ||
import mozilla.components.concept.storage.CreditCardValidationDelegate.Result | ||
import mozilla.components.concept.storage.UpdatableCreditCardFields | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@ExperimentalCoroutinesApi | ||
@RunWith(AndroidJUnit4::class) | ||
class DefaultCreditCardValidationDelegateTest { | ||
|
||
private lateinit var validationDelegate: DefaultCreditCardValidationDelegate | ||
private lateinit var scope: TestCoroutineScope | ||
|
||
private val storage = AutofillCreditCardsAddressesStorage(testContext) | ||
|
||
init { | ||
testContext.getDatabasePath(AUTOFILL_DB_NAME)!!.parentFile!!.mkdirs() | ||
} | ||
|
||
@Before | ||
fun before() = runBlocking { | ||
scope = TestCoroutineScope() | ||
validationDelegate = | ||
DefaultCreditCardValidationDelegate(storage = lazy { storage }, scope = scope) | ||
} | ||
|
||
@Test | ||
fun `WHEN no credit cards exist in the storage, THEN add the new credit card to storage`() = | ||
runBlocking { | ||
val newCreditCard = createCreditCard(guid = "1") | ||
val result = validationDelegate.validate(newCreditCard).await() | ||
|
||
assertEquals(Result.CanBeCreated, result) | ||
} | ||
|
||
@Test | ||
fun `WHEN existing credit card matches by guid and card number, THEN update the credit card in storage`() = | ||
runBlocking { | ||
val creditCardFields = UpdatableCreditCardFields( | ||
billingName = "Pineapple Orange", | ||
cardNumber = "4111111111111111", | ||
expiryMonth = 2, | ||
expiryYear = 2028, | ||
cardType = "visa" | ||
) | ||
val creditCard = storage.addCreditCard(creditCardFields) | ||
val result = validationDelegate.validate(creditCard).await() | ||
|
||
assertEquals(Result.CanBeUpdated(creditCard), result) | ||
} | ||
|
||
@Test | ||
fun `WHEN existing credit card matches by guid only, THEN update the credit card in storage`() = | ||
runBlocking { | ||
val creditCardFields = UpdatableCreditCardFields( | ||
billingName = "Pineapple Orange", | ||
cardNumber = "4111111111111113", | ||
expiryMonth = 2, | ||
expiryYear = 2028, | ||
cardType = "visa" | ||
) | ||
val creditCard = storage.addCreditCard(creditCardFields) | ||
val newCreditCard = createCreditCard(guid = creditCard.guid) | ||
val result = validationDelegate.validate(newCreditCard).await() | ||
|
||
assertEquals(Result.CanBeUpdated(creditCard), result) | ||
} | ||
|
||
@Test | ||
fun `WHEN existing credit card matches by card number only, THEN update the credit card in storage`() = | ||
runBlocking { | ||
val creditCardFields = UpdatableCreditCardFields( | ||
billingName = "Pineapple Orange", | ||
cardNumber = "4111111111111114", | ||
expiryMonth = 2, | ||
expiryYear = 2028, | ||
cardType = "visa" | ||
) | ||
val creditCard = storage.addCreditCard(creditCardFields) | ||
val newCreditCard = createCreditCard( | ||
guid = creditCard.guid + 1, | ||
billingName = creditCard.billingName, | ||
cardNumber = creditCard.cardNumber, | ||
expiryMonth = creditCard.expiryMonth, | ||
expiryYear = creditCard.expiryYear, | ||
cardType = creditCard.cardType | ||
) | ||
val result = validationDelegate.validate(newCreditCard).await() | ||
|
||
assertEquals(Result.CanBeUpdated(creditCard), result) | ||
} | ||
|
||
@Test | ||
fun `WHEN existing credit card does not match by guid and card number, THEN add the new credit card to storage`() = | ||
runBlocking { | ||
val newCreditCard = createCreditCard(guid = "2") | ||
val creditCardFields = UpdatableCreditCardFields( | ||
billingName = "Pineapple Orange", | ||
cardNumber = "4111111111111116", | ||
expiryMonth = 2, | ||
expiryYear = 2028, | ||
cardType = "visa" | ||
) | ||
storage.addCreditCard(creditCardFields) | ||
|
||
val result = validationDelegate.validate(newCreditCard).await() | ||
|
||
assertEquals(Result.CanBeCreated, result) | ||
} | ||
} | ||
|
||
fun createCreditCard( | ||
guid: String = "id", | ||
billingName: String = "Banana Apple", | ||
cardNumber: String = "4111111111111110", | ||
expiryMonth: Long = 1, | ||
expiryYear: Long = 2030, | ||
cardType: String = "amex" | ||
) = CreditCard( | ||
guid = guid, | ||
billingName = billingName, | ||
cardNumber = cardNumber, | ||
expiryMonth = expiryMonth, | ||
expiryYear = expiryYear, | ||
cardType = cardType | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters