-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on [CSDK-394], I realized that `Purchases.logIn` and `Purchases.logOut` had no tests. [CSDK-394]: https://revenuecats.atlassian.net/browse/CSDK-394?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
- Loading branch information
Showing
4 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
Tests/UnitTests/Purchasing/Purchases/PurchasesLogInTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// | ||
// 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 | ||
// | ||
// PurchasesLogInTests.swift | ||
// | ||
// Created by Nacho Soto on 8/15/22. | ||
|
||
import Nimble | ||
import StoreKit | ||
import XCTest | ||
|
||
@testable import RevenueCat | ||
|
||
class PurchasesLogInTests: BasePurchasesTests { | ||
|
||
private typealias LogInResult = Result<(customerInfo: CustomerInfo, created: Bool), Error> | ||
private typealias LogOutResult = Result<CustomerInfo, Error> | ||
|
||
override func setUpWithError() throws { | ||
try super.setUpWithError() | ||
|
||
self.setupAnonPurchases() | ||
} | ||
|
||
func testLogInWithSuccess() { | ||
let created = Bool.random() | ||
|
||
self.identityManager.mockLogInResult = .success((Self.mockLoggedInInfo, created)) | ||
|
||
var result: LogInResult! | ||
|
||
self.purchases.logIn(Self.appUserID) { customerInfo, created, error in | ||
result = Self.logInResult(customerInfo, created, error) | ||
} | ||
|
||
expect(result).toEventuallyNot(beNil()) | ||
|
||
expect(result).to(beSuccess()) | ||
expect(result.value) == (Self.mockLoggedInInfo, created) | ||
expect(self.identityManager.invokedLogInCount) == 1 | ||
expect(self.identityManager.invokedLogInParametersList) == [Self.appUserID] | ||
} | ||
|
||
func testLogInWithFailure() { | ||
let error: BackendError = .networkError(.offlineConnection()) | ||
self.identityManager.mockLogInResult = .failure(error) | ||
|
||
var result: LogInResult! | ||
|
||
self.purchases.logIn(Self.appUserID) { customerInfo, created, error in | ||
result = Self.logInResult(customerInfo, created, error) | ||
} | ||
|
||
expect(result).toEventuallyNot(beNil()) | ||
|
||
expect(result).to(beFailure()) | ||
expect(result.error).to(matchError(error)) | ||
expect(self.identityManager.invokedLogInCount) == 1 | ||
expect(self.identityManager.invokedLogInParametersList) == [Self.appUserID] | ||
} | ||
|
||
func testLogOutWithSuccess() { | ||
self.identityManager.mockLogOutError = nil | ||
self.backend.overrideCustomerInfoResult = .success(Self.mockLoggedOutInfo) | ||
|
||
expect(self.backend.getSubscriberCallCount) == 1 | ||
|
||
var result: Result<CustomerInfo, Error>! | ||
self.purchases.logOut { customerInfo, error in | ||
result = .init(customerInfo, error) | ||
} | ||
|
||
expect(result).toEventuallyNot(beNil()) | ||
expect(result).to(beSuccess()) | ||
expect(result.value) == Self.mockLoggedOutInfo | ||
|
||
expect(self.backend.getSubscriberCallCount) == 2 | ||
expect(self.identityManager.invokedLogOutCount) == 1 | ||
} | ||
|
||
func testLogOutWithFailure() { | ||
let error: BackendError = .networkError(.offlineConnection()) | ||
|
||
self.identityManager.mockLogOutError = error | ||
|
||
var result: Result<CustomerInfo, Error>! | ||
self.purchases.logOut { customerInfo, error in | ||
result = .init(customerInfo, error) | ||
} | ||
|
||
expect(result).toEventuallyNot(beNil()) | ||
expect(result).to(beFailure()) | ||
expect(result.error).to(matchError(error)) | ||
|
||
expect(self.backend.getSubscriberCallCount) == 1 | ||
expect(self.identityManager.invokedLogOutCount) == 1 | ||
} | ||
|
||
} | ||
|
||
// MARK: - | ||
|
||
private extension PurchasesLogInTests { | ||
|
||
// swiftlint:disable force_try | ||
static let mockLoggedInInfo = try! CustomerInfo(data: PurchasesLogInTests.loggedInCustomerInfoData) | ||
static let mockLoggedOutInfo = try! CustomerInfo(data: PurchasesLogInTests.loggedOutCustomerInfoData) | ||
// swiftlint:enable force_try | ||
|
||
private static let loggedInCustomerInfoData: [String: Any] = [ | ||
"request_date": "2019-08-16T10:30:42Z", | ||
"subscriber": [ | ||
"first_seen": "2019-07-17T00:05:54Z", | ||
"original_app_user_id": "user", | ||
"subscriptions": [:], | ||
"other_purchases": [:], | ||
"original_application_version": NSNull() | ||
] | ||
] | ||
|
||
private static let loggedOutCustomerInfoData: [String: Any] = [ | ||
"request_date": "2019-08-16T10:30:42Z", | ||
"subscriber": [ | ||
"first_seen": "2019-07-17T00:05:54Z", | ||
"original_app_user_id": "$RCAnonymousID:5b6fdbad3a0c4f879e43d269ecdf9ba1", | ||
"subscriptions": [:], | ||
"other_purchases": [:], | ||
"original_application_version": NSNull() | ||
] | ||
] | ||
|
||
/// Converts the result of `Purchases.logIn` into `LogInResult` | ||
private static func logInResult(_ info: CustomerInfo?, _ created: Bool, _ error: Error?) -> LogInResult { | ||
return .init(info.map { ($0, created) }, error) | ||
} | ||
|
||
} |