|
| 1 | +import importlib |
| 2 | +import pathlib |
| 3 | +import sys |
| 4 | +import types |
| 5 | +import unittest |
| 6 | + |
| 7 | +from flask import Flask |
| 8 | + |
| 9 | +VALID_GRAPHML = ( |
| 10 | + '<graphml xmlns="http://graphml.graphdrawing.org/xmlns">' |
| 11 | + '<graph edgedefault="directed">' |
| 12 | + '<actionHistory><hash>hash-1</hash></actionHistory>' |
| 13 | + '</graph>' |
| 14 | + '</graphml>' |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class FakeWorkFlowModel: |
| 19 | + def __init__(self, graph_response): |
| 20 | + self.graph_response = graph_response |
| 21 | + |
| 22 | + def get(self, _server_id): |
| 23 | + return self.graph_response |
| 24 | + |
| 25 | + def insert(self, graphml, latestHash): |
| 26 | + return 'test01' |
| 27 | + |
| 28 | + def update(self, serverID, graphml, latestHash, allHash): |
| 29 | + return (True, latestHash) |
| 30 | + |
| 31 | + def forceUpdate(self, serverID, graphml, latestHash): |
| 32 | + return (True, latestHash) |
| 33 | + |
| 34 | + |
| 35 | +class WorkflowControllerTests(unittest.TestCase): |
| 36 | + @classmethod |
| 37 | + def setUpClass(cls): |
| 38 | + server_root = pathlib.Path(__file__).resolve().parents[1] |
| 39 | + if str(server_root) not in sys.path: |
| 40 | + sys.path.insert(0, str(server_root)) |
| 41 | + |
| 42 | + fake_model_pkg = types.ModuleType('model') |
| 43 | + fake_model_workflows = types.ModuleType('model.workflows') |
| 44 | + |
| 45 | + class StubWorkFlowModel: |
| 46 | + def get(self, _server_id): |
| 47 | + return None |
| 48 | + |
| 49 | + fake_model_workflows.WorkFlowModel = StubWorkFlowModel |
| 50 | + fake_model_pkg.workflows = fake_model_workflows |
| 51 | + |
| 52 | + sys.modules['model'] = fake_model_pkg |
| 53 | + sys.modules['model.workflows'] = fake_model_workflows |
| 54 | + |
| 55 | + if 'controller.workflow' in sys.modules: |
| 56 | + del sys.modules['controller.workflow'] |
| 57 | + cls.workflow_module = importlib.import_module('controller.workflow') |
| 58 | + |
| 59 | + def make_client(self, graph_response): |
| 60 | + self.workflow_module.workFlowModel = FakeWorkFlowModel(graph_response) |
| 61 | + app = Flask(__name__) |
| 62 | + app.register_blueprint(self.workflow_module.workFlow, url_prefix='/workflow') |
| 63 | + return app.test_client() |
| 64 | + |
| 65 | + def test_missing_workflow_returns_404_for_none(self): |
| 66 | + client = self.make_client(None) |
| 67 | + response = client.get('/workflow/missing-id') |
| 68 | + self.assertEqual(response.status_code, 404) |
| 69 | + self.assertEqual(response.get_data(as_text=True), 'Not Found') |
| 70 | + |
| 71 | + def test_missing_workflow_returns_404_for_legacy_tuple(self): |
| 72 | + client = self.make_client((False, 'Record Not Found')) |
| 73 | + response = client.get('/workflow/missing-id') |
| 74 | + self.assertEqual(response.status_code, 404) |
| 75 | + self.assertEqual(response.get_data(as_text=True), 'Not Found') |
| 76 | + |
| 77 | + def test_hash_header_returns_400_for_different_history(self): |
| 78 | + client = self.make_client(VALID_GRAPHML) |
| 79 | + response = client.get('/workflow/existing-id', headers={'X-Latest-Hash': 'unknown-hash'}) |
| 80 | + self.assertEqual(response.status_code, 400) |
| 81 | + self.assertEqual(response.get_data(as_text=True), 'Different History') |
| 82 | + |
| 83 | + def test_hash_header_returns_200_for_matching_history(self): |
| 84 | + client = self.make_client(VALID_GRAPHML) |
| 85 | + response = client.get('/workflow/existing-id', headers={'X-Latest-Hash': 'hash-1'}) |
| 86 | + self.assertEqual(response.status_code, 200) |
| 87 | + self.assertEqual(response.get_data(as_text=True), VALID_GRAPHML) |
| 88 | + |
| 89 | + def test_post_workflow_returns_server_id(self): |
| 90 | + client = self.make_client(None) |
| 91 | + response = client.post('/workflow/', data=VALID_GRAPHML, |
| 92 | + content_type='application/xml') |
| 93 | + self.assertEqual(response.status_code, 200) |
| 94 | + self.assertEqual(response.get_data(as_text=True), 'test01') |
| 95 | + |
| 96 | + def test_post_workflow_invalid_xml_returns_400(self): |
| 97 | + client = self.make_client(None) |
| 98 | + response = client.post('/workflow/', data=b'not xml', |
| 99 | + content_type='application/xml') |
| 100 | + self.assertEqual(response.status_code, 400) |
| 101 | + |
| 102 | + def test_update_workflow_returns_200(self): |
| 103 | + client = self.make_client(None) |
| 104 | + response = client.post('/workflow/test01', data=VALID_GRAPHML, |
| 105 | + content_type='application/xml') |
| 106 | + self.assertEqual(response.status_code, 200) |
| 107 | + |
| 108 | + def test_update_workflow_invalid_xml_returns_400(self): |
| 109 | + client = self.make_client(None) |
| 110 | + response = client.post('/workflow/test01', data=b'not xml', |
| 111 | + content_type='application/xml') |
| 112 | + self.assertEqual(response.status_code, 400) |
| 113 | + |
| 114 | + def test_force_update_workflow_returns_200(self): |
| 115 | + client = self.make_client(None) |
| 116 | + response = client.post('/workflow/test01?force=true', data=VALID_GRAPHML, |
| 117 | + content_type='application/xml') |
| 118 | + self.assertEqual(response.status_code, 200) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + unittest.main() |
0 commit comments