-
Notifications
You must be signed in to change notification settings - Fork 69
/
git_config_test.py
339 lines (308 loc) · 13.1 KB
/
git_config_test.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import logging
import os
from typing import NamedTuple
import pytest
from git import Repo # noqa: TID251
from demisto_sdk.commands.common.git_content_config import (
GitContentConfig,
GitCredentials,
GitProvider,
)
from demisto_sdk.commands.common.handlers import DEFAULT_JSON_HANDLER as json
from demisto_sdk.commands.common.legacy_git_tools import git_path
from TestSuite.test_tools import str_in_call_args_list
GIT_ROOT = git_path()
VALID_GITLAB_RESPONSE = (
f"{GIT_ROOT}/demisto_sdk/tests/test_files/valid_gitlab_search_response.json"
)
DEFAULT_GITHUB_BASE_API = "https://raw.githubusercontent.com/demisto/content"
class Urls(NamedTuple):
urls: list
class TestGitContentConfig:
@staticmethod
def teardown_method():
# runs after each method, making sure tests do not interfere
GitContentConfig.NOTIFIED_PRIVATE_REPO = False
@pytest.mark.parametrize(
"url, repo_name",
[
("ssh://git@github.com/demisto/content-dist.git", "demisto/content-dist"),
("git@github.com:demisto/content-dist.git", "demisto/content-dist"),
# clone using github ssh example
("https://github.com/demisto/content-dist.git", "demisto/content-dist"),
# clone using github https example
("https://github.com/demisto/content-dist", "demisto/content-dist"),
],
)
def test_valid_githubs(self, mocker, requests_mock, url: str, repo_name):
"""
Given:
valid github remote urls
When:
Trying to get github configuration
Then:
Validate the correct repo configuration got back
"""
mocker.patch.object(GitContentConfig, "_search_gitlab_repo", return_value=None)
mocker.patch.object(Repo, "remote", return_value=Urls([url]))
requests_mock.get(f"https://api.github.com/repos/{repo_name}")
git_config = GitContentConfig()
assert git_config.current_repository == repo_name
assert "githubusercontent.com" in git_config.base_api
@pytest.mark.parametrize(
"url, repo_name",
[
("https://code.pan.run/xsoar/content-dist", "xsoar/content-dist"), # gitlab
("https://code.pan.run/xsoar/content-dist.git", "xsoar/content-dist"),
(
"https://gitlab-ci-token:token@code.pan.run/xsoar/content-dist.git",
"xsoar/content-dist",
),
],
)
def test_valid_gitlabs(self, mocker, requests_mock, url, repo_name):
"""
Given:
valid gitlab remote urls
When:
Trying to get git configuration
Then:
Validate the correct repo configuration got back
"""
requests_mock.get("https://code.pan.run/api/v4/projects", json=[{"id": 3606}])
mocker.patch.object(Repo, "remote", return_value=Urls([url]))
git_config = GitContentConfig()
assert (
git_config.current_repository is None
) # does not relevant to gitlab at all
assert git_config.git_provider == GitProvider.GitLab
assert git_config.base_api == GitContentConfig.BASE_RAW_GITLAB_LINK.format(
GITLAB_HOST="code.pan.run", GITLAB_ID=3606
)
# We give the repo hostname, but there is a response from the remote url hostname
git_config = GitContentConfig(
repo_hostname="my-gitlab-hostname.com", git_provider=GitProvider.GitLab
)
assert git_config.base_api == GitContentConfig.BASE_RAW_GITLAB_LINK.format(
GITLAB_HOST="code.pan.run", GITLAB_ID=3606
)
def test_custom_github_url(self, mocker, requests_mock):
"""
Given:
A github remote url and the environment variable is set
When:
Trying to get git configuration
Then:
Validate the correct repo configuration got back
"""
mocker.patch.object(GitContentConfig, "_search_gitlab_repo", return_value=None)
custom_github = "my-own-github-url.com"
repo_name = "org/repo"
requests_mock.get(f"https://api.{custom_github}/repos/{repo_name}")
url = f"https://{custom_github}/{repo_name}"
mocker.patch.dict(
os.environ, {GitContentConfig.ENV_REPO_HOSTNAME_NAME: custom_github}
) # test with env var
mocker.patch.object(Repo, "remote", return_value=Urls([url]))
git_config = GitContentConfig()
assert git_config.git_provider == GitProvider.GitHub
assert git_config.current_repository == "org/repo"
assert git_config.base_api == f"https://raw.{custom_github}/org/repo"
mocker.patch.dict(os.environ, {})
mocker.patch.object(Repo, "remote", return_value=Urls([url]))
git_config = GitContentConfig(repo_hostname=custom_github) # test with argument
assert git_config.git_provider == GitProvider.GitHub
assert git_config.current_repository == "org/repo"
assert git_config.base_api == f"https://raw.{custom_github}/org/repo"
# test with no args, should find it with remote address
git_config = GitContentConfig() # test with argument
assert git_config.git_provider == GitProvider.GitHub
assert git_config.current_repository == "org/repo"
assert git_config.base_api == f"https://raw.{custom_github}/org/repo"
def test_custom_github_url_invalid(self, mocker, requests_mock):
"""
Given:
A github remote url but the repo is not exists
When:
Trying to get git configuration
Then:
Validate we got back original content
"""
mocker.patch.object(GitContentConfig, "_search_gitlab_repo", return_value=None)
custom_github = "my-own-github-url.com"
repo_name = "org/repo"
requests_mock.get(
f"https://api.{custom_github}/repos/{repo_name}", status_code=404
)
requests_mock.get(f"https://api.github.com/repos/{repo_name}", status_code=404)
url = f"https://{custom_github}/{repo_name}"
mocker.patch.object(Repo, "remote", return_value=Urls([url]))
git_config = GitContentConfig()
assert (
git_config.current_repository == GitContentConfig.OFFICIAL_CONTENT_REPO_NAME
)
assert git_config.base_api == DEFAULT_GITHUB_BASE_API
def test_gitlab_id_not_found(self, mocker, monkeypatch):
"""
Given:
Specify to use gitlab but cannot find the project id
When:
Trying to get git configuration
Then:
Validate we got back original content
"""
logger_info = mocker.patch.object(logging.getLogger("demisto-sdk"), "info")
monkeypatch.setenv("COLUMNS", "1000")
mocker.patch.object(GitContentConfig, "_search_gitlab_repo", return_value=None)
url = "https://code.pan.run/xsoar/very-private-repo"
mocker.patch.object(Repo, "remote", return_value=Urls([url]))
git_config = GitContentConfig(git_provider=GitProvider.GitLab)
assert git_config.git_provider == GitProvider.GitHub
assert (
git_config.current_repository == GitContentConfig.OFFICIAL_CONTENT_REPO_NAME
)
assert git_config.base_api == DEFAULT_GITHUB_BASE_API
assert all(
[
str_in_call_args_list(logger_info.call_args_list, current_str)
for current_str in [
GitContentConfig.ENV_REPO_HOSTNAME_NAME,
GitCredentials.ENV_GITLAB_TOKEN_NAME,
]
]
)
assert GitContentConfig.NOTIFIED_PRIVATE_REPO
def test_get_repo_name_gitlab_invalid(self, mocker):
"""
Given:
No repository (not running in git)
When:
A known output of git.Repo().remotes().url, but this url not found in GitLab API
Then:
Ignore gitlab and get back to content (demisto/content)
"""
url = "https://code.pan.run/xsoar/very-private-repo"
mocker.patch.object(Repo, "remote", return_value=Urls([url]))
mocker.patch.object(GitContentConfig, "_search_gitlab_repo", return_value=None)
git_config = GitContentConfig()
# for invalid response should return the official content repo
assert (
git_config.current_repository == GitContentConfig.OFFICIAL_CONTENT_REPO_NAME
)
def test_get_repo_name_empty_case(self, mocker):
"""
Given:
No repository (not running in git)
When:
Searching for repository name
Then:
Validate the correct repo got back - demisto/content
"""
mocker.patch.object(Repo, "remote", return_value=Urls([""]))
mocker.patch.object(GitContentConfig, "_search_github_repo", return_value=None)
git_config = GitContentConfig()
assert (
git_config.current_repository == GitContentConfig.OFFICIAL_CONTENT_REPO_NAME
)
def test_search_gitlab_id_valid(self, mocker, requests_mock):
"""
Given:
A valid repo name
When:
Searching for the id of the repo
Then:
The id of the repo should be returned
"""
with open(VALID_GITLAB_RESPONSE) as f:
gitlab_response = json.load(f)
repo = "content-internal-dist"
host = "code.pan.run"
url = f"https://{host}/api/v4/projects?search={repo}"
requests_mock.get(url, json=gitlab_response)
mocker.patch.object(
Repo,
"remote",
return_value=Urls(["https://code.pan.run/xsoar/content-internal-dist.git"]),
)
git_config = GitContentConfig()
assert (
git_config.project_id == 3606
) # this is the project id of `content-internal-dist`
assert git_config.base_api == GitContentConfig.BASE_RAW_GITLAB_LINK.format(
GITLAB_HOST=host, GITLAB_ID=3606
)
def test_search_gitlab_id_invalid(self, mocker, requests_mock):
"""
Given:
An invalid repo name
When:
Searching for the id of the repo
Then:
None should be returned
"""
repo = "no-real-repo"
host = "code.pan.run"
search_api_url1 = f"https://code.pan.run/api/v4/projects?search={repo}"
search_api_url2 = "https://code.pan.run/api/v4/projects?search=test"
requests_mock.get(search_api_url1, json=[])
requests_mock.get(search_api_url2, json=[])
mocker.patch.object(
Repo, "remote", return_value=Urls(["https://code.pan.run/xsoar/test"])
)
mocker.patch.object(GitContentConfig, "_search_github_repo", return_value=None)
git_config = GitContentConfig()
assert git_config._search_gitlab_repo(host, repo) is None
assert (
git_config.current_repository == GitContentConfig.OFFICIAL_CONTENT_REPO_NAME
)
assert git_config.git_provider == GitProvider.GitHub
assert git_config.base_api == DEFAULT_GITHUB_BASE_API
def test_provide_repo_name(self, mocker, requests_mock):
"""
Given:
A repo name argument to git config
When:
Calling git config to other repo instead of the one that configured in the env
Then:
The git config should be as the repo name provided
"""
requests_mock.get(
f"https://api.github.com/repos/{GitContentConfig.OFFICIAL_CONTENT_REPO_NAME}"
)
git_config = GitContentConfig(GitContentConfig.OFFICIAL_CONTENT_REPO_NAME)
assert (
git_config.current_repository == GitContentConfig.OFFICIAL_CONTENT_REPO_NAME
)
assert git_config.base_api == DEFAULT_GITHUB_BASE_API
custom_repo_name = "org/repo"
requests_mock.get(f"https://api.github.com/repos/{custom_repo_name}")
requests_mock.get("https://api.github.com/repos/demisto/demisto-sdk")
git_config = GitContentConfig(custom_repo_name)
assert git_config.current_repository == custom_repo_name
assert (
git_config.base_api
== f"https://raw.githubusercontent.com/{custom_repo_name}"
)
mocker.patch.object(
GitContentConfig, "_search_gitlab_repo", return_value=("gitlab.com", 3)
)
git_config = GitContentConfig(custom_repo_name, git_provider=GitProvider.GitLab)
assert git_config.current_repository == custom_repo_name
assert git_config.base_api == "https://gitlab.com/api/v4/projects/3/repository"
def test_provide_project_id(self, requests_mock):
"""
Given:
Project id to gitlab
When:
Calling git config with repo based on gitlab
Then:
The git config should be as the repo name provided
"""
requests_mock.get("https://code.pan.run/api/v4/projects/3")
git_config = GitContentConfig(
project_id=3, git_provider=GitProvider.GitLab, repo_hostname="code.pan.run"
)
assert git_config.project_id == 3
assert (
git_config.base_api == "https://code.pan.run/api/v4/projects/3/repository"
)