| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
import pbtest |
| 6 |
|
| 7 |
|
| 8 |
class CookieTest(pbtest.PBSeleniumTest): |
| 9 |
"""Basic test to make sure the PB doesn't mess up with the cookies.""" |
| 10 |
|
| 11 |
def assert_pass_opera_cookie_test(self, url, test_name): |
| 12 |
self.load_url(url) |
| 13 |
assert self.txt_by_css("#result") == "PASS", ( |
| 14 |
f"Cookie test failed: {test_name}") |
| 15 |
|
| 16 |
def test_should_pass_std_cookie_test(self): |
| 17 |
self.assert_pass_opera_cookie_test(( |
| 18 |
"https://efforg.github.io/privacybadger-test-fixtures/html/" |
| 19 |
"first_party_cookie.html" |
| 20 |
), "Set 1st party cookie") |
| 21 |
|
| 22 |
def test_cookie_tracker_detection(self): |
| 23 |
"""Tests basic cookie tracking. The tracking site has no DNT file, |
| 24 |
and gets blocked by PB. |
| 25 |
|
| 26 |
Visits three sites, all of which have an iframe that points to a fourth site |
| 27 |
that reads and writes a cookie. The third party cookie will be picked up by |
| 28 |
PB after each of the site loads, but no action will be taken. Then the first |
| 29 |
site will be reloaded, and the UI will show the third party domain as blocked.""" |
| 30 |
|
| 31 |
SITE1_URL = "https://ddrybktjfxh4.cloudfront.net/" |
| 32 |
SITE2_URL = "https://d3syxqe9po5ji0.cloudfront.net/" |
| 33 |
SITE3_URL = "https://d3b37ucnz1m2l2.cloudfront.net/" |
| 34 |
|
| 35 |
THIRD_PARTY_DOMAIN = "efforg.github.io" |
| 36 |
|
| 37 |
# remove pre-trained domains |
| 38 |
self.clear_tracker_data() |
| 39 |
|
| 40 |
# enable local learning |
| 41 |
self.wait_for_script("return window.OPTIONS_INITIALIZED") |
| 42 |
self.find_el_by_css('a[href="#tab-general-settings"]').click() |
| 43 |
self.find_el_by_css('#local-learning-checkbox').click() |
| 44 |
|
| 45 |
# load the first site with the third party code that reads and writes a cookie |
| 46 |
self.load_url(SITE1_URL) |
| 47 |
self.open_popup(SITE1_URL) |
| 48 |
# TODO it takes another visit (or a page reload) |
| 49 |
# TODO to show the domain as not-yet-blocked-but-tracking? |
| 50 |
#sliders = self.get_tracker_state() |
| 51 |
#assert THIRD_PARTY_DOMAIN in sliders['notYetBlocked'] |
| 52 |
self.close_window_with_url(SITE1_URL) |
| 53 |
|
| 54 |
# go to second site |
| 55 |
self.load_url(SITE2_URL) |
| 56 |
self.open_popup(SITE2_URL) |
| 57 |
sliders = self.get_tracker_state() |
| 58 |
assert THIRD_PARTY_DOMAIN in sliders['notYetBlocked'] |
| 59 |
self.close_window_with_url(SITE2_URL) |
| 60 |
|
| 61 |
# go to third site |
| 62 |
self.load_url(SITE3_URL) |
| 63 |
self.open_popup(SITE3_URL) |
| 64 |
sliders = self.get_tracker_state() |
| 65 |
assert THIRD_PARTY_DOMAIN in sliders['notYetBlocked'] |
| 66 |
self.close_window_with_url(SITE3_URL) |
| 67 |
|
| 68 |
# revisiting the first site should cause |
| 69 |
# the third-party domain to be blocked |
| 70 |
self.load_url(SITE1_URL) |
| 71 |
self.open_popup(SITE1_URL) |
| 72 |
sliders = self.get_tracker_state() |
| 73 |
assert THIRD_PARTY_DOMAIN in sliders['blocked'] |
| 74 |
|
| 75 |
|
| 76 |
if __name__ == "__main__": |
| 77 |
unittest.main() |