Unverified Commit bbeb6939 authored by Duncan Macleod's avatar Duncan Macleod
Browse files

test: make credential tests independent of environment

so that the tests don't fail because the test system happens to have real credentials present
parent aea1745b
Pipeline #459746 passed with stages
in 5 minutes and 52 seconds
......@@ -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():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment