Files
purchases-ios/Tests/StoreKitUnitTests/LocalReceiptParserStoreKitTests.swift
T

104 lines
4.0 KiB
Swift
Raw Normal View History

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// LocalReceiptParserStoreKitTests.swift
//
// Created by Andrés Boedo on 4/4/22.
import Foundation
import Nimble
@testable import RevenueCat
import StoreKit
import XCTest
2023-03-16 13:11:24 -07:00
@available(iOS 14.0, tvOS 14.0, macOS 11.0, watchOS 7.0, *)
class LocalReceiptParserStoreKitTests: StoreKitConfigTestCase {
2022-08-31 13:30:38 -07:00
private let operationDispatcher: OperationDispatcher = .default
private let receiptRefreshRequestFactory = ReceiptRefreshRequestFactory()
private var requestFetcher: StoreKitRequestFetcher!
private var systemInfo: SystemInfo!
private var receiptFetcher: ReceiptFetcher!
private var parser: PurchasesReceiptParser!
override func setUpWithError() throws {
try super.setUpWithError()
2022-08-31 13:30:38 -07:00
self.requestFetcher = StoreKitRequestFetcher(requestFactory: receiptRefreshRequestFactory,
operationDispatcher: operationDispatcher)
2023-07-13 08:24:51 -07:00
self.systemInfo = SystemInfo(platformInfo: Purchases.platformInfo,
finishTransactions: true,
operationDispatcher: operationDispatcher,
2024-07-01 12:37:21 -07:00
storeKitVersion: .storeKit1)
2022-08-31 13:30:38 -07:00
self.receiptFetcher = ReceiptFetcher(requestFetcher: self.requestFetcher, systemInfo: systemInfo)
self.parser = .default
}
2022-08-31 13:30:38 -07:00
@MainActor
func testReceiptParserParsesEmptyReceipt() async throws {
let data = try await XCTAsyncUnwrap(await self.receiptFetcher.receiptData(refreshPolicy: .always))
let receipt = try self.parser.parse(from: data)
expect(receipt.bundleId) == "com.revenuecat.StoreKitUnitTestsHostApp"
2024-01-09 18:07:05 -08:00
#if os(macOS)
expect(receipt.applicationVersion) == SystemInfo.frameworkVersion.replacingOccurrences(
of: "-SNAPSHOT",
with: ""
)
#else
expect(receipt.applicationVersion) == "1"
2024-01-09 18:07:05 -08:00
#endif
expect(receipt.originalApplicationVersion).to(beNil())
expect(receipt.opaqueValue).toNot(beNil())
expect(receipt.sha1Hash).toNot(beNil())
expect(receipt.creationDate).to(beCloseToNow())
expect(receipt.expirationDate).toNot(beNil())
expect(receipt.expirationDate).toNot(beCloseToNow())
expect(receipt.inAppPurchases).to(beEmpty())
}
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
func testReceiptParserParsesReceiptWithSingleIAP() async throws {
2024-07-01 12:37:21 -07:00
try AvailabilityChecks.iOS16APIAvailableOrSkipTest()
let product = try await fetchSk2Product()
let purchaseTime = Date()
_ = try await product.purchase()
2022-08-31 13:30:38 -07:00
let receiptData = await self.receiptFetcher.receiptData(refreshPolicy: .always)
let data = try XCTUnwrap(receiptData)
let receipt = try self.parser.parse(from: data)
2022-08-22 16:00:39 -07:00
let firstPurchase = try XCTUnwrap(receipt.inAppPurchases.onlyElement)
expect(firstPurchase.quantity) == 1
expect(firstPurchase.productId) == product.id
expect(firstPurchase.transactionId).toNot(beNil())
expect(firstPurchase.originalTransactionId).to(beNil())
expect(firstPurchase.productType) == .unknown
expect(firstPurchase.purchaseDate).to(beCloseTo(purchaseTime, within: 5))
expect(firstPurchase.originalPurchaseDate).to(beNil())
expect(firstPurchase.expiresDate).toNot(beNil())
expect(firstPurchase.expiresDate).toNot(beCloseToNow())
expect(firstPurchase.isSubscription) == true
expect(firstPurchase.cancellationDate).to(beNil())
expect(firstPurchase.isInTrialPeriod).to(beNil())
expect(firstPurchase.isInIntroOfferPeriod) == true
expect(firstPurchase.webOrderLineItemId).to(beNil())
expect(firstPurchase.promotionalOfferIdentifier).to(beNil())
}
}