diff --git a/ios/MullvadVPNUITests/AccountTests.swift b/ios/MullvadVPNUITests/AccountTests.swift index 8b6814b4d0ea..a1fbda5a1ca5 100644 --- a/ios/MullvadVPNUITests/AccountTests.swift +++ b/ios/MullvadVPNUITests/AccountTests.swift @@ -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 @@ -33,7 +26,7 @@ class AccountTests: LoggedOutUITestCase { } func testDeleteAccount() throws { - let accountNumber = mullvadAPIWrapper.createAccount() + let accountNumber = createTemporaryAccountWithoutTime() LoginPage(app) .tapAccountNumberTextField() @@ -99,7 +92,7 @@ class AccountTests: LoggedOutUITestCase { func testLoginToAccountWithTooManyDevices() throws { // Setup - let temporaryAccountNumber = mullvadAPIWrapper.createAccount() + let temporaryAccountNumber = createTemporaryAccountWithoutTime() mullvadAPIWrapper.addDevices(5, account: temporaryAccountNumber) // Teardown @@ -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") diff --git a/ios/MullvadVPNUITests/Base/BaseUITestCase.swift b/ios/MullvadVPNUITests/Base/BaseUITestCase.swift index 0df61d39f235..e7ef66f0fd55 100644 --- a/ios/MullvadVPNUITests/Base/BaseUITestCase.swift +++ b/ios/MullvadVPNUITests/Base/BaseUITestCase.swift @@ -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" @@ -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 { diff --git a/ios/MullvadVPNUITests/Networking/MullvadAPIWrapper.swift b/ios/MullvadVPNUITests/Networking/MullvadAPIWrapper.swift index b936e4259603..5e22d0c522e5 100644 --- a/ios/MullvadVPNUITests/Networking/MullvadAPIWrapper.swift +++ b/ios/MullvadVPNUITests/Networking/MullvadAPIWrapper.swift @@ -86,14 +86,13 @@ 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() @@ -101,20 +100,18 @@ class MullvadAPIWrapper { 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() @@ -122,12 +119,10 @@ class MullvadAPIWrapper { 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 { @@ -136,7 +131,7 @@ 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() @@ -144,7 +139,6 @@ class MullvadAPIWrapper { 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. @@ -157,7 +151,6 @@ 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 { @@ -165,7 +158,7 @@ class MullvadAPIWrapper { 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() @@ -173,21 +166,19 @@ class MullvadAPIWrapper { 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() @@ -195,7 +186,6 @@ class MullvadAPIWrapper { let waitResult = XCTWaiter.wait(for: [requestCompletedExpectation], timeout: throttleWaitTimeout) XCTAssertEqual(waitResult, .completed, "Get devices request completed") - XCTAssertNil(requestError, "Get devices error is nil") return devices }