2021-08-27 19:06: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
|
|
|
|
|
|
//
|
2021-12-02 09:26:53 -03:00
|
|
|
|
// ManageSubscriptionsHelperTests.swift
|
2021-08-27 19:06:06 -03:00
|
|
|
|
//
|
|
|
|
|
|
// Created by Andrés Boedo on 8/20/21.
|
|
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
import Nimble
|
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
2021-09-02 13:53:59 -07:00
|
|
|
|
@testable import RevenueCat
|
2021-08-27 19:06:06 -03:00
|
|
|
|
|
2023-08-09 11:19:40 -07:00
|
|
|
|
#if os(macOS) || os(iOS) || VISION_OS
|
2021-08-27 19:06:06 -03:00
|
|
|
|
|
2022-05-10 12:40:40 -07:00
|
|
|
|
class ManageSubscriptionsHelperTests: TestCase {
|
2021-08-27 19:06:06 -03:00
|
|
|
|
|
|
|
|
|
|
private var systemInfo: MockSystemInfo!
|
2021-11-22 12:37:33 -03:00
|
|
|
|
private var customerInfoManager: MockCustomerInfoManager!
|
2022-04-08 15:39:34 -07:00
|
|
|
|
private var currentUserProvider: CurrentUserProvider!
|
2021-12-02 09:26:53 -03:00
|
|
|
|
private var helper: ManageSubscriptionsHelper!
|
2021-11-22 12:37:33 -03:00
|
|
|
|
private let mockCustomerInfoData: [String: Any] = [
|
2021-08-27 19:06:06 -03:00
|
|
|
|
"request_date": "2018-12-21T02:40:36Z",
|
|
|
|
|
|
"subscriber": [
|
|
|
|
|
|
"original_app_user_id": "app_user_id",
|
|
|
|
|
|
"first_seen": "2019-06-17T16:05:33Z",
|
2023-04-11 08:55:21 -07:00
|
|
|
|
"subscriptions": [:] as [String: Any],
|
|
|
|
|
|
"other_purchases": [:] as [String: Any],
|
2021-08-27 19:06:06 -03:00
|
|
|
|
"original_application_version": NSNull()
|
2023-04-11 08:55:21 -07:00
|
|
|
|
] as [String: Any],
|
2021-08-27 19:06:06 -03:00
|
|
|
|
"managementURL": NSNull()
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
|
override func setUpWithError() throws {
|
|
|
|
|
|
try super.setUpWithError()
|
2022-06-09 11:13:41 -07:00
|
|
|
|
|
|
|
|
|
|
self.systemInfo = MockSystemInfo(finishTransactions: true)
|
|
|
|
|
|
self.customerInfoManager = MockCustomerInfoManager(
|
2023-05-19 13:27:44 -04:00
|
|
|
|
offlineEntitlementsManager: MockOfflineEntitlementsManager(),
|
2022-06-09 11:13:41 -07:00
|
|
|
|
operationDispatcher: MockOperationDispatcher(),
|
|
|
|
|
|
deviceCache: MockDeviceCache(sandboxEnvironmentDetector: self.systemInfo),
|
|
|
|
|
|
backend: MockBackend(),
|
2023-05-30 12:38:18 -07:00
|
|
|
|
transactionFetcher: MockStoreKit2TransactionFetcher(),
|
|
|
|
|
|
transactionPoster: MockTransactionPoster(),
|
2022-06-09 11:13:41 -07:00
|
|
|
|
systemInfo: self.systemInfo
|
|
|
|
|
|
)
|
|
|
|
|
|
self.currentUserProvider = MockCurrentUserProvider(mockAppUserID: "appUserID")
|
|
|
|
|
|
self.helper = ManageSubscriptionsHelper(systemInfo: self.systemInfo,
|
|
|
|
|
|
customerInfoManager: self.customerInfoManager,
|
|
|
|
|
|
currentUserProvider: self.currentUserProvider)
|
2021-08-27 19:06:06 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-26 11:20:46 -07:00
|
|
|
|
func testShowManageSubscriptions() throws {
|
2021-08-27 19:06:06 -03:00
|
|
|
|
// given
|
2022-09-05 13:46:07 -07:00
|
|
|
|
var receivedResult: Result<Void, PurchasesError>?
|
2022-03-15 15:17:04 -05:00
|
|
|
|
customerInfoManager.stubbedCustomerInfoResult = .success(try CustomerInfo(data: mockCustomerInfoData))
|
2021-12-02 09:26:53 -03:00
|
|
|
|
|
2021-08-27 19:06:06 -03:00
|
|
|
|
// when
|
2021-12-02 09:26:53 -03:00
|
|
|
|
helper.showManageSubscriptions { result in
|
2021-08-27 19:06:06 -03:00
|
|
|
|
receivedResult = result
|
|
|
|
|
|
}
|
2021-12-02 09:26:53 -03:00
|
|
|
|
|
2021-08-27 19:06:06 -03:00
|
|
|
|
// then
|
2022-05-26 11:20:46 -07:00
|
|
|
|
expect(receivedResult).toEventuallyNot(beNil())
|
|
|
|
|
|
expect(receivedResult).to(beSuccess())
|
2021-08-27 19:06:06 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-02 09:26:53 -03:00
|
|
|
|
func testShowManageSubscriptionsFailsIfCouldntGetCustomerInfo() throws {
|
2022-04-12 15:11:22 -07:00
|
|
|
|
let error: BackendError = .networkError(.errorResponse(
|
2022-11-11 08:02:28 -08:00
|
|
|
|
.init(code: .badRequest,
|
|
|
|
|
|
originalCode: BackendErrorCode.badRequest.rawValue,
|
|
|
|
|
|
message: nil,
|
|
|
|
|
|
attributeErrors: [:]),
|
2022-04-12 15:11:22 -07:00
|
|
|
|
400)
|
|
|
|
|
|
)
|
2022-03-15 15:17:04 -05:00
|
|
|
|
|
2021-08-27 19:06:06 -03:00
|
|
|
|
// given
|
2022-09-05 13:46:07 -07:00
|
|
|
|
var receivedResult: Result<Void, PurchasesError>?
|
2022-03-15 15:17:04 -05:00
|
|
|
|
customerInfoManager.stubbedCustomerInfoResult = .failure(error)
|
2021-08-27 19:06:06 -03:00
|
|
|
|
|
|
|
|
|
|
// when
|
2021-12-02 09:26:53 -03:00
|
|
|
|
helper.showManageSubscriptions { result in
|
2021-08-27 19:06:06 -03:00
|
|
|
|
receivedResult = result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// then
|
2022-05-26 11:20:46 -07:00
|
|
|
|
expect(receivedResult).toEventuallyNot(beNil())
|
|
|
|
|
|
|
2022-09-05 13:46:07 -07:00
|
|
|
|
let nonNilReceivedResult = try XCTUnwrap(receivedResult)
|
2021-12-02 09:26:53 -03:00
|
|
|
|
let expectedErrorMessage = "Failed to get managementURL from CustomerInfo. " +
|
2021-08-27 19:06:06 -03:00
|
|
|
|
"Details: The operation couldn’t be completed"
|
2021-12-13 16:19:51 -08:00
|
|
|
|
let expectedError = ErrorUtils.customerInfoError(withMessage: expectedErrorMessage,
|
2022-03-15 15:17:04 -05:00
|
|
|
|
error: error)
|
2021-08-27 19:06:06 -03:00
|
|
|
|
expect(nonNilReceivedResult).to(beFailure { error in
|
|
|
|
|
|
expect(error).to(matchError(expectedError))
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2021-12-02 09:26:53 -03:00
|
|
|
|
|
2021-08-27 19:06:06 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|