|
| 1 | +from copy import deepcopy |
| 2 | +from ...zephyr_session import ZephyrSession |
| 3 | +from .paths import ServerPaths as Paths |
| 4 | + |
| 5 | + |
| 6 | +class EndpointTemplate: |
| 7 | + """Class with basic constructor for endpoint classes""" |
| 8 | + def __init__(self, session: ZephyrSession): |
| 9 | + self.session = session |
| 10 | + |
| 11 | + def _dict_merge(self, a, b): |
| 12 | + """Recursively merges 2 dictionaries and return the merged dictionary""" |
| 13 | + result = deepcopy(a) |
| 14 | + for k, v in b.items(): |
| 15 | + if k in result and isinstance(result[k], dict): |
| 16 | + result[k] = self._dict_merge(result[k], v) |
| 17 | + else: |
| 18 | + result[k] = deepcopy(v) |
| 19 | + return result |
| 20 | + |
| 21 | +class TestCaseEndpoints(EndpointTemplate): |
| 22 | + """Api wrapper for "Test Case" endpoints""" |
| 23 | + |
| 24 | + def get_test_case(self, test_case_key, **params): |
| 25 | + """Retrieve the Test Case matching the given key""" |
| 26 | + return self.session.get(Paths.CASE_KEY.format(test_case_key), |
| 27 | + params=params) |
| 28 | + |
| 29 | + def get_test_steps(self, test_case_id): |
| 30 | + """Retrieve the Test Case matching the given key""" |
| 31 | + return self.session.get(Paths.CASE_KEY_STEPS.format(test_case_id)) |
| 32 | + |
| 33 | + def _get_test_case_type(self): |
| 34 | + """Retrieve the Test Case matching the given key""" |
| 35 | + case_type = self.session.get(Paths.CASE_ISSUE_TYPE) |
| 36 | + return case_type["testcaseIssueTypeId"] |
| 37 | + |
| 38 | + def create_test_case(self, project_id, summary, data): |
| 39 | + """ |
| 40 | + Creates a new Test Case based on a minimum required fields. |
| 41 | + (https://support.smartbear.com/zephyr-squad-server/docs/api/how-to/create-tests.html) |
| 42 | + See Zephyr Squad Test Case creation documentation to better understand what can be |
| 43 | + modified. |
| 44 | + """ |
| 45 | + case_type = self._get_test_case_type() |
| 46 | + json = { |
| 47 | + "fields": { |
| 48 | + "project": { |
| 49 | + "id": project_id |
| 50 | + }, |
| 51 | + 'issuetype': { |
| 52 | + 'id': case_type |
| 53 | + }, |
| 54 | + "summary": summary, |
| 55 | + } |
| 56 | + } |
| 57 | + merged_json = self._dict_merge(data, json) |
| 58 | + return self.session.post(Paths.CASE, json=merged_json) |
| 59 | + |
| 60 | + def add_test_to_cycle(self, project_id, cycle_id, method, data): |
| 61 | + """ |
| 62 | + Add an existing Test Case to a Test Cycle based on a minimum required fields. |
| 63 | + (https://support.smartbear.com/zephyr-squad-server/docs/api/how-to/add-tests-to-cycle.html) |
| 64 | + See Zephyr Squad documentation to better understand what can be modified. |
| 65 | + """ |
| 66 | + json = { |
| 67 | + 'cycleId': cycle_id, |
| 68 | + 'projectId': project_id, |
| 69 | + 'method': method |
| 70 | + } |
| 71 | + merged_json = self._dict_merge(data, json) |
| 72 | + return self.session.post(Paths.CASE_TO_CYCLE, json=merged_json) |
| 73 | + |
| 74 | + |
| 75 | +class TestUtilsEndpoints(EndpointTemplate): |
| 76 | + """Api wrapper for different helpers endpoints like ZQL search""" |
| 77 | + |
| 78 | + def run_zql_search(self, query, **params): |
| 79 | + """Retrieve the Test Cycle matching the given key""" |
| 80 | + params.update(zqlQuery=f'({query})') |
| 81 | + return self.session.get(Paths.ZQL_SEARCH, params=params) |
| 82 | + |
| 83 | + |
| 84 | +class TestCyclesEndpoints(EndpointTemplate): |
| 85 | + """Api wrapper for "Test Cycles" endpoints""" |
| 86 | + |
| 87 | + def get_test_cycle(self, project_id, version_id, **params): |
| 88 | + """Retrieve the Test Cycle matching the given key""" |
| 89 | + params.update(projectId=project_id, versionId=version_id) |
| 90 | + return self.session.get(Paths.CYCLE, params=params) |
| 91 | + |
| 92 | + def create_test_cycle(self, project_id, version_id, cycle_name, data): |
| 93 | + """ |
| 94 | + Creates a new Test Cycle based on a minimum required fields. |
| 95 | + (https://support.smartbear.com/zephyr-squad-server/docs/api/how-to/create-cycle.html) |
| 96 | + See Zephyr Squad Test Cycle creation documentation to better understand what can be |
| 97 | + modified. |
| 98 | + """ |
| 99 | + json = { |
| 100 | + 'name': cycle_name, |
| 101 | + 'projectId': project_id, |
| 102 | + 'versionId': version_id |
| 103 | + } |
| 104 | + merged_json = self._dict_merge(data, json) |
| 105 | + return self.session.post(Paths.CYCLE, json=merged_json) |
| 106 | + |
| 107 | + |
| 108 | +class TestExecutionEndpoints(EndpointTemplate): |
| 109 | + """Api wrapper for "Test Execution" endpoints""" |
| 110 | + |
| 111 | + def get_test_execution(self, test_id_or_key, **params): |
| 112 | + """Retrieve all execution of a test case by the issue id or issue key""" |
| 113 | + params.update(testIdOrKey=test_id_or_key) |
| 114 | + return self.session.get(Paths.EXEC_BY_TEST, params=params) |
| 115 | + |
| 116 | + def get_execution_properties(self, exec_id, **params): |
| 117 | + """Retrieve all execution details""" |
| 118 | + return self.session.get(Paths.EXEC_PROP.format(exec_id), |
| 119 | + params=params) |
| 120 | + |
| 121 | + def get_test_steps(self, exec_id, **params): |
| 122 | + """Retrieve all execution steps""" |
| 123 | + params.update(executionId=exec_id) |
| 124 | + params.update(expand="") |
| 125 | + return self.session.get(Paths.STEP, params=params) |
| 126 | + |
| 127 | + def update_test_step_status(self, step_id, status): |
| 128 | + """ |
| 129 | + Updates a Test Step Status. Available status values: |
| 130 | + -1 = UNEXECUTED | 1 = PASS | 2 = FAIL | 3 = WIP | 4 = BLOCKED |
| 131 | + """ |
| 132 | + json = { |
| 133 | + "status": status |
| 134 | + } |
| 135 | + return self.session.put(Paths.STEP_ID.format(step_id), json=json) |
| 136 | + |
| 137 | + def update_execution_status(self, exec_id, status): |
| 138 | + """ |
| 139 | + Updates an Test Execution Status. Available status values: |
| 140 | + -1 = UNEXECUTED | 1 = PASS | 2 = FAIL | 3 = WIP | 4 = BLOCKED |
| 141 | + """ |
| 142 | + json = { |
| 143 | + "status": status |
| 144 | + } |
| 145 | + return self.session.put(Paths.EXEC.format(exec_id), json=json) |
| 146 | + |
| 147 | + def delete_bulk_execution(self, execution_id): |
| 148 | + """Delete bulk Execution by Execution Id""" |
| 149 | + if not isinstance(execution_id, list): |
| 150 | + execution_id = [ execution_id ] |
| 151 | + |
| 152 | + json = { |
| 153 | + "executions": execution_id |
| 154 | + } |
| 155 | + return self.session.delete(Paths.EXEC_DEL, json=json) |
| 156 | + |
| 157 | + |
| 158 | +class FolderEndpoints(EndpointTemplate): |
| 159 | + """Api wrapper for "Folder" endpoints""" |
| 160 | + |
| 161 | + def get_folder(self, project_id, version_id, cycle_id, **params): |
| 162 | + """Retrieve the Folders from a Test Cycle""" |
| 163 | + params.update(projectId=project_id, versionId=version_id) |
| 164 | + return self.session.get(Paths.FOLDER.format(cycle_id), params=params) |
| 165 | + |
| 166 | + def create_folder(self, project_id, version_id, cycle_id, data): |
| 167 | + """ |
| 168 | + Create a Folder in a Test Cycle based on a minimum required fields. |
| 169 | + (https://support.smartbear.com/zephyr-squad-server/docs/api/how-to/create-folder-in-cycle.html) |
| 170 | + See Zephyr Squad Folder creation documentation to better understand what can be |
| 171 | + modified. |
| 172 | + """ |
| 173 | + json = { |
| 174 | + 'cycleId': cycle_id, |
| 175 | + 'projectId': project_id, |
| 176 | + 'versionId': version_id |
| 177 | + } |
| 178 | + merged_json = self._dict_merge(data, json) |
| 179 | + return self.session.post(Paths.FOLDER_CREATE, json=merged_json) |
| 180 | + |
| 181 | + def update_folder(self, project_id, cycle_id, version_id, folder_id, name, data=dict()): |
| 182 | + """ |
| 183 | + Updates a Test Folder information. |
| 184 | + (https://zephyrsquadserver.docs.apiary.io/#reference/folderresource/update-a-folder-information/update-a-folder-information) |
| 185 | + """ |
| 186 | + json = { |
| 187 | + 'cycleId': cycle_id, |
| 188 | + 'projectId': project_id, |
| 189 | + 'versionId': version_id, |
| 190 | + "folderId": folder_id, |
| 191 | + "name": name, |
| 192 | + } |
| 193 | + merged_json = self._dict_merge(data, json) |
| 194 | + return self.session.put(Paths.FOLDER_ID.format(folder_id), json=merged_json) |
| 195 | + |
| 196 | + |
| 197 | +class ProjectEndpoints(EndpointTemplate): |
| 198 | + """Api wrapper for "Project" endpoints""" |
| 199 | + |
| 200 | + def get_project_info(self, project_key): |
| 201 | + """ |
| 202 | + Retrieve project information |
| 203 | + :params project_key: project key (Ex: DEMOPROJ) |
| 204 | + :returns: dictionary containing the project information |
| 205 | + """ |
| 206 | + return self.session.get(Paths.PRJ_ID.format(project_key)) |
| 207 | + |
| 208 | + def get_project_versions_by_key(self, project_key): |
| 209 | + """ |
| 210 | + Retrieve all project versions (releases) by project key using Jira API |
| 211 | + :params project_key: project key (Ex: DEMOPROJ) |
| 212 | + :returns: dictionary containing the project information |
| 213 | + """ |
| 214 | + return self.session.get(Paths.PRJ_VERSIONS_BY_KEY.format(project_key)) |
| 215 | + |
| 216 | + def get_project_versions_by_id(self, project_id, **params): |
| 217 | + """ |
| 218 | + Retrieve all project versions (releases) by project id using Zephyr API |
| 219 | + :params project_key: project id (Ex: 77303) |
| 220 | + :returns: dictionary containing the project information |
| 221 | + """ |
| 222 | + params.update(projectId=project_id) |
| 223 | + return self.session.get(Paths.PRJ_VERSIONS_BY_ID.format(project_id), |
| 224 | + params=params) |
0 commit comments