|
9 | 9 |
|
10 | 10 | @pytest.fixture |
11 | 11 | def fake_api_key(): |
12 | | - """Fixture to provide a fake API key. Note: these credentials have been inactivated""" |
| 12 | + """Fixture to provide a fake API key. Note: these credentials have been inactivated.""" |
| 13 | + # This is a valid JWT and UUID, but is not active. |
13 | 14 | return { |
14 | | - "api_key": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImZlbmNlX2tleV9rZXkiLCJ0eXAiOiJKV1QifQ.eyJwdXIiOiJhcGlfa2V5Iiwic3ViIjoiMjEiLCJpc3MiOiJodHRwczovL2RhdGEudGVzdC5iaW9jb21tb25zLm9yZy5hdS91c2VyIiwiYXVkIjpbImh0dHBzOi8vZGF0YS50ZXN0LmJpb2NvbW1vbnMub3JnLmF1L3VzZXIiXSwiaWF0IjoxNzQyMjUzNDgwLCJleHAiOjE3NDQ4NDU0ODAsImp0aSI6ImI5MDQyNzAxLWIwOGYtNDBkYS04OWEzLTc1M2JlNGVkMTIyOSIsImF6cCI6IiIsInNjb3BlIjpbImdvb2dsZV9jcmVkZW50aWFscyIsIm9wZW5pZCIsImdvb2dsZV9zZXJ2aWNlX2FjY291bnQiLCJkYXRhIiwiZmVuY2UiLCJnb29nbGVfbGluayIsImFkbWluIiwidXNlciIsImdhNGdoX3Bhc3Nwb3J0X3YxIl19.SGPjs6ljCJbwDu-6WAnI5dN8o5467_ktcnsxRFrX_aCQNrOwSPgTCDvWEzamRmB5Oa0yB6cnjduhWRKnPWIZDal86H0etm77wilCteHF_zFl1IV6LW23AfOVOG3zB9KL6o-ZYqpSRyo0FDj0vQJzrHXPjqvQ15S6Js2sIwIa3ONTeHbR6fRecfPaLK1uGIY5tJFeigXzrLzlifKCEnt_2gqpMU2_b2QgW1315FixNIUOl8A7FZJ2-ddSMJPO0IYQ0QMSWV9-bbxie4Zjsaa1HtQYOhfXLU3vSdUOBO0btSfd6-NnWfx_-lDo5V9lkSH_aecEyew0IHBx-e7rSR5cxA", |
| 15 | + "api_key": ( |
| 16 | + "eyJhbGciOiJSUzI1NiIsImtpZCI6ImZlbmNlX2tleV9rZXkiLCJ0eXAiOiJKV1QifQ." |
| 17 | + "eyJwdXIiOiJhcGlfa2V5Iiwic3ViIjoiMjEiLCJpc3MiOiJodHRwczovL2RhdGEudGVzdC5i" |
| 18 | + "aW9jb21tb25zLm9yZy5hdS91c2VyIiwiYXVkIjpbImh0dHBzOi8vZGF0YS50ZXN0LmJpb2Nv" |
| 19 | + "bW1vbnMub3JnLmF1L3VzZXIiXSwiaWF0IjoxNzQyMjUzNDgwLCJleHAiOjE3NDQ4NDU0ODAs" |
| 20 | + "Imp0aSI6ImI5MDQyNzAxLWIwOGYtNDBkYS04OWEzLTc1M2JlNGVkMTIyOSIsImF6cCI6IiIs" |
| 21 | + "InNjb3BlIjpbImdvb2dsZV9jcmVkZW50aWFscyIsIm9wZW5pZCIsImdvb2dsZV9zZXJ2aWNl" |
| 22 | + "X2FjY291bnQiLCJkYXRhIiwiZmVuY2UiLCJnb29nbGVfbGluayIsImFkbWluIiwidXNlciIs" |
| 23 | + "ImdhNGdoX3Bhc3Nwb3J0X3YxIl19." |
| 24 | + "SGPjs6ljCJbwDu-6WAnI5dN8o5467_ktcnsxRFrX_aCQNrOwSPgTCDvWEzamRmB5Oa0yB6cn" |
| 25 | + "jduhWRKnPWIZDal86H0etm77wilCteHF_zFl1IV6LW23AfOVOG3zB9KL6o-ZYqpSRyo0FDj0" |
| 26 | + "vQJzrHXPjqvQ15S6Js2sIwIa3ONTeHbR6fRecfPaLK1uGIY5tJFeigXzrLzlifKCEnt_2gqp" |
| 27 | + "MU2_b2QgW1315FixNIUOl8A7FZJ2-ddSMJPO0IYQ0QMSWV9-bbxie4Zjsaa1HtQYOhfXLU3v" |
| 28 | + "SdUOBO0btSfd6-NnWfx_-lDo5V9lkSH_aecEyew0IHBx-e7rSR5cxA" |
| 29 | + ), |
15 | 30 | "key_id": "b9042701-b08f-40da-89a3-753be4ed1229" |
16 | 31 | } |
17 | 32 |
|
18 | 33 | @pytest.fixture |
19 | 34 | def gen3_metadata_parser(): |
20 | 35 | """Fixture to create a Gen3MetadataParser instance.""" |
21 | | - return Gen3MetadataParser( |
22 | | - key_file_path="fake_credentials.json" |
23 | | - ) |
| 36 | + return Gen3MetadataParser(key_file_path="fake_credentials.json") |
24 | 37 |
|
| 38 | +@pytest.fixture |
| 39 | +def malformed_json_credentials(): |
| 40 | + """Fixture for malformed JSON credentials (no quotes, not valid Python dict).""" |
| 41 | + # This is a string, not a dict, to simulate malformed file content. |
| 42 | + return '{api_key: abc.def.ghi, key_id: 18bdaa-b018}' |
| 43 | + |
| 44 | +def test_add_quotes_to_json_valid(gen3_metadata_parser): |
| 45 | + """Test _add_quotes_to_json with valid JSON (should parse as-is).""" |
| 46 | + valid_json = '{"api_key": "abc.def.ghi", "key_id": "18bdaa-b018"}' |
| 47 | + result = gen3_metadata_parser._add_quotes_to_json(valid_json) |
| 48 | + assert result == {"api_key": "abc.def.ghi", "key_id": "18bdaa-b018"} |
| 49 | + |
| 50 | +def test_add_quotes_to_json_malformed(gen3_metadata_parser): |
| 51 | + """Test _add_quotes_to_json with malformed JSON (no quotes).""" |
| 52 | + malformed = '{api_key: abc.def.ghi, key_id: 18bdaa-b018}' |
| 53 | + result = gen3_metadata_parser._add_quotes_to_json(malformed) |
| 54 | + assert result == {"api_key": "abc.def.ghi", "key_id": "18bdaa-b018"} |
| 55 | + |
| 56 | +def test_add_quotes_to_json_url_and_uuid(gen3_metadata_parser): |
| 57 | + """Test _add_quotes_to_json with keys/values including url and uuid.""" |
| 58 | + malformed = '{key1: value1, key2:123, url: https://example.com, uuid: 18bdaa-b018}' |
| 59 | + result = gen3_metadata_parser._add_quotes_to_json(malformed) |
| 60 | + assert result == { |
| 61 | + "key1": "value1", |
| 62 | + "key2": "123", |
| 63 | + "url": "https://example.com", |
| 64 | + "uuid": "18bdaa-b018" |
| 65 | + } |
| 66 | + |
| 67 | +def test_add_quotes_to_json_invalid(gen3_metadata_parser): |
| 68 | + """Test _add_quotes_to_json with unrecoverable malformed JSON.""" |
| 69 | + bad = '{key1 value1, key2:}' |
| 70 | + with pytest.raises(ValueError): |
| 71 | + gen3_metadata_parser._add_quotes_to_json(bad) |
25 | 72 |
|
26 | | -def test_load_api_key(gen3_metadata_parser, fake_api_key): |
27 | | - """Test the _load_api_key method.""" |
28 | | - # Mock open() to simulate reading the fake API key from a file |
| 73 | +def test_load_api_key_valid_json(gen3_metadata_parser, fake_api_key): |
| 74 | + """Test the _load_api_key method with valid JSON file content.""" |
| 75 | + # Simulate reading a valid JSON file |
29 | 76 | with patch("builtins.open", mock_open(read_data=json.dumps(fake_api_key))): |
30 | 77 | result = gen3_metadata_parser._load_api_key() |
31 | 78 | assert result == fake_api_key |
32 | 79 |
|
| 80 | +def test_load_api_key_malformed_json(gen3_metadata_parser, malformed_json_credentials): |
| 81 | + """Test the _load_api_key method with malformed JSON (no quotes).""" |
| 82 | + # Simulate reading a malformed JSON file (no quotes) |
| 83 | + with patch("builtins.open", mock_open(read_data=malformed_json_credentials)): |
| 84 | + result = gen3_metadata_parser._load_api_key() |
| 85 | + assert result == {"api_key": "abc.def.ghi", "key_id": "18bdaa-b018"} |
| 86 | + |
| 87 | +def test_load_api_key_invalid_json(gen3_metadata_parser): |
| 88 | + """Test the _load_api_key method with unrecoverable malformed JSON.""" |
| 89 | + # Simulate reading a badly malformed JSON file |
| 90 | + bad_content = '{key1 value1, key2:}' |
| 91 | + with patch("builtins.open", mock_open(read_data=bad_content)): |
| 92 | + with pytest.raises(ValueError): |
| 93 | + gen3_metadata_parser._load_api_key() |
| 94 | + |
33 | 95 | def test_url_from_jwt(gen3_metadata_parser, fake_api_key): |
34 | 96 | """Test if you can infer the data commons url from the JWT token""" |
35 | 97 | url = gen3_metadata_parser._url_from_jwt(fake_api_key) |
|
0 commit comments