From aa4529c331df32572d4ac8f73054c7d34e97db3a Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Jun 18 2022 01:15:08 +0000 Subject: Fix tests with bodhi-client 6+ bodhi-client was ported to use OIDC Client for authentication. We need to mock out its OIDC provider metadata discovery because it does an unavoidable network request, and we also need to make sure `HOME` is defined in `os.environ` because the new OIDC code expects to read it (in `BodhiClient._build_oidc_client()`). Signed-off-by: Adam Williamson --- diff --git a/test/test_cli.py b/test/test_cli.py index 03c9403..bd7f7d6 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -49,6 +49,11 @@ except ImportError: bodhi = None +def _mock_metadata(self, _): + """Minimal replacement for OIDCClient._get_provider_metadata""" + self.metadata = {"token_endpoint": "", "authorization_endpoint": ""} + + class TestIsUpdateAborted(CliTestCase): """Test is_update_aborted""" @@ -111,7 +116,7 @@ class TestUpdate(CliTestCase): self.mock_check_bodhi_version = \ self.check_bodhi_version_patcher.start() - self.os_environ_patcher = patch.dict('os.environ', {'EDITOR': 'vi'}) + self.os_environ_patcher = patch.dict('os.environ', {'EDITOR': 'vi', 'HOME': '/tmp'}) self.os_environ_patcher.start() self.user_patcher = patch('pyrpkg.Commands.user', @@ -136,15 +141,11 @@ class TestUpdate(CliTestCase): with io.open(clog_file, 'w', encoding='utf-8') as f: f.write(os.linesep.join(self.fake_clog)) - # Get 'bodhi_client' version. Particular versions have differences - # across distributions. - self.bodhi_version = None - try: - version_object = pkg_resources.get_distribution('bodhi_client') - if version_object.has_version(): - self.bodhi_version = int(version_object.version.split('.')[0]) - except pkg_resources.DistributionNotFound: - pass + if parse_version(bodhi_version) >= parse_version("6.0.0"): + self.oidcmeta_patcher = patch( + 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata + ) + self.oidcmeta_patcher.start() def tearDown(self): if os.path.exists('bodhi.template'): @@ -158,6 +159,8 @@ class TestUpdate(CliTestCase): self.check_bodhi_version_patcher.stop() self.run_command_patcher.stop() self.nvr_patcher.stop() + if parse_version(bodhi_version) >= parse_version("6.0.0"): + self.oidcmeta_patcher.stop() super(TestUpdate, self).tearDown() def get_cli(self, cli_cmd, name='fedpkg', cfg=None): @@ -221,7 +224,7 @@ class TestUpdate(CliTestCase): # there wasn't the option in older releases of bodhi, but these # releases are still active (epel7, epel8) - if self.bodhi_version <= 4: + if parse_version(bodhi_version) <= parse_version("4.0.0"): del expected_data["display_name"] with patch('os.unlink') as unlink: @@ -1569,12 +1572,24 @@ class TestBodhiOverride(CliTestCase): self.anon_kojisession_m = self.anon_kojisession_p.start() self.kojisession = self.anon_kojisession_m.return_value + self.os_environ_patcher = patch.dict('os.environ', {'EDITOR': 'vi', 'HOME': '/tmp'}) + self.os_environ_patcher.start() + + if parse_version(bodhi_version) >= parse_version("6.0.0"): + self.oidcmeta_patcher = patch( + 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata + ) + self.oidcmeta_patcher.start() + # Fake build returned from Koji for the specified build NVR in tests self.kojisession.getBuild.return_value = {'build_id': 1} def tearDown(self): self.anon_kojisession_p.stop() self.cbv_p.stop() + self.os_environ_patcher.stop() + if parse_version(bodhi_version) >= parse_version("6.0.0"): + self.oidcmeta_patcher.stop() super(TestBodhiOverride, self).tearDown() def test_raise_error_if_build_not_exist(self): @@ -1802,6 +1817,15 @@ class TestBodhiOverrideExtend(CliTestCase): self.anon_kojisession_m = self.anon_kojisession_p.start() self.kojisession = self.anon_kojisession_m.return_value + self.os_environ_patcher = patch.dict('os.environ', {'EDITOR': 'vi', 'HOME': '/tmp'}) + self.os_environ_patcher.start() + + if parse_version(bodhi_version) >= parse_version("6.0.0"): + self.oidcmeta_patcher = patch( + 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata + ) + self.oidcmeta_patcher.start() + self.load_cookies_p = patch( 'fedora.client.OpenIdBaseClient._load_cookies') self.mock_load_cookies = self.load_cookies_p.start() @@ -1813,6 +1837,9 @@ class TestBodhiOverrideExtend(CliTestCase): self.load_cookies_p.stop() self.anon_kojisession_p.stop() self.cbv_p.stop() + self.os_environ_patcher.stop() + if parse_version(bodhi_version) >= parse_version("6.0.0"): + self.oidcmeta_patcher.stop() super(TestBodhiOverrideExtend, self).tearDown() def test_specified_build_not_exist(self):