2022-07-06 14:49:05 +01:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2022-07-01 13:56:52 +03:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2022-07-01 13:56:52 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2022-07-06 14:49:05 +01:00
|
|
|
import XCTest
|
2022-07-01 13:56:52 +03:00
|
|
|
|
|
|
|
@testable import ElementX
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class SessionVerificationViewModelTests: XCTestCase {
|
2023-04-24 18:48:07 +03:00
|
|
|
var viewModel: SessionVerificationScreenViewModelProtocol!
|
2022-07-01 13:56:52 +03:00
|
|
|
var context: SessionVerificationViewModelType.Context!
|
2023-03-08 17:04:31 +01:00
|
|
|
var sessionVerificationController: SessionVerificationControllerProxyMock!
|
2022-07-01 13:56:52 +03:00
|
|
|
|
|
|
|
override func setUpWithError() throws {
|
2023-03-08 17:04:31 +01:00
|
|
|
sessionVerificationController = SessionVerificationControllerProxyMock.configureMock()
|
2025-02-10 20:07:11 +02:00
|
|
|
viewModel = SessionVerificationScreenViewModel(sessionVerificationControllerProxy: sessionVerificationController,
|
|
|
|
flow: .deviceInitiator,
|
|
|
|
appSettings: AppSettings(),
|
|
|
|
mediaProvider: MediaProviderMock(configuration: .init()))
|
2022-07-01 13:56:52 +03:00
|
|
|
context = viewModel.context
|
|
|
|
}
|
2022-08-11 15:02:47 +03:00
|
|
|
|
2023-03-08 17:04:31 +01:00
|
|
|
func testRequestVerification() async throws {
|
2022-07-01 13:56:52 +03:00
|
|
|
XCTAssertEqual(context.viewState.verificationState, .initial)
|
|
|
|
|
2023-01-05 18:06:39 +02:00
|
|
|
context.send(viewAction: .requestVerification)
|
2022-07-01 13:56:52 +03:00
|
|
|
|
2023-03-08 17:04:31 +01:00
|
|
|
try await Task.sleep(for: .milliseconds(100))
|
2025-02-10 20:07:11 +02:00
|
|
|
XCTAssert(sessionVerificationController.requestDeviceVerificationCallsCount == 1)
|
2022-07-01 13:56:52 +03:00
|
|
|
XCTAssertEqual(context.viewState.verificationState, .requestingVerification)
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:56:17 +01:00
|
|
|
func testVerificationCancellation() async throws {
|
2022-07-01 13:56:52 +03:00
|
|
|
XCTAssertEqual(context.viewState.verificationState, .initial)
|
|
|
|
|
2023-01-05 18:06:39 +02:00
|
|
|
context.send(viewAction: .requestVerification)
|
2022-07-01 13:56:52 +03:00
|
|
|
|
2024-03-21 14:01:23 +02:00
|
|
|
viewModel.stop()
|
2022-07-01 13:56:52 +03:00
|
|
|
|
|
|
|
XCTAssertEqual(context.viewState.verificationState, .cancelling)
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
let deferred = deferFulfillment(context.$viewState) { state in
|
|
|
|
state.verificationState == .cancelled
|
|
|
|
}
|
|
|
|
|
|
|
|
try await deferred.fulfill()
|
2022-07-01 13:56:52 +03:00
|
|
|
|
|
|
|
XCTAssertEqual(context.viewState.verificationState, .cancelled)
|
|
|
|
|
|
|
|
context.send(viewAction: .restart)
|
|
|
|
|
|
|
|
XCTAssertEqual(context.viewState.verificationState, .initial)
|
2023-03-08 17:04:31 +01:00
|
|
|
|
2025-02-10 20:07:11 +02:00
|
|
|
XCTAssert(sessionVerificationController.requestDeviceVerificationCallsCount == 1)
|
2023-03-08 17:04:31 +01:00
|
|
|
XCTAssert(sessionVerificationController.cancelVerificationCallsCount == 1)
|
2022-07-01 13:56:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testReceiveChallenge() {
|
|
|
|
setupChallengeReceived()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAcceptChallenge() {
|
|
|
|
setupChallengeReceived()
|
|
|
|
|
|
|
|
let waitForAcceptance = XCTestExpectation(description: "Wait for acceptance")
|
|
|
|
|
2024-10-29 15:21:17 +02:00
|
|
|
let cancellable = sessionVerificationController.actions
|
2023-01-18 13:28:59 +00:00
|
|
|
.delay(for: .seconds(0.1), scheduler: DispatchQueue.main) // Allow the view model to process the callback first.
|
2022-07-01 13:56:52 +03:00
|
|
|
.sink { callback in
|
|
|
|
switch callback {
|
|
|
|
case .finished:
|
|
|
|
waitForAcceptance.fulfill()
|
|
|
|
default:
|
|
|
|
XCTFail("Unexpected session verification controller callback")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
|
|
|
cancellable.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
context.send(viewAction: .accept)
|
|
|
|
|
|
|
|
wait(for: [waitForAcceptance], timeout: 10.0)
|
|
|
|
|
|
|
|
XCTAssertEqual(context.viewState.verificationState, .verified)
|
2023-03-08 17:04:31 +01:00
|
|
|
XCTAssert(sessionVerificationController.approveVerificationCallsCount == 1)
|
2022-07-01 13:56:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testDeclineChallenge() {
|
|
|
|
setupChallengeReceived()
|
|
|
|
|
|
|
|
let expectation = XCTestExpectation(description: "Wait for cancellation")
|
|
|
|
|
2024-10-29 15:21:17 +02:00
|
|
|
let cancellable = sessionVerificationController.actions
|
2023-01-18 13:28:59 +00:00
|
|
|
.delay(for: .seconds(0.1), scheduler: DispatchQueue.main) // Allow the view model to process the callback first.
|
2022-07-01 13:56:52 +03:00
|
|
|
.sink { callback in
|
|
|
|
switch callback {
|
|
|
|
case .cancelled:
|
|
|
|
expectation.fulfill()
|
|
|
|
default:
|
|
|
|
XCTFail("Unexpected session verification controller callback")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
|
|
|
cancellable.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
context.send(viewAction: .decline)
|
|
|
|
|
|
|
|
wait(for: [expectation], timeout: 10.0)
|
|
|
|
|
|
|
|
XCTAssertEqual(context.viewState.verificationState, .cancelled)
|
2023-03-08 17:04:31 +01:00
|
|
|
XCTAssert(sessionVerificationController.declineVerificationCallsCount == 1)
|
2022-07-01 13:56:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private func setupChallengeReceived() {
|
2023-01-05 18:06:39 +02:00
|
|
|
let requestAcceptanceExpectation = XCTestExpectation(description: "Wait for request acceptance")
|
|
|
|
let sasVerificationStartExpectation = XCTestExpectation(description: "Wait for SaS verification start")
|
|
|
|
let verificationDataReceivalExpectation = XCTestExpectation(description: "Wait for Emoji data")
|
2022-07-01 13:56:52 +03:00
|
|
|
|
2024-10-29 15:21:17 +02:00
|
|
|
let cancellable = sessionVerificationController.actions
|
2023-01-18 13:28:59 +00:00
|
|
|
.delay(for: .seconds(0.1), scheduler: DispatchQueue.main) // Allow the view model to process the callback first.
|
2022-07-01 13:56:52 +03:00
|
|
|
.sink { callback in
|
|
|
|
switch callback {
|
2023-01-05 18:06:39 +02:00
|
|
|
case .acceptedVerificationRequest:
|
|
|
|
requestAcceptanceExpectation.fulfill()
|
|
|
|
case .startedSasVerification:
|
|
|
|
sasVerificationStartExpectation.fulfill()
|
2022-07-01 13:56:52 +03:00
|
|
|
case .receivedVerificationData:
|
2023-01-05 18:06:39 +02:00
|
|
|
verificationDataReceivalExpectation.fulfill()
|
2022-07-01 13:56:52 +03:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
|
|
|
cancellable.cancel()
|
|
|
|
}
|
|
|
|
|
2023-01-05 18:06:39 +02:00
|
|
|
context.send(viewAction: .requestVerification)
|
|
|
|
wait(for: [requestAcceptanceExpectation], timeout: 10.0)
|
|
|
|
XCTAssertEqual(context.viewState.verificationState, .verificationRequestAccepted)
|
2022-07-01 13:56:52 +03:00
|
|
|
|
2023-01-05 18:06:39 +02:00
|
|
|
context.send(viewAction: .startSasVerification)
|
|
|
|
wait(for: [sasVerificationStartExpectation], timeout: 10.0)
|
|
|
|
XCTAssertEqual(context.viewState.verificationState, .sasVerificationStarted)
|
2022-07-01 13:56:52 +03:00
|
|
|
|
2023-01-05 18:06:39 +02:00
|
|
|
wait(for: [verificationDataReceivalExpectation], timeout: 10.0)
|
2023-03-08 17:04:31 +01:00
|
|
|
XCTAssertEqual(context.viewState.verificationState, .showingChallenge(emojis: SessionVerificationControllerProxyMock.emojis))
|
|
|
|
|
2025-02-10 20:07:11 +02:00
|
|
|
XCTAssert(sessionVerificationController.requestDeviceVerificationCallsCount == 1)
|
2023-03-08 17:04:31 +01:00
|
|
|
XCTAssert(sessionVerificationController.startSasVerificationCallsCount == 1)
|
2022-07-01 13:56:52 +03:00
|
|
|
}
|
|
|
|
}
|