From 9ec1f66d90fa4601237017dbbef7fe9f954968a5 Mon Sep 17 00:00:00 2001 From: "duncan.macleod" Date: Thu, 29 Sep 2022 14:50:52 +0100 Subject: [PATCH] test: make credential tests independent of environment so that the tests don't fail because the test system happens to have real credentials present --- ligo/gracedb/test/test_client.py | 75 ++++++++++++-------------------- 1 file changed, 28 insertions(+), 47 deletions(-) diff --git a/ligo/gracedb/test/test_client.py b/ligo/gracedb/test/test_client.py index 3083e3d..c517e6f 100644 --- a/ligo/gracedb/test/test_client.py +++ b/ligo/gracedb/test/test_client.py @@ -124,68 +124,49 @@ def test_x509_lookup_proxy_from_envvars(): assert creds['x509'].get('key_file') == proxy_file -@pytest.mark.parametrize("fail_if_noauth", [True, False]) -def test_no_credentials(fail_if_noauth): +@mock.patch("igwn_auth_utils.requests.find_scitoken", return_value=None) +@mock.patch("igwn_auth_utils.requests.find_x509_credentials", + return_value=None) +def test_no_credentials(_find_x509, _find_token): """Test client instantiation with no credentials at all""" - # Unset environment: - os_environ_func = 'os.environ' - mock_environ_dict = {} - with mock.patch.dict(os_environ_func, mock_environ_dict): - - # Initialize client - if fail_if_noauth: - err_str = 'no valid authorisation credentials found' - with pytest.raises(IgwnAuthError, match=err_str): - g = GraceDb(fail_if_noauth=fail_if_noauth) - else: - # Check credentials - g = GraceDb(fail_if_noauth=fail_if_noauth) - assert not g.auth_type + # Check credentials + g = GraceDb(fail_if_noauth=False) + assert not g.auth_type +@mock.patch.dict("os.environ", { + "X509_USER_CERT": "cert_file", + "X509_USER_KEY": "key_file", +}) def test_force_noauth(): """Test forcing no authentication, even with X509 certs available""" - # Setup - cert_file = '/tmp/cert_file' - key_file = '/tmp/key_file' - # Initialize client - environ_dict = 'os.environ' - mock_environ_dict = { - 'X509_USER_CERT': cert_file, - 'X509_USER_KEY': key_file, - } - with mock.patch.dict(environ_dict, mock_environ_dict): # noqa: E127 - - # Initialize client - g = GraceDb(force_noauth=True) + g = GraceDb(force_noauth=True) # Check credentials assert not g.auth_type -@pytest.mark.parametrize("creds_found", [True, False]) -def test_fail_if_noauth(creds_found): +@mock.patch("igwn_auth_utils.requests.find_scitoken", return_value=None) +@mock.patch("igwn_auth_utils.requests.find_x509_credentials", + return_value=None) +def test_fail_if_noauth(*patches): """Test failing if no authentication credentials are provided""" + err_str = 'no valid authorisation credentials found' + with pytest.raises(IgwnAuthError, match=err_str): + GraceDb(fail_if_noauth=True) + + +def test_fail_if_noauth_creds(): + """Test fail_if_noauth doesn't error if credentials are provided""" cert_file = '/tmp/cert_file' key_file = '/tmp/key_file' - # Unset environment: - os_environ_func = 'os.environ' - mock_environ_dict = {} - with mock.patch.dict(os_environ_func, mock_environ_dict): - - if not creds_found: - # Initialize client - err_str = 'no valid authorisation credentials found' - with pytest.raises(IgwnAuthError, match=err_str): - g = GraceDb(fail_if_noauth=True) - else: - # Initialize client: - g = GraceDb(cred=(cert_file, key_file), fail_if_noauth=True) - - # Check credentials - assert len(g.cert) == 2 + # Initialize client: + g = GraceDb(cred=(cert_file, key_file), fail_if_noauth=True) + + # Check credentials + assert g.cert == (cert_file, key_file) def test_force_noauth_and_fail_if_noauth(): -- GitLab