Skip to content

Add offline entitlements integration tests#1006

Merged
tonidero merged 15 commits into
mainfrom
toniricodiez/sdk-3126-offline-entitlements-add-integration
May 23, 2023
Merged

Add offline entitlements integration tests#1006
tonidero merged 15 commits into
mainfrom
toniricodiez/sdk-3126-offline-entitlements-add-integration

Conversation

@tonidero

Copy link
Copy Markdown
Contributor

Description

Depends on #987

This PR adds some integration tests checking some basic behaviors of offline entitlements. Mostly making sure the mode is turn on and off as expected.

@tonidero tonidero added the test label May 16, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I moved some logic from PurchasesIntegrationTest to this base class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was added because onActivityReady wasn't guaranteed to be executed in order which resulted in some flaky tests. This way, we execute the test setup code and the test itself within a single onActivityReady.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copied this from the BackendIntegrationTest which are actually unit tests unlike this.

Comment thread purchases/src/integrationTest/AndroidManifest.xml Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ideally we should also migrate these tests to use a single onActivityReady. But will do that in a different PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not great to duplicate these cache keys... But I didn't want to expose them in the common module just for this... I think it's ok? Lmk what you think

@tonidero tonidero May 16, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Creating these fake products is a bit hacky... We could create them from mocked google product details but that also doesn't feel great to me though, I think this is relatively simpler. Note that this is needed whether we use a proxy or not since we can't get the product info from Google directly.

@tonidero

Copy link
Copy Markdown
Contributor Author

Still want to write some more tests including:

  • Does not enter offline entitlements if it's a non-500 error
  • Recovers from offline entitlements if either a getCustomerInfo or a postReceipt request succeed
  • ...

@codecov

codecov Bot commented May 16, 2023

Copy link
Copy Markdown

Codecov Report

Merging #1006 (f2b61ce) into main (594138c) will decrease coverage by 0.04%.
The diff coverage is 50.00%.

❗ Current head f2b61ce differs from pull request most recent head f59ea0d. Consider uploading reports for the commit f59ea0d to get more accurate results

@@            Coverage Diff             @@
##             main    #1006      +/-   ##
==========================================
- Coverage   85.40%   85.37%   -0.04%     
==========================================
  Files         169      169              
  Lines        5997     6002       +5     
  Branches      836      839       +3     
==========================================
+ Hits         5122     5124       +2     
- Misses        544      545       +1     
- Partials      331      333       +2     
Impacted Files Coverage Δ
.../offlineentitlements/OfflineEntitlementsManager.kt 89.36% <50.00%> (-3.67%) ⬇️
.../main/kotlin/com/revenuecat/purchases/Purchases.kt 82.87% <50.00%> (-0.11%) ⬇️

@tonidero tonidero left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is still WIP but I would like some feedback on the approach @RevenueCat/coresdk .

I think this is much simpler than having to run a separate process for the proxy and enable/disable different endpoints in the middle of the test. On the other hand, I agree that hardcoding the success responses makes it much more brittle. I believe that it's somewhat acceptable to hardcode the responses, since we are hitting the backend in other integration tests. We can still use the proxy for manual testing though. Lmk what you think!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added these to be able to mock the initial state of shared preferences and active purchases within the integration test.

@tonidero tonidero requested a review from a team May 17, 2023 15:26
@tonidero tonidero force-pushed the enable-offline-entitlements branch from c6f739b to 8d526d4 Compare May 19, 2023 10:06
@tonidero tonidero force-pushed the toniricodiez/sdk-3126-offline-entitlements-add-integration branch from ee9953f to fcadf07 Compare May 19, 2023 10:08

@tonidero tonidero left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These tests don't look nearly as nice in Android as they do in iOS. One of the main differences is coroutine support. That would make the tests much more legible. I tried to avoid as many callbacks as possible but still not nearly as nice. I guess another reason to consider supporting coroutines soon

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a completion here to be able to wait upon it on tests. It won't be used in production. Still it's tested.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As an improvement you could forward the error too so integration tests can ensure it actually worked?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Had to increase the timeout... Honestly this is already large, but in Android we do all requests sequentially and that's taking a long time for the new tests. Also, randomly the tests will take twice as long... We will probably want to improve the performance of the integration tests soon, but for now I wanted to get them running first. Also, the fact that we are using callbacks and not coroutines makes it necessary for us to put a timeout for the whole test :(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

iOS integration tests are also not the fastest because we need to wait on purchase expirations. I think it's okay that these tests aren't the fastest, as long as unit tests are.

