138 lines
5.0 KiB
Swift
138 lines
5.0 KiB
Swift
//
|
|
// CustomerRepoTests.swift
|
|
// PrivadoTests
|
|
//
|
|
// Created by Juraldinio on 2/16/22.
|
|
// Copyright © 2022 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import XCTest
|
|
@testable import PrivadoVPN
|
|
|
|
final class CustomerRepoTests: XCTestCase {
|
|
|
|
struct CustomerRepoEnvironment: NetworkEnvironment {
|
|
var host: String { "" }
|
|
var stub: StubBehavior? { .immediate }
|
|
|
|
func sampleData(for service: ServiceType) -> Data? {
|
|
switch service {
|
|
case CustomerService.customerData:
|
|
let url = Bundle(for: CustomerRepoTests.self).url(forResource: "Customer", withExtension: "data")
|
|
return try! Data(contentsOf: url!)
|
|
default: return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func testSuccessParse() throws {
|
|
|
|
let expect = self.expectation(description: "Callback called")
|
|
expect.expectedFulfillmentCount = 1
|
|
|
|
let repo = CustomerRepo(with: NetworkRouter(environment: CustomerRepoEnvironment()))
|
|
repo.getCustomerData { result in
|
|
expect.fulfill()
|
|
XCTAssertTrue(result.isSuccess)
|
|
|
|
let customer = result.optional!!
|
|
XCTAssertEqual(customer.username, "CustomerUsername")
|
|
XCTAssertEqual(customer.email, "CustomerUsername@someemail.com")
|
|
XCTAssertEqual(customer.trafficTotal, 200000)
|
|
XCTAssertEqual(customer.trafficLeft, 100000)
|
|
XCTAssertFalse(customer.isPremium)
|
|
XCTAssertEqual(customer.connectionsLimit, 42)
|
|
XCTAssertEqual(customer.startDate, "2022-02-16T18:20:34.009")
|
|
XCTAssertEqual(customer.endDate, "2022-04-17T18:20:34.008")
|
|
XCTAssertEqual(customer.plan, .premium(value: "PREMIUM120"))
|
|
XCTAssertEqual(customer.speedLimit, 146)
|
|
XCTAssertEqual(customer.premiumDate, "2022-02-16T18:20:34.028")
|
|
XCTAssertEqual(customer.serverGroup, "Premium")
|
|
XCTAssertEqual(customer.loginUrl, "http://juraldinio.com")
|
|
XCTAssertTrue(customer.isVPNActive!)
|
|
XCTAssertNil(customer.localIp)
|
|
|
|
let protocols = customer.protocols
|
|
XCTAssertNotNil(protocols)
|
|
XCTAssertNotNil(protocols?.ios)
|
|
|
|
let sorted = protocols?.ios?
|
|
.map { $0.rawValue }
|
|
.sorted(by: { $0 < $1 })
|
|
XCTAssertEqual(sorted, ["IKEV2", "OPENVPN/UDP/1194"])
|
|
}
|
|
|
|
waitForExpectations(timeout: 0.2, handler: nil)
|
|
}
|
|
|
|
func testFailureParse() throws {
|
|
|
|
struct CustomerRepoFailureEnvironment: NetworkEnvironment {
|
|
var host: String { "" }
|
|
var stub: StubBehavior? { .immediate }
|
|
|
|
func sampleData(for service: ServiceType) -> Data? { nil }
|
|
}
|
|
|
|
let expect = self.expectation(description: "Callback called")
|
|
expect.expectedFulfillmentCount = 1
|
|
|
|
let repo = CustomerRepo(with: NetworkRouter(environment: CustomerRepoFailureEnvironment()))
|
|
repo.getCustomerData { result in
|
|
expect.fulfill()
|
|
XCTAssertFalse(result.isSuccess)
|
|
|
|
let error = result.maybeError!
|
|
XCTAssertTrue(error is DecodingError, "Error must be Decoding Error")
|
|
}
|
|
|
|
waitForExpectations(timeout: 0.2, handler: nil)
|
|
}
|
|
|
|
func testDummyCustomer() {
|
|
let repo = CustomerRepo(with: NetworkRouter())
|
|
let customer = repo.createDummyPremiumCustomer()
|
|
|
|
XCTAssertEqual(customer.plan, .premium(value: ""))
|
|
XCTAssertTrue(customer.email.isEmpty)
|
|
XCTAssertTrue(customer.endDate.isEmpty)
|
|
XCTAssertTrue(customer.username.isEmpty)
|
|
XCTAssertTrue(customer.isPremium, "Must be premium")
|
|
XCTAssertTrue(customer.startDate.isEmpty)
|
|
XCTAssertNil(customer.premiumDate)
|
|
XCTAssertEqual(customer.serverGroup, "Premium")
|
|
XCTAssertEqual(customer.trafficLeft, 0)
|
|
XCTAssertEqual(customer.trafficTotal, 0)
|
|
XCTAssertEqual(customer.speedLimit, 0)
|
|
XCTAssertNil(customer.connectionsLimit)
|
|
XCTAssertNil(customer.loginUrl)
|
|
XCTAssertTrue(customer.isVPNActive!, "Must be VPN active")
|
|
XCTAssertNil(customer.localIp)
|
|
XCTAssertNil(customer.protocols)
|
|
}
|
|
|
|
func testCustomerEmitter() throws {
|
|
|
|
var currentCustomer: Customer? = nil
|
|
|
|
let expect = self.expectation(description: "Callback called")
|
|
expect.expectedFulfillmentCount = 2
|
|
|
|
let repo = CustomerRepo(with: NetworkRouter(environment: CustomerRepoEnvironment()))
|
|
repo.getCustomerData { result in
|
|
expect.fulfill()
|
|
XCTAssertTrue(result.isSuccess)
|
|
XCTAssertNotNil(result.optional!)
|
|
currentCustomer = result.optional!!
|
|
}
|
|
|
|
repo.customerEmitter.addReaction { customer in
|
|
expect.fulfill()
|
|
XCTAssertEqual(currentCustomer, customer)
|
|
return false
|
|
}
|
|
|
|
waitForExpectations(timeout: 0.3, handler: nil)
|
|
}
|
|
}
|