Files
purchases-ios/Tests/StoreKitUnitTests/ManageSubscriptionsHelperTests.swift
T
NachoSoto 2cbb71eb20 Extracted SandboxEnvironmentDetector (#1645)
### Motivation:

For [CSDK-53], `EntitlementInfos` will need access to the environment. This provides a lighter interface for that, as well as a simple implementation to inject that doesn't require the entire `SystemInfo`

[CSDK-53]: https://revenuecats.atlassian.net/browse/CSDK-53?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
2022-06-09 11:13:41 -07:00

102 lines
3.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// 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
//
// ManageSubscriptionsHelperTests.swift
//
// Created by Andrés Boedo on 8/20/21.
import Foundation
import Nimble
import XCTest
@testable import RevenueCat
#if os(macOS) || os(iOS)
class ManageSubscriptionsHelperTests: TestCase {
private var systemInfo: MockSystemInfo!
private var customerInfoManager: MockCustomerInfoManager!
private var currentUserProvider: CurrentUserProvider!
private var helper: ManageSubscriptionsHelper!
private let mockCustomerInfoData: [String: Any] = [
"request_date": "2018-12-21T02:40:36Z",
"subscriber": [
"original_app_user_id": "app_user_id",
"first_seen": "2019-06-17T16:05:33Z",
"subscriptions": [:],
"other_purchases": [:],
"original_application_version": NSNull()
],
"managementURL": NSNull()
]
override func setUpWithError() throws {
try super.setUpWithError()
self.systemInfo = MockSystemInfo(finishTransactions: true)
self.customerInfoManager = MockCustomerInfoManager(
operationDispatcher: MockOperationDispatcher(),
deviceCache: MockDeviceCache(sandboxEnvironmentDetector: self.systemInfo),
backend: MockBackend(),
systemInfo: self.systemInfo
)
self.currentUserProvider = MockCurrentUserProvider(mockAppUserID: "appUserID")
self.helper = ManageSubscriptionsHelper(systemInfo: self.systemInfo,
customerInfoManager: self.customerInfoManager,
currentUserProvider: self.currentUserProvider)
}
func testShowManageSubscriptions() throws {
// given
var receivedResult: Result<Void, Error>?
customerInfoManager.stubbedCustomerInfoResult = .success(try CustomerInfo(data: mockCustomerInfoData))
// when
helper.showManageSubscriptions { result in
receivedResult = result
}
// then
expect(receivedResult).toEventuallyNot(beNil())
expect(receivedResult).to(beSuccess())
}
func testShowManageSubscriptionsFailsIfCouldntGetCustomerInfo() throws {
let error: BackendError = .networkError(.errorResponse(
.init(code: BackendErrorCode.badRequest, message: nil, attributeErrors: [:]),
400)
)
// given
var receivedResult: Result<Void, Error>?
customerInfoManager.stubbedCustomerInfoResult = .failure(error)
// when
helper.showManageSubscriptions { result in
receivedResult = result
}
// then
expect(receivedResult).toEventuallyNot(beNil())
let nonNilReceivedResult: Result<Void, Error> = try XCTUnwrap(receivedResult)
let expectedErrorMessage = "Failed to get managementURL from CustomerInfo. " +
"Details: The operation couldn’t be completed"
let expectedError = ErrorUtils.customerInfoError(withMessage: expectedErrorMessage,
error: error)
expect(nonNilReceivedResult).to(beFailure { error in
expect(error).to(matchError(expectedError))
})
}
}
#endif