This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAutoconsentIntegrationTests.swift
218 lines (176 loc) · 7.8 KB
/
AutoconsentIntegrationTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// AutoconsentIntegrationTests.swift
//
// Copyright © 2023 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 Combine
import Common
import PrivacyDashboard
import XCTest
@testable import DuckDuckGo_Privacy_Browser
@available(macOS 12.0, *)
class AutoconsentIntegrationTests: XCTestCase {
var window: NSWindow!
var mainViewController: MainViewController {
(window.contentViewController as! MainViewController)
}
var tabViewModel: TabViewModel {
mainViewController.browserTabViewController.tabViewModel!
}
@MainActor
override func setUp() {
// disable GPC redirects
PrivacySecurityPreferences.shared.gpcEnabled = false
window = WindowsManager.openNewWindow(with: .none)!
}
override func tearDown() {
window.close()
window = nil
PrivacySecurityPreferences.shared.gpcEnabled = true
}
// MARK: - Tests
@MainActor
func testWhenAutoconsentEnabled_cookieConsentManaged() async throws {
// enable the feature
PrivacySecurityPreferences.shared.autoconsentEnabled = true
let url = URL(string: "http://privacy-test-pages.site/features/autoconsent/")!
let tab = self.tabViewModel.tab
// expect cookieConsentManaged to be published
let cookieConsentManagedPromise = tab.privacyInfoPublisher
.compactMap {
$0?.$cookieConsentManaged
}
.switchToLatest()
.compactMap {
$0?.isConsentManaged == true ? true : nil
}
.timeout(10)
.first()
.promise()
_=await tab.setUrl(url, userEntered: nil)?.value?.result
let cookieConsentManaged = try await cookieConsentManagedPromise.value
XCTAssertTrue(cookieConsentManaged)
}
@MainActor
func testWhenAutoconsentDisabled_promptIsDisplayed() async throws {
// reset the feature setting
PrivacySecurityPreferences.shared.autoconsentEnabled = nil
let url = URL(string: "http://privacy-test-pages.site/features/autoconsent/")!
let tab = self.tabViewModel.tab
_=await tab.setUrl(url, userEntered: nil)?.value?.result
// expect cookieConsent request to be published
let cookieConsentPromptRequestPromise = tab.cookieConsentPromptRequestPublisher
.compactMap { $0 != nil ? true : nil }
.timeout(5)
.first()
.promise()
let cookieConsentPromptRequestPublished = try await cookieConsentPromptRequestPromise.value
XCTAssertTrue(cookieConsentPromptRequestPublished)
XCTAssertTrue(mainViewController.view.window!.childWindows?.first?.contentViewController is CookieConsentUserPermissionViewController)
// expect cookieConsent popover to be hidden when opening a new tab
mainViewController.browserTabViewController.openNewTab(with: .none)
XCTAssertFalse(mainViewController.view.window!.childWindows?.first?.contentViewController is CookieConsentUserPermissionViewController)
// switch back: popover should be reopen
mainViewController.tabCollectionViewModel.select(at: .unpinned(0))
XCTAssertTrue(mainViewController.view.window!.childWindows?.first?.contentViewController is CookieConsentUserPermissionViewController)
}
@MainActor
func testCosmeticRule_whenFakeCookieBannerIsDisplayed_bannerIsHidden() async throws {
// enable the feature
PrivacySecurityPreferences.shared.autoconsentEnabled = true
let url = URL(string: "http://privacy-test-pages.site/features/autoconsent/banner.html")!
let tab = self.tabViewModel.tab
// expect `cosmetic` to be published
let cookieConsentManagedPromise = tab.privacyInfoPublisher
.compactMap {
return $0?.$cookieConsentManaged
}
.switchToLatest()
.compactMap {
return $0?.isCosmeticRuleApplied == true ? true : nil
}
.receive(on: DispatchQueue.main)
.timeout(10)
.first()
.promise()
_=await tab.setUrl(url, userEntered: nil)?.value?.result
do {
let cookieConsentManaged = try await cookieConsentManagedPromise.value
XCTAssertTrue(cookieConsentManaged == true)
} catch {
struct ErrorWithHTML: Error, LocalizedError, CustomDebugStringConvertible {
let originalError: Error
let html: String
var errorDescription: String? {
(originalError as CustomDebugStringConvertible).debugDescription + "\nHTML:\n\(html)"
}
var debugDescription: String {
errorDescription!
}
}
let html = try await tab.webView.evaluateJavaScript("document.documentElement.outerHTML") as? String
if let html {
throw ErrorWithHTML(originalError: error, html: html)
} else {
throw error
}
}
let isBannerHidden = try await tab.webView.evaluateJavaScript("window.getComputedStyle(banner).display === 'none'") as? Bool
XCTAssertTrue(isBannerHidden == true)
}
@MainActor
func testCosmeticRule_whenFakeCookieBannerIsDisplayedAndScriptsAreReloaded_bannerIsHidden() async throws {
// enable the feature
PrivacySecurityPreferences.shared.autoconsentEnabled = true
let url = URL(string: "http://privacy-test-pages.site/features/autoconsent/banner.html")!
let tab = self.tabViewModel.tab
// expect `cosmetic` to be published
let cookieConsentManagedPromise = tab.privacyInfoPublisher
.compactMap {
return $0?.$cookieConsentManaged
}
.switchToLatest()
.compactMap {
return $0?.isCosmeticRuleApplied == true ? true : nil
}
.receive(on: DispatchQueue.main)
.timeout(5)
.first()
.promise()
os_log("starting navigation to http://privacy-test-pages.site/features/autoconsent/banner.html")
let navigation = await tab.setUrl(url, userEntered: nil)?.value
navigation?.appendResponder(navigationResponse: { response in
os_log("navigationResponse: %s", "\(String(describing: response))")
// cause UserScripts reload (ContentBlockingUpdating)
PrivacySecurityPreferences.shared.gpcEnabled = true
PrivacySecurityPreferences.shared.gpcEnabled = false
return .allow
})
_=await navigation?.result
os_log("navigation done")
let cookieConsentManaged = try await cookieConsentManagedPromise.value
XCTAssertTrue(cookieConsentManaged == true)
let isBannerHidden = try await tab.webView.evaluateJavaScript("window.getComputedStyle(banner).display === 'none'") as? Bool
XCTAssertTrue(isBannerHidden == true)
}
}
private extension CookieConsentInfo {
var isConsentManaged: Bool {
Mirror(reflecting: self).children.first(where: { $0.label == "consentManaged" })?.value as! Bool
}
var isCosmeticRuleApplied: Bool {
(Mirror(reflecting: self).children.first(where: { $0.label == "cosmetic" })?.value as? Bool) ?? false
}
}