-
Notifications
You must be signed in to change notification settings - Fork 35
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
Remote Messaging Framework for macOS #876
Changes from 36 commits
a7adf0f
419e6df
dfe0f9c
fcdfa1d
861d19c
59935c9
cc6afff
61f6379
65e1d06
d484ebc
80a8a79
088bf37
172e980
70aa7b1
37f51ef
ccb40d0
df1d513
3a9e5dd
1181fa6
2d6d684
c8778cb
fd1358c
687cdcc
687bfba
9644245
aadcd91
7fd8d07
8334115
99023c9
e7c7b09
7c39e61
4d7daa3
23b0dc1
8e742e3
4d111ca
324baf8
1cc19c3
d27f306
2319ec8
ed3cf77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ let package = Package( | |
.library(name: "Common", targets: ["Common"]), | ||
.library(name: "TestUtils", targets: ["TestUtils"]), | ||
.library(name: "DDGSync", targets: ["DDGSync"]), | ||
.library(name: "BrowserServicesKitTestsUtils", targets: ["BrowserServicesKitTestsUtils"]), | ||
.library(name: "Persistence", targets: ["Persistence"]), | ||
.library(name: "Bookmarks", targets: ["Bookmarks"]), | ||
.library(name: "BloomFilterWrapper", targets: ["BloomFilterWrapper"]), | ||
|
@@ -26,6 +27,7 @@ let package = Package( | |
.library(name: "Configuration", targets: ["Configuration"]), | ||
.library(name: "Networking", targets: ["Networking"]), | ||
.library(name: "RemoteMessaging", targets: ["RemoteMessaging"]), | ||
.library(name: "RemoteMessagingTestsUtils", targets: ["RemoteMessagingTestsUtils"]), | ||
.library(name: "Navigation", targets: ["Navigation"]), | ||
.library(name: "SyncDataProviders", targets: ["SyncDataProviders"]), | ||
.library(name: "NetworkProtection", targets: ["NetworkProtection"]), | ||
|
@@ -76,6 +78,12 @@ let package = Package( | |
.define("DEBUG", .when(configuration: .debug)) | ||
] | ||
), | ||
.target( | ||
name: "BrowserServicesKitTestsUtils", | ||
dependencies: [ | ||
"BrowserServicesKit", | ||
] | ||
), | ||
.target( | ||
name: "Persistence", | ||
dependencies: [ | ||
|
@@ -261,6 +269,7 @@ let package = Package( | |
name: "RemoteMessaging", | ||
dependencies: [ | ||
"Common", | ||
"Configuration", | ||
"BrowserServicesKit", | ||
"Networking", | ||
"Persistence", | ||
|
@@ -272,6 +281,12 @@ let package = Package( | |
.define("DEBUG", .when(configuration: .debug)) | ||
] | ||
), | ||
.target( | ||
name: "RemoteMessagingTestsUtils", | ||
dependencies: [ | ||
"RemoteMessaging", | ||
] | ||
), | ||
.target( | ||
name: "SyncDataProviders", | ||
dependencies: [ | ||
|
@@ -400,7 +415,7 @@ let package = Package( | |
name: "BrowserServicesKitTests", | ||
dependencies: [ | ||
"BrowserServicesKit", | ||
"RemoteMessaging", // Move tests later (lots of test dependencies in BSK) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RemoteMessaging has its own tests target now. |
||
"BrowserServicesKitTestsUtils", | ||
"SecureStorageTestsUtils", | ||
"TestUtils", | ||
"Subscription" | ||
|
@@ -482,10 +497,16 @@ let package = Package( | |
.testTarget( | ||
name: "RemoteMessagingTests", | ||
dependencies: [ | ||
"BrowserServicesKitTestsUtils", | ||
"RemoteMessaging", | ||
"RemoteMessagingTestsUtils", | ||
"TestUtils", | ||
], | ||
resources: [ | ||
.copy("Resources/remote-messaging-config-example.json"), | ||
.copy("Resources/remote-messaging-config-malformed.json"), | ||
.copy("Resources/remote-messaging-config-unsupported-items.json"), | ||
.copy("Resources/remote-messaging-config.json"), | ||
] | ||
), | ||
.testTarget( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,31 @@ public struct BookmarkUtils { | |
return result.compactMap { $0[#keyPath(BookmarkEntity.title)] as? String } | ||
} | ||
|
||
public static func numberOfBookmarks(in context: NSManagedObjectContext) -> Int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These 2 functions were used exclusively for RMF in the iOS repo. Now they're in BookmarkUtils and can be reused by macOS. |
||
let request = BookmarkEntity.fetchRequest() | ||
request.predicate = NSPredicate( | ||
format: "%K == false AND %K == false AND (%K == NO OR %K == nil)", | ||
#keyPath(BookmarkEntity.isFolder), | ||
#keyPath(BookmarkEntity.isPendingDeletion), | ||
#keyPath(BookmarkEntity.isStub), #keyPath(BookmarkEntity.isStub)) | ||
return (try? context.count(for: request)) ?? 0 | ||
} | ||
|
||
public static func numberOfFavorites(for displayMode: FavoritesDisplayMode, in context: NSManagedObjectContext) -> Int { | ||
guard let displayedFavoritesFolder = BookmarkUtils.fetchFavoritesFolder(withUUID: displayMode.displayedFolder.rawValue, in: context) else { | ||
return 0 | ||
} | ||
|
||
let request = BookmarkEntity.fetchRequest() | ||
request.predicate = NSPredicate(format: "%K CONTAINS %@ AND %K == false AND %K == false AND (%K == NO OR %K == nil)", | ||
#keyPath(BookmarkEntity.favoriteFolders), | ||
displayedFavoritesFolder, | ||
#keyPath(BookmarkEntity.isFolder), | ||
#keyPath(BookmarkEntity.isPendingDeletion), | ||
#keyPath(BookmarkEntity.isStub), #keyPath(BookmarkEntity.isStub)) | ||
return (try? context.count(for: request)) ?? 0 | ||
} | ||
|
||
// MARK: Internal | ||
|
||
@discardableResult | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// MockEmailManagerRequestDelegate.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
@testable import BrowserServicesKit | ||
import Foundation | ||
|
||
public class MockEmailManagerRequestDelegate: EmailManagerRequestDelegate { | ||
|
||
public init(didSendMockAliasRequest: @escaping () -> Void = {}) { | ||
self.didSendMockAliasRequest = didSendMockAliasRequest | ||
} | ||
|
||
public var activeTask: URLSessionTask? | ||
public var mockAliases: [String] = [] | ||
public var waitlistTimestamp: Int = 1 | ||
public var didSendMockAliasRequest: () -> Void | ||
|
||
// swiftlint:disable function_parameter_count | ||
public func emailManager(_ emailManager: EmailManager, requested url: URL, method: String, headers: [String: String], parameters: [String: String]?, httpBody: Data?, timeoutInterval: TimeInterval) async throws -> Data { | ||
switch url.absoluteString { | ||
case EmailUrls.Url.emailAlias: return try processMockAliasRequest().get() | ||
default: fatalError("\(#file): Unsupported URL passed to mock request delegate: \(url)") | ||
} | ||
} | ||
// swiftlint:enable function_parameter_count | ||
|
||
public var keychainAccessErrorAccessType: EmailKeychainAccessType? | ||
public var keychainAccessError: EmailKeychainAccessError? | ||
|
||
public func emailManagerKeychainAccessFailed(_ emailManager: EmailManager, | ||
accessType: EmailKeychainAccessType, | ||
error: EmailKeychainAccessError) { | ||
keychainAccessErrorAccessType = accessType | ||
keychainAccessError = error | ||
} | ||
|
||
private func processMockAliasRequest() -> Result<Data, Error> { | ||
didSendMockAliasRequest() | ||
|
||
if mockAliases.first != nil { | ||
let alias = mockAliases.removeFirst() | ||
let jsonString = "{\"address\":\"\(alias)\"}" | ||
let data = jsonString.data(using: .utf8)! | ||
return .success(data) | ||
} else { | ||
return .failure(AliasRequestError.noDataError) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// MockEmailManagerStorage.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import BrowserServicesKit | ||
import Foundation | ||
|
||
public class MockEmailManagerStorage: EmailManagerStorage { | ||
|
||
public var mockError: EmailKeychainAccessError? | ||
|
||
public var mockUsername: String? | ||
public var mockToken: String? | ||
public var mockAlias: String? | ||
public var mockCohort: String? | ||
public var mockLastUseDate: String? | ||
|
||
public var storeTokenCallback: ((String, String, String?) -> Void)? | ||
public var storeAliasCallback: ((String) -> Void)? | ||
public var storeLastUseDateCallback: ((String) -> Void)? | ||
public var deleteAliasCallback: (() -> Void)? | ||
public var deleteAuthenticationStateCallback: (() -> Void)? | ||
public var deleteWaitlistStateCallback: (() -> Void)? | ||
|
||
public init() {} | ||
|
||
public func getUsername() throws -> String? { | ||
if let mockError = mockError { throw mockError } | ||
return mockUsername | ||
} | ||
|
||
public func getToken() throws -> String? { | ||
if let mockError = mockError { throw mockError } | ||
return mockToken | ||
} | ||
|
||
public func getAlias() throws -> String? { | ||
if let mockError = mockError { throw mockError } | ||
return mockAlias | ||
} | ||
|
||
public func getCohort() throws -> String? { | ||
if let mockError = mockError { throw mockError } | ||
return mockCohort | ||
} | ||
|
||
public func getLastUseDate() throws -> String? { | ||
if let mockError = mockError { throw mockError } | ||
return mockLastUseDate | ||
} | ||
|
||
public func store(token: String, username: String, cohort: String?) throws { | ||
storeTokenCallback?(token, username, cohort) | ||
} | ||
|
||
public func store(alias: String) throws { | ||
storeAliasCallback?(alias) | ||
} | ||
|
||
public func store(lastUseDate: String) throws { | ||
storeLastUseDateCallback?(lastUseDate) | ||
} | ||
|
||
public func deleteAlias() { | ||
deleteAliasCallback?() | ||
} | ||
|
||
public func deleteAuthenticationState() { | ||
deleteAuthenticationStateCallback?() | ||
} | ||
|
||
public func deleteWaitlistState() { | ||
deleteWaitlistStateCallback?() | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// MockStatisticsStore.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import BrowserServicesKit | ||
import Foundation | ||
|
||
public class MockStatisticsStore: StatisticsStore { | ||
|
||
public init() {} | ||
|
||
public var installDate: Date? | ||
public var atb: String? | ||
public var searchRetentionAtb: String? | ||
public var appRetentionAtb: String? | ||
|
||
public var hasInstallStatistics: Bool { | ||
return atb != nil | ||
} | ||
|
||
public var variant: String? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// MockVariant.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import BrowserServicesKit | ||
import Foundation | ||
|
||
public class MockVariant: Variant { | ||
public var name: String | ||
public var weight: Int | ||
public var isIncluded: () -> Bool | ||
public var features: [FeatureName] | ||
|
||
public init(name: String, weight: Int, isIncluded: @escaping () -> Bool, features: [FeatureName]) { | ||
self.name = name | ||
self.weight = weight | ||
self.isIncluded = isIncluded | ||
self.features = features | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RemoteMessagingTestsUtils contains mocks for RemoteMessaging related protocols