Commit d0c83785 authored by Jose Blaya's avatar Jose Blaya
Browse files

Update tests

New tests for trials
parent 87f279bb
......@@ -246,6 +246,12 @@ class PIAWebServices: WebServices, ConfigurationAccess {
let info = AppStoreInformation(products: products,
isInIntroOfferPeriod: isIntroOffer,
isTrialPeriod: isTrialPeriod)
//If either of these fields are true for a given subscription, the user is not eligible for an introductory offer on that subscription product or any other products within the same subscription group.
if info.isInIntroOfferPeriod || info.isTrialPeriod {
Client.configuration.eligibleForTrial = false
} else {
Client.configuration.eligibleForTrial = true
}
callback?(info, nil)
} else {
callback?(nil, error)
......
......@@ -51,7 +51,11 @@ extension Client {
Uses a mock in-app provider for testing purchases.
*/
public static func useMockInAppProvider() {
store = MockInAppProvider()
store = MockInAppProvider(with: Data())
}
public static func useMockInAppProviderWithoutReceipt() {
store = MockInAppProvider(with: nil)
}
#endif
}
......@@ -103,6 +103,22 @@ public class MockAccountProvider: AccountProvider, WebServicesConsumer {
renewUrl: nil
)
}
webServices.appstoreInformationEligible = {
return AppStoreInformation(products: [Product(identifier: "com.product.monthly",
plan: .monthly,
price: "3.99",
legacy: false)],
isInIntroOfferPeriod: false,
isTrialPeriod: false)
}
webServices.appstoreInformationNotEligible = {
return AppStoreInformation(products: [Product(identifier: "com.product.monthly",
plan: .monthly,
price: "3.99",
legacy: false)],
isInIntroOfferPeriod: true,
isTrialPeriod: false)
}
}
// MARK: AccountProvider
......
......@@ -31,9 +31,13 @@ private class MockTransaction: InAppTransaction {
}
class MockInAppProvider: InAppProvider, ConfigurationAccess {
init(with receipt: Data? = Data()) {
self.paymentReceipt = receipt
}
var availableProducts: [InAppProduct]?
var paymentReceipt: Data? = Data()
var paymentReceipt: Data?
var hasUncreditedTransactions: Bool {
return false
......
......@@ -14,7 +14,11 @@ class MockWebServices: WebServices {
var accountInfo: (() -> AccountInfo)?
var invitesInformation: (() -> InvitesInformation)?
var appstoreInformationEligible: (() -> AppStoreInformation)?
var appstoreInformationNotEligible: (() -> AppStoreInformation)?
var serversBundle: (() -> ServersBundle)?
func token(credentials: Credentials, _ callback: ((String?, Error?) -> Void)?) {
......@@ -68,13 +72,22 @@ class MockWebServices: WebServices {
}
func subscriptionInformation(with receipt: Data?, _ callback: LibraryCallback<AppStoreInformation>?) {
let appstoreInfo = AppStoreInformation(products: [Product(identifier: "com.product.monthly",
plan: .monthly,
price: "3.99",
legacy: false)],
isInIntroOfferPeriod: false,
isTrialPeriod: false)
callback?(appstoreInfo, nil)
let result = { () -> AppStoreInformation? in
if let _ = receipt {
return self.appstoreInformationNotEligible?()
} else {
return self.appstoreInformationEligible?()
}
}
if result()!.isInIntroOfferPeriod || result()!.isTrialPeriod {
Client.configuration.eligibleForTrial = false
} else {
Client.configuration.eligibleForTrial = true
}
callback?(result(), nil)
}
func invitesInformation(_ callback: LibraryCallback<InvitesInformation>?) {
......
......@@ -29,14 +29,14 @@ class ProductTests: XCTestCase {
func testMockProductIdentifiers() {
let expUpdate = expectation(description: "productIdentifiers")
mock.accountProvider.updatePlanProductIdentifiers { products, error in
mock.accountProvider.subscriptionInformation { subscriptionInfo, error in
if let _ = error {
print("error found: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
return
}
guard let _ = products else {
guard let _ = subscriptionInfo else {
print("testMockProductIdentifiers: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
......@@ -47,11 +47,58 @@ class ProductTests: XCTestCase {
waitForExpectations(timeout: 5.0, handler: nil)
}
func testMockTrialsUserNotEligible() {
let expUpdate = expectation(description: "trials")
mock.accountProvider.subscriptionInformation { subscriptionInfo, error in
if let _ = error {
print("error found: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
return
}
guard let _ = subscriptionInfo else {
print("testMockTrials: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
return
}
XCTAssertFalse(Client.configuration.eligibleForTrial)
expUpdate.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}
func testMockTrialsUserEligible() {
Client.useMockInAppProviderWithoutReceipt()
let expUpdate = expectation(description: "trials")
mock.accountProvider.subscriptionInformation { subscriptionInfo, error in
if let _ = error {
print("error found: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
return
}
guard let _ = subscriptionInfo else {
print("testMockTrials: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
return
}
XCTAssertTrue(Client.configuration.eligibleForTrial)
expUpdate.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}
private func __testRetrieveSubscriptionPlans(webServices: PIAWebServices) {
let exp = expectation(description: "subscription.plans")
webServices.planProductIdentifiers({ products, error in
webServices.subscriptionInformation(with: nil) { subscriptionInfo, error in
if let _ = error {
print("Request error: \(error!)")
......@@ -60,11 +107,11 @@ class ProductTests: XCTestCase {
return
}
if let products = products,
products.count > 0 {
XCTAssertEqual(products.count, self.subscriptionProductIds.count)
XCTAssertEqual(products.first!.identifier, self.subscriptionProductIds.first!)
XCTAssertEqual(products.last!.identifier, self.subscriptionProductIds.last!)
if let subscriptionInfo = subscriptionInfo,
subscriptionInfo.products.count > 0 {
XCTAssertEqual(subscriptionInfo.products.count, self.subscriptionProductIds.count)
XCTAssertEqual(subscriptionInfo.products.first!.identifier, self.subscriptionProductIds.first!)
XCTAssertEqual(subscriptionInfo.products.last!.identifier, self.subscriptionProductIds.last!)
exp.fulfill()
} else {
XCTAssert(error as? ClientError != ClientError.malformedResponseData, "malformedResponseData")
......@@ -72,7 +119,7 @@ class ProductTests: XCTestCase {
exp.fulfill()
}
})
}
waitForExpectations(timeout: 5.0, handler: nil)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment