Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: MLS service UT - WPB-16280 #2590

Open
wants to merge 2 commits into
base: release/cycle-3.118
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions wire-ios-data-model/Tests/MLS/MLSServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@testable import WireDataModel
@testable import WireDataModelSupport

// NOTE: necessary since CryptoError doesn't publically conform to Error

Check warning on line 29 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Conformance of 'CryptoError' to protocol 'Error' was already stated in the type's module 'WireCoreCrypto'

Conformance of 'CryptoError' to protocol 'Error' was already stated in the type's module 'WireCoreCrypto'
extension CryptoError: Error {}

final class MLSServiceTests: ZMConversationTestsBase, MLSServiceDelegate {
Expand Down Expand Up @@ -349,7 +349,7 @@
)

// Then
await fulfillment(of: [expectation], timeout: 1)

Check warning on line 352 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Instance method 'waitForAllGroupsToBeEmpty' is unavailable from asynchronous contexts; Don't use wait in async contexts!; this is an error in the Swift 6 language mode

Instance method 'waitForAllGroupsToBeEmpty' is unavailable from asynchronous contexts; Don't use wait in async contexts!; this is an error in the Swift 6 language mode
_ = waitForAllGroupsToBeEmpty(withTimeout: 0.5)
}

Expand Down Expand Up @@ -379,7 +379,7 @@
))
}

// When

Check warning on line 382 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Result of call to 'createGroup(for:removalKeys:)' is unused

Result of call to 'createGroup(for:removalKeys:)' is unused
try await sut.createGroup(for: groupID)

// Then
Expand Down Expand Up @@ -408,7 +408,7 @@
}

// when / then
do {

Check warning on line 411 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Result of call to 'createGroup(for:removalKeys:)' is unused

Result of call to 'createGroup(for:removalKeys:)' is unused
try await sut.createGroup(for: groupID)
XCTFail("Unexpected success")
} catch MLSService.MLSGroupCreationError.failedToCreateGroup {
Expand Down Expand Up @@ -493,7 +493,7 @@
))
}

// When

Check warning on line 496 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Result of call to 'establishGroup(for:with:removalKeys:)' is unused

Result of call to 'establishGroup(for:with:removalKeys:)' is unused
try await sut.establishGroup(for: groupID, with: [])

// Then
Expand Down Expand Up @@ -550,7 +550,7 @@
))
}

// When

Check warning on line 553 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Result of call to 'establishGroup(for:with:removalKeys:)' is unused

Result of call to 'establishGroup(for:with:removalKeys:)' is unused
try await sut.establishGroup(for: groupID, with: users)

// Then
Expand Down Expand Up @@ -600,7 +600,7 @@
mockCoreCrypto.wipeConversationConversationId_MockMethod = { _ in }

// When
await assertItThrows(error: MLSService.MLSAddMembersError.failedToClaimKeyPackages(users: usersIncludingSelf)) {

Check warning on line 603 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Result of call to 'establishGroup(for:with:removalKeys:)' is unused

Result of call to 'establishGroup(for:with:removalKeys:)' is unused
try await sut.establishGroup(for: groupID, with: users)
}

Expand Down Expand Up @@ -984,7 +984,7 @@
// Mock no subconversations
mockSubconversationGroupIDRepository.fetchSubconversationGroupIDForTypeParentGroupID_MockValue = .some(nil)

// Mock committing pending proposal.

Check warning on line 987 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Initialization of variable 'mockCommitPendingProposalArguments' was never used; consider replacing with assignment to '_' or removing it

Initialization of variable 'mockCommitPendingProposalArguments' was never used; consider replacing with assignment to '_' or removing it
var mockCommitPendingProposalArguments = [(MLSGroupID, Date)]()
let updateEvent = dummyMemberJoinEvent()

Expand Down Expand Up @@ -1261,7 +1261,7 @@
XCTAssertEqual(subgroupInvocations.first?.type, .conference)
XCTAssertEqual(subgroupInvocations.first?.parentGroupID, parentGroupdID)

// Then we try to commit pending proposals twice, once for the subgroup, once for the parent

Check warning on line 1264 in wire-ios-data-model/Tests/MLS/MLSServiceTests.swift

View workflow job for this annotation

GitHub Actions / Test Results

Variable 'mockCommitPendingProposalArguments' was never mutated; consider changing to 'let' constant

