Skip to content

Commit

Permalink
Make shouldConsume nullable and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonidero committed May 9, 2024
1 parent c5faffb commit 965ff7e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ internal fun buildPostReceiptResponse(result: HTTPResult) = PostReceiptResponse(
productInfoByProductId = result.body.optJSONObject("purchased_products")?.let {
it.toMap<JSONObject>().mapValues { (_, value) ->
PostReceiptProductInfo(

Check warning on line 12 in purchases/src/main/kotlin/com/revenuecat/purchases/common/networking/PostReceiptResponse.kt

View check run for this annotation

Codecov / codecov/patch

purchases/src/main/kotlin/com/revenuecat/purchases/common/networking/PostReceiptResponse.kt#L11-L12

Added lines #L11 - L12 were not covered by tests
shouldConsume = value.optBoolean("should_consume"),
shouldConsume = value.takeIf { value.has("should_consume") }?.optBoolean("should_consume"),
)
}
},
body = result.body,
)

internal data class PostReceiptProductInfo(
val shouldConsume: Boolean,
val shouldConsume: Boolean?,
)

internal data class PostReceiptResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,114 @@ class PostReceiptHelperTest {

// endregion paywall data

// region purchased products data

@Test
fun `postTransactionAndConsumeIfNeeded tries to consume products if product data indicates it should consume`() {
mockPostReceiptSuccess(
purchasedProductsInfo = mapOf("lifetime_product" to true)
)

postReceiptHelper.postTransactionAndConsumeIfNeeded(
purchase = mockStoreTransaction,
storeProduct = mockStoreProduct,
isRestore = false,
appUserID = appUserID,
initiationSource = initiationSource,
onSuccess = { _, _ -> },
onError = { _, _ -> fail("Should succeed") }
)

verify(exactly = 1) {
billing.consumeAndSave(
finishTransactions = true,
purchase = mockStoreTransaction,
shouldConsume = true,
initiationSource = initiationSource
)
}
}

@Test
fun `postTransactionAndConsumeIfNeeded does not try to consume products if product data indicates it should not consume`() {
mockPostReceiptSuccess(
purchasedProductsInfo = mapOf("lifetime_product" to false)
)

postReceiptHelper.postTransactionAndConsumeIfNeeded(
purchase = mockStoreTransaction,
storeProduct = mockStoreProduct,
isRestore = false,
appUserID = appUserID,
initiationSource = initiationSource,
onSuccess = { _, _ -> },
onError = { _, _ -> fail("Should succeed") }
)

verify(exactly = 1) {
billing.consumeAndSave(
finishTransactions = true,
purchase = mockStoreTransaction,
shouldConsume = false,
initiationSource = initiationSource
)
}
}

@Test
fun `postTransactionAndConsumeIfNeeded tries to consume products if product data not available`() {
mockPostReceiptSuccess(
purchasedProductsInfo = mapOf()
)

postReceiptHelper.postTransactionAndConsumeIfNeeded(
purchase = mockStoreTransaction,
storeProduct = mockStoreProduct,
isRestore = false,
appUserID = appUserID,
initiationSource = initiationSource,
onSuccess = { _, _ -> },
onError = { _, _ -> fail("Should succeed") }
)

verify(exactly = 1) {
billing.consumeAndSave(
finishTransactions = true,
purchase = mockStoreTransaction,
shouldConsume = true,
initiationSource = initiationSource
)
}
}

@Test
fun `postTransactionAndConsumeIfNeeded tries to consume products if product data product id does not match transactions`() {
mockPostReceiptSuccess(
purchasedProductsInfo = mapOf("other_product" to false)
)

postReceiptHelper.postTransactionAndConsumeIfNeeded(
purchase = mockStoreTransaction,
storeProduct = mockStoreProduct,
isRestore = false,
appUserID = appUserID,
initiationSource = initiationSource,
onSuccess = { _, _ -> },
onError = { _, _ -> fail("Should succeed") }
)

verify(exactly = 1) {
billing.consumeAndSave(
finishTransactions = true,
purchase = mockStoreTransaction,
shouldConsume = true,
initiationSource = initiationSource
)
}
}

// endregion purchased products data

// region helpers

private enum class PostType {
Expand Down

0 comments on commit 965ff7e

Please sign in to comment.