Prompt user when inviting users with uncached identities (#5331)

If the user attempts to invite someone (to a room or creating a DM) whose identity is not cached, we prompt them to make sure this was their intention.
This commit is contained in:
Skye Elliot
2026-04-14 12:51:58 +01:00
committed by GitHub
parent 2daf23fd02
commit 477bf859c5
45 changed files with 541 additions and 40 deletions

View File

@@ -11,16 +11,28 @@ import Combine
import Testing
@MainActor
struct InviteUsersScreenViewModelTests {
final class InviteUsersScreenViewModelTests {
var viewModel: InviteUsersScreenViewModelProtocol!
var userDiscoveryService: UserDiscoveryServiceMock!
var clientProxy: ClientProxyMock!
var appSettings: AppSettings!
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
ServiceLocator.shared.register(appSettings: appSettings)
}
deinit {
AppSettings.resetAllSettings()
}
var context: InviteUsersScreenViewModel.Context {
viewModel.context
}
@Test
mutating func selectUser() {
func selectUser() {
let roomProxy = JoinedRoomProxyMock(.init(name: "newroom", members: []))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: true)
@@ -32,7 +44,7 @@ struct InviteUsersScreenViewModelTests {
}
@Test
mutating func reselectUser() {
func reselectUser() {
let roomProxy = JoinedRoomProxyMock(.init(name: "newroom", members: []))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: true)
@@ -46,7 +58,7 @@ struct InviteUsersScreenViewModelTests {
}
@Test
mutating func deselectUser() {
func deselectUser() {
let roomProxy = JoinedRoomProxyMock(.init(name: "newroom", members: []))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: true)
@@ -60,7 +72,7 @@ struct InviteUsersScreenViewModelTests {
}
@Test
mutating func inviteButton() async throws {
func inviteButton() async throws {
let mockedMembers: [RoomMemberProxyMock] = [.mockAlice, .mockBob]
let roomProxy = JoinedRoomProxyMock(.init(name: "test", members: mockedMembers))
roomProxy.inviteUserIDReturnValue = .success(())
@@ -87,10 +99,82 @@ struct InviteUsersScreenViewModelTests {
#expect(roomProxy.inviteUserIDReceivedInvocations == [RoomMemberProxyMock.mockAlice.userID])
}
private mutating func setupViewModel(roomProxy: JoinedRoomProxyProtocol, isSkippable: Bool) {
// MARK: - History Sharing
@Test
func invitingUnknownUsersOpensConfirmationDialog() async throws {
appSettings.enableKeyShareOnInvite = true
let mockedMembers: [RoomMemberProxyMock] = [.mockAlice, .mockBob]
let roomProxy = JoinedRoomProxyMock(.init(name: "test", members: mockedMembers))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: false)
// Mock the lack of cached user identity
clientProxy.userIdentityForFallBackToServerReturnValue = .success(nil)
let deferredState = deferFulfillment(viewModel.context.$viewState) { state in
state.isUserSelected(.mockAlice) && state.usersToConfirm.contains(.mockAlice)
}
context.send(viewAction: .toggleUser(.mockAlice))
try await deferredState.fulfill()
context.send(viewAction: .proceed)
#expect(context.presentConfirmationDialog)
let deferredAction = deferFulfillment(viewModel.actions) { action in
switch action {
case .dismiss:
return true
}
}
context.send(viewAction: .confirmUnknownUsers)
try await deferredAction.fulfill()
#expect(roomProxy.inviteUserIDReceivedInvocations == [RoomMemberProxyMock.mockAlice.userID])
}
@Test
func removeButtonRemovesUnknownUsers() async throws {
appSettings.enableKeyShareOnInvite = true
let mockedMembers: [RoomMemberProxyMock] = [.mockAlice, .mockBob]
let roomProxy = JoinedRoomProxyMock(.init(name: "test", members: mockedMembers))
roomProxy.inviteUserIDReturnValue = .success(())
setupViewModel(roomProxy: roomProxy, isSkippable: false)
// Mock the lack of cached user identity
clientProxy.userIdentityForFallBackToServerReturnValue = .success(nil)
var deferredState = deferFulfillment(viewModel.context.$viewState) { state in
state.isUserSelected(.mockAlice) && state.usersToConfirm.contains(.mockAlice)
}
context.send(viewAction: .toggleUser(.mockAlice))
try await deferredState.fulfill()
context.send(viewAction: .proceed)
#expect(context.presentConfirmationDialog)
deferredState = deferFulfillment(viewModel.context.$viewState) { state in
!state.usersToConfirm.contains(.mockAlice) && !state.selectedUsers.contains(.mockAlice)
}
context.send(viewAction: .removeUnknownUsers)
try await deferredState.fulfill()
}
// MARK: - Helpers
private func setupViewModel(roomProxy: JoinedRoomProxyProtocol, isSkippable: Bool) {
userDiscoveryService = UserDiscoveryServiceMock()
userDiscoveryService.searchProfilesWithReturnValue = .success([])
let viewModel = InviteUsersScreenViewModel(userSession: UserSessionMock(.init()),
clientProxy = ClientProxyMock(.init(userID: "@mock:client.com"))
let viewModel = InviteUsersScreenViewModel(userSession: UserSessionMock(.init(clientProxy: clientProxy)),
roomProxy: roomProxy,
isSkippable: isSkippable,
userDiscoveryService: userDiscoveryService,

View File

@@ -11,6 +11,7 @@ import Testing
@MainActor
struct RoomMemberDetailsViewModelTests {
var clientProxy: ClientProxy!
var viewModel: RoomMemberDetailsScreenViewModelProtocol!
var roomProxyMock: JoinedRoomProxyMock!
var roomMemberProxyMock: RoomMemberProxyMock!
@@ -151,6 +152,52 @@ struct RoomMemberDetailsViewModelTests {
#expect(context.ignoreUserAlert == nil)
#expect(context.alertInfo == nil)
}
// MARK: - History Sharing
@Test
mutating func inviteConfirmationFetchesIdentity() async throws {
let clientProxy = ClientProxyMock(.init())
setup(roomMemberProxyMock: .mockBob, clientProxy: clientProxy)
let waitForMemberToLoad = deferFulfillment(context.$viewState) { $0.memberDetails != nil }
try await waitForMemberToLoad.fulfill()
ServiceLocator.shared.settings.enableKeyShareOnInvite = true
clientProxy.directRoomForUserIDReturnValue = .success(nil)
clientProxy.userIdentityForFallBackToServerReturnValue = .success(UserIdentityProxyMock(configuration: .init(verificationState: .notVerified)))
// The user identity becomes known, i.e. not unknown.
let deferred = deferFulfillment(viewModel.context.$viewState.compactMap(\.bindings.inviteConfirmationUser)) {
!$0.isUnknown
}
context.send(viewAction: .openDirectChat)
try await deferred.fulfill()
#expect(clientProxy.userIdentityForFallBackToServerCalled)
}
@Test
mutating func inviteConfirmationFallsBackToUnknownIdentityOnFailure() async throws {
let clientProxy = ClientProxyMock(.init())
setup(roomMemberProxyMock: .mockBob, clientProxy: clientProxy)
let waitForMemberToLoad = deferFulfillment(context.$viewState) { $0.memberDetails != nil }
try await waitForMemberToLoad.fulfill()
ServiceLocator.shared.settings.enableKeyShareOnInvite = true
clientProxy.directRoomForUserIDReturnValue = .success(nil)
clientProxy.userIdentityForFallBackToServerReturnValue = .failure(.forbiddenAccess)
// The user identity is always unknown, i.e. never not unknown.
let deferred = deferFailure(viewModel.context.$viewState.compactMap(\.bindings.inviteConfirmationUser), timeout: .seconds(5)) {
!$0.isUnknown
}
context.send(viewAction: .openDirectChat)
try await deferred.fulfill()
#expect(clientProxy.userIdentityForFallBackToServerCalled)
}
// MARK: - Helpers
@@ -166,6 +213,7 @@ struct RoomMemberDetailsViewModelTests {
roomProxy: roomProxyMock,
userSession: userSession,
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics)
analytics: ServiceLocator.shared.analytics,
appSettings: ServiceLocator.shared.settings)
}
}

View File

@@ -85,6 +85,40 @@ struct StartChatScreenViewModelTests {
try await deferred.fulfill()
}
// MARK: - History Sharing
@Test
func inviteConfirmationFetchesIdentity() async throws {
ServiceLocator.shared.settings.enableKeyShareOnInvite = true
clientProxy.directRoomForUserIDReturnValue = .success(nil)
clientProxy.userIdentityForFallBackToServerReturnValue = .success(UserIdentityProxyMock(configuration: .init(verificationState: .notVerified)))
// User identity becomes known, i.e. not unknown
let deferred = deferFulfillment(viewModel.context.$viewState.compactMap(\.bindings.selectedUserToInvite)) {
!$0.isUnknown
}
context.send(viewAction: .selectUser(.mockBob))
try await deferred.fulfill()
#expect(clientProxy.userIdentityForFallBackToServerCalled)
}
@Test
func inviteConfirmationFallsBackToUnknownIdentityOnFailure() async throws {
ServiceLocator.shared.settings.enableKeyShareOnInvite = true
clientProxy.directRoomForUserIDReturnValue = .success(nil)
clientProxy.userIdentityForFallBackToServerReturnValue = .failure(.forbiddenAccess)
// User identity never becomes known, i.e. is never not unknown
let deferred = deferFailure(viewModel.context.$viewState.compactMap(\.bindings.selectedUserToInvite), timeout: .seconds(5)) {
!$0.isUnknown
}
context.send(viewAction: .selectUser(.mockBob))
try await deferred.fulfill()
#expect(clientProxy.userIdentityForFallBackToServerCalled)
}
// MARK: - Private
private func assertSearchResults(toBe count: Int) {

View File

@@ -21,7 +21,8 @@ struct UserProfileScreenViewModelTests {
isPresentedModally: false,
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics)
analytics: ServiceLocator.shared.analytics,
appSettings: ServiceLocator.shared.settings)
let context = viewModel.context
let waitForMemberToLoad = deferFulfillment(context.observe(\.viewState.userProfile)) { $0 != nil }
@@ -42,7 +43,8 @@ struct UserProfileScreenViewModelTests {
isPresentedModally: false,
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics)
analytics: ServiceLocator.shared.analytics,
appSettings: ServiceLocator.shared.settings)
let context = viewModel.context
let waitForMemberToLoad = deferFulfillment(context.observe(\.viewState.userProfile)) { $0 != nil }
@@ -52,4 +54,32 @@ struct UserProfileScreenViewModelTests {
#expect(context.viewState.userProfile == profile)
#expect(context.viewState.permalink != nil)
}
@Test
func startingDmWithUnknownUserFetchesIdentity() async throws {
ServiceLocator.shared.settings.enableKeyShareOnInvite = true
let profile = UserProfileProxy.mockAlice
let clientProxy = ClientProxyMock(.init())
clientProxy.directRoomForUserIDReturnValue = .success(nil)
clientProxy.userIdentityForFallBackToServerReturnValue = .success(nil)
let viewModel = UserProfileScreenViewModel(userID: profile.userID,
isPresentedModally: false,
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics,
appSettings: ServiceLocator.shared.settings)
let context = viewModel.context
let waitForMemberToLoad = deferFulfillment(context.observe(\.viewState.userProfile)) { $0 != nil }
try await waitForMemberToLoad.fulfill()
let deferred = deferFulfillment(context.observe(\.viewState.bindings).compactMap(\.inviteConfirmationUser), timeout: .seconds(5)) { $0.isUnknown }
context.send(viewAction: .openDirectChat)
try await deferred.fulfill()
}
}