summaryrefslogtreecommitdiffstats
path: root/tests/selenium/storage_test.py
blob: d8e6c640b1ee9461cdd64e770e8d70d7fa49816c (plain)
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import unittest
import pbtest
from time import sleep

# time to wait for loading privacy policy from eff.org
POLICY_DOWNLOAD_TIMEOUT = 20
PB_POLICY_HASH_LEN = 40  # https://www.eff.org/files/dnt-policies.json


class StorageTest(pbtest.PBSeleniumTest):
    """Privacy Badger storage initialization tests."""

    def check_policy_download(self):
        timeout = POLICY_DOWNLOAD_TIMEOUT
        dnt_hashes_not_empty = (
            "return ("
            "chrome.extension.getBackgroundPage()."
            "badger.storage.getStore('dnt_hashes') != {}"
            ")"
        )
        # give updatePrivacyPolicyHashes() some time to download the policy hash
        while (timeout > 0 and not self.js(dnt_hashes_not_empty)):
            sleep(1)
            timeout -= 1

        # make sure we didn't time out
        self.assertGreater(timeout, 0, "Timed out waiting for DNT hashes")
        # now check the downloaded policy hash
        get_dnt_hashes = (
            "return ("
            "chrome.extension.getBackgroundPage()."
            "badger.storage.getStore('dnt_hashes')."
            "getItemClones()"
            ")"
        )
        policy_hashes = self.js(get_dnt_hashes)
        for policy_hash in policy_hashes.keys():
            self.assertEqual(PB_POLICY_HASH_LEN, len(policy_hash))

    def test_should_init_storage_entries(self):
        self.load_url(self.options_url)

        self.check_policy_download()
        self.assertEqual(
            self.js(
                "return chrome.extension.getBackgroundPage()."
                "constants.YELLOWLIST_URL"
            ),
            "https://www.eff.org/files/cookieblocklist_new.txt"
        )

        disabled_sites = self.js(
            "return chrome.extension.getBackgroundPage()."
            "badger.getSettings().getItem('disabledSites')"
        )
        self.assertFalse(
            len(disabled_sites),
            "Shouldn't have any disabledSites after installation"
        )

        self.assertTrue(self.js(
            "return chrome.extension.getBackgroundPage()."
            "badger.getSettings().getItem('checkForDNTPolicy')"
        ), "Should start with DNT policy enabled")


if __name__ == "__main__":
    unittest.main()