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

Offline Entitlements: allow creating offline CustomerInfo with empty ProductEntitlementMapping #2504

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions Sources/OfflineEntitlements/OfflineCustomerInfoCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class OfflineCustomerInfoCreator {
func create(for userID: String) async throws -> CustomerInfo {
Logger.info(Strings.offlineEntitlements.computing_offline_customer_info)

guard let mapping = self.productEntitlementMappingFetcher.productEntitlementMapping,
!mapping.entitlementsByProduct.isEmpty else {
guard let mapping = self.productEntitlementMappingFetcher.productEntitlementMapping else {
Logger.warn(Strings.offlineEntitlements.computing_offline_customer_info_with_no_entitlement_mapping)
throw Error.noEntitlementMappingAvailable
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ class CustomerInfoOfflineEntitlementsStoreKitTest: StoreKitConfigTestCase {
expect(info.entitlements.all).to(beEmpty())
}

func testEmptyMapping() async throws {
let transaction = try await self.createTransactionWithPurchase()
let mapping: ProductEntitlementMapping = .empty

let info = self.create(with: [transaction], mapping: mapping)

self.verifyInfo(info)
expect(info.activeSubscriptions) == [transaction.productID]
expect(info.nonSubscriptions).to(beEmpty())
expect(info.entitlements.all).to(beEmpty())
}

func testMultiplePurchasedProducts() async throws {
let product1 = try await self.fetchSk2Product(Self.productID)
let product2 = try await self.fetchSk2Product("com.revenuecat.annual_39.99_no_trial")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,6 @@ class OfflineCustomerInfoResponseHandlerTests: BaseCustomerInfoResponseHandlerTe
)
}

func testServerErrorFailsWhenCreatingOfflineCustomerInfoWithEmptyMapping() async {
self.fetcher.stubbedResult = .success([])
self.factory.stubbedResult = Self.offlineCustomerInfo

let error: NetworkError = .serverDown()
let logger = TestLogHandler()

let result = await self.handle(.failure(error), .empty)
expect(result).to(beFailure())
expect(result.error).to(matchError(BackendError.networkError(error)))

expect(self.factory.createRequested) == false

logger.verifyMessageWasLogged(
Strings.offlineEntitlements.computing_offline_customer_info_with_no_entitlement_mapping,
level: .warn
)
}

func testServerErrorCreatesOfflineCustomerInfo() async {
self.fetcher.stubbedResult = .success([
Self.purchasedProduct
Expand All @@ -233,6 +214,19 @@ class OfflineCustomerInfoResponseHandlerTests: BaseCustomerInfoResponseHandlerTe
)
}

func testCreatesOfflineCustomerInfoWithEmptyMapping() async {
self.fetcher.stubbedResult = .success([])
self.factory.stubbedResult = Self.offlineCustomerInfo

let error: NetworkError = .serverDown()

let result = await self.handle(.failure(error), .empty)
expect(result).to(beSuccess())
expect(result.value) == Self.offlineCustomerInfo

expect(self.factory.createRequested) == true
}

func testServerErrorWithFailingPurchasedProductsFetcher() async {
let logger = TestLogHandler()

Expand Down