|
1 | 1 | """Teat Module for the App""" |
2 | 2 | import unittest |
3 | | -from unittest.mock import patch |
4 | | -from app import app |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | +from app import get_workflows, get_workflow_runs, app, get_all_workflow_runs |
5 | 5 |
|
6 | 6 |
|
7 | | -class TestApp(unittest.TestCase): |
| 7 | +class AppTests(unittest.TestCase): |
8 | 8 | """Test cases for the App module.""" |
9 | 9 |
|
10 | 10 | def setUp(self): |
11 | | - """Set up the test environment.""" |
12 | | - app.testing = True |
13 | | - app.config['BASIC_AUTH_USERNAME'] = 'user' |
14 | | - app.config['BASIC_AUTH_PASSWORD'] = 'pass' |
15 | | - self.client = app.test_client() |
16 | | - self.headers = { |
17 | | - 'Authorization': 'Basic dXNlcjpwYXNz' |
18 | | - } |
19 | | - |
20 | | - def test_health_endpoint(self): |
21 | | - """Test health check status & version in the health route.""" |
22 | | - response = self.client.get('/health') |
23 | | - self.assertEqual(response.status_code, 200) |
24 | | - self.assertIn(b'{"status":"ok","version":"2.2.0"}', response.data) |
25 | | - |
26 | | - def test_index_missing_parameters(self): |
27 | | - """Test handling missing parameters in the index route.""" |
28 | | - response = self.client.get('/', headers=self.headers) |
29 | | - self.assertEqual(response.status_code, 400) |
30 | | - self.assertIn(b"Missing parameter(s)", response.data) |
31 | | - |
32 | | - @patch('app.get_all_workflow_runs') |
33 | | - def test_index_successful_response(self, mock_get_all_workflow_runs): |
34 | | - """Test successful response in the index route.""" |
35 | | - mock_get_all_workflow_runs.return_value = [ |
36 | | - { |
37 | | - "name": "CI", |
38 | | - "status": "completed", |
39 | | - "conclusion": "success", |
40 | | - "updated_at": "2021-09-20T18:25:34Z", |
41 | | - "html_url": "https://github.com/owner/repo/actions/runs/1234" |
42 | | - } |
43 | | - ] |
44 | | - response = self.client.get('/?owner=owner&repo=repo', headers=self.headers) |
45 | | - self.assertEqual(response.status_code, 200) |
46 | | - self.assertIn(b'<Project', response.data) |
47 | | - self.assertIn(b'name="repo/CI"', response.data) |
48 | | - self.assertIn(b'activity="Sleeping', response.data) |
49 | | - self.assertIn(b'lastBuildStatus="Success"', response.data) |
50 | | - self.assertIn(b'webUrl="https://github.com/owner/repo/actions/runs/1234"', response.data) |
51 | | - |
52 | | - @patch('app.get_all_workflow_runs') |
53 | | - def test_index_unknown_build_status(self, mock_get_all_workflow_runs): |
54 | | - """Test unknown build status in the index route.""" |
55 | | - mock_get_all_workflow_runs.return_value = [ |
56 | | - { |
57 | | - "name": "CI", |
58 | | - "status": "in_progress", |
59 | | - "conclusion": None, |
60 | | - "updated_at": "2021-09-20T18:25:34Z", |
61 | | - "html_url": "https://github.com/owner/repo/actions/runs/1234" |
62 | | - } |
63 | | - ] |
64 | | - response = self.client.get('/?owner=owner&repo=repo', headers=self.headers) |
65 | | - self.assertEqual(response.status_code, 200) |
66 | | - self.assertIn(b'<Project', response.data) |
67 | | - self.assertIn(b'lastBuildStatus="Unknown"', response.data) |
68 | | - |
69 | | - @patch('app.get_all_workflow_runs') |
70 | | - def test_index_failed_response(self, mock_get_all_workflow_runs): |
71 | | - """Test failed response in the index route.""" |
72 | | - mock_get_all_workflow_runs.return_value = [] |
73 | | - response = self.client.get('/?owner=owner&repo=repo', headers=self.headers) |
74 | | - self.assertEqual(response.status_code, 200) |
75 | | - self.assertIn(b'<Projects />', response.data) |
76 | | - |
77 | | - |
78 | | -if __name__ == '__main__': |
79 | | - unittest.main() |
| 11 | + """Setup the app""" |
| 12 | + self.app = app.test_client() |
| 13 | + |
| 14 | + def test_get_workflows(self): |
| 15 | + """Test case for getting workflows""" |
| 16 | + owner = "test_owner" |
| 17 | + repo = "test_repo" |
| 18 | + headers = {"Authorization": "Bearer test_token", |
| 19 | + "Accept": "application/vnd.github.v3+json"} |
| 20 | + |
| 21 | + with patch('requests.get') as mock_get: |
| 22 | + mock_response = MagicMock() |
| 23 | + mock_response.status_code = 200 |
| 24 | + mock_response.json.return_value = { |
| 25 | + "workflows": ["workflow1", "workflow2"]} |
| 26 | + mock_get.return_value = mock_response |
| 27 | + |
| 28 | + workflows = get_workflows(owner, repo, headers) |
| 29 | + |
| 30 | + self.assertEqual(workflows, ["workflow1", "workflow2"]) |
| 31 | + |
| 32 | + def test_get_workflow_runs(self): |
| 33 | + """Test case for getting workflow runs""" |
| 34 | + workflow = {"url": "test_url"} |
| 35 | + headers = {"Authorization": "Bearer test_token", |
| 36 | + "Accept": "application/vnd.github.v3+json"} |
| 37 | + |
| 38 | + with patch('requests.get') as mock_get: |
| 39 | + mock_response = MagicMock() |
| 40 | + mock_response.status_code = 200 |
| 41 | + mock_response.json.return_value = { |
| 42 | + "workflow_runs": ["run1", "run2"]} |
| 43 | + mock_get.return_value = mock_response |
| 44 | + |
| 45 | + workflow_runs = get_workflow_runs(workflow, headers) |
| 46 | + |
| 47 | + self.assertEqual(workflow_runs, ["run1", "run2"]) |
| 48 | + |
| 49 | + def test_get_all_workflow_runs(self): |
| 50 | + """Test case for getting all workflow runs""" |
| 51 | + owner = "test_owner" |
| 52 | + repo = "test_repo" |
| 53 | + token = "test_token" |
| 54 | + headers = {"Authorization": "Bearer test_token", |
| 55 | + "Accept": "application/vnd.github.v3+json"} |
| 56 | + workflows = [{"url": "workflow1"}, {"url": "workflow2"}] |
| 57 | + runs1 = [{"id": 1, "status": "completed", "conclusion": "success"}] |
| 58 | + runs2 = [{"id": 2, "status": "completed", "conclusion": "failure"}] |
| 59 | + |
| 60 | + with patch('app.get_workflows') as mock_get_workflows, \ |
| 61 | + patch('app.get_workflow_runs') as mock_get_workflow_runs: |
| 62 | + |
| 63 | + mock_get_workflows.return_value = workflows |
| 64 | + mock_get_workflow_runs.side_effect = [runs1, runs2] |
| 65 | + |
| 66 | + result = get_all_workflow_runs(owner, repo, token) |
| 67 | + |
| 68 | + self.assertEqual(result, runs1 + runs2) |
| 69 | + mock_get_workflows.assert_called_once_with(owner, repo, headers) |
| 70 | + mock_get_workflow_runs.assert_any_call(workflows[0], headers) |
| 71 | + mock_get_workflow_runs.assert_any_call(workflows[1], headers) |
0 commit comments