Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated tests for gen_mappings #931

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 251 additions & 0 deletions tests/scripts/gen_mappings_utest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,257 @@
__import__("sys").modules["unittest.util"]._MAX_LENGTH = 999999999


class TestMakeCreLink(unittest.TestCase):
def test_make_cre_link_valid(self):
self.assertEqual(gm.make_cre_link("123", frontend=True), f"{gm.opencre_base_url}/cre/123")
self.assertEqual(gm.make_cre_link("123", frontend=False), f"{gm.opencre_rest_url}/id/123")

def test_make_cre_link_empty(self):
self.assertEqual(gm.make_cre_link("", frontend=True), f"{gm.opencre_base_url}/cre/")
self.assertEqual(gm.make_cre_link("", frontend=False), f"{gm.opencre_rest_url}/id/")


class TestProduceWebappMappings(unittest.TestCase):
@patch("security.safe_requests.get")
def test_produce_webapp_mappings_valid(self, mock_get):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"data": {"links": [{"document": {"name": "ASVS", "sectionID": "123-ASVS"}}]}}
mock_get.return_value = mock_response

source_file = {"suits": [{"cards": [{"cre": ["123"]}]}]}
standards_to_add = ["ASVS"]

result = gm.produce_webapp_mappings(source_file, standards_to_add)

expected = {
"meta": {
"edition": "webapp",
"component": "mappings",
"language": "ALL",
"version": "1.20",
},
"suits": [{"cards": [{"cre": ["123"], "ASVS": "123-ASVS"}]}],
}
self.assertEqual(result, expected)

@patch("security.safe_requests.get")
def test_produce_webapp_mappings_api_error(self, mock_get):
mock_get.return_value = MagicMock(status_code=500)

source_file = {"suits": [{"cards": [{"cre": ["123"]}]}]}
standards_to_add = ["ASVS"]

result = gm.produce_webapp_mappings(source_file, standards_to_add)

expected = {
"meta": {
"edition": "webapp",
"component": "mappings",
"language": "ALL",
"version": "1.20",
},
"suits": [{"cards": [{"cre": ["123"]}]}],
}
self.assertEqual(result, expected)

def test_produce_webapp_mappings_empty_input(self):
result = gm.produce_webapp_mappings({"suits": []}, ["ASVS"])
expected = {
"meta": {
"edition": "webapp",
"component": "mappings",
"language": "ALL",
"version": "1.20",
},
"suits": [],
}
self.assertEqual(result, expected)


class TestGenerateQrImages(unittest.TestCase):
@patch("qrcode.make")
@patch("builtins.open", new_callable=MagicMock)
def test_generate_qr_images(self, mock_open, mock_qrcode):
mock_img = MagicMock()
mock_qrcode.return_value = mock_img

existing_mappings = {"suits": [{"cards": [{"cre": ["123"]}]}]}
gm.generate_qr_images(existing_mappings, "/path/to/directory")

mock_qrcode.assert_called_once_with(f"{gm.opencre_base_url}/cre/123", image_factory=qrcode.image.svg.SvgImage)
mock_open.assert_called_once_with("/path/to/directory/123", "wb")
mock_img.save.assert_called()

def test_generate_qr_images_invalid_mapping(self):
with self.assertRaises(KeyError):
gm.generate_qr_images({"invalid_key": []}, "/path/to/directory")


class TestMain(unittest.TestCase):
@patch("builtins.open", new_callable=MagicMock)
@patch("yaml.safe_load")
@patch("yaml.safe_dump")
@patch("scripts.gen_mappings.produce_webapp_mappings")
@patch("argparse.ArgumentParser.parse_args")
def test_main_generate_mappings(self, mock_args, mock_produce, mock_dump, mock_load, mock_open):
mock_args.return_value = argparse.Namespace(
cres="test.yaml", target="output.yaml", staging=False, qr_images=None
)
mock_load.return_value = {"suits": []}

gm.main()

mock_load.assert_called_once()
mock_produce.assert_called_once()
mock_dump.assert_called_once()
mock_open.assert_any_call("output.yaml", "w")

@patch("builtins.print")
@patch("argparse.ArgumentParser.parse_args")
def test_main_missing_file(self, mock_args, mock_print):
mock_args.return_value = argparse.Namespace(cres="nonexistent.yaml", target=None, staging=False, qr_images=None)

with self.assertRaises(FileNotFoundError):
gm.main()

mock_print.assert_not_called()


@patch("builtins.open", new_callable=MagicMock)
@patch("yaml.safe_dump")
def test_main_with_target(self, mock_safe_dump, mock_open):
args = {"cres": "source/cre-mappings.yaml", "staging": False, "qr_images": None, "target": "output.yaml"}
mock_file = mock_open.return_value.__enter__.return_value
mock_file.read.return_value = """
suits:
- name: "Test Suit"
cards:
- cre: ["cre1"]
value: "Test Value"
"""