@tonidero tonidero marked this pull request as ready for review May 19, 2023 14:58
@tonidero

Copy link
Copy Markdown
Contributor Author

This is ready for review @RevenueCat/coresdk

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note that I didn't add automated tests that tested offline entitlements with multiple purchases. Reasons being:

  • I would need to assume many things about how Google works in order to mock it realistically. I really wish Google released something to test purchases more easily :(.
  • We would need another valid token for a different product if we wanted to test it against the backend

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wanted to note that this only works for these tests that are using offline entitlements. The token we currently are using for integration tests is for a expired purchase, that means that the backend won't return it as an active purchase. For these tests I'm mocking the purchase as returned from the Google method "queryPurchases" which returns active purchases, that's why it ends up as an active purchase there.

That's also the reason why I'm not verifying that the purchases are active after recovering from offline entitlements mode and only verifying that the purchase is acknowledged and sent to the backend.

This is not really great. I think we can improve this a lot by having an active purchase forever in a company credit card. Will study this option and try to improve this a bit.

@NachoSoto NachoSoto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a great start 👍🏻

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As an improvement you could forward the error too so integration tests can ensure it actually worked?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

iOS integration tests are also not the fastest because we need to wait on purchase expirations. I think it's okay that these tests aren't the fastest, as long as unit tests are.

Comment on lines 133 to 134

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it a problem that this is in this order? The code waiting for the latch would be notified before you fail the test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, I think we should completely remove the countdown in these error cases, I added it on an early iteration but it's not really needed, and can cause errors as you said. Good catch!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe verify the error is a server error and not something else?

Comment on lines 194 to 195

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto order and in the other tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a noun, so postSetup makes sense 😅

@tonidero

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions @NachoSoto! Refactored the integration tests pretty heavily into different classes. So will need another review 🙏

@tonidero tonidero requested a review from NachoSoto May 22, 2023 11:58
Comment on lines 21 to 27

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍🏻

Base automatically changed from enable-offline-entitlements to main May 23, 2023 08:46
@tonidero tonidero force-pushed the toniricodiez/sdk-3126-offline-entitlements-add-integration branch from 10ed485 to f2b61ce Compare May 23, 2023 08:48
@tonidero tonidero enabled auto-merge (squash) May 23, 2023 08:49
@tonidero tonidero force-pushed the toniricodiez/sdk-3126-offline-entitlements-add-integration branch from 91a1706 to f2b61ce Compare May 23, 2023 09:06
@tonidero tonidero force-pushed the toniricodiez/sdk-3126-offline-entitlements-add-integration branch from f2b61ce to f59ea0d Compare May 23, 2023 09:10
@tonidero tonidero merged commit 6f48f28 into main May 23, 2023
@tonidero tonidero deleted the toniricodiez/sdk-3126-offline-entitlements-add-integration branch May 23, 2023 09:26
tonidero added a commit that referenced this pull request May 24, 2023
### Description
After adding the integration tests for offline entitlements in #1006, I
was testing running each set of tests separately. However, I was
changing the parameters we use to run integration tests between normal
integration tests and the offline entitlement tests in order to make
them pass. In order to make them compatible, I'm just hardcoding the
entitlements we verify are granted after purchases during offline
entitlements integration tests.
This was referenced May 24, 2023
tonidero added a commit that referenced this pull request May 25, 2023
**This is an automatic release.**

### New Features
* Support DEFERRED mode (#985) via swehner (@swehner)
* Add completion callback to syncPurchases API (#1002) via Toni Rico
(@tonidero)
### Bugfixes
* Workaround bug in android 4 for JSON objects with List<String> (#942)
via Andy Boedo (@aboedo)
### Dependency Updates
* Bump fastlane-plugin-revenuecat_internal from `fe45299` to `13773d2`
(#1015) via dependabot[bot] (@dependabot[bot])
### Other Changes
* Bump dokka to 1.8.10 to support Gradle 8 (#1009) via Toni Rico
(@tonidero)
* Disable offline entitlements temporarily (#1023) via Toni Rico
(@tonidero)
* Fix integration tests in CI (#1019) via Toni Rico (@tonidero)
* Add offline entitlements integration tests (#1006) via Toni Rico
(@tonidero)
* Disable offline entitlements in observer mode (#1014) via Toni Rico
(@tonidero)
* Extracts setup and teardown to BasePurchasesTest (#1011) via Cesar de
la Vega (@vegaro)
* Support forcing server errors for tests (#1008) via Toni Rico
(@tonidero)

---------

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
Co-authored-by: Toni Rico <antonio.rico.diez@revenuecat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants