Skip to content

Commit

Permalink
Mock data for snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
mojganii committed May 13, 2024
1 parent 53364a8 commit 6a8d915
Show file tree
Hide file tree
Showing 19 changed files with 637 additions and 109 deletions.
19 changes: 19 additions & 0 deletions ios/MullvadMockData/MullvadMockData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// MullvadMockData.h
// MullvadMockData
//
// Created by Mojgan on 2024-05-03.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//

#import <Foundation/Foundation.h>

//! Project version number for MullvadMockData.
FOUNDATION_EXPORT double MullvadMockDataVersionNumber;

//! Project version string for MullvadMockData.
FOUNDATION_EXPORT const unsigned char MullvadMockDataVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <MullvadMockData/PublicHeader.h>


Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//

import Foundation
@testable import MullvadREST
@testable import MullvadTypes
@testable import WireGuardKitTypes
import MullvadREST
import MullvadTypes
import WireGuardKitTypes

struct APIProxyStub: APIQuerying {
func getAddressList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

import Foundation
@testable import MullvadREST
@testable import MullvadTypes
import MullvadREST
import MullvadTypes

struct AccessTokenManagerStub: RESTAccessTokenManagement {
func getAccessToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@
//

import Foundation
@testable import MullvadREST
@testable import MullvadTypes
import MullvadREST
import MullvadTypes

struct AccountProxyStubError: Error {}

struct AccountsProxyStub: RESTAccountHandling {
var createAccountResult: Result<REST.NewAccountData, Error>?
var createAccountResult: Result<REST.NewAccountData, Error> = .failure(AccountProxyStubError())
var deleteAccountResult: Result<Void, Error> = .failure(AccountProxyStubError())
func createAccount(
retryStrategy: REST.RetryStrategy,
completion: @escaping MullvadREST.ProxyCompletionHandler<REST.NewAccountData>
completion: @escaping ProxyCompletionHandler<REST.NewAccountData>
) -> Cancellable {
if let createAccountResult = createAccountResult {
completion(createAccountResult)
}
completion(createAccountResult)
return AnyCancellable()
}

func getAccountData(accountNumber: String) -> any RESTRequestExecutor<Account> {
RESTRequestExecutorStub<Account>(success: {
Account(id: accountNumber, expiry: .distantFuture, maxDevices: 1, canAddDevices: true)
Account(
id: accountNumber,
expiry: Calendar.current.date(byAdding: .day, value: 38, to: Date())!,
maxDevices: 1,
canAddDevices: true
)
})
}

Expand All @@ -33,6 +39,7 @@ struct AccountsProxyStub: RESTAccountHandling {
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Void>
) -> Cancellable {
AnyCancellable()
completion(deleteAccountResult)
return AnyCancellable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@
//

import Foundation
@testable import MullvadREST
@testable import MullvadTypes
@testable import WireGuardKitTypes
import MullvadREST
import MullvadTypes
import WireGuardKitTypes

struct DevicesProxyStubError: Error {}

struct DevicesProxyStub: DeviceHandling {
let mockDevice = Device.mock(publicKey: PrivateKey().publicKey)
var deviceResult: Result<Device, Error> = .failure(DevicesProxyStubError())
func getDevice(
accountNumber: String,
identifier: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable {
AnyCancellable()
completion(deviceResult)
return AnyCancellable()
}

func getDevices(
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<[Device]>
) -> Cancellable {
AnyCancellable()
switch deviceResult {
case let .success(success):
completion(.success([success]))
case let .failure(failure):
completion(.failure(failure))
}
return AnyCancellable()
}

func createDevice(
Expand All @@ -36,7 +45,7 @@ struct DevicesProxyStub: DeviceHandling {
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable {
completion(.success(mockDevice))
completion(deviceResult)
return AnyCancellable()
}

Expand All @@ -57,6 +66,7 @@ struct DevicesProxyStub: DeviceHandling {
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable {
AnyCancellable()
completion(deviceResult)
return AnyCancellable()
}
}
52 changes: 52 additions & 0 deletions ios/MullvadMockData/MullvadREST/MockProxyFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// MockProxyFactory.swift
// MullvadMockData
//
// Created by Mojgan on 2024-05-03.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadREST
import MullvadTypes
import WireGuardKitTypes

public struct MockProxyFactory: ProxyFactoryProtocol {
public var configuration: REST.AuthProxyConfiguration

public func createAPIProxy() -> any APIQuerying {
REST.APIProxy(configuration: configuration)
}

public func createAccountsProxy() -> any RESTAccountHandling {
AccountsProxyStub(createAccountResult: .success(.mockValue()))
}

public func createDevicesProxy() -> any DeviceHandling {
DevicesProxyStub(deviceResult: .success(Device.mock(publicKey: PrivateKey().publicKey)))
}

public static func makeProxyFactory(
transportProvider: any RESTTransportProvider,
addressCache: REST.AddressCache
) -> any ProxyFactoryProtocol {
let basicConfiguration = REST.ProxyConfiguration(
transportProvider: transportProvider,
addressCacheStore: addressCache
)

let authenticationProxy = REST.AuthenticationProxy(
configuration: basicConfiguration
)
let accessTokenManager = REST.AccessTokenManager(
authenticationProxy: authenticationProxy
)

let authConfiguration = REST.AuthProxyConfiguration(
proxyConfiguration: basicConfiguration,
accessTokenManager: accessTokenManager
)

return MockProxyFactory(configuration: authConfiguration)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
//

import Foundation
@testable import MullvadREST
@testable import MullvadTypes
import MullvadREST
import MullvadTypes

struct RESTRequestExecutorStub<Success>: RESTRequestExecutor {
typealias Success = Success

var success: (() -> Success)?

func execute(completionHandler: @escaping (Result<Success, Error>) -> Void) -> Cancellable {
AnyCancellable()
if let result = success?() {
completionHandler(.success(result))
}
return AnyCancellable()
}

func execute(
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping (Result<Success, Error>) -> Void
) -> Cancellable {
AnyCancellable()
if let result = success?() {
completionHandler(.success(result))
}
return AnyCancellable()
}

func execute() async throws -> Success {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import MullvadTypes

extension Account {
static func mock(expiry: Date = .distantFuture) -> Account {
public static func mock(expiry: Date = .distantFuture) -> Account {
Account(
id: "account-id",
expiry: expiry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import MullvadTypes
import WireGuardKitTypes

extension Device {
static func mock(publicKey: PublicKey) -> Device {
public static func mock(publicKey: PublicKey) -> Device {
Device(
id: "device-id",
name: "Devicey McDeviceface",
name: "Secure Mole",
pubkey: publicKey,
hijackDNS: false,
created: Date(),
Expand Down
24 changes: 18 additions & 6 deletions ios/MullvadREST/ApiHandlers/RESTProxyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@
//

import Foundation
public protocol ProxyFactoryProtocol {
var configuration: REST.AuthProxyConfiguration { get }

func createAPIProxy() -> APIQuerying
func createAccountsProxy() -> RESTAccountHandling
func createDevicesProxy() -> DeviceHandling

static func makeProxyFactory(
transportProvider: RESTTransportProvider,
addressCache: REST.AddressCache
) -> ProxyFactoryProtocol
}

extension REST {
public final class ProxyFactory {
public let configuration: AuthProxyConfiguration
public final class ProxyFactory: ProxyFactoryProtocol {
public var configuration: AuthProxyConfiguration

public class func makeProxyFactory(
transportProvider: RESTTransportProvider,
addressCache: AddressCache
) -> ProxyFactory {
public static func makeProxyFactory(
transportProvider: any RESTTransportProvider,
addressCache: REST.AddressCache
) -> any ProxyFactoryProtocol {
let basicConfiguration = REST.ProxyConfiguration(
transportProvider: transportProvider,
addressCacheStore: addressCache
Expand Down
6 changes: 0 additions & 6 deletions ios/MullvadRESTTests/Mocks/TimeServerProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,3 @@ final class TimeServerProxy: REST.Proxy<REST.ProxyConfiguration> {
struct TimeResponse: Codable {
var dateTime: Date
}

extension REST.ProxyFactory {
func createTimeServerProxy() -> TimeServerProxy {
return TimeServerProxy(configuration: configuration)
}
}
2 changes: 1 addition & 1 deletion ios/MullvadRESTTests/RequestExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class RequestExecutorTests: XCTestCase {
transportProvider: transportProvider,
addressCache: addressCache
)
timerServerProxy = proxyFactory.createTimeServerProxy()
timerServerProxy = TimeServerProxy(configuration: proxyFactory.configuration)
}

func testExecuteAsync() async throws {
Expand Down
Loading

0 comments on commit 6a8d915

Please sign in to comment.