project.py 7.64 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-

import base
import swagger_client
5
from swagger_client.rest import ApiException
6

7 8 9 10 11 12 13 14 15 16 17
def is_member_exist_in_project(members, member_user_name, expected_member_role_id = None):
    result = False
    for member in members:
        if member.entity_name == member_user_name:
            if expected_member_role_id != None:
                if member.role_id == expected_member_role_id:
                    return True
            else:
                return True
    return result

18
class Project(base.Base):
19
    def create_project(self, name=None, metadata=None, expect_status_code = 201, expect_response_body = None, **kwargs):
Yan's avatar
Yan committed
20 21
        if name is None:
            name = base._random_name("project")
danfengliu's avatar
danfengliu committed
22 23
        if metadata is None:
            metadata = {}
24
        client = self._get_client(**kwargs)
25 26 27 28 29 30 31 32 33 34 35 36 37 38

        try:
            _, status_code, header = client.projects_post_with_http_info(swagger_client.ProjectReq(name, metadata))
        except ApiException as e:
            if e.status == expect_status_code:
                if expect_response_body is not None and e.body.strip() != expect_response_body.strip():
                    raise Exception(r"Create project result body is not as expected {} actual status is {}.".format(expect_response_body.strip(), e.body.strip()))
                else:
                    return e.reason, e.body
            else:
                raise Exception(r"Create project result is not as expected {} actual status is {}.".format(expect_status_code, e.status))

        base._assert_status_code(expect_status_code, status_code)
        return base._get_id_from_header(header), name
danfengliu's avatar
danfengliu committed
39 40 41 42 43 44 45 46

    def get_projects(self, params, **kwargs):
        client = self._get_client(**kwargs)
        data = []
        data, status_code, _ = client.projects_get_with_http_info(**params)
        base._assert_status_code(200, status_code)
        return data

47 48 49 50 51 52 53 54
    def projects_should_exist(self, params, expected_count = None, expected_project_id = None, **kwargs):
        project_data = self.get_projects(params, **kwargs)
        actual_count = len(project_data)
        if expected_count is not None and actual_count!= expected_count:
            raise Exception(r"Private project count should be {}.".format(expected_count))
        if expected_project_id is not None and actual_count == 1 and str(project_data[0].project_id) != str(expected_project_id):
            raise Exception(r"Project-id check failed, expect {} but got {}, please check this test case.".format(str(expected_project_id), str(project_data[0].project_id)))

danfengliu's avatar
danfengliu committed
55 56 57 58 59 60 61 62
    def check_project_name_exist(self, name=None, **kwargs):
        client = self._get_client(**kwargs)
        _, status_code, _ = client.projects_head_with_http_info(name)
        return {
            200: True,
            404: False,
        }.get(status_code,'error')

63
    def get_project(self, project_id, expect_status_code = 200, **kwargs):
danfengliu's avatar
danfengliu committed
64
        client = self._get_client(**kwargs)
65 66 67 68 69 70 71 72
        try:
            data, status_code, _ = client.projects_project_id_get_with_http_info(project_id)
        except ApiException as e:
            if e.status == expect_status_code:
                return e.reason, e.body
            else:
                raise Exception(r"Get project result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
        base._assert_status_code(expect_status_code, status_code)
danfengliu's avatar
danfengliu committed
73 74 75 76 77 78 79 80
        return data

    def update_project(self, project_id, metadata, **kwargs):
        client = self._get_client(**kwargs)
        project = swagger_client.Project(project_id, None, None, None, None, None, None, None, None, None, None, metadata)
        _, status_code, _ = client.projects_project_id_put_with_http_info(project_id, project)
        base._assert_status_code(200, status_code)

81
    def delete_project(self, project_id, expect_status_code = 200, **kwargs):
danfengliu's avatar
danfengliu committed
82 83
        client = self._get_client(**kwargs)
        _, status_code, _ = client.projects_project_id_delete_with_http_info(project_id)
84
        base._assert_status_code(expect_status_code, status_code)
danfengliu's avatar
danfengliu committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    def get_project_metadata_by_name(self, project_id, meta_name, **kwargs):
        client = self._get_client(**kwargs)
        ProjectMetadata = swagger_client.ProjectMetadata()
        ProjectMetadata, status_code, _ = client.projects_project_id_metadatas_meta_name_get_with_http_info(project_id, meta_name)
        base._assert_status_code(200, status_code)
        return {
            'public': ProjectMetadata.public,
            'enable_content_trust': ProjectMetadata.enable_content_trust,
            'prevent_vul': ProjectMetadata.prevent_vul,
            'auto_scan': ProjectMetadata.auto_scan,
            'severity': ProjectMetadata.severity,
        }.get(meta_name,'error')

    def get_project_members(self, project_id, **kwargs):
100 101 102 103
        client = self._get_client(**kwargs)
        return client.projects_project_id_members_get(project_id)

    def get_project_member(self, project_id, member_id, expect_status_code = 200, **kwargs):
danfengliu's avatar
danfengliu committed
104 105
        client = self._get_client(**kwargs)
        data = []
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        try:
            data, status_code, _ = client.projects_project_id_members_mid_get_with_http_info(project_id, member_id,)
        except ApiException as e:
            if e.status == expect_status_code:
                return e.reason, e.body
            else:
                raise Exception(r"Get project member result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
        base._assert_status_code(expect_status_code, status_code)
        return data

    def check_project_member_not_exist(self, project_id, member_user_name, **kwargs):
        members = self.get_project_members(project_id, **kwargs)
        result = is_member_exist_in_project(list(members), member_user_name)
        if result == True:
            raise Exception(r"User {} should not be a member of project with ID {}.".format(member_user_name, project_id))

    def check_project_members_exist(self, project_id, member_user_name, expected_member_role_id = None, **kwargs):
        members = self.get_project_members(project_id, **kwargs)
        result = is_member_exist_in_project(members, member_user_name, expected_member_role_id = expected_member_role_id)
        if result == False:
            raise Exception(r"User {} should be a member of project with ID {}.".format(member_user_name, project_id))

    def update_project_member_role(self, project_id, member_id, member_role_id, expect_status_code = 200, **kwargs):
        client = self._get_client(**kwargs)
        role = swagger_client.Role(role_id = member_role_id)
        data = []
        data, status_code, _ = client.projects_project_id_members_mid_put_with_http_info(project_id, member_id, role = role)
        base._assert_status_code(expect_status_code, status_code)
danfengliu's avatar
danfengliu committed
134 135
        return data

136 137 138 139 140
    def delete_project_member(self, project_id, member_id, expect_status_code = 200, **kwargs):
        client = self._get_client(**kwargs)
        _, status_code, _ = client.projects_project_id_members_mid_delete_with_http_info(project_id, member_id)
        base._assert_status_code(expect_status_code, status_code)

141
    def add_project_members(self, project_id, user_id, member_role_id = None, expect_status_code = 201, **kwargs):
danfengliu's avatar
danfengliu committed
142 143 144 145 146 147
        if member_role_id is None:
            member_role_id = 1
        _member_user = {"user_id": int(user_id)}
        projectMember = swagger_client.ProjectMember(member_role_id, member_user = _member_user)
        client = self._get_client(**kwargs)
        data = []
148
        data, status_code, header = client.projects_project_id_members_post_with_http_info(project_id, project_member = projectMember)
danfengliu's avatar
danfengliu committed
149
        base._assert_status_code(201, status_code)
150 151
        return base._get_id_from_header(header)