From 636d5659a51eb2446715e72b26f26886fe4a09fe Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Fri, 30 Aug 2024 10:05:05 -0500 Subject: [PATCH] Fix tests --- .../Mocks/MockPrivacyConfiguration.swift | 2 +- .../HTTPSUpgradeIntegrationTests.swift | 1 - .../DataBrokerProtection/Package.swift | 2 + .../DataBrokerProtectionAgentManager.swift | 4 +- ...ataBrokerProtectionAgentManagerTests.swift | 82 ++++++++++++++++--- .../DataBrokerProtectionTests/Mocks.swift | 15 ++++ .../Common/UserDefaultsWrapperTests.swift | 50 +++++------ .../MockConfigurationStore.swift | 4 + 8 files changed, 120 insertions(+), 40 deletions(-) diff --git a/DuckDuckGo/ContentBlocker/Mocks/MockPrivacyConfiguration.swift b/DuckDuckGo/ContentBlocker/Mocks/MockPrivacyConfiguration.swift index 472cf2fe7c..77b7e2506f 100644 --- a/DuckDuckGo/ContentBlocker/Mocks/MockPrivacyConfiguration.swift +++ b/DuckDuckGo/ContentBlocker/Mocks/MockPrivacyConfiguration.swift @@ -91,7 +91,7 @@ final class MockPrivacyConfigurationManager: NSObject, PrivacyConfigurationManag } func reload(etag: String?, data: Data?) -> BrowserServicesKit.PrivacyConfigurationManager.ReloadResult { - fatalError("not implemented") + return .embedded } var updatesPublisher: AnyPublisher = Just(()).eraseToAnyPublisher() diff --git a/IntegrationTests/HTTPSUpgrade/HTTPSUpgradeIntegrationTests.swift b/IntegrationTests/HTTPSUpgrade/HTTPSUpgradeIntegrationTests.swift index 9d8f51425d..2609eb2f98 100644 --- a/IntegrationTests/HTTPSUpgrade/HTTPSUpgradeIntegrationTests.swift +++ b/IntegrationTests/HTTPSUpgrade/HTTPSUpgradeIntegrationTests.swift @@ -43,7 +43,6 @@ class HTTPSUpgradeIntegrationTests: XCTestCase { window = WindowsManager.openNewWindow(with: .none)! XCTAssertTrue(AppPrivacyFeatures.shared.contentBlocking.privacyConfigurationManager.privacyConfig.isFeature(.httpsUpgrade, enabledForDomain: "privacy-test-pages.site")) - await ConfigurationManager.shared.refreshIfNeeded()?.value } @MainActor diff --git a/LocalPackages/DataBrokerProtection/Package.swift b/LocalPackages/DataBrokerProtection/Package.swift index a79685ddb4..30afef8b97 100644 --- a/LocalPackages/DataBrokerProtection/Package.swift +++ b/LocalPackages/DataBrokerProtection/Package.swift @@ -41,6 +41,8 @@ let package = Package( .product(name: "SwiftUIExtensions", package: "SwiftUIExtensions"), .byName(name: "XPCHelper"), .product(name: "PixelKit", package: "BrowserServicesKit"), + .product(name: "Configuration", package: "BrowserServicesKit"), + .product(name: "Persistence", package: "BrowserServicesKit") ], resources: [.process("Resources")], swiftSettings: [ diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionAgentManager.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionAgentManager.swift index eeb14753b7..0e33c1728c 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionAgentManager.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionAgentManager.swift @@ -116,7 +116,7 @@ public final class DataBrokerProtectionAgentManager { private let operationDependencies: DataBrokerOperationDependencies private let pixelHandler: EventMapping private let agentStopper: DataBrokerProtectionAgentStopper - private let configurationManger: ConfigurationManager + private let configurationManger: DefaultConfigurationManager // Used for debug functions only, so not injected private lazy var browserWindowManager = BrowserWindowManager() @@ -131,7 +131,7 @@ public final class DataBrokerProtectionAgentManager { operationDependencies: DataBrokerOperationDependencies, pixelHandler: EventMapping, agentStopper: DataBrokerProtectionAgentStopper, - configurationManager: ConfigurationManager + configurationManager: DefaultConfigurationManager ) { self.userNotificationService = userNotificationService self.activityScheduler = activityScheduler diff --git a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionAgentManagerTests.swift b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionAgentManagerTests.swift index 185e21f2f8..40f037538d 100644 --- a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionAgentManagerTests.swift +++ b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionAgentManagerTests.swift @@ -17,6 +17,8 @@ // import XCTest +import Configuration +import Persistence @testable import DataBrokerProtection final class DataBrokerProtectionAgentManagerTests: XCTestCase { @@ -32,6 +34,7 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { private var mockDependencies: DefaultDataBrokerOperationDependencies! private var mockProfile: DataBrokerProtectionProfile! private var mockAgentStopper: MockAgentStopper! + private var mockConfigurationManager: MockConfigurationManager! override func setUpWithError() throws { @@ -39,6 +42,7 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { mockActivityScheduler = MockDataBrokerProtectionBackgroundActivityScheduler() mockNotificationService = MockUserNotificationService() mockAgentStopper = MockAgentStopper() + mockConfigurationManager = MockConfigurationManager() let mockDatabase = MockDatabase() let mockMismatchCalculator = MockMismatchCalculator(database: mockDatabase, pixelHandler: mockPixelHandler) @@ -78,7 +82,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockDataManager.profileToReturn = mockProfile @@ -121,7 +126,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: agentStopper) + agentStopper: agentStopper, + configurationManager: mockConfigurationManager) mockDataManager.profileToReturn = nil @@ -153,7 +159,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockDataManager.profileToReturn = nil @@ -190,7 +197,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockDataManager.profileToReturn = mockProfile @@ -216,7 +224,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockDataManager.profileToReturn = mockProfile @@ -242,7 +251,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockNotificationService.reset() @@ -263,7 +273,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockNotificationService.reset() @@ -284,7 +295,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockNotificationService.reset() mockQueueManager.startImmediateOperationsIfPermittedCompletionError = DataBrokerProtectionAgentErrorCollection(oneTimeError: NSError(domain: "test", code: 10)) @@ -306,7 +318,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockNotificationService.reset() mockDataManager.shouldReturnHasMatches = true @@ -328,7 +341,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) mockNotificationService.reset() mockDataManager.shouldReturnHasMatches = false @@ -350,7 +364,8 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { dataManager: mockDataManager, operationDependencies: mockDependencies, pixelHandler: mockPixelHandler, - agentStopper: mockAgentStopper) + agentStopper: mockAgentStopper, + configurationManager: mockConfigurationManager) var startScheduledScansCalled = false mockQueueManager.startScheduledOperationsIfPermittedCalledCompletion = { _ in @@ -364,3 +379,48 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase { XCTAssertTrue(startScheduledScansCalled) } } + +struct MockConfigurationFetcher: ConfigurationFetching { + func fetch(_ configuration: Configuration, isDebug: Bool) async throws { + return + } + + func fetch(all configurations: [Configuration]) async throws { + return + } +} + +struct MockConfigurationStore: ConfigurationStoring { + func loadData(for configuration: Configuration) -> Data? { + return nil + } + + func loadEtag(for configuration: Configuration) -> String? { + return nil + } + + func loadEmbeddedEtag(for configuration: Configuration) -> String? { + return nil + } + + mutating func saveData(_ data: Data, for configuration: Configuration) throws { + return + } + + mutating func saveEtag(_ etag: String, for configuration: Configuration) throws { + return + } + + func fileUrl(for configuration: Configuration) -> URL { + return URL(string: "file:///\(configuration.rawValue)")! + } + +} + +final class MockConfigurationManager: DefaultConfigurationManager { + override init(fetcher: ConfigurationFetching = MockConfigurationFetcher(), + store: ConfigurationStoring = MockConfigurationStore(), + defaults: KeyValueStoring = UserDefaults()) { + super.init(fetcher: fetcher, store: store) + } +} diff --git a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/Mocks.swift b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/Mocks.swift index 27874b9905..8cb1b5a407 100644 --- a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/Mocks.swift +++ b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/Mocks.swift @@ -19,6 +19,7 @@ import BrowserServicesKit import Combine import Common +import Configuration import Foundation import GRDB import SecureStorage @@ -1929,3 +1930,17 @@ struct MockMigrationsProvider: DataBrokerProtectionDatabaseMigrationsProvider { return { _ in } } } + +//struct MockConfigurationFetcher: ConfigurationFetching { +// func fetch(_ configuration: Configuration, isDebug: Bool) async throws { +// return +// } +// +// func fetch(all configurations: [Configuration]) async throws { +// return +// } +//} +// +//final class MockConfigurationManager: DefaultConfigurationManager { +// +//} diff --git a/UnitTests/Common/UserDefaultsWrapperTests.swift b/UnitTests/Common/UserDefaultsWrapperTests.swift index daf2f90f89..8d4e2f2f12 100644 --- a/UnitTests/Common/UserDefaultsWrapperTests.swift +++ b/UnitTests/Common/UserDefaultsWrapperTests.swift @@ -304,24 +304,24 @@ final class UserDefaultsWrapperTests: XCTestCase { // MARK: Optional with enum Key func testOptionalRawRepresentableValueDefaultValueWithEnumKey() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaultValue: MyRawRepresentable(rawValue: "value"), defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaultValue: MyRawRepresentable(rawValue: "value"), defaults: defaults) XCTAssertEqual(wrapper.wrappedValue, MyRawRepresentable(rawValue: "value")) } func testOptionalRawRepresentableValueNilValueWithEnumKey() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) XCTAssertNil(wrapper.wrappedValue) } func testOptionalRawRepresentableValueUpdatingWithEnumKey() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) wrapper.wrappedValue = MyRawRepresentable(rawValue: "new") XCTAssertEqual(wrapper.wrappedValue, MyRawRepresentable(rawValue: "new")) - XCTAssertEqual(defaults.dictionary as! [String: String], [UserDefaultsWrapper.Key.configLastUpdated.rawValue: "new"]) + XCTAssertEqual(defaults.dictionary as! [String: String], [UserDefaultsWrapper.Key.lastCrashReportCheckDate.rawValue: "new"]) } func testOptionalRawRepresentableValueRemovalWithEnumKey() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) wrapper.wrappedValue = MyRawRepresentable(rawValue: "new") wrapper.wrappedValue = nil XCTAssertNil(wrapper.wrappedValue) @@ -329,7 +329,7 @@ final class UserDefaultsWrapperTests: XCTestCase { } func testOptionalRawRepresentableValueClearWithEnumKey() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) wrapper.wrappedValue = MyRawRepresentable(rawValue: "new") wrapper.clear() XCTAssertNil(wrapper.wrappedValue) @@ -337,14 +337,14 @@ final class UserDefaultsWrapperTests: XCTestCase { } func testOptionalRawRepresentableValueSharedDefaultsWithEnumKey() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate) XCTAssertNil(wrapper.wrappedValue) wrapper.wrappedValue = MyRawRepresentable(rawValue: "new") XCTAssertEqual(wrapper.wrappedValue, MyRawRepresentable(rawValue: "new")) - UserDefaultsWrapper.clear(.configLastUpdated) + UserDefaultsWrapper.clear(.lastCrashReportCheckDate) XCTAssertNil(wrapper.wrappedValue) - XCTAssertNil(UserDefaultsWrapper.sharedDefaults.object(forKey: UserDefaultsWrapper.Key.configLastUpdated.rawValue)) + XCTAssertNil(UserDefaultsWrapper.sharedDefaults.object(forKey: UserDefaultsWrapper.Key.lastCrashReportCheckDate.rawValue)) } // MARK: Date @@ -353,19 +353,19 @@ final class UserDefaultsWrapperTests: XCTestCase { let date2 = Date().addingTimeInterval(1) func testDateValueDefaultValue() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaultValue: date1, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaultValue: date1, defaults: defaults) XCTAssertEqual(wrapper.wrappedValue, date1) } func testDateValueUpdating() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaultValue: date1, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaultValue: date1, defaults: defaults) wrapper.wrappedValue = date2 XCTAssertEqual(wrapper.wrappedValue, date2) - XCTAssertEqual(defaults.dictionary as! [String: Date], [UserDefaultsWrapper.Key.configLastUpdated.rawValue: date2]) + XCTAssertEqual(defaults.dictionary as! [String: Date], [UserDefaultsWrapper.Key.lastCrashReportCheckDate.rawValue: date2]) } func testDateValueClear() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaultValue: date1, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaultValue: date1, defaults: defaults) wrapper.wrappedValue = date2 wrapper.clear() XCTAssertEqual(wrapper.wrappedValue, date1) @@ -373,37 +373,37 @@ final class UserDefaultsWrapperTests: XCTestCase { } func testDateValueSharedDefaults() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaultValue: date1) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaultValue: date1) XCTAssertEqual(wrapper.wrappedValue, date1) wrapper.wrappedValue = date2 XCTAssertEqual(wrapper.wrappedValue, date2) - UserDefaultsWrapper.clear(.configLastUpdated) + UserDefaultsWrapper.clear(.lastCrashReportCheckDate) XCTAssertEqual(wrapper.wrappedValue, date1) - XCTAssertNil(UserDefaultsWrapper.sharedDefaults.object(forKey: UserDefaultsWrapper.Key.configLastUpdated.rawValue)) + XCTAssertNil(UserDefaultsWrapper.sharedDefaults.object(forKey: UserDefaultsWrapper.Key.lastCrashReportCheckDate.rawValue)) } // MARK: Optional func testOptionalDateValueDefaultValue() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaultValue: date1, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaultValue: date1, defaults: defaults) XCTAssertEqual(wrapper.wrappedValue, date1) } func testOptionalDateValueNilValue() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) XCTAssertNil(wrapper.wrappedValue) } func testOptionalDateValueUpdating() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) wrapper.wrappedValue = date2 XCTAssertEqual(wrapper.wrappedValue, date2) - XCTAssertEqual(defaults.dictionary as! [String: Date], [UserDefaultsWrapper.Key.configLastUpdated.rawValue: date2]) + XCTAssertEqual(defaults.dictionary as! [String: Date], [UserDefaultsWrapper.Key.lastCrashReportCheckDate.rawValue: date2]) } func testOptionalDateValueRemoval() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) wrapper.wrappedValue = date2 wrapper.wrappedValue = nil XCTAssertNil(wrapper.wrappedValue) @@ -411,7 +411,7 @@ final class UserDefaultsWrapperTests: XCTestCase { } func testOptionalDateValueClear() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated, defaults: defaults) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate, defaults: defaults) wrapper.wrappedValue = date2 wrapper.clear() XCTAssertNil(wrapper.wrappedValue) @@ -419,14 +419,14 @@ final class UserDefaultsWrapperTests: XCTestCase { } func testOptionalDateValueSharedDefaults() { - let wrapper = UserDefaultsWrapper(key: .configLastUpdated) + let wrapper = UserDefaultsWrapper(key: .lastCrashReportCheckDate) XCTAssertNil(wrapper.wrappedValue) wrapper.wrappedValue = date2 XCTAssertEqual(wrapper.wrappedValue, date2) - UserDefaultsWrapper.clear(.configLastUpdated) + UserDefaultsWrapper.clear(.lastCrashReportCheckDate) XCTAssertNil(wrapper.wrappedValue) - XCTAssertNil(UserDefaultsWrapper.sharedDefaults.object(forKey: UserDefaultsWrapper.Key.configLastUpdated.rawValue)) + XCTAssertNil(UserDefaultsWrapper.sharedDefaults.object(forKey: UserDefaultsWrapper.Key.lastCrashReportCheckDate.rawValue)) } } diff --git a/UnitTests/ContentBlocker/MockConfigurationStore.swift b/UnitTests/ContentBlocker/MockConfigurationStore.swift index f7d19af95d..1204b3cf8c 100644 --- a/UnitTests/ContentBlocker/MockConfigurationStore.swift +++ b/UnitTests/ContentBlocker/MockConfigurationStore.swift @@ -60,4 +60,8 @@ final class MockConfigurationStore: ConfigurationStoring { func log() { } + func fileUrl(for configuration: Configuration) -> URL { + return URL(string: "file///\(configuration.rawValue)")! + } + }