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: added tests to verify offerings and product entitlement mapping #2667

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
6 changes: 5 additions & 1 deletion Sources/Purchasing/OfferingsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,13 @@ class OfferingsManager {
return productIDsFromBackend.subtracting(productIDsFromStore)
}

func invalidateCachedOfferings(appUserID: String) {
self.deviceCache.clearOfferingsCache(appUserID: appUserID)
}

func invalidateAndReFetchCachedOfferingsIfAppropiate(appUserID: String) {
let cachedOfferings = self.deviceCache.cachedOfferings
self.deviceCache.clearOfferingsCache(appUserID: appUserID)
self.invalidateCachedOfferings(appUserID: appUserID)

if cachedOfferings != nil {
self.offerings(appUserID: appUserID, fetchPolicy: .ignoreNotFoundProducts) { @Sendable _ in }
Expand Down
9 changes: 7 additions & 2 deletions Sources/Purchasing/Purchases/Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1373,8 +1373,9 @@ extension Purchases: InternalPurchasesType {
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func productEntitlementMapping() async throws -> ProductEntitlementMapping {
let response = try await Async.call { completion in
self.backend.offlineEntitlements.getProductEntitlementMapping(withRandomDelay: false,
completion: completion)
self.backend.offlineEntitlements.getProductEntitlementMapping(withRandomDelay: false) { result in
completion(result.mapError(\.asPublicError))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was forwarding the wrong error.

}
}

return response.toMapping()
Expand Down Expand Up @@ -1472,6 +1473,10 @@ internal extension Purchases {
return self.receiptFetcher.receiptURL
}

func invalidateOfferingsCache() {
self.offeringsManager.invalidateCachedOfferings(appUserID: self.appUserID)
}

}

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ class InformationalSignatureVerificationIntegrationTests: BaseSignatureVerificat
expect(user.entitlements.verification) == .failed
}

func testOfferingsWithInvalidSignatureDontThrowError() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

self.invalidSignature = true

Purchases.shared.invalidateOfferingsCache()

// Currently there's no API to detect signature failures
// See also `EnforcedSignatureVerificationIntegrationTests.testOfferingsWithInvalidSignature`
_ = try await Purchases.shared.offerings()
}

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func testEntitlementMappingWithInvalidSignatureDontThrowError() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

self.invalidSignature = true
_ = try await Purchases.shared.productEntitlementMapping()
}

}

class EnforcedSignatureVerificationIntegrationTests: BaseSignatureVerificationIntegrationTests {
Expand All @@ -138,12 +158,29 @@ class EnforcedSignatureVerificationIntegrationTests: BaseSignatureVerificationIn
}
}

func testOfferingsWithInvalidSignature() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

self.invalidSignature = true

Purchases.shared.invalidateOfferingsCache()

try await Self.verifyThrowsSignatureVerificationFailed {
_ = try await Purchases.shared.offerings()
}
}

func testOfferingsWithValidSignature() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

Purchases.shared.invalidateOfferingsCache()
_ = try await Purchases.shared.offerings()
}

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func testEntitlementMappingWithInvalidSignature() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

XCTExpectFailure("Not implemented yet")

self.invalidSignature = true

try await Self.verifyThrowsSignatureVerificationFailed {
Expand Down