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

[StoreKit 2] Raise error if no AppTransaction and no Transaction present #4054

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions Sources/Purchasing/Purchases/PurchasesOrchestrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,14 @@ private extension PurchasesOrchestrator {
return
}

// We dont have an AppTransactionJWS, and no transaction, return error
if appTransactionJWS == nil {
completion?(.failure(ErrorUtils.storeProblemError(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make sure that this is dispatched on the main thread?

withMessage: Strings.storeKit.sk2_app_transaction_unavailable.description
)))
return
}

self.backend.post(receipt: .empty,
productData: nil,
transactionData: .init(appUserID: currentAppUserID,
Expand Down
21 changes: 21 additions & 0 deletions Tests/StoreKitUnitTests/PurchasesOrchestratorSK2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,27 @@ class PurchasesOrchestratorSK2Tests: BasePurchasesOrchestratorTests, PurchasesOr
expect(customerInfo) == mockCustomerInfo
}

func testSyncPurchasesSK2DoesNotPostReceiptAndRaisesErrorIfNoTransactionsAndNoAppTransactionJWSToken()
async throws {
self.mockTransactionFetcher.stubbedFirstVerifiedTransaction = nil
self.mockTransactionFetcher.stubbedAppTransactionJWS = nil
self.customerInfoManager.stubbedCachedCustomerInfoResult = CustomerInfo.missingOriginalApplicationVersion

do {
_ = try await self.orchestrator.syncPurchases(receiptRefreshPolicy: .always,
isRestore: true,
initiationSource: .restore)
fail("Expected error")
} catch {
expect(error).to(matchError(ErrorCode.storeProblemError))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to make sure that this error is thrown from the main thread?

Copy link
Member Author

Choose a reason for hiding this comment

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

not here afaik, we'll figure out a different way to test this

Copy link
Contributor

Choose a reason for hiding this comment

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

I was just doing some similar stuff on the retry PR, one thing that you might be able to do to help is to mock out the OperationDispatcher and then verify that invokedDispatchOnMainThread is true. While you're not directly testing the thread, it's close in that it's verifying how the OperationDispatcher is executing the work.

}

expect(self.backend.invokedPostReceiptData).to(beFalse())

expect(self.customerInfoManager.invokedCachedCustomerInfo).to(beTrue())
expect(self.customerInfoManager.invokedCachedCustomerInfoCount) == 1
}

func testSyncPurchasesCallsSuccessDelegateMethod() async throws {
let transaction = try await createTransaction(finished: true)
self.mockTransactionFetcher.stubbedFirstVerifiedTransaction = transaction
Expand Down