Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 committed Feb 17, 2025
1 parent 9c83326 commit 6e000d1
Showing 1 changed file with 50 additions and 26 deletions.
76 changes: 50 additions & 26 deletions WultraMobileTokenSDKTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import XCTest
import PowerAuth2
@testable import WultraMobileTokenSDK


/**
For integration test to be successfully executed, you need to provide
configuration json file. To more information, visit `WultraMobileTokenSDKTests/Configs/Readme.md`.
Expand All @@ -33,6 +32,8 @@ class IntegrationTests: XCTestCase {

private let pin = "1234"

private let defaultTimeout: TimeInterval = 30 // quite some time, because azure infra is very slow simetimes

override func setUp() {
super.setUp()
WMTLogger.verboseLevel = .debug
Expand All @@ -50,7 +51,7 @@ class IntegrationTests: XCTestCase {
}

let waiter = XCTWaiter()
waiter.wait(for: [exp], timeout: 20)
waiter.wait(for: [exp], timeout: defaultTimeout)
}

override func tearDown() {
Expand All @@ -69,7 +70,7 @@ class IntegrationTests: XCTestCase {
}

let waiter = XCTWaiter()
waiter.wait(for: [exp], timeout: 20)
waiter.wait(for: [exp], timeout: defaultTimeout)
}

/// By default, operation list should be empty
Expand All @@ -88,7 +89,7 @@ class IntegrationTests: XCTestCase {

}

waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}


Expand Down Expand Up @@ -135,7 +136,7 @@ class IntegrationTests: XCTestCase {
}
}

waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}

/// Test of the Operation cancel
Expand All @@ -145,10 +146,7 @@ class IntegrationTests: XCTestCase {
proxy.createNonPersonalisedPACOperation { op in
if let op {
DispatchQueue.main.async {
guard let operation = self.ops.getDetail(operationId: op.operationId, completion: { _ in
XCTFail("Operation should be already canceled")
exp.fulfill()
}) else {
guard let operation = self.ops.getDetail(operationId: op.operationId, completion: { _ in }) else {
XCTFail("Failed to create operation")
exp.fulfill()
return
Expand All @@ -166,7 +164,7 @@ class IntegrationTests: XCTestCase {
}

// Wait for expectation to be fulfilled
waitForExpectations(timeout: 5, handler: nil)
waitForExpectations(timeout: 10, handler: nil)
}

func testOperationCanceledWithReason() {
Expand Down Expand Up @@ -203,7 +201,7 @@ class IntegrationTests: XCTestCase {
}
}
}
waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}

/// Operation IDs should be equal
Expand Down Expand Up @@ -257,7 +255,7 @@ class IntegrationTests: XCTestCase {
}
}

waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}

/// `currentServerDate` was removed from WMTOperations in favor of more precise powerAuth timeService
Expand Down Expand Up @@ -398,7 +396,7 @@ class IntegrationTests: XCTestCase {
}
}

waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}

/// Test of Payment rejecting (1FA)
Expand Down Expand Up @@ -436,7 +434,7 @@ class IntegrationTests: XCTestCase {
}
}

waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}

/// Testing that operation polling works.
Expand Down Expand Up @@ -487,7 +485,7 @@ class IntegrationTests: XCTestCase {
}
}

waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}

func testOperationChangedDelegate() {
Expand Down Expand Up @@ -627,11 +625,15 @@ class IntegrationTests: XCTestCase {
}

func testQROperation() {

let exp = expectation(description: "QR Operation integration test")
let measure = MeasureElapsed("QROperation")

// create regular operation
proxy.createOperation { op in

measure.mark("Operation created")

guard let op = op else {
XCTFail("Failed to create operation")
exp.fulfill()
Expand All @@ -640,6 +642,9 @@ class IntegrationTests: XCTestCase {

// get QR data of the operation
self.proxy.getQROperation(operation: op) { qrData in

measure.mark("QR data retrieved")

guard let qrData = qrData else {
XCTFail("Failed to retrieve QR data")
exp.fulfill()
Expand All @@ -653,13 +658,18 @@ class IntegrationTests: XCTestCase {
let auth = PowerAuthAuthentication.possessionWithPassword(password: self.pin)

// get the OTP with the "offline" signing
_ = self.ops.authorize(qrOperation: qrOp, authentication: auth) { qrAuthResult in
self.ops.authorize(qrOperation: qrOp, authentication: auth) { qrAuthResult in

measure.mark("Authorized")

switch qrAuthResult {
case .success(let otp):

// verify the operation on the backend with the OTP
self.proxy.verifyQROperation(operation: op, operationData: qrData, otp: otp) { verified in

measure.mark("OTP verified")

print("Operation verified with \(verified?.otpValid.description ?? "ERROR") result")

// success?
Expand All @@ -681,8 +691,7 @@ class IntegrationTests: XCTestCase {
}
}
}
// there are 3 backend calls, give it some time...
waitForExpectations(timeout: 20, handler: nil)
waitForExpectations(timeout: defaultTimeout, handler: nil)
}

// MARK: - Inbox
Expand All @@ -706,7 +715,7 @@ class IntegrationTests: XCTestCase {
}
getMessages.fulfill()
}
XCTWaiter().wait(for: [getMessages], timeout: 20)
XCTWaiter().wait(for: [getMessages], timeout: defaultTimeout)

// Now test received messages
compareMessages(expected: messages, received: messageList)
Expand All @@ -724,7 +733,7 @@ class IntegrationTests: XCTestCase {
}
getMessageDetail.fulfill()
}
XCTWaiter().wait(for: [getMessageDetail], timeout: 20)
XCTWaiter().wait(for: [getMessageDetail], timeout: defaultTimeout)

XCTAssertNotNil(messageDetail)
XCTAssertEqual(firstMessage.id, messageDetail?.id)
Expand All @@ -751,7 +760,7 @@ class IntegrationTests: XCTestCase {
}
getAllMessages.fulfill()
}
XCTWaiter().wait(for: [getAllMessages], timeout: 20)
XCTWaiter().wait(for: [getAllMessages], timeout: defaultTimeout)
XCTAssertEqual(count, receivedMessages.count)
compareMessages(expected: messages, received: receivedMessages)
}
Expand Down Expand Up @@ -786,7 +795,7 @@ class IntegrationTests: XCTestCase {
}
setMessageAsRead.fulfill()
}
XCTWaiter().wait(for: [setMessageAsRead, readMessageDetail], timeout: 20)
XCTWaiter().wait(for: [setMessageAsRead, readMessageDetail], timeout: defaultTimeout)

// Now update list
receivedMessages = fetchAllMessages(onlyUnread: true)
Expand Down Expand Up @@ -843,7 +852,7 @@ class IntegrationTests: XCTestCase {
}
setMessagesAsRead.fulfill()
}
XCTWaiter().wait(for: [setMessagesAsRead, allReadMessages, allUnreadMessages], timeout: 20)
XCTWaiter().wait(for: [setMessagesAsRead, allReadMessages, allUnreadMessages], timeout: defaultTimeout)
XCTAssertEqual(0, allMsgsUnread.count)
XCTAssertEqual(count, allMsgsRead.count)

Expand All @@ -864,7 +873,7 @@ class IntegrationTests: XCTestCase {
}
getMessagesCount.fulfill()
}
XCTWaiter().wait(for: [getMessagesCount], timeout: 20)
XCTWaiter().wait(for: [getMessagesCount], timeout: defaultTimeout)
return receivedCount
}

Expand All @@ -880,7 +889,7 @@ class IntegrationTests: XCTestCase {
}
getAllMessages.fulfill()
}
XCTWaiter().wait(for: [getAllMessages], timeout: 20)
XCTWaiter().wait(for: [getAllMessages], timeout: defaultTimeout)
return receivedMessages
}

Expand All @@ -891,7 +900,7 @@ class IntegrationTests: XCTestCase {
messages = msgs
prepareExp.fulfill()
}
XCTWaiter().wait(for: [prepareExp], timeout: 20)
XCTWaiter().wait(for: [prepareExp], timeout: defaultTimeout)
XCTAssertEqual(count, messages.count)
return messages
}
Expand Down Expand Up @@ -946,3 +955,18 @@ private extension Array where Element == WMTInboxMessage {
return nil
}
}

class MeasureElapsed {

private let started = Date()
private let name: String

init(_ name: String) {
self.name = name
print("Measuring elapsed time: \(name)")
}

func mark(_ reason: String? = nil) {
print("Elapsed time for '\(name)\(reason != nil ? ":\(reason!)" : "")' - \(String(format: "%.3f", Date().timeIntervalSince(started)))s.")
}
}

0 comments on commit 6e000d1

Please sign in to comment.