|
| 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