From d11cce7dba96c20be2c5ab3db0206e684a370e6c Mon Sep 17 00:00:00 2001 From: MementoMori <1003350679@qq.com> Date: Fri, 16 Jul 2021 09:32:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=9A=84=E7=94=A8=E6=88=B7=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E5=85=B3=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javcra/application/serialize/validate.py | 35 ++++++++++++++----- release-assistant/javcra/common/constant.py | 35 +++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/release-assistant/javcra/application/serialize/validate.py b/release-assistant/javcra/application/serialize/validate.py index c3c63bf..71498cc 100644 --- a/release-assistant/javcra/application/serialize/validate.py +++ b/release-assistant/javcra/application/serialize/validate.py @@ -13,18 +13,37 @@ """ Verification method """ +from javcra.common.constant import PERMISSION_INFO +from javcra.common.constant import COMMAND_DICT -def validate_giteeid(issue_id, gitee_id, person): + +def validate_giteeid(user, comment, personnel_authority: dict, permission_info=PERMISSION_INFO, + command_dict=COMMAND_DICT): """ Description: get the ID with comment permission from the corresponding Gitee Issue Args: - issue_id: the openEuler update version issue ID - gitee_id: the gitee id who comment this issue + user: user id + comment: Related permissions + personnel_authority: personnel authority e.g:{'developer': 'xxx', 'version_manager':'xxx'} + permission_info: permission info + command_dict: command info + Returns: + True or False """ - - permission = True - print("the permission is given to : " + person) - if not permission: - print("Sorry! You do not have the permisson to commit this operation.") + try: + user_id = '' + user_list = command_dict[comment] + for key, value in personnel_authority.items(): + if user in value and key in user_list: + user_id = key + if comment not in permission_info[user_id]: + print("{} has no operation authority:{}, please contact the administrator".format(user, comment)) + return False + except KeyError as e: + print("Error:{}\n Gitee id:{}\n Comment:{}\n Related permissions: {} \n" + "please check and try again".format(e, user, comment, permission_info)) + return False + except AttributeError as e: + print("Personnel Authority:{} \n User id:{} \n Comment:{} \n {}".format(personnel_authority, user, comment, e)) return False return True diff --git a/release-assistant/javcra/common/constant.py b/release-assistant/javcra/common/constant.py index 2d39ba5..f2d52bb 100644 --- a/release-assistant/javcra/common/constant.py +++ b/release-assistant/javcra/common/constant.py @@ -26,3 +26,38 @@ PERMISSION_DICT = { 'cvrfok': 'security', 'start': 'manager' } +PERMISSION_INFO = { + "version_manager": ["/start-update", "/no-release"], + "security_committee": [ + "/add-cve", + "/delete-cve", + "/cve-ok", + "/check-ok", + "/cvrf-ok", + ], + "developer": [ + "/add-bugfix", + "/delete-bugfix", + "/bugfix-ok", + "/check-status", + "/get-requires", + ], + "tester": ["/test-ok"], + "tc": ["/check-ok"], + "release": ["/check-ok"], + "qa": ["/check-ok"] +} + +COMMAND_DICT = { + "/start-update": ["version_manager"], + "/no-release": ["version_manager"], + "/get-requires": ["developer"], + "/check-status": ["developer"], + "/test-ok": ["tester"], + "/check-ok": ["tc", "qa", "release", "security_committee"], + "/cvrf-ok": ["security_committee"], + "/add-cve": ["security_committee"], + "/add-bugfix": ["developer"], + "/delete-cve": ["security_committee"], + "/delete-bugfix": ["developer"], +} -- Gitee From 44ef9eda3024fb8b5ec0558cf44e4fa369c912f8 Mon Sep 17 00:00:00 2001 From: MementoMori <1003350679@qq.com> Date: Wed, 28 Jul 2021 18:26:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0startpart=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- release-assistant/test/base/basetest.py | 64 ++++++++++++++++++- .../test/test_start/test_start_cli.py | 57 ++++++++++++++++- 2 files changed, 117 insertions(+), 4 deletions(-) diff --git a/release-assistant/test/base/basetest.py b/release-assistant/test/base/basetest.py index 02114e2..df4ce42 100644 --- a/release-assistant/test/base/basetest.py +++ b/release-assistant/test/base/basetest.py @@ -18,11 +18,13 @@ import json import sys import unittest from io import StringIO - +from unittest import mock import requests - +from pathlib import Path from javcra.cli.base import BaseCommand +MOCK_DATA_FOLDER = str(Path(Path(__file__).parents[1], "mock_data")) + class TestBase(unittest.TestCase): """ @@ -96,7 +98,7 @@ class TestBase(unittest.TestCase): return super().tearDown() -class TestMixin(unittest.TestCase): +class TestMixin(TestBase): """ The base class that sends HTTP requests """ @@ -192,3 +194,59 @@ class TestMixin(unittest.TestCase): issuse_describe_url, data=dict(access_token=access_token) ).get("body") return describe + + def _create_patch(self, mock_name, **kwargs): + """create_patch + + Args: + method_name (str): mock method or attribute name + + """ + patcher = mock.patch(mock_name, **kwargs) + self._to_clean_patchers.append(patcher) + patcher.start() + + def _to_update_kw_and_make_mock(self, mock_name, effect=None, **kwargs): + """_to_update_kw_and_make_mock + + Args: + mock_name (str): mock method or attribute name + effect (Any, optional): side effect value + + Raises: + ValueError: If the side_effect or return_value keyword parameter is not specified + specify the value of the effect keyword parameter + """ + if "side_effect" not in kwargs and "return_value" not in kwargs: + if effect is None: + raise ValueError( + "If the side_effect or return_value keyword parameter is not specified," + "specify the value of the effect keyword parameter" + ) + kwargs["side_effect"] = effect + self._create_patch(mock_name, **kwargs) + + @staticmethod + def read_file_content(path, folder=MOCK_DATA_FOLDER, is_json=True): + """to read file content if is_json is True return dict else return str + + Args: + path: Absolute path or the path relative of mock_data folder + is_json: if is True use json.loads to load data else not load + + Raises: + FileNotFoundError:Check Your path Please + JSONDecodeError:Check Your Josn flie Please + + Returns: + file's content:if is_json is True return dict else return str + """ + curr_path = Path(folder, path) + if Path(path).is_absolute(): + curr_path = path + + with open(str(curr_path), "r", encoding="utf-8") as f_p: + if is_json: + return json.loads(f_p.read()) + else: + return f_p.read() diff --git a/release-assistant/test/test_start/test_start_cli.py b/release-assistant/test/test_start/test_start_cli.py index 726d914..52a7822 100644 --- a/release-assistant/test/test_start/test_start_cli.py +++ b/release-assistant/test/test_start/test_start_cli.py @@ -13,4 +13,59 @@ # -*- coding:utf-8 -*- """ TestStart -""" \ No newline at end of file +""" +import os +from javcra.cli.commands.startpart import StartCommand +from test.base.basetest import TestMixin + +EXPECT_DATA_FILE = os.path.join(os.path.abspath(os.path.dirname(__file__)), "expected_data") + + +class TestStart(TestMixin): + """ + class for test TestStart + """ + cmd_class = StartCommand + + def test_success(self): + """ + test success + """ + self.except_str = self.read_file_content('success.txt', folder=EXPECT_DATA_FILE, is_json=False) + self.command_params = ["--giteeid=MementoMoriCheng", "--token=xxx", "I401NU"] + self.assert_result() + + def test_failed(self): + """ + test failed + """ + self.expect_str = """ +Issue has CVE content, have you already operated start update command +[ERROR] failed to start update. + """ + self.command_params = ["--giteeid=MementoMoriCheng", "--token=xxx", "I401NU"] + self.assert_result() + + def test_no_permission(self): + """ + test no permission + """ + self.expect_str = """ +[ERROR] The current user does not have relevant operation permissions + """ + self.command_params = ["--giteeid=MementoMoriChengxx", "--token=xxx", "I401NU"] + self.assert_result() + + def test_no_personnel_authority(self): + """ + test no personnel authority + """ + self.expect_str = """ +[ERROR] no personnel authority + """ + self.command_params = ["--giteeid=MementoMoriChengxx", "--token=xxx", "I401NU"] + self._to_update_kw_and_make_mock( + "javcra.application.checkpart.checktest.CheckTest.parsing_body", + effect={}, + ) + self.assert_result() \ No newline at end of file -- Gitee