Skip to content

Commit

Permalink
Create accounts using partner API when available
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasberglund authored and buggmagnet committed Sep 24, 2024
1 parent 071548d commit 4fd7110
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
13 changes: 3 additions & 10 deletions ios/MullvadVPNUITests/AccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
import XCTest

class AccountTests: LoggedOutUITestCase {
lazy var mullvadAPIWrapper: MullvadAPIWrapper = {
do {
// swiftlint:disable:next force_try
return try! MullvadAPIWrapper()
}
}()

override func setUpWithError() throws {
continueAfterFailure = false

Expand All @@ -33,7 +26,7 @@ class AccountTests: LoggedOutUITestCase {
}

func testDeleteAccount() throws {
let accountNumber = mullvadAPIWrapper.createAccount()
let accountNumber = createTemporaryAccountWithoutTime()

LoginPage(app)
.tapAccountNumberTextField()
Expand Down Expand Up @@ -99,7 +92,7 @@ class AccountTests: LoggedOutUITestCase {

func testLoginToAccountWithTooManyDevices() throws {
// Setup
let temporaryAccountNumber = mullvadAPIWrapper.createAccount()
let temporaryAccountNumber = createTemporaryAccountWithoutTime()
mullvadAPIWrapper.addDevices(5, account: temporaryAccountNumber)

// Teardown
Expand Down Expand Up @@ -133,7 +126,7 @@ class AccountTests: LoggedOutUITestCase {
}

func testLogOut() throws {
let newAccountNumber = mullvadAPIWrapper.createAccount()
let newAccountNumber = createTemporaryAccountWithoutTime()
login(accountNumber: newAccountNumber)
XCTAssertEqual(try mullvadAPIWrapper.getDevices(newAccountNumber).count, 1, "Account has one device")

Expand Down
18 changes: 18 additions & 0 deletions ios/MullvadVPNUITests/Base/BaseUITestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ class BaseUITestCase: XCTestCase {
.infoDictionary?["IOSDevicePinCode"] as! String
let attachAppLogsOnFailure = Bundle(for: BaseUITestCase.self)
.infoDictionary?["AttachAppLogsOnFailure"] as! String == "1"
let partnerApiToken = Bundle(for: BaseUITestCase.self).infoDictionary?["PartnerApiToken"] as? String
// swiftlint:enable force_cast

lazy var mullvadAPIWrapper: MullvadAPIWrapper = {
do {
// swiftlint:disable:next force_try
return try! MullvadAPIWrapper()
}
}()

static func testDeviceIsIPad() -> Bool {
if let testDeviceIsIPad = Bundle(for: BaseUITestCase.self).infoDictionary?["TestDeviceIsIPad"] as? String {
return testDeviceIsIPad == "1"
Expand Down Expand Up @@ -80,6 +88,16 @@ class BaseUITestCase: XCTestCase {
}
}

/// Create temporary account without time. Will be created using partner API if token is configured, else falling back to app API
func createTemporaryAccountWithoutTime() -> String {
if let partnerApiToken {
let partnerAPIClient = PartnerAPIClient()
return partnerAPIClient.createAccount()
} else {
return mullvadAPIWrapper.createAccount()
}
}

/// Get an account number without time. If an account without time is specified in the configuration file that account will be used, else a temporary account will be created.
func getAccountWithoutTime() -> String {
if let configuredAccountWithoutTime = bundleNoTimeAccountNumber, !configuredAccountWithoutTime.isEmpty {
Expand Down
20 changes: 5 additions & 15 deletions ios/MullvadVPNUITests/Networking/MullvadAPIWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,48 +86,43 @@ class MullvadAPIWrapper {

func createAccount() -> String {
var accountNumber = String()
var requestError: Error?
let requestCompletedExpectation = XCTestExpectation(description: "Create account request completed")

throttle {
do {
accountNumber = try self.mullvadAPI.createAccount()
} catch {
requestError = MullvadAPIError.requestError
XCTFail("Failed to create account with error: \(error.localizedDescription)")
}

requestCompletedExpectation.fulfill()
}

let waitResult = XCTWaiter().wait(for: [requestCompletedExpectation], timeout: throttleWaitTimeout)
XCTAssertEqual(waitResult, .completed, "Create account request completed")
XCTAssertNil(requestError, "Create account error is nil")

return accountNumber
}

func deleteAccount(_ accountNumber: String) {
var requestError: Error?
let requestCompletedExpectation = XCTestExpectation(description: "Delete account request completed")

throttle {
do {
try self.mullvadAPI.delete(account: accountNumber)
} catch {
requestError = MullvadAPIError.requestError
XCTFail("Failed to delete account with error: \(error.localizedDescription)")
}

requestCompletedExpectation.fulfill()
}

let waitResult = XCTWaiter().wait(for: [requestCompletedExpectation], timeout: throttleWaitTimeout)
XCTAssertEqual(waitResult, .completed, "Delete account request completed")
XCTAssertNil(requestError, "Delete account error is nil")
}

/// Add another device to specified account. A dummy WireGuard key will be generated.
func addDevice(_ account: String) {
var addDeviceError: Error?
let requestCompletedExpectation = XCTestExpectation(description: "Add device request completed")

throttle {
Expand All @@ -136,15 +131,14 @@ class MullvadAPIWrapper {
do {
try self.mullvadAPI.addDevice(forAccount: account, publicKey: devicePublicKey)
} catch {
addDeviceError = MullvadAPIError.requestError
XCTFail("Failed to add device with error: \(error.localizedDescription)")
}

requestCompletedExpectation.fulfill()
}

let waitResult = XCTWaiter().wait(for: [requestCompletedExpectation], timeout: throttleWaitTimeout)
XCTAssertEqual(waitResult, .completed, "Add device request completed")
XCTAssertNil(addDeviceError, "Add device error is nil")
}

/// Add multiple devices to specified account. Dummy WireGuard keys will be generated.
Expand All @@ -157,45 +151,41 @@ class MullvadAPIWrapper {

func getAccountExpiry(_ account: String) throws -> Date {
var accountExpiryDate: Date = .distantPast
var requestError: Error?
let requestCompletedExpectation = XCTestExpectation(description: "Get account expiry request completed")

throttle {
do {
let accountExpiryTimestamp = Double(try self.mullvadAPI.getExpiry(forAccount: account))
accountExpiryDate = Date(timeIntervalSince1970: accountExpiryTimestamp)
} catch {
requestError = MullvadAPIError.requestError
XCTFail("Failed to get account expiry with error: \(error.localizedDescription)")
}

requestCompletedExpectation.fulfill()
}

let waitResult = XCTWaiter().wait(for: [requestCompletedExpectation], timeout: throttleWaitTimeout)
XCTAssertEqual(waitResult, .completed, "Get account expiry request completed")
XCTAssertNil(requestError, "Get account expiry error is nil")

return accountExpiryDate
}

func getDevices(_ account: String) throws -> [Device] {
var devices: [Device] = []
var requestError: Error?
let requestCompletedExpectation = XCTestExpectation(description: "Get devices request completed")

throttle {
do {
devices = try self.mullvadAPI.listDevices(forAccount: account)
} catch {
requestError = MullvadAPIError.requestError
XCTFail("Failed to get devices with error: \(error.localizedDescription)")
}

requestCompletedExpectation.fulfill()
}

let waitResult = XCTWaiter.wait(for: [requestCompletedExpectation], timeout: throttleWaitTimeout)
XCTAssertEqual(waitResult, .completed, "Get devices request completed")
XCTAssertNil(requestError, "Get devices error is nil")

return devices
}
Expand Down

0 comments on commit 4fd7110

Please sign in to comment.