Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trusted entitlements: Add integration tests #1071

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.revenuecat.purchases.trustedentitlements

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.revenuecat.purchases.BasePurchasesIntegrationTest
import com.revenuecat.purchases.CacheFetchPolicy
import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.EntitlementVerificationMode
import com.revenuecat.purchases.ExperimentalPreviewRevenueCatPurchasesAPI
import com.revenuecat.purchases.PurchaseParams
import com.revenuecat.purchases.Purchases
import com.revenuecat.purchases.VerificationResult
import com.revenuecat.purchases.factories.StoreProductFactory
import com.revenuecat.purchases.factories.StoreTransactionFactory
import com.revenuecat.purchases.forceSigningErrors
import com.revenuecat.purchases.getCustomerInfoWith
import com.revenuecat.purchases.helpers.mockQueryProductDetails
import com.revenuecat.purchases.purchaseWith
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalPreviewRevenueCatPurchasesAPI::class)
@RunWith(AndroidJUnit4::class)
class TrustedEntitlementsInformationalModeIntegrationTest : BasePurchasesIntegrationTest() {

@Before
fun setup() {
ensureBlockFinishes { latch ->
setUpTest(entitlementVerificationMode = EntitlementVerificationMode.INFORMATIONAL) {
latch.countDown()
}
}
}

@Test
fun initialCustomerInfoIsVerified() {
var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't wait to see these with coroutines 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty straightforward and neat 💅 #1077

Purchases.sharedInstance.getCustomerInfoWith(
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo = it
latch.countDown()
},
)
}

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.VERIFIED)
}

@Test
fun canPurchaseProductWithVerificationStatusCorrect() {
tonidero marked this conversation as resolved.
Show resolved Hide resolved
val storeProduct = StoreProductFactory.createGoogleStoreProduct()
val storeTransaction = StoreTransactionFactory.createStoreTransaction()
mockBillingAbstract.mockQueryProductDetails(queryProductDetailsSubsReturn = listOf(storeProduct))

var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.purchaseWith(
purchaseParams = PurchaseParams.Builder(activity, storeProduct).build(),
onError = { error, _ -> fail("Purchase should be successful. Error: ${error.message}") },
onSuccess = { _, customerInfo ->
receivedCustomerInfo = customerInfo
latch.countDown()
},
)
latestPurchasesUpdatedListener!!.onPurchasesUpdated(listOf(storeTransaction))
}

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.VERIFIED)
assertThat(receivedCustomerInfo?.entitlements?.all).isNotEmpty
assertThat(
receivedCustomerInfo?.entitlements?.all?.values?.first()?.verification,
).isEqualTo(VerificationResult.VERIFIED)
}

@Test
fun verificationChangesAfterSuccessIsNotified() {
var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.getCustomerInfoWith(
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo = it
latch.countDown()
},
)
}

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.VERIFIED)

Purchases.sharedInstance.forceSigningErrors = true

var receivedCustomerInfo2: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.getCustomerInfoWith(
fetchPolicy = CacheFetchPolicy.FETCH_CURRENT,
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo2 = it
latch.countDown()
},
)
}

assertThat(receivedCustomerInfo2).isNotNull
assertThat(receivedCustomerInfo2?.entitlements?.verification).isEqualTo(VerificationResult.FAILED)
}

@Test
fun initialCustomerInfoFailsToVerify() {
Purchases.sharedInstance.forceSigningErrors = true

var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.getCustomerInfoWith(
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo = it
latch.countDown()
},
)
}

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.FAILED)
}

@Test
fun canPurchaseProductFailingToVerifyStatus() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice 👍🏻

Purchases.sharedInstance.forceSigningErrors = true

val storeProduct = StoreProductFactory.createGoogleStoreProduct()
val storeTransaction = StoreTransactionFactory.createStoreTransaction()
mockBillingAbstract.mockQueryProductDetails(queryProductDetailsSubsReturn = listOf(storeProduct))

var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.purchaseWith(
purchaseParams = PurchaseParams.Builder(activity, storeProduct).build(),
onError = { error, _ -> fail("Purchase should be successful. Error: ${error.message}") },
onSuccess = { _, customerInfo ->
receivedCustomerInfo = customerInfo
latch.countDown()
},
)
latestPurchasesUpdatedListener!!.onPurchasesUpdated(listOf(storeTransaction))
}

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.FAILED)
assertThat(receivedCustomerInfo?.entitlements?.all).isNotEmpty
assertThat(
receivedCustomerInfo?.entitlements?.all?.values?.first()?.verification,
).isEqualTo(VerificationResult.FAILED)
}
}