|
| 1 | +import json |
1 | 2 | import unittest |
| 3 | +from unittest import mock |
2 | 4 |
|
3 | 5 |
|
4 | 6 | import contentstack_management |
@@ -49,3 +51,72 @@ def test_delete_branch(self): |
49 | 51 | self.assertEqual(response.request.url, f"{self.client.endpoint}stacks/branches/{branch_uid}") |
50 | 52 | self.assertEqual(response.request.method, "DELETE") |
51 | 53 | self.assertEqual(response.request.headers["Content-Type"], "application/json") |
| 54 | + |
| 55 | + |
| 56 | +class BranchCreateEnvironmentsUnitTests(unittest.TestCase): |
| 57 | + """Branch create with publish_config.environments, mocked at the HTTP layer (no credentials).""" |
| 58 | + |
| 59 | + def setUp(self): |
| 60 | + self.client = contentstack_management.Client(host=host) |
| 61 | + self.requests_patch = mock.patch("contentstack_management._api_client.requests.request") |
| 62 | + self.request = self.requests_patch.start() |
| 63 | + self.request.return_value = mock.Mock(status_code=200, json=lambda: {"branch": {"uid": "release"}}) |
| 64 | + self.addCleanup(self.requests_patch.stop) |
| 65 | + |
| 66 | + def _create(self, data): |
| 67 | + return self.client.stack(api_key).branch().create(data) |
| 68 | + |
| 69 | + def _posted_body(self): |
| 70 | + self.assertEqual(self.request.call_count, 1) |
| 71 | + kwargs = self.request.call_args.kwargs |
| 72 | + self.assertEqual(self.request.call_args.args[0], "POST") |
| 73 | + self.assertEqual(self.request.call_args.args[1], f"{self.client.endpoint}stacks/branches") |
| 74 | + return json.loads(kwargs["data"]) |
| 75 | + |
| 76 | + def test_two_environment_names_are_posted_as_given(self): |
| 77 | + data = {"branch": {"uid": "release", "source": "main", |
| 78 | + "publish_config": {"environments": ["development", "staging"]}}} |
| 79 | + self._create(data) |
| 80 | + self.assertEqual(self._posted_body(), data) |
| 81 | + |
| 82 | + def test_empty_environment_list_is_posted_as_empty_list(self): |
| 83 | + data = {"branch": {"uid": "release", "source": "main", "publish_config": {"environments": []}}} |
| 84 | + self._create(data) |
| 85 | + self.assertEqual(self._posted_body()["branch"]["publish_config"], {"environments": []}) |
| 86 | + |
| 87 | + def test_duplicates_long_list_and_mixed_case_are_posted_unchanged(self): |
| 88 | + cases = [ |
| 89 | + ["development", "development"], |
| 90 | + [f"env_{i}" for i in range(150)], |
| 91 | + ["Development", " staging ", "PRODUCTION"], |
| 92 | + ] |
| 93 | + for environments in cases: |
| 94 | + with self.subTest(count=len(environments)): |
| 95 | + self.request.reset_mock() |
| 96 | + self._create({"branch": {"uid": "release", "source": "main", |
| 97 | + "publish_config": {"environments": environments}}}) |
| 98 | + self.assertEqual(self._posted_body()["branch"]["publish_config"]["environments"], environments) |
| 99 | + |
| 100 | + def test_omitted_environment_list_posts_todays_body(self): |
| 101 | + data = {"branch": {"uid": "release", "source": "main"}} |
| 102 | + self._create(data) |
| 103 | + self.assertEqual(self._posted_body(), data) |
| 104 | + self.assertNotIn("publish_config", self._posted_body()["branch"]) |
| 105 | + |
| 106 | + def test_empty_publish_config_is_posted_as_empty_object(self): |
| 107 | + self._create({"branch": {"uid": "release", "source": "main", "publish_config": {}}}) |
| 108 | + self.assertEqual(self._posted_body()["branch"]["publish_config"], {}) |
| 109 | + |
| 110 | + def test_api_422_for_environment_list_is_returned_unchanged(self): |
| 111 | + api_body = { |
| 112 | + "error_message": "Environment not found.", |
| 113 | + "error_code": 422, |
| 114 | + "errors": {"publish_config.environments": ["no_such_env"]}, |
| 115 | + } |
| 116 | + self.request.return_value = mock.Mock(status_code=422, json=lambda: api_body) |
| 117 | + response = self._create({"branch": {"uid": "release", "source": "main", |
| 118 | + "publish_config": {"environments": ["development", "no_such_env"]}}}) |
| 119 | + self.assertEqual(response.status_code, 422) |
| 120 | + self.assertEqual(response.json(), api_body) |
| 121 | + self.assertEqual(self._posted_body()["branch"]["publish_config"]["environments"], |
| 122 | + ["development", "no_such_env"]) |
0 commit comments