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

Handle contingency settings state on remote config #926

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ let package = Package(
.target(
name: "DuckPlayer",
dependencies: [
"Common"
"Common",
"BrowserServicesKit"
],
swiftSettings: [
.define("DEBUG", .when(configuration: .debug))
Expand Down
71 changes: 71 additions & 0 deletions Sources/DuckPlayer/DuckPlayerContingencyHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// DuckPlayerContingencyHandler.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 Foundation
import Common
import BrowserServicesKit

/// A protocol that defines the requirements for handling DuckPlayer contingency scenarios.
/// >Tech Design: https://app.asana.com/0/481882893211075/1207926753747908/f
public protocol DuckPlayerContingencyHandler {
/// A Boolean value indicating whether a contingency message should be displayed.
var shouldDisplayContingencyMessage: Bool { get }

/// A URL pointing to a "Learn More" page for additional information.
var learnMoreURL: URL? { get }
}

/// A default implementation of the `DuckPlayerContingencyHandler` protocol uses `PrivacyConfigurationManaging` to define its values.
public struct DefaultDuckPlayerContingencyHandler: DuckPlayerContingencyHandler {
private let privacyConfigurationManager: PrivacyConfigurationManaging

/// A Boolean value indicating whether a contingency message should be displayed.
/// The message should be displayed if the `learnMoreURL` is not nil and the DuckPlayer feature is not enabled.
public var shouldDisplayContingencyMessage: Bool {
learnMoreURL != nil && !isDuckPlayerFeatureEnabled
}

private var isDuckPlayerFeatureEnabled: Bool {
privacyConfigurationManager.privacyConfig.isEnabled(featureKey: .duckPlayer)
}

/// A URL pointing to a "Learn More" page for additional information.
/// The URL is derived from the privacy configuration settings.
public var learnMoreURL: URL? {
let settings = privacyConfigurationManager.privacyConfig.settings(for: .duckPlayer)
guard let link = settings[.duckPlayerDisabledHelpPageLink] as? String,
let pageLink = URL(string: link) else { return nil }
return pageLink
}

public init(privacyConfigurationManager: PrivacyConfigurationManaging) {
self.privacyConfigurationManager = privacyConfigurationManager
}
}

// MARK: - Settings key for Dictionary extension

private enum SettingsKey: String {
case duckPlayerDisabledHelpPageLink
}

private extension Dictionary where Key == String {
subscript(key: SettingsKey) -> Value? {
return self[key.rawValue]
}
}
279 changes: 279 additions & 0 deletions Tests/DuckPlayerTests/DuckPlayerContingencyHandlerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
//
// DuckPlayerContingencyHandlerTests.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 XCTest
import BrowserServicesKit
import DuckPlayer
import BrowserServicesKitTestsUtils

final class DuckPlayerContingencyHandlerTests: XCTestCase {

func testShouldDisplayContingencyMessageWhenFeatureDisabledAndLinkExists() {
let mockEmbeddedData = MockEmbeddedDataProvider(data: MockConfig.featureDisabledAndLinkPresent, etag: "a")

let manager = PrivacyConfigurationManager(fetchedETag: nil,
fetchedData: nil,
embeddedDataProvider: mockEmbeddedData,
localProtection: MockDomainsProtectionStore(),
internalUserDecider: DefaultInternalUserDecider())

let handler = DefaultDuckPlayerContingencyHandler(privacyConfigurationManager: manager)
XCTAssertTrue(handler.shouldDisplayContingencyMessage)
XCTAssertEqual(handler.learnMoreURL, URL(string: MockConfig.learnMoreURL))
}

func testShouldNotDisplayContingencyMessageWhenFeatureEnabledAndLinkExists() {
let mockEmbeddedData = MockEmbeddedDataProvider(data: MockConfig.featureEnabledAndLinkPresent, etag: "a")

let manager = PrivacyConfigurationManager(fetchedETag: nil,
fetchedData: nil,
embeddedDataProvider: mockEmbeddedData,
localProtection: MockDomainsProtectionStore(),
internalUserDecider: DefaultInternalUserDecider())

let handler = DefaultDuckPlayerContingencyHandler(privacyConfigurationManager: manager)
XCTAssertFalse(handler.shouldDisplayContingencyMessage)
XCTAssertEqual(handler.learnMoreURL, URL(string: MockConfig.learnMoreURL))
}

func testShouldNotDisplayContingencyMessageWhenFeatureEnabled() {
let mockEmbeddedData = MockEmbeddedDataProvider(data: MockConfig.featureEnabledAndLinkAbsent, etag: "a")

let manager = PrivacyConfigurationManager(fetchedETag: nil,
fetchedData: nil,
embeddedDataProvider: mockEmbeddedData,
localProtection: MockDomainsProtectionStore(),
internalUserDecider: DefaultInternalUserDecider())

let handler = DefaultDuckPlayerContingencyHandler(privacyConfigurationManager: manager)
XCTAssertFalse(handler.shouldDisplayContingencyMessage)
XCTAssertNil(handler.learnMoreURL)
}

func testShouldNotDisplayContingencyMessageWhenLinkDoesNotExist() {
let mockEmbeddedData = MockEmbeddedDataProvider(data: MockConfig.featureDisabledAndLinkAbsent, etag: "a")

let manager = PrivacyConfigurationManager(fetchedETag: nil,
fetchedData: nil,
embeddedDataProvider: mockEmbeddedData,
localProtection: MockDomainsProtectionStore(),
internalUserDecider: DefaultInternalUserDecider())

let handler = DefaultDuckPlayerContingencyHandler(privacyConfigurationManager: manager)
XCTAssertFalse(handler.shouldDisplayContingencyMessage)
XCTAssertNil(handler.learnMoreURL)
}

func testLearnMoreURLWhenLinkExists() {
let mockEmbeddedData = MockEmbeddedDataProvider(data: MockConfig.featureDisabledAndLinkPresent, etag: "a")

let manager = PrivacyConfigurationManager(fetchedETag: nil,
fetchedData: nil,
embeddedDataProvider: mockEmbeddedData,
localProtection: MockDomainsProtectionStore(),
internalUserDecider: DefaultInternalUserDecider())

let handler = DefaultDuckPlayerContingencyHandler(privacyConfigurationManager: manager)
XCTAssertEqual(handler.learnMoreURL, URL(string: MockConfig.learnMoreURL))
}

func testLearnMoreURLWhenLinkDoesNotExist() {
let mockEmbeddedData = MockEmbeddedDataProvider(data: MockConfig.featureDisabledAndLinkAbsent, etag: "a")

let manager = PrivacyConfigurationManager(fetchedETag: nil,
fetchedData: nil,
embeddedDataProvider: mockEmbeddedData,
localProtection: MockDomainsProtectionStore(),
internalUserDecider: DefaultInternalUserDecider())

let handler = DefaultDuckPlayerContingencyHandler(privacyConfigurationManager: manager)
XCTAssertNil(handler.learnMoreURL)
}
}

private struct MockConfig {
static let learnMoreURL = "https://duckduckgo.com/duckduckgo-help-pages/duck-player/"
static let featureEnabledAndLinkAbsent =
"""
{
"readme": "https://github.com/duckduckgo/privacy-configuration",
"version": 1722602607085,
"features": {
"duckPlayer": {
"exceptions": [],
"features": {
"pip": {
"state": "enabled"
},
"autoplay": {
"state": "disabled"
},
"openInNewTab": {
"state": "disabled"
}
},
"settings": {
},
"state": "enabled",
"hash": "5ccb9e0379c691ea67bb2e73ba0ac194"
}
}
}
""".data(using: .utf8)!

static let featureDisabledAndLinkAbsent =
"""
{
"readme": "https://github.com/duckduckgo/privacy-configuration",
"version": 1722602607085,
"features": {
"duckPlayer": {
"exceptions": [],
"features": {
"pip": {
"state": "enabled"
},
"autoplay": {
"state": "disabled"
},
"openInNewTab": {
"state": "disabled"
}
},
"settings": {
"tryDuckPlayerLink": "https://www.youtube.com/watch?v=yKWIA-Pys4c",
},
"state": "enabled",
"hash": "5ccb9e0379c691ea67bb2e73ba0ac194"
}
}
}
""".data(using: .utf8)!

static let featureDisabledAndLinkPresent =
"""
{
"readme": "https://github.com/duckduckgo/privacy-configuration",
"version": 1722602607085,
"features": {
"duckPlayer": {
"exceptions": [],
"features": {
"pip": {
"state": "enabled"
},
"autoplay": {
"state": "disabled"
},
"openInNewTab": {
"state": "disabled"
}
},
"settings": {
"tryDuckPlayerLink": "https://www.youtube.com/watch?v=yKWIA-Pys4c",
"duckPlayerDisabledHelpPageLink": "\(MockConfig.learnMoreURL)"
},
"state": "disabled",
"hash": "5ccb9e0379c691ea67bb2e73ba0ac194"
}
}
}
""".data(using: .utf8)!

static let featureEnabledAndLinkPresent =
"""
{
"readme": "https://github.com/duckduckgo/privacy-configuration",
"version": 1722602607085,
"features": {
"duckPlayer": {
"exceptions": [],
"features": {
"pip": {
"state": "enabled"
},
"autoplay": {
"state": "disabled"
},
"openInNewTab": {
"state": "disabled"
}
},
"settings": {
"tryDuckPlayerLink": "https://www.youtube.com/watch?v=yKWIA-Pys4c",
"duckPlayerDisabledHelpPageLink": "\(MockConfig.learnMoreURL)"
},
"state": "enabled",
"hash": "5ccb9e0379c691ea67bb2e73ba0ac194"
}
}
}
""".data(using: .utf8)!
}

import Combine

class MockPrivacyConfigurationManager: PrivacyConfigurationManaging {
var currentConfig: Data = .init()
var updatesSubject = PassthroughSubject<Void, Never>()
let updatesPublisher: AnyPublisher<Void, Never>
var privacyConfig: PrivacyConfiguration
let internalUserDecider: InternalUserDecider
func reload(etag: String?, data: Data?) -> PrivacyConfigurationManager.ReloadResult {
.downloaded
}

init(privacyConfig: PrivacyConfiguration, internalUserDecider: InternalUserDecider) {
self.updatesPublisher = updatesSubject.eraseToAnyPublisher()
self.privacyConfig = privacyConfig
self.internalUserDecider = internalUserDecider
}
}

private final class MockEmbeddedDataProvider: EmbeddedDataProvider {
var embeddedDataEtag: String

var embeddedData: Data

init(data: Data, etag: String) {
embeddedData = data
embeddedDataEtag = etag
}
}

private final class MockDomainsProtectionStore: DomainsProtectionStore {
var unprotectedDomains = Set<String>()

func disableProtection(forDomain domain: String) {
unprotectedDomains.insert(domain)
}

func enableProtection(forDomain domain: String) {
unprotectedDomains.remove(domain)
}
}

private final class MockInternalUserStoring: InternalUserStoring {
var isInternalUser: Bool = false
}

extension DefaultInternalUserDecider {
fileprivate convenience init(mockedStore: MockInternalUserStoring = MockInternalUserStoring()) {
self.init(store: mockedStore)
}
}
Loading