def test_gsettings_cmdline_correct(self):
        """The command line used to get the proxy settings is the right one."""
        expected = "gsettings list-recursively org.gnome.system.proxy".split()
        called = []

        def append_output(args):
            """Append the output and return some settings."""
            called.append(args)
            return TEMPLATE_GSETTINGS_OUTPUT.format(**BASE_GSETTINGS_VALUES)

        self.patch(gsettings.subprocess, "check_output", append_output)
        gsettings.get_proxy_settings()
        self.assertEqual(called[0], expected)
 def test_gsettings_auth_over_url(self):
     """Test that the settings are more important that the url."""
     template_values = dict(BASE_GSETTINGS_VALUES)
     expected_host = "expected_host"
     expected_port = 54321
     expected_user = "******"
     expected_password = "******"
     composed_url = '%s:%s@%s' % ('user', 'random',
                                  expected_host)
     http_expected = {
         "host": expected_host,
         "port": expected_port,
         "username": expected_user,
         "password": expected_password,
     }
     template_values.update({
         "mode": "manual",
         "http_host": composed_url,
         "http_port": expected_port,
         "auth_user": expected_user,
         "auth_password": expected_password,
         "http_use_auth": "true",
     })
     fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
     self.patch(gsettings.subprocess, "check_output",
                lambda _: fake_output)
     ps = gsettings.get_proxy_settings()
     self.assertEqual(ps["http"], http_expected)
 def _assert_parser_authenticated_url(self, scheme):
     """Test a parser of gsettings with creds in the url."""
     template_values = dict(BASE_GSETTINGS_VALUES)
     expected_host = "expected_host"
     expected_port = 54321
     expected_user = "******"
     expected_password = "******"
     composed_url = '%s:%s@%s' % (expected_user, expected_password,
                                  expected_host)
     expected = {
         "host": expected_host,
         "port": expected_port,
         "username": expected_user,
         "password": expected_password,
     }
     template_values.update({
         "mode": "manual",
         scheme + "_host": composed_url,
         scheme + "_port": expected_port,
         "http_use_auth": "false",
     })
     fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
     self.patch(gsettings.subprocess, "check_output",
                lambda _: fake_output)
     ps = gsettings.get_proxy_settings()
     self.assertEqual(ps[scheme], expected)
 def test_gsettings_parser_http_authenticated(self):
     """Test a parser of gsettings."""
     template_values = dict(BASE_GSETTINGS_VALUES)
     expected_host = "expected_host"
     expected_port = 54321
     expected_user = "******"
     expected_password = "******"
     expected = {
         "host": expected_host,
         "port": expected_port,
         "username": expected_user,
         "password": expected_password,
     }
     template_values.update({
         "mode": "manual",
         "http_host": expected_host,
         "http_port": expected_port,
         "auth_user": expected_user,
         "auth_password": expected_password,
         "http_use_auth": "true",
     })
     fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
     self.patch(gsettings.subprocess, "check_output",
                lambda _: fake_output)
     ps = gsettings.get_proxy_settings()
     self.assertEqual(ps["http"], expected)
 def test_gsettings_parser_none(self):
     """Test a parser of gsettings."""
     expected = {}
     fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**BASE_GSETTINGS_VALUES)
     self.patch(gsettings.subprocess, "check_output",
                lambda _: fake_output)
     ps = gsettings.get_proxy_settings()
     self.assertEqual(ps, expected)
 def test_gsettings_empty_ignore_hosts(self):
     """Missing values in the ignore hosts."""
     troublesome_value = "@as []"
     template_values = dict(BASE_GSETTINGS_VALUES)
     template_values["ignore_hosts"] = troublesome_value
     fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
     self.patch(gsettings.subprocess, "check_output",
                lambda _: fake_output)
     ps = gsettings.get_proxy_settings()
     self.assertEqual(ps, {})
 def _assert_parser_empty_url(self, scheme):
     """Assert the parsing of an empty url."""
     template_values = dict(BASE_GSETTINGS_VALUES)
     template_values.update({
         "mode": "manual",
         scheme + "_host": '',
         scheme + "_port": 0,
         "http_use_auth": "false",
     })
     fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
     self.patch(gsettings.subprocess, "check_output",
                lambda _: fake_output)
     ps = gsettings.get_proxy_settings()
     self.assertNotIn(scheme, ps)
def check_proxy_enabled(host, port):
    """Check if the proxy is enabled."""
    port = int(port)
    if sys.platform.startswith("linux"):
        settings = gsettings.get_proxy_settings()
        enabled = len(settings) > 0
        if enabled:
            proxy = build_proxy(settings)
            QNetworkProxy.setApplicationProxy(proxy)
        else:
            logger.info("Proxy is disabled.")
        return enabled
    else:
        QNetworkProxyFactory.setUseSystemConfiguration(True)
        query = QNetworkProxyQuery(host, port)
        proxies = QNetworkProxyFactory.systemProxyForQuery(query)
        return len(proxies) and proxies[0].type() != QNetworkProxy.NoProxy
    def test_gsettings_cannot_parse(self):
        """Some weird setting that cannot be parsed is logged with warning."""
        memento = MementoHandler()
        memento.setLevel(logging.DEBUG)
        gsettings.logger.addHandler(memento)
        self.addCleanup(gsettings.logger.removeHandler, memento)

        troublesome_value = "#bang"
        template_values = dict(BASE_GSETTINGS_VALUES)
        template_values["ignore_hosts"] = troublesome_value
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
        self.patch(gsettings.subprocess, "check_output",
                   lambda _: fake_output)
        ps = gsettings.get_proxy_settings()
        self.assertTrue(memento.check_warning(gsettings.CANNOT_PARSE_WARNING %
                                              troublesome_value))
        self.assertEqual(ps, {})
 def _assert_parser_anonymous(self, scheme):
     """Assert the parsing of anonymous settings."""
     template_values = dict(BASE_GSETTINGS_VALUES)
     expected_host = "expected_host"
     expected_port = 54321
     expected = {
         "host": expected_host,
         "port": expected_port,
     }
     template_values.update({
         "mode": "manual",
         scheme + "_host": expected_host,
         scheme + "_port": expected_port,
     })
     fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
     self.patch(gsettings.subprocess, "check_output",
                lambda _: fake_output)
     ps = gsettings.get_proxy_settings()
     self.assertEqual(ps[scheme], expected)