Skip to content

Commit

Permalink
SDK-2859: add integration tests for ghost transfer behavior
Browse files Browse the repository at this point in the history
This is now fixed on the backend. These new tests reflect the new
expected behavior.

---------

Co-authored-by: Will Taylor <wtaylor151@gmail.com>
  • Loading branch information
NachoSoto and fire-at-will authored Sep 13, 2023
1 parent 7087218 commit 1072870
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 5 deletions.
13 changes: 13 additions & 0 deletions Tests/BackendIntegrationTests/BaseStoreKitIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ extension BaseStoreKitIntegrationTests {
)
}

func verifyReceiptIsEventuallyPosted(
file: FileString = #file,
line: UInt = #line
) async throws {
try await self.logger.verifyMessageIsEventuallyLogged(
Strings.network.operation_state(PostReceiptDataOperation.self, state: "Finished").description,
timeout: .seconds(3),
pollInterval: .milliseconds(100),
file: file,
line: line
)
}

func expireSubscription(_ entitlement: EntitlementInfo) async throws {
guard let expirationDate = entitlement.expirationDate else { return }

Expand Down
96 changes: 96 additions & 0 deletions Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,102 @@ class StoreKit1IntegrationTests: BaseStoreKitIntegrationTests {
self.assertNoPurchases(currentCustomerInfo)
}

func testRenewalsOnASeparateUserDontTransferPurchases() async throws {
let prefix = UUID().uuidString
let userID1 = "\(prefix)-user-1"
let userID2 = "\(prefix)-user-2"

let anonymousUser = try self.purchases.appUserID
let productIdentifier = try await self.monthlyPackage.storeProduct.productIdentifier

// 1. Purchase with user 1
let user1CustomerInfo = try await self.purchases.logIn(userID1).customerInfo
self.assertNoPurchases(user1CustomerInfo)
expect(user1CustomerInfo.originalAppUserId) == anonymousUser
try await self.purchaseMonthlyOffering()

// 2. Change to user 2
let (identifiedCustomerInfo, _) = try await self.purchases.logIn(userID2)
self.assertNoPurchases(identifiedCustomerInfo)

// 3. Renew subscription
self.logger.clearMessages()

try self.testSession.forceRenewalOfSubscription(productIdentifier: productIdentifier)

try await self.verifyReceiptIsEventuallyPosted()

// 4. Verify new user does not have entitlement
let currentCustomerInfo = try await self.purchases.customerInfo(fetchPolicy: .fetchCurrent)
expect(currentCustomerInfo.originalAppUserId) == userID2
self.assertNoPurchases(currentCustomerInfo)
}

func testUserCanMakePurchaseAfterTransferBlocked() async throws {
let prefix = UUID().uuidString
let userID1 = "\(prefix)-user-1"
let userID2 = "\(prefix)-user-2"

let anonymousUser = try self.purchases.appUserID
let productIdentifier = try await self.monthlyPackage.storeProduct.productIdentifier

// 1. Purchase with user 1
var user1CustomerInfo = try await self.purchases.logIn(userID1).customerInfo
self.assertNoPurchases(user1CustomerInfo)
expect(user1CustomerInfo.originalAppUserId) == anonymousUser
try await self.purchaseMonthlyOffering()

// 2. Change to user 2
let (identifiedCustomerInfo, _) = try await self.purchases.logIn(userID2)
self.assertNoPurchases(identifiedCustomerInfo)

// 3. Renew subscription
self.logger.clearMessages()

try self.testSession.forceRenewalOfSubscription(productIdentifier: productIdentifier)

try await self.verifyReceiptIsEventuallyPosted()

// 4. Verify new user does not have entitlement
var currentCustomerInfo = try await self.purchases.customerInfo(fetchPolicy: .fetchCurrent)
expect(currentCustomerInfo.originalAppUserId) == userID2
self.assertNoPurchases(currentCustomerInfo)

// 5. Make purchase with user 2
self.logger.clearMessages()
currentCustomerInfo = try await self.purchaseMonthlyOffering().customerInfo
try await self.verifyReceiptIsEventuallyPosted()

// 6. Verify user 2 has purchases
expect(currentCustomerInfo.originalAppUserId) == userID2
expect(currentCustomerInfo.entitlements.all).toNot(beEmpty())

// 7. Verify that user 1 does not have purchases because they were transferred to user 2
user1CustomerInfo = try await self.purchases.logIn(userID1).customerInfo
self.assertNoPurchases(user1CustomerInfo)
}

func testPurchaseAfterSigningIntoNewUser() async throws {
let prefix = UUID().uuidString
let userID1 = "\(prefix)-user-1"
let userID2 = "\(prefix)-user-2"

let anonymousUser = try self.purchases.appUserID

// 1. Purchase with user 1
let user1CustomerInfo = try await self.purchases.logIn(userID1).customerInfo
expect(user1CustomerInfo.originalAppUserId) == anonymousUser
try await self.purchaseMonthlyOffering()

// 2. Change to user 2
let (identifiedCustomerInfo, _) = try await self.purchases.logIn(userID2)
self.assertNoPurchases(identifiedCustomerInfo)

// 3. Purchase again and verify user gets entitlement
let newCustomerInfo = try await self.purchaseMonthlyOffering().customerInfo
expect(newCustomerInfo.originalAppUserId) == userID2
}

func testLogOutRemovesEntitlements() async throws {
let anonUserID = try self.purchases.appUserID
let identifiedUserID = "identified_\(anonUserID)".replacingOccurrences(of: "RCAnonymous", with: "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ class StoreKit2ObserverModeIntegrationTests: StoreKit1ObserverModeIntegrationTes

try self.testSession.forceRenewalOfSubscription(productIdentifier: productID)

try await self.logger.verifyMessageIsEventuallyLogged(
Strings.network.operation_state(PostReceiptDataOperation.self, state: "Finished").description,
timeout: .seconds(3),
pollInterval: .milliseconds(100)
)
try await self.verifyReceiptIsEventuallyPosted()
}

}
Expand Down

0 comments on commit 1072870

Please sign in to comment.