Variable 'mockCommitPendingProposalArguments' was never mutated; consider changing to 'let' constant
var mockCommitPendingProposalArguments = await commitPendingProposalsArgumentsActor.items
XCTAssertEqual(mockCommitPendingProposalArguments.count, 2)
let (id1, commitTime1) = try XCTUnwrap(mockCommitPendingProposalArguments.first)
Expand Down Expand Up @@ -1471,49 +1471,45 @@

func test_RepairOutOfSyncConversations_RejoinsOutOfSyncConversations() async throws {
// GIVEN
let conversationAndOutOfSyncTuples = await uiMOC.perform { [self] in
let outOfSyncGroupID1 = MLSGroupID.random()
let outOfSyncGroupID2 = MLSGroupID.random()
let inSyncGroupID3 = MLSGroupID.random()
let currentEpoch: UInt64 = 1

_ = await uiMOC.perform { [self] in
[
createConversation(outOfSync: true),
createConversation(outOfSync: true),
createConversation(outOfSync: false)
createConversation(outOfSync: true, currentEpoch: currentEpoch, groupID: outOfSyncGroupID1),
createConversation(outOfSync: true, currentEpoch: currentEpoch, groupID: outOfSyncGroupID2),
createConversation(outOfSync: false, currentEpoch: currentEpoch, groupID: inSyncGroupID3)
]
}

await uiMOC.perform {
// mock conversation epoch
self.mockCoreCrypto.conversationEpochConversationId_MockMethod = { groupID in
guard let tuple = conversationAndOutOfSyncTuples.first(
where: { element in
self.uiMOC.performGroupedAndWait {
element.conversation.mlsGroupID?.data
} == groupID
}
) else {
return 1
}
let isOutOfSync = (groupID == outOfSyncGroupID1.data) || (groupID == outOfSyncGroupID2.data)

let epoch = await self.uiMOC.perform { tuple.conversation.epoch }
return tuple.isOutOfSync ? epoch - 1 : epoch
return isOutOfSync ? currentEpoch - 1 : currentEpoch
}
}

// mock fetching group info
mockActionsProvider.fetchConversationGroupInfoConversationIdDomainSubgroupTypeContext_MockValue = Data()

// mock joining group
let expectations = await uiMOC.perform { self.expectations(from: conversationAndOutOfSyncTuples) }
await uiMOC.perform { [self] in
mockMLSActionExecutor.mockJoinGroup = { groupID, _ in
expectations[groupID]?.fulfill()
XCTAssert(groupID.data.isOne(of: outOfSyncGroupID1.data, outOfSyncGroupID2.data))
XCTAssertNotEqual(groupID.data, inSyncGroupID3.data)
return []
}
}

// WHEN
try await sut.repairOutOfSyncConversations()

// THEN
await fulfillment(of: Array(expectations.values), timeout: 1.5)
// THEN, 2 out-of-sync conversations were repaired
XCTAssertEqual(mockMLSActionExecutor.mockJoinGroupCount, 2)
}

func test_FetchAndRepairConversation_RejoinsOutOfSyncConversation() async throws {
Expand Down Expand Up @@ -1697,29 +1693,21 @@

private typealias ConversationAndOutOfSyncTuple = (conversation: ZMConversation, isOutOfSync: Bool)

private func createConversation(outOfSync: Bool) -> ConversationAndOutOfSyncTuple {
private func createConversation(
outOfSync: Bool,
currentEpoch: UInt64 = 1,
groupID: MLSGroupID = .random()
) -> ConversationAndOutOfSyncTuple {
let conversation = ZMConversation.insertNewObject(in: uiMOC)
conversation.mlsGroupID = .random()
conversation.mlsGroupID = groupID
conversation.remoteIdentifier = UUID()
conversation.domain = "domain.com"
conversation.messageProtocol = .mls
conversation.epoch = 1
conversation.epoch = currentEpoch

return (conversation, outOfSync)
}

private func expectations(from tuples: [ConversationAndOutOfSyncTuple]) -> [MLSGroupID: XCTestExpectation] {
tuples.reduce(into: [MLSGroupID: XCTestExpectation]()) { expectations, tuple in
guard let groupID = tuple.conversation.mlsGroupID else {
return
}

let expectation = XCTestExpectation(description: "joined group")
expectation.isInverted = !tuple.isOutOfSync
expectations[groupID] = expectation
}
}

// MARK: - Wipe Groups

func test_WipeGroup_IsSuccessfull() async throws {
Expand Down
Loading