Skip to content

Commit

Permalink
Paywalls: PaywallData errors shouldn't make Offerings fail to d…
Browse files Browse the repository at this point in the history
…ecode

This also adds coverage for `PaywallData` deserialization inside of `Offering`
  • Loading branch information
NachoSoto committed Oct 27, 2023
1 parent 1dd51ae commit 7310663
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ internal abstract class OfferingParser {

val paywallDataJson = offeringJson.optJSONObject("paywall")

val paywallData = paywallDataJson?.let {
json.decodeFromString<PaywallData>(it.toString())
val paywallData: PaywallData? = paywallDataJson?.let {
try {
json.decodeFromString<PaywallData>(it.toString())
} catch (e: IllegalArgumentException) {
errorLog("Error deserializing paywall data", e)
null

Check warning on line 73 in purchases/src/main/kotlin/com/revenuecat/purchases/common/OfferingParser.kt

View check run for this annotation

Codecov / codecov/patch

purchases/src/main/kotlin/com/revenuecat/purchases/common/OfferingParser.kt#L71-L73

Added lines #L71 - L73 were not covered by tests
}
}

return if (availablePackages.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,63 @@ class OfferingsFactoryTest {
"'description': 'This is the base offering', " +
"'packages': []}]," +
"'current_offering_id': '$STUB_OFFERING_IDENTIFIER'}")
private val oneOfferingWithInvalidPaywallResponse = JSONObject(
"" +
"{" +
"'offerings': [" +
"{" +
"'identifier': '$STUB_OFFERING_IDENTIFIER', " +
"'description': 'This is the base offering', " +
"'packages': [" +
"{'identifier': '\$rc_monthly','platform_product_identifier': '$STUB_PRODUCT_IDENTIFIER'}" +
"]," +
"'paywall': 'not a paywall'" +
"}" +
"]," +
"'current_offering_id': '$STUB_OFFERING_IDENTIFIER'" +
"}"
)
private val oneOfferingWithPaywall = JSONObject(
"" +
"{" +
"'offerings': [" +
"{" +
"'identifier': '$STUB_OFFERING_IDENTIFIER', " +
"'description': 'This is the base offering', " +
"'packages': [" +
"{'identifier': '\$rc_monthly','platform_product_identifier': '$STUB_PRODUCT_IDENTIFIER'}" +
"]," +
"'paywall': {\n" +
" \"template_name\": \"1\",\n" +
" \"localized_strings\": {\n" +
" \"en_US\": {\n" +
" \"title\": \"Paywall\",\n" +
" \"call_to_action\": \"Purchase\",\n" +
" \"subtitle\": \"Description\"\n" +
" }\n" +
" },\n" +
" \"config\": {\n" +
" \"packages\": [\"\$rc_monthly\"],\n" +
" \"default_package\": \"\$rc_monthly\",\n" +
" \"images\": {},\n" +
" \"colors\": {\n" +
" \"light\": {\n" +
" \"background\": \"#FF00AA\",\n" +
" \"text_1\": \"#FF00AA22\",\n" +
" \"call_to_action_background\": \"#FF00AACC\",\n" +
" \"call_to_action_foreground\": \"#FF00AA\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"asset_base_url\": \"https://rc-paywalls.s3.amazonaws.com\",\n" +
" \"revision\": 7\n" +
"}" +
"}" +
"]," +
"'current_offering_id': '$STUB_OFFERING_IDENTIFIER'" +
"}"
)

private val oneOfferingResponse = JSONObject(ONE_OFFERINGS_RESPONSE)
private val oneOfferingInAppProductResponse = JSONObject(ONE_OFFERINGS_INAPP_PRODUCT_RESPONSE)

Expand Down Expand Up @@ -142,6 +199,42 @@ class OfferingsFactoryTest {
assertThat(offerings!![STUB_OFFERING_IDENTIFIER]!!.monthly!!.product).isNotNull
}

@Test
fun `createOfferings with paywall`() {
val productIds = listOf(productId)
mockStoreProduct(productIds, emptyList(), ProductType.SUBS)
mockStoreProduct(productIds, productIds, ProductType.INAPP)

var offerings: Offerings? = null
offeringsFactory.createOfferings(
oneOfferingWithPaywall,
{ fail("Error: $it") },
{ offerings = it }
)

assertThat(offerings).isNotNull
assertThat(offerings!!.current).isNotNull
assertThat(offerings!!.current?.paywall).isNotNull
}

@Test
fun `createOfferings does not fail if paywall is invalid`() {
val productIds = listOf(productId)
mockStoreProduct(productIds, emptyList(), ProductType.SUBS)
mockStoreProduct(productIds, productIds, ProductType.INAPP)

var offerings: Offerings? = null
offeringsFactory.createOfferings(
oneOfferingWithInvalidPaywallResponse,
{ fail("Error: $it") },
{ offerings = it }
)

assertThat(offerings).isNotNull
assertThat(offerings!!.current).isNotNull
assertThat(offerings!!.current?.paywall).isNull()
}

// region helpers

private fun mockStoreProduct(
Expand Down

0 comments on commit 7310663

Please sign in to comment.