GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

test_onion_location.py 6.28 KB
Newer Older
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
from marionette_driver import By, Wait
from marionette_driver.errors import MarionetteException, NoSuchElementException, TimeoutException

from marionette_harness import MarionetteTestCase, WindowManagerMixin


class Test(WindowManagerMixin, MarionetteTestCase):

    def test_onion_location(self):
        m = self.marionette
        m.timeout.implicit = 10

        self.assertEqual(None, m.get_pref(
            'privacy.prioritizeonions.showNotification'))

        with m.using_context('content'):
            m.navigate('https://www.torproject.org/')

        with m.using_context('chrome'):
            # Check whether the urlbar badge is displayed
            self.assertTrue(m.find_element(
                'id', 'onion-location-box').is_displayed())

            # Check whether the notification is displayed the first time
            notification = m.find_element('id', 'onion-location-notification')
            self.assertTrue(notification.is_displayed())
            self.assertFalse(m.get_pref(
                'privacy.prioritizeonions.showNotification'))
            always_prioritize = notification.find_element(
                'css selector', '.popup-notification-primary-button')
            self.assertEqual(always_prioritize.get_attribute(
                'label'), 'Always Prioritize Onions')

            # Check learn more link
            notification.find_element(
                'css selector', '.popup-notification-learnmore-link').click()
            with m.using_context('content'):
                Wait(m, timeout=m.timeout.page_load).until(
                    lambda _: len(m.window_handles) > 1)
                m.switch_to_window(m.window_handles[1])
                Wait(m, timeout=m.timeout.page_load).until(
                    lambda _: m.get_url() != "about:blank")
                self.assertEqual(
                    m.get_url(), "https://tb-manual.torproject.org/onion-services/")
                m.close()
                m.switch_to_window(m.window_handles[0])

        with m.using_context('chrome'):
            # Close the notification and check that it's not displayed anymore.
            notification = m.find_element('id', 'onion-location-notification')
            not_now = notification.find_element(
                'css selector', '.popup-notification-secondary-button')
            self.assertEqual(not_now.get_attribute('label'), 'Not Now')
            not_now.click()
            try:
                self.assertFalse(m.find_element(
                    'id', 'onion-location-notification').is_displayed())
            except NoSuchElementException:
                pass

            # Show the notification again
            m.set_pref('privacy.prioritizeonions.showNotification', None)
            new_tab = self.open_tab()
            m.switch_to_window(new_tab)
            m.close()
            m.switch_to_window(self.start_tab)

            # Click "Always prioritize" in the notification
            notification = m.find_element('id', 'onion-location-notification')
            notification.find_element(
                'css selector', '.popup-notification-primary-button').click()
            self.assertTrue(m.get_pref('privacy.prioritizeonions.enabled'))

            with m.using_context('content'):
                m.switch_to_window(m.window_handles[1])
                self.assertEqual(
                    m.get_url(), 'about:preferences#privacy-onionservices')
                m.close()
                m.switch_to_window(self.start_tab)

                # Check that the original page is redirected to .onion
                Wait(m, timeout=m.timeout.page_load).until(
                    lambda _: m.get_url() != 'https://www.torproject.org/')
                self.assertEqual(
                    m.get_url(), 'http://expyuzz4wqqyqhjn.onion/index.html')

                # Check that auto-redirects work
                m.navigate('https://www.torproject.org/')
                self.assertEqual(m.get_url(), 'https://www.torproject.org/')
                Wait(m, timeout=m.timeout.page_load).until(
                    lambda _: m.get_url() != 'https://www.torproject.org/')
                self.assertEqual(
                    m.get_url(), 'http://expyuzz4wqqyqhjn.onion/index.html')

                # Go to preferences and disable auto-redirects
                new_tab = self.open_tab()
                m.switch_to_window(new_tab)
                m.navigate('about:preferences#privacy-onionservices')
                m.find_element('id', 'onionServicesRadioAsk').click()
                self.assertFalse(m.get_pref(
                    'privacy.prioritizeonions.enabled'))
                m.close()
                m.switch_to_window(self.start_tab)
                m.navigate('https://www.torproject.org/')
                try:
                    Wait(m, timeout=5).until(lambda _: m.get_url()
                                             != 'https://www.torproject.org/')
                    self.assertTrue(False, "Should not redirect")
                except TimeoutException:
                    pass

            # Check that the page is redirected when clicking on the urlbar badge
            with m.using_context('chrome'):
                self.assertTrue(m.find_element(
                    'id', 'onion-location-box').is_displayed())
                m.find_element('id', 'onion-location-box').click()
                with m.using_context('content'):
                    Wait(m, timeout=5).until(lambda _: m.get_url()
                                             != 'https://www.torproject.org/')
                    self.assertEqual(
                        m.get_url(), 'http://expyuzz4wqqyqhjn.onion/index.html')

            # Check learn more link
            with m.using_context('content'):
                m.navigate('about:preferences#privacy-onionservices')
                m.find_element('id', 'onionServicesLearnMore').click()
                Wait(m, timeout=m.timeout.page_load).until(
                    lambda _: len(m.window_handles) > 1)
                m.switch_to_window(m.window_handles[1])
                Wait(m, timeout=m.timeout.page_load).until(
                    lambda _: m.get_url() != "about:blank")
                self.assertEqual(
                    m.get_url(), "https://tb-manual.torproject.org/onion-services/")
                m.close()
                m.switch_to_window(m.window_handles[0])