2022-04-05 16:18:06 -03:00
|
|
|
//
|
|
|
|
|
// 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
|
2022-04-06 15:44:00 -03:00
|
|
|
import StoreKit
|
2022-04-05 16:18:06 -03:00
|
|
|
import XCTest
|
|
|
|
|
|
|
|
|
|
class LocalReceiptParserStoreKitTests: StoreKitConfigTestCase {
|
|
|
|
|
|
2022-04-06 15:43:26 -03:00
|
|
|
let operationDispatcher: OperationDispatcher = .default
|
|
|
|
|
let receiptRefreshRequestFactory = ReceiptRefreshRequestFactory()
|
|
|
|
|
var requestFetcher: StoreKitRequestFetcher!
|
|
|
|
|
var systemInfo: SystemInfo!
|
|
|
|
|
var receiptFetcher: ReceiptFetcher!
|
|
|
|
|
var parser: ReceiptParser!
|
2022-04-05 16:18:06 -03:00
|
|
|
|
2022-04-06 15:43:26 -03:00
|
|
|
override func setUpWithError() throws {
|
|
|
|
|
try super.setUpWithError()
|
|
|
|
|
requestFetcher = StoreKitRequestFetcher(requestFactory: receiptRefreshRequestFactory,
|
|
|
|
|
operationDispatcher: operationDispatcher)
|
2022-04-05 16:18:06 -03:00
|
|
|
|
2022-04-06 15:43:26 -03:00
|
|
|
systemInfo = try SystemInfo(platformInfo: Purchases.platformInfo,
|
|
|
|
|
finishTransactions: true,
|
|
|
|
|
operationDispatcher: operationDispatcher,
|
2022-04-11 11:41:47 -07:00
|
|
|
storeKit2Setting: .disabled)
|
2022-04-06 15:43:26 -03:00
|
|
|
receiptFetcher = ReceiptFetcher(requestFetcher: requestFetcher, systemInfo: systemInfo)
|
|
|
|
|
parser = ReceiptParser()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testReceiptParserParsesEmptyReceipt() async throws {
|
2022-04-13 17:29:07 -04:00
|
|
|
let data = try await XCTAsyncUnwrap(await receiptFetcher.receiptData(refreshPolicy: .always))
|
2022-04-06 15:43:26 -03:00
|
|
|
|
|
|
|
|
let receipt = try self.parser.parse(from: data)
|
2022-04-05 16:18:06 -03:00
|
|
|
|
2022-04-06 15:44:00 -03:00
|
|
|
expect(receipt.bundleId) == "com.revenuecat.StoreKitUnitTestsHostApp"
|
2022-04-05 16:18:06 -03:00
|
|
|
expect(receipt.applicationVersion) == "1"
|
2022-04-06 15:44:00 -03:00
|
|
|
expect(receipt.originalApplicationVersion).to(beNil())
|
|
|
|
|
expect(receipt.opaqueValue).toNot(beNil())
|
|
|
|
|
expect(receipt.sha1Hash).toNot(beNil())
|
|
|
|
|
expect(receipt.creationDate).to(beCloseTo(Date(), within: 1))
|
|
|
|
|
expect(receipt.expirationDate).toNot(beNil())
|
|
|
|
|
expect(receipt.expirationDate).toNot(beCloseTo(Date(), within: 1))
|
|
|
|
|
expect(receipt.inAppPurchases).to(beEmpty())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
|
|
|
|
|
func testReceiptParserParsesReceiptWithSingleIAP() async throws {
|
|
|
|
|
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()
|
|
|
|
|
let product = try await fetchSk2Product()
|
|
|
|
|
_ = try await product.purchase()
|
|
|
|
|
|
2022-04-13 17:29:07 -04:00
|
|
|
let data = try await XCTAsyncUnwrap(await receiptFetcher.receiptData(refreshPolicy: .always))
|
2022-04-06 15:44:00 -03:00
|
|
|
|
|
|
|
|
let receipt = try self.parser.parse(from: data)
|
|
|
|
|
|
|
|
|
|
expect(receipt.inAppPurchases.count) == 1
|
|
|
|
|
|
|
|
|
|
let firstPurchase = try XCTUnwrap(receipt.inAppPurchases.first)
|
|
|
|
|
|
|
|
|
|
expect(firstPurchase.quantity) == 1
|
|
|
|
|
expect(firstPurchase.productId) == product.id
|
|
|
|
|
expect(firstPurchase.transactionId).toNot(beNil())
|
|
|
|
|
expect(firstPurchase.originalTransactionId).to(beNil())
|
|
|
|
|
expect(firstPurchase.productType).to(beNil())
|
|
|
|
|
|
2022-05-26 14:54:30 -07:00
|
|
|
expect(firstPurchase.purchaseDate).to(beCloseTo(Date(), within: 5))
|
2022-04-06 15:44:00 -03:00
|
|
|
expect(firstPurchase.originalPurchaseDate).to(beNil())
|
|
|
|
|
|
|
|
|
|
expect(firstPurchase.expiresDate).toNot(beNil())
|
|
|
|
|
expect(firstPurchase.expiresDate).toNot(beCloseTo(Date(), within: 1))
|
|
|
|
|
|
|
|
|
|
expect(firstPurchase.cancellationDate).to(beNil())
|
|
|
|
|
expect(firstPurchase.isInTrialPeriod).to(beNil())
|
|
|
|
|
expect(firstPurchase.isInIntroOfferPeriod) == true
|
|
|
|
|
expect(firstPurchase.webOrderLineItemId).to(beNil())
|
|
|
|
|
expect(firstPurchase.promotionalOfferIdentifier).to(beNil())
|
|
|
|
|
|
2022-04-05 16:18:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|