def test_dont_setup_when_proxy_alive(
     self, mock_get_proxy_info, mock_run_with_gbasf2
 ):
     mock_get_proxy_info.return_value = {"secondsLeft": 999}
     setup_dirac_proxy()
     # check that gb2_proxy_init was never called via subprocess
     self.assertEqual(mock_run_with_gbasf2.call_count, 0)
 def test_raises_error_when_errormsg_in_stdout(
     self, mock_get_proxy_info, mock_run_with_gbasf2
 ):
     mock_get_proxy_info.return_value = {"secondsLeft": 0}
     # check that gb2_proxy_init was never called via subprocess
     mock_run_with_gbasf2.return_value = MockProcess(self.error_msg, "")
     with self.assertRaises(CalledProcessError):
         setup_dirac_proxy()
 def test_setup_proxy_when_no_proxy_info(
     self, mock_get_proxy_info, mock_run_with_gbasf2
 ):
     # pretend proxy is not initalized yet, then get_proxy_info raises CalledProcessError
     mock_get_proxy_info.side_effect = CalledProcessError(
         1, ["gb2_proxy_info", "-g", "belle"]
     )
     mock_run_with_gbasf2.return_value = MockProcess(self.success_msg, "")
     setup_dirac_proxy()
     self.assertEqual(mock_run_with_gbasf2.call_count, 1)
    def test_retry_on_wrong_password(self, mock_get_proxy_info, mock_run_with_gbasf2):
        # force setting up of new proxy
        mock_get_proxy_info.return_value = {"secondsLeft": 0}

        # mock run_with_gbasf2 to first return wrong PW message a couple of times, before returning successfully
        return_processes = [
            MockProcess(self.wrong_pw_msg, ""),
            MockProcess("", self.wrong_pw_msg),
            MockProcess(self.success_msg, ""),
        ]
        mock_run_with_gbasf2.side_effect = return_processes

        stderr = StringIO()
        with redirect_stderr(stderr):
            setup_dirac_proxy()
        self.assertListEqual(
            stderr.getvalue().splitlines(),
            [
                "Wrong certificate password, please try again.",
                "Wrong certificate password, please try again.",
            ],
        )
        self.assertEqual(mock_run_with_gbasf2.call_count, len(return_processes))
 def test_setup_proxy_on_0_seconds(self, mock_get_proxy_info, mock_run_with_gbasf2):
     # force setting up of new proxy
     mock_get_proxy_info.return_value = {"secondsLeft": 0}
     mock_run_with_gbasf2.return_value = MockProcess(self.success_msg, "")
     setup_dirac_proxy()
     self.assertEqual(mock_run_with_gbasf2.call_count, 1)