-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsq_api_utils.py
More file actions
149 lines (129 loc) · 5.23 KB
/
sq_api_utils.py
File metadata and controls
149 lines (129 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#
# Sonar Scanner Python
# Copyright (C) 2011-2024 SonarSource Sàrl.
# mailto:info AT sonarsource DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
#
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import contextlib
from typing import Optional
from pysonar_scanner import utils
from pysonar_scanner.api import JRE, BaseUrls, SonarQubeApi
import responses
from responses import matchers
def get_sq_server() -> SonarQubeApi:
return SonarQubeApi(
base_urls=BaseUrls(base_url="http://sq.home", api_base_url="http://sq.home/api/v2", is_sonar_qube_cloud=False),
token="<fake_token>",
)
def get_sq_cloud() -> SonarQubeApi:
return SonarQubeApi(
base_urls=BaseUrls(
base_url="http://sonarcloud.io", api_base_url="http://api.sonarcloud.io", is_sonar_qube_cloud=True
),
token="<fake_token>",
)
class SQApiMocker:
def __init__(self, base_url: str = "http://sq.home", rsps: Optional[responses.RequestsMock] = None):
self.base_url = base_url
self.api_url = f"{base_url}/api/v2"
self.rsps = rsps or responses
def mock_analysis_version(self, version: str = "", status: int = 200) -> responses.BaseResponse:
return self.rsps.get(url=f"{self.api_url}/analysis/version", body=version, status=status)
def mock_analysis_engine(
self,
filename: Optional[str] = None,
sha256: Optional[str] = None,
download_url: Optional[str] = None,
status: int = 200,
) -> responses.BaseResponse:
def prepare_json_obj() -> dict:
json_response = {}
if filename:
json_response["filename"] = filename
if sha256:
json_response["sha256"] = sha256
if download_url:
json_response["downloadUrl"] = download_url
return json_response
return self.rsps.get(
url=f"{self.api_url}/analysis/engine",
json=prepare_json_obj(),
status=status,
match=[matchers.header_matcher({"Accept": "application/json"})],
)
def mock_analysis_engine_download(self, body: bytes = b"", status: int = 200) -> responses.BaseResponse:
return self.rsps.get(
url=f"{self.api_url}/analysis/engine",
body=body,
status=status,
match=[matchers.header_matcher({"Accept": "application/octet-stream"})],
)
def mock_analysis_jres(
self,
body: Optional[list[dict]] = None,
status: int = 200,
os_matcher: Optional[str] = "linux",
arch_matcher: Optional[str] = "x64",
) -> responses.BaseResponse:
return self.rsps.get(
url=f"{self.api_url}/analysis/jres",
json=body,
status=status,
match=[
matchers.header_matcher({"Accept": "application/json"}),
matchers.query_param_matcher(utils.filter_none_values({"os": os_matcher, "arch": arch_matcher})),
],
)
def mock_analysis_jre_download(
self, id: str, body: bytes = b"", status: int = 200, redirect_url: Optional[str] = None
) -> responses.BaseResponse:
return self.rsps.get(
url=f"{self.api_url}/analysis/jres/{id}",
body=body,
headers={"Location": redirect_url} if redirect_url else None,
status=status,
match=[matchers.header_matcher({"Accept": "application/octet-stream"})],
)
def mock_download_url(
self,
url: str,
body: bytes = b"",
status: int = 200,
redirect_url: Optional[str] = None,
) -> responses.BaseResponse:
return self.rsps.get(
url=url,
body=body,
headers={"Location": redirect_url} if redirect_url else None,
status=status,
match=[matchers.header_matcher({"Accept": "application/octet-stream"})],
)
def mock_server_version(self, version: str = "", status: int = 200) -> responses.BaseResponse:
return self.rsps.get(url=f"{self.base_url}/api/server/version", body=version, status=status)
@contextlib.contextmanager
def sq_api_mocker(base_url: str = "http://sq.home", assert_all_requests_are_fired: bool = True):
with responses.RequestsMock(assert_all_requests_are_fired=assert_all_requests_are_fired) as rsps:
yield SQApiMocker(base_url=base_url, rsps=rsps)
def jre_to_dict(jre: JRE) -> dict:
return {
"id": jre.id,
"filename": jre.filename,
"sha256": jre.sha256,
"javaPath": jre.java_path,
"os": jre.os,
"arch": jre.arch,
"downloadUrl": jre.download_url,
}