Skip to content

Commit b95a030

Browse files
authored
Merge pull request #7939 from guohelu/feat_test_commont_template
fix: 公共流程补充测试用例并修复错误 #7824
2 parents 1856e6e + 62570f0 commit b95a030

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

gcloud/core/apis/drf/serilaziers/common_template.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ def validate_project_scope(self, value):
154154
self._validate_scope_changes(value)
155155
return value
156156

157+
def validate(self, attrs):
158+
pipeline_tree = json.loads(attrs["pipeline_tree"])
159+
project_scope = attrs["extra_info"]["project_scope"]
160+
161+
for activity in pipeline_tree.get("activities").values():
162+
if activity.get("type") != "SubProcess" or activity.get("template_source") != "common":
163+
continue
164+
common_template_id = activity.get("template_id")
165+
result = CommonTemplate.objects.get(id=common_template_id)
166+
common_project_scope = result.extra_info.get("project_scope")
167+
if common_project_scope == ["*"]:
168+
continue
169+
if not set(project_scope).issubset(set(common_project_scope)):
170+
raise serializers.ValidationError(f"保存流程失败,子流程{result.pipeline_template.name}的可见范围与当前流程得可见范围存在冲突")
171+
return attrs
172+
157173
class Meta:
158174
model = CommonTemplate
159175
fields = [
@@ -183,3 +199,7 @@ class PatchCommonTemplateSerializer(CreateCommonTemplateSerializer):
183199
class Meta:
184200
model = CommonTemplate
185201
fields = ["project_scope"]
202+
203+
def validate_project_scope(self, value):
204+
self._validate_scope_changes(value)
205+
return value
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
4+
Edition) available.
5+
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved.
6+
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://opensource.org/licenses/MIT
9+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
specific language governing permissions and limitations under the License.
12+
"""
13+
14+
import factory
15+
from django.db.models import signals
16+
from django_test_toolkit.mixins.account import SuperUserMixin
17+
from django_test_toolkit.mixins.blueking import LoginExemptMixin, StandardResponseAssertionMixin
18+
from django_test_toolkit.mixins.drf import DrfPermissionExemptMixin
19+
from django_test_toolkit.testcases import ToolkitApiTestCase
20+
from pipeline.models import PipelineTemplate, Snapshot
21+
22+
from gcloud.common_template.models import CommonTemplate
23+
from unittest.mock import patch
24+
25+
26+
class TestCommonTemplateView(
27+
ToolkitApiTestCase,
28+
SuperUserMixin,
29+
LoginExemptMixin,
30+
DrfPermissionExemptMixin,
31+
StandardResponseAssertionMixin,
32+
):
33+
@factory.django.mute_signals(signals.pre_save, signals.post_save)
34+
def setUp(self):
35+
super(TestCommonTemplateView, self).setUp()
36+
self.test_snapshot = Snapshot.objects.create_snapshot({})
37+
self.pipeline_template = PipelineTemplate.objects.create(
38+
template_id="template_id", creator="creator", snapshot=self.test_snapshot
39+
)
40+
self.template_url = "/api/v3/common_template/"
41+
self.common_template = CommonTemplate.objects.create(
42+
category="category",
43+
pipeline_template=self.pipeline_template,
44+
)
45+
self.mock_referencer_data = [
46+
{"template_type": "project", "id": 2, "name": "test_task_template", "project_id": 100},
47+
]
48+
49+
def test_filter_project_template(self):
50+
query_params = {"project_id": 1}
51+
response = self.client.get(self.template_url, data=query_params)
52+
self.assertTrue(response.data["result"])
53+
self.assertIsNotNone(response.data["data"])
54+
55+
def test_update_common_template(self):
56+
self.template_url = "/api/v3/common_template/{}/update_specific_fields/".format(self.common_template.id)
57+
query_params = {"project_scope": ["1"]}
58+
response = self.client.post(self.template_url, data=query_params, format="json")
59+
self.assertTrue(response.data["result"])
60+
self.assertIsNotNone(response.data["data"])
61+
62+
def test_update_common_template_fail(self):
63+
with patch.object(CommonTemplate, "referencer", return_value=self.mock_referencer_data) as mock_referencer:
64+
self.template_url = "/api/v3/common_template/{}/update_specific_fields/".format(self.common_template.id)
65+
query_params = {"project_scope": ["1"]}
66+
response = self.client.post(self.template_url, data=query_params, format="json")
67+
self.assertFalse(response.data["result"])
68+
mock_referencer.assert_called_once()

0 commit comments

Comments
 (0)