|
| 1 | +from unittest import TestCase |
| 2 | +from webapp.app import create_app |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | + |
| 6 | +class TestEndpoints(TestCase): |
| 7 | + def setUp(self): |
| 8 | + self.app = create_app(testing=True) |
| 9 | + self.client = self.app.test_client() |
| 10 | + self._log_in(is_canonical=False) |
| 11 | + |
| 12 | + def _log_in(self, is_canonical=False): |
| 13 | + test_macaroon = "test_macaroon" |
| 14 | + with self.client.session_transaction() as s: |
| 15 | + s["publisher"] = { |
| 16 | + "account_id": "test_account_id", |
| 17 | + "image": None, |
| 18 | + "nickname": "XYZ", |
| 19 | + "fullname": "ABC XYZ", |
| 20 | + "email": "testing@testing.com", |
| 21 | + "stores": [], |
| 22 | + "is_canonical": is_canonical, |
| 23 | + } |
| 24 | + s["macaroons"] = test_macaroon |
| 25 | + s["developer_token"] = test_macaroon |
| 26 | + s["exchanged_developer_token"] = True |
| 27 | + |
| 28 | + def _set_user_is_canonical(self, value): |
| 29 | + self._log_in(is_canonical=value) |
| 30 | + |
| 31 | + |
| 32 | +class TestModelServiceEndpoints(TestEndpoints): |
| 33 | + @patch( |
| 34 | + "webapp.publisher.cve.cve_helper.CveHelper.has_cve_data", |
| 35 | + return_value=True, |
| 36 | + ) |
| 37 | + @patch( |
| 38 | + "canonicalwebteam.store_api.dashboard.Dashboard.get_snap_info", |
| 39 | + return_value={"snap_id": "id"}, |
| 40 | + ) |
| 41 | + def test_get_policies_for_canonical_user( |
| 42 | + self, mock_get_snap_info, mock_get |
| 43 | + ): |
| 44 | + self._set_user_is_canonical(True) |
| 45 | + |
| 46 | + response = self.client.get("api/snaps/cve/test") |
| 47 | + data = response.json |
| 48 | + |
| 49 | + self.assertEqual(response.status_code, 200) |
| 50 | + self.assertEqual(data["success"], True) |
| 51 | + |
| 52 | + @patch( |
| 53 | + "webapp.publisher.cve.cve_helper.CveHelper.has_cve_data", |
| 54 | + return_value=False, |
| 55 | + ) |
| 56 | + @patch( |
| 57 | + "canonicalwebteam.store_api.dashboard.Dashboard.get_snap_info", |
| 58 | + return_value={"snap_id": "id"}, |
| 59 | + ) |
| 60 | + def test_get_policies_no_data(self, mock_get_snap_info, mock_get): |
| 61 | + self._set_user_is_canonical(True) |
| 62 | + |
| 63 | + response = self.client.get("api/snaps/cve/test") |
| 64 | + data = response.json |
| 65 | + |
| 66 | + self.assertEqual(response.status_code, 404) |
| 67 | + self.assertEqual(data["success"], False) |
| 68 | + |
| 69 | + def test_get_policies_for_non_canonical_user(self): |
| 70 | + response = self.client.get("api/snaps/cve/test") |
| 71 | + data = response.json |
| 72 | + |
| 73 | + self.assertEqual(response.status_code, 403) |
| 74 | + self.assertEqual(data["success"], False) |
0 commit comments