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

[PM-15360] Consider Credential Exchange export policies. #1263

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ExportCXFProcessor: StateProcessor<ExportCXFState, ExportCXFAction, Export
typealias Services = HasConfigService
& HasErrorReporter
& HasExportCXFCiphersRepository
& HasPolicyService
& HasStateService
& HasVaultRepository

Expand Down Expand Up @@ -70,6 +71,12 @@ class ExportCXFProcessor: StateProcessor<ExportCXFState, ExportCXFAction, Export
/// Loads the initial data for the view.
private func load() async {
do {
if await services.policyService.policyAppliesToUser(.disablePersonalVaultExport) {
state.status = .failure(message: Localizations.disablePersonalVaultExportPolicyInEffect)
state.showMainButton = false
return
}

let totalItemsToExport = try await services.exportCXFCiphersRepository.getCipherCountToExportCXF()
guard totalItemsToExport > 0 else {
state.status = .failure(message: Localizations.noItems)
Expand Down Expand Up @@ -141,6 +148,11 @@ class ExportCXFProcessor: StateProcessor<ExportCXFState, ExportCXFAction, Export

/// Shows the alert confirming the user wants to export items later.
private func cancelWithConfirmation() {
guard state.showMainButton else {
coordinator.navigate(to: .dismiss)
return
}

coordinator.showAlert(.confirmCancelCXFExport { [weak self] in
guard let self else { return }
coordinator.navigate(to: .dismiss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ExportCXFProcessorTests: BitwardenTestCase { // swiftlint:disable:this typ
var delegate: MockExportCXFProcessorDelegate!
var errorReporter: MockErrorReporter!
var exportCXFCiphersRepository: MockExportCXFCiphersRepository!
var policyService: MockPolicyService!
var stackNavigator: MockStackNavigator!
var stateService: MockStateService!
var subject: ExportCXFProcessor!
Expand All @@ -28,6 +29,7 @@ class ExportCXFProcessorTests: BitwardenTestCase { // swiftlint:disable:this typ
delegate = MockExportCXFProcessorDelegate()
errorReporter = MockErrorReporter()
exportCXFCiphersRepository = MockExportCXFCiphersRepository()
policyService = MockPolicyService()
stackNavigator = MockStackNavigator()
stateService = MockStateService()
vaultRepository = MockVaultRepository()
Expand All @@ -38,6 +40,7 @@ class ExportCXFProcessorTests: BitwardenTestCase { // swiftlint:disable:this typ
configService: configService,
errorReporter: errorReporter,
exportCXFCiphersRepository: exportCXFCiphersRepository,
policyService: policyService,
stateService: stateService,
vaultRepository: vaultRepository
),
Expand All @@ -53,6 +56,7 @@ class ExportCXFProcessorTests: BitwardenTestCase { // swiftlint:disable:this typ
delegate = nil
errorReporter = nil
exportCXFCiphersRepository = nil
policyService = nil
stackNavigator = nil
stateService = nil
subject = nil
Expand All @@ -69,6 +73,22 @@ class ExportCXFProcessorTests: BitwardenTestCase { // swiftlint:disable:this typ
XCTAssertEqual(subject.state.totalItemsToExport, 100)
}

/// `perform(_:)` appeared doesn't load the initial data when `.disablePersonalVaultExport`
/// applies to user changing the status to failure.
@MainActor
func test_perform_appearedDisablePersonalVaultExportPolicy() async throws {
policyService.policyAppliesToUserResult[.disablePersonalVaultExport] = true

await subject.perform(.appeared)

guard case let .failure(message) = subject.state.status else {
XCTFail("Status should be failure")
return
}
XCTAssertEqual(message, Localizations.disablePersonalVaultExportPolicyInEffect)
XCTAssertFalse(subject.state.showMainButton)
}

/// `perform(_:)` appeared loads the initial data but there are zero items
/// so a failure state is displayed.
@MainActor
Expand Down Expand Up @@ -133,6 +153,23 @@ class ExportCXFProcessorTests: BitwardenTestCase { // swiftlint:disable:this typ
XCTAssertTrue(coordinator.routes.isEmpty)
}

/// `perform(_:)` with `.cancel` when not showing main button navigates to dismiss.
@MainActor
func test_perform_cancelMainButtonNotShown() async throws {
subject.state.showMainButton = false
let task = Task {
await subject.perform(.cancel)
}
defer { task.cancel() }

try await waitForAsync { [weak self] in
guard let self else { return true }
return !coordinator.routes.isEmpty
}

XCTAssertEqual(.dismiss, coordinator.routes.last)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐ŸŽจ Could this test be simplified to avoid the extra task and waitFor? It seems like everything in this flow uses async/await so the waitFor might not be necessary?

}

/// `perform(_:)` with `.mainButtonTapped` in `.start` status prepares export.
@MainActor
func test_perform_mainButtonTappedStartPreparesExport() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ExportCXFCoordinator: Coordinator, HasStackNavigator {
typealias Services = HasConfigService
& HasErrorReporter
& HasExportCXFCiphersRepository
& HasPolicyService
& HasStateService
& HasVaultRepository

Expand Down
Loading