with patch.object(argparse, "ArgumentParser") as mock_parser:
mock_parser.return_value.parse_args.return_value = argparse.Namespace(**args)
with patch.object(yaml, "safe_load") as mock_load:
mock_load.return_value = {
"suits": [{"name": "Test Suit", "cards": [{"cre": ["cre1"], "value": "Test Value"}]}]
}
gm.main()

# Check that produce_webapp_mappings was called
mock_safe_dump.assert_called_once()
mock_open.assert_called_with("output.yaml", "w")


@patch("scripts.gen_mappings.generate_qr_images")
@patch("builtins.open", new_callable=MagicMock)
@patch("yaml.safe_load")
def test_main_with_qr_images(self, mock_safe_load, mock_open, mock_generate_qr_images):
args = {"cres": "source/cre-mappings.yaml", "staging": False, "qr_images": "/path/to/qr_images", "target": None}
mock_file = mock_open.return_value.__enter__.return_value
mock_file.read.return_value = """
suits:
- name: "Test Suit"
cards:
- cre: ["cre1"]
value: "Test Value"
"""
mock_safe_load.return_value = {
"suits": [{"name": "Test Suit", "cards": [{"cre": ["cre1"], "value": "Test Value"}]}]
}

with patch.object(argparse, "ArgumentParser") as mock_parser:
mock_parser.return_value.parse_args.return_value = argparse.Namespace(**args)
gm.main()

# Check that generate_qr_images was called
mock_generate_qr_images.assert_called_once_with(
{"suits": [{"name": "Test Suit", "cards": [{"cre": ["cre1"], "value": "Test Value"}]}]},
"/path/to/qr_images",
)


@patch("builtins.open", new_callable=MagicMock)
@patch("builtins.print")
def test_main_without_target_or_qr_images(self, mock_print, mock_open):
args = {"cres": "source/cre-mappings.yaml", "staging": False, "qr_images": None, "target": None}
mock_file = mock_open.return_value.__enter__.return_value
mock_file.read.return_value = """
suits:
- name: "Test Suit"
cards:
- cre: ["cre1"]
value: "Test Value"
"""

with patch.object(argparse, "ArgumentParser") as mock_parser:
mock_parser.return_value.parse_args.return_value = argparse.Namespace(**args)
with patch.object(yaml, "safe_load") as mock_load:
mock_load.return_value = {
"suits": [{"name": "Test Suit", "cards": [{"cre": ["cre1"], "value": "Test Value"}]}]
}
gm.main()

# Ensure print was not called since no output is specified
mock_print.assert_not_called()


class TestProduceWebappMappingsAdditional(unittest.TestCase):
@patch("security.safe_requests.get")
def test_empty_suits(self, mock_requests_get):
source_file = {"suits": []}
standards_to_add = ["ASVS"]

result = gm.produce_webapp_mappings(source_file, standards_to_add)

expected_result = {
"meta": {"edition": "webapp", "component": "mappings", "language": "ALL", "version": gm.CORNUCOPIA_VERSION},
"suits": [],
}
self.assertEqual(result, expected_result)

@patch("security.safe_requests.get")
def test_missing_links_in_response(self, mock_requests_get):
source_file = {"suits": [{"cards": [{"cre": ["cre1"]}]}]}
standards_to_add = ["ASVS"]

mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"data": {"links": []}}
mock_requests_get.return_value = mock_response

result = gm.produce_webapp_mappings(source_file, standards_to_add)

expected_result = {
"meta": {"edition": "webapp", "component": "mappings", "language": "ALL", "version": gm.CORNUCOPIA_VERSION},
"suits": [{"cards": [{"cre": ["cre1"]}]}],
}
self.assertEqual(result, expected_result)


class TestGenerateQRImagesAdditional(unittest.TestCase):
@patch("qrcode.make")
def test_invalid_directory_path(self, mock_qrcode_make):
existing_mappings = {"suits": [{"cards": [{"cre": ["cre1"]}]}]}
directory_path = "/invalid/path/to/directory"

with self.assertRaises(Exception):
gm.generate_qr_images(existing_mappings, directory_path)


class TestMainAdditional(unittest.TestCase):
@patch("builtins.open", side_effect=FileNotFoundError)
@patch("builtins.print")
def test_main_file_not_found(self, mock_print, mock_open):
args = {"cres": "missing.yaml", "staging": False, "qr_images": None, "target": None}

with patch.object(argparse, "ArgumentParser") as mock_parser:
mock_parser.return_value.parse_args.return_value = argparse.Namespace(**args)
with self.assertRaises(FileNotFoundError):
gm.main()


class TestGenerateQRImages(unittest.TestCase):
@patch("scripts.gen_mappings.make_cre_link")
@patch("qrcode.make")
Expand Down
Loading