Skip to content

Commit

Permalink
added gfi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aschroed committed Nov 2, 2022
1 parent 5e7de2d commit 25e3f89
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 12 deletions.
119 changes: 110 additions & 9 deletions tests/test_get_field_info.py
Expand Up @@ -40,29 +40,124 @@ def mkey():
return gfi.FDN_Key(keypairs, "default")


def test_key():
def test_key_as_dict():
key = gfi.FDN_Key(keypairs, "default")
assert key
assert isinstance(key.con_key["server"], str)
assert isinstance(key.con_key['key'], str)
assert isinstance(key.con_key['secret'], str)


@pytest.fixture
def keydirname():
return './tests/data_files/'

@pytest.fixture
def keydir(keydirname):
return Path(keydirname)

@pytest.fixture
def keyfilename():
return 'keypairs.json'


@pytest.fixture
def keypath(keydir, keyfilename):
return keydir.joinpath(keyfilename)


@pytest.fixture
def missing_dirname():
return './missing/keydir/'


@pytest.fixture
def missing_dir(missing_dirname):
return Path(missing_dirname)

@pytest.mark.file_operation
def test_key_file(keypath):
''' testing when an actual keyfile path is provided as per --keyfile option'''
key = gfi.FDN_Key(keypath, "default")
assert key
assert isinstance(key.con_key["server"], str)
assert isinstance(key.con_key['key'], str)
assert isinstance(key.con_key['secret'], str)


@pytest.mark.file_operation
def test_key_from_env(mocker, keydirname):
''' testing getting directory where keypairs.json is stored when directory location
is set in an enviromental var - by mocking os.environ.get function
to hit this clause the expected default keypath must be passed to the constructor'''
default_keypath = CONFDIR / DEFAULT_KEYPAIR_FILE
mocker.patch('wranglertools.get_field_info.os.environ.get', return_value=keydirname)
key = gfi.FDN_Key(default_keypath, 'default')
assert key
assert isinstance(key.con_key["server"], str)
assert isinstance(key.con_key['key'], str)
assert isinstance(key.con_key['secret'], str)


def test_key_from_env_set_wrong(mocker, capsys):
''' testing when directory location is set in an enviromental var and the expected 'keypairs.json'
is not found in the director - by mocking os.environ.get function
to hit this clause the expected default keypath must be passed to the constructor'''
default_keypath = CONFDIR / DEFAULT_KEYPAIR_FILE
baddir = 'some/other/name/'
mocker.patch('wranglertools.get_field_info.os.environ.get', return_value=baddir)
# import pdb; pdb.set_trace()
key = gfi.FDN_Key(default_keypath, 'default')
out = capsys.readouterr()[0]
assert key.error
assert out == f'\n{baddir} directory set as an env variable does not contain {DEFAULT_KEYPAIR_FILE}\n\n'


@pytest.mark.file_operation
def test_key_file():
key = gfi.FDN_Key('./tests/data_files/keypairs.json', "default")
def test_key_from_default_location(mocker, keydir, keydirname, keyfilename):
'''little bit wonky as we are "mocking" the default location to be where the test file is stored
by over-riding the constant'''
mocker.patch("wranglertools.get_field_info.CONFDIR", keydir)
default_keypath = keydirname + keyfilename
key = gfi.FDN_Key(default_keypath, 'default')
assert key
assert isinstance(key.con_key["server"], str)
assert isinstance(key.con_key['key'], str)
assert isinstance(key.con_key['secret'], str)


def test_key_from_env(mocker):
#mocker.patch('wranglertools.get_field_info.os.environ.get', return_value='mock/dir')
#mocker.patch.object(Path, 'is_file')
#import pdb; pdb.set_trace()
key = gfi.FDN_Key('keypairs.json', 'default')
print(key)
@pytest.mark.file_operation
def test_key_from_home_location(mocker, keydir, keydirname, keyfilename):
'''little bit wonky as we are "mocking" the default location to be where the test file is stored
by over-riding the constant'''
mocker.patch("wranglertools.get_field_info.HOME", keydir)
default_keypath = keydirname + keyfilename
key = gfi.FDN_Key(default_keypath, 'default')
assert key
assert isinstance(key.con_key["server"], str)
assert isinstance(key.con_key['key'], str)
assert isinstance(key.con_key['secret'], str)


def test_key_default_file_missing(mocker, capsys, missing_dir, missing_dirname, keyfilename):
''' in this case we are mocking the default filename so it's not found'''
mocker.patch("wranglertools.get_field_info.CONFDIR", missing_dir)
mocker.patch("wranglertools.get_field_info.HOME", missing_dir)
mocker.patch('wranglertools.get_field_info.os.environ.get', return_value=None)
default_keypath = missing_dirname + keyfilename
key = gfi.FDN_Key(str(default_keypath), 'default')
out = capsys.readouterr()[0]
assert key.error
assert out == f"\nThe keyfile does not exist! Add keypairs.json to {missing_dir} or use the --keyfile option\n\n"


def test_key_no_keyfile(capsys):
''' this is testing something that should not be possible when running get_field_info but if using FDN_Key
in another context/script this could be relevant
'''
gfi.FDN_Key(None, 'default')
out = capsys.readouterr()[0]
assert out == "keyfile parameter missing\n"


def test_key_error_wrong_format(capsys):
Expand All @@ -72,6 +167,12 @@ def test_key_error_wrong_format(capsys):
assert out.strip() == message


def test_key_error_bad_keyname(capsys):
key = gfi.FDN_Key(keypairs, "nosuchkey")
out = capsys.readouterr()[0]
assert key.error
assert out == "ERROR: No key with name 'nosuchkey' found - check your keypairs file\n"

def bad_connection_will_exit():
with pytest.raises(SystemExit) as excinfo:
keypairs = {
Expand Down
6 changes: 3 additions & 3 deletions wranglertools/get_field_info.py
Expand Up @@ -116,14 +116,14 @@ def __init__(self, keyfile, keyname):
'''
self.error = False
keys = None
default_location = str(CONFDIR.joinpath(DEFAULT_KEYPAIR_FILE))
default_location = CONFDIR.joinpath(DEFAULT_KEYPAIR_FILE)
if not keyfile: # this should not happen as defaults are supplied in gfi or imp but just in case
msg = "keyfile parameter missing"
self.set_error(msg)
return
elif isinstance(keyfile, dict): # is the keyfile a dictionary
keys = keyfile
elif str(keyfile) != default_location:
elif pp.Path(str(keyfile)) != default_location:
fpath = pp.Path(str(keyfile))
if not fpath.is_file():
msg = f"\nThe keyfile {keyfile} does not exist\ncheck the --keyfile path or add {DEFAULT_KEYPAIR_FILE} to {CONFDIR}\n"
Expand Down Expand Up @@ -156,7 +156,7 @@ def __init__(self, keyfile, keyname):
try:
self.con_key = keys[keyname]
except KeyError:
msg = f"ERROR: No key with {keyname} found - check your keypairs file"
msg = f"ERROR: No key with name '{keyname}' found - check your keypairs file"
self.set_error(msg)
return
if not self.con_key['server'].endswith("/"):
Expand Down

0 comments on commit 25e3f89

Please sign in to comment.