|
| 1 | +import json |
| 2 | +import os |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import responses |
| 6 | + |
| 7 | +from launchable.utils.http_client import get_base_url |
| 8 | +from tests.cli_test_case import CliTestCase |
| 9 | + |
| 10 | + |
| 11 | +class GateTest(CliTestCase): |
| 12 | + @responses.activate |
| 13 | + @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) |
| 14 | + def test_gate_passed(self): |
| 15 | + """Test gate command exits with 0 when status is PASSED""" |
| 16 | + responses.add( |
| 17 | + responses.GET, |
| 18 | + "{}/intake/organizations/{}/workspaces/{}/gate".format( |
| 19 | + get_base_url(), |
| 20 | + self.organization, |
| 21 | + self.workspace), |
| 22 | + json={ |
| 23 | + 'status': 'PASSED', |
| 24 | + 'quarantinedFailures': 5, |
| 25 | + 'actionableFailures': 0 |
| 26 | + }, |
| 27 | + status=200) |
| 28 | + |
| 29 | + result = self.cli('gate', '--session', self.session) |
| 30 | + self.assert_success(result) |
| 31 | + self.assertIn('PASSED', result.output) |
| 32 | + |
| 33 | + @responses.activate |
| 34 | + @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) |
| 35 | + def test_gate_failed(self): |
| 36 | + """Test gate command exits with 1 when status is FAILED""" |
| 37 | + responses.add( |
| 38 | + responses.GET, |
| 39 | + "{}/intake/organizations/{}/workspaces/{}/gate".format( |
| 40 | + get_base_url(), |
| 41 | + self.organization, |
| 42 | + self.workspace), |
| 43 | + json={ |
| 44 | + 'status': 'FAILED', |
| 45 | + 'quarantinedFailures': 2, |
| 46 | + 'actionableFailures': 3 |
| 47 | + }, |
| 48 | + status=200) |
| 49 | + |
| 50 | + result = self.cli('gate', '--session', self.session) |
| 51 | + self.assert_exit_code(result, 1) |
| 52 | + self.assertIn('FAILED', result.output) |
| 53 | + |
| 54 | + @responses.activate |
| 55 | + @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) |
| 56 | + def test_gate_passed_json_format(self): |
| 57 | + """Test gate command with --json flag when status is PASSED""" |
| 58 | + gate_data = { |
| 59 | + 'status': 'PASSED', |
| 60 | + 'quarantinedFailures': 5, |
| 61 | + 'actionableFailures': 0 |
| 62 | + } |
| 63 | + |
| 64 | + responses.add( |
| 65 | + responses.GET, |
| 66 | + "{}/intake/organizations/{}/workspaces/{}/gate".format( |
| 67 | + get_base_url(), |
| 68 | + self.organization, |
| 69 | + self.workspace), |
| 70 | + json=gate_data, |
| 71 | + status=200) |
| 72 | + |
| 73 | + result = self.cli('gate', '--session', self.session, '--json') |
| 74 | + self.assert_success(result) |
| 75 | + |
| 76 | + # Verify JSON output |
| 77 | + output_json = json.loads(result.output) |
| 78 | + self.assertEqual(output_json['status'], 'PASSED') |
| 79 | + self.assertEqual(output_json['quarantinedFailures'], 5) |
| 80 | + self.assertEqual(output_json['actionableFailures'], 0) |
| 81 | + |
| 82 | + @responses.activate |
| 83 | + @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) |
| 84 | + def test_gate_failed_json_format(self): |
| 85 | + """Test gate command with --json flag when status is FAILED""" |
| 86 | + gate_data = { |
| 87 | + 'status': 'FAILED', |
| 88 | + 'quarantinedFailures': 2, |
| 89 | + 'actionableFailures': 3 |
| 90 | + } |
| 91 | + |
| 92 | + responses.add( |
| 93 | + responses.GET, |
| 94 | + "{}/intake/organizations/{}/workspaces/{}/gate".format( |
| 95 | + get_base_url(), |
| 96 | + self.organization, |
| 97 | + self.workspace), |
| 98 | + json=gate_data, |
| 99 | + status=200) |
| 100 | + |
| 101 | + result = self.cli('gate', '--session', self.session, '--json') |
| 102 | + self.assert_exit_code(result, 1) |
| 103 | + |
| 104 | + # Verify JSON output |
| 105 | + output_json = json.loads(result.output) |
| 106 | + self.assertEqual(output_json['status'], 'FAILED') |
| 107 | + self.assertEqual(output_json['quarantinedFailures'], 2) |
| 108 | + self.assertEqual(output_json['actionableFailures'], 3) |
| 109 | + |
| 110 | + @responses.activate |
| 111 | + @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) |
| 112 | + def test_gate_not_found(self): |
| 113 | + """Test gate command when gate data is not available""" |
| 114 | + responses.add( |
| 115 | + responses.GET, |
| 116 | + "{}/intake/organizations/{}/workspaces/{}/gate".format( |
| 117 | + get_base_url(), |
| 118 | + self.organization, |
| 119 | + self.workspace), |
| 120 | + json={}, |
| 121 | + status=404) |
| 122 | + |
| 123 | + result = self.cli('gate', '--session', self.session) |
| 124 | + # Should exit with 0 when gate data is not available (non-error case) |
| 125 | + self.assert_success(result) |
| 126 | + self.assertIn('Gate data currently not available', result.output) |
0 commit comments