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
|
import pytest
import requests
import gitlab
@pytest.fixture(
scope="session",
params=[{"get_all": True}, {"all": True}],
ids=["get_all=True", "all=True"],
)
def get_all_kwargs(request):
"""A tiny parametrized fixture to inject both `get_all=True` and
`all=True` to ensure they behave the same way for pagination."""
return request.param
def test_auth_from_config(gl, temp_dir):
"""Test token authentication from config file"""
test_gitlab = gitlab.Gitlab.from_config(
config_files=[temp_dir / "python-gitlab.cfg"]
)
test_gitlab.auth()
assert isinstance(test_gitlab.user, gitlab.v4.objects.CurrentUser)
def test_no_custom_session(gl, temp_dir):
"""Test no custom session"""
custom_session = requests.Session()
test_gitlab = gitlab.Gitlab.from_config(
config_files=[temp_dir / "python-gitlab.cfg"]
)
assert test_gitlab.session != custom_session
def test_custom_session(gl, temp_dir):
"""Test custom session"""
custom_session = requests.Session()
test_gitlab = gitlab.Gitlab.from_config(
config_files=[temp_dir / "python-gitlab.cfg"], session=custom_session
)
assert test_gitlab.session == custom_session
def test_broadcast_messages(gl, get_all_kwargs):
msg = gl.broadcastmessages.create({"message": "this is the message"})
msg.color = "#444444"
msg.save()
msg_id = msg.id
msg = gl.broadcastmessages.list(**get_all_kwargs)[0]
assert msg.color == "#444444"
msg = gl.broadcastmessages.get(msg_id)
assert msg.color == "#444444"
msg.delete()
assert msg not in gl.broadcastmessages.list()
def test_markdown(gl):
html = gl.markdown("foo")
assert "foo" in html
def test_markdown_in_project(gl, project):
html = gl.markdown("foo", project=project.path_with_namespace)
assert "foo" in html
def test_lint(gl):
with pytest.deprecated_call():
success, errors = gl.lint("Invalid")
assert success is False
assert errors
def test_sidekiq_queue_metrics(gl):
out = gl.sidekiq.queue_metrics()
assert isinstance(out, dict)
assert "default" in out["queues"]
def test_sidekiq_process_metrics(gl):
out = gl.sidekiq.process_metrics()
assert isinstance(out, dict)
assert "hostname" in out["processes"][0]
def test_sidekiq_job_stats(gl):
out = gl.sidekiq.job_stats()
assert isinstance(out, dict)
assert "processed" in out["jobs"]
def test_sidekiq_compound_metrics(gl):
out = gl.sidekiq.compound_metrics()
assert isinstance(out, dict)
assert "jobs" in out
assert "processes" in out
assert "queues" in out
@pytest.mark.gitlab_premium
def test_geo_nodes(gl):
# Very basic geo nodes tests because we only have 1 node.
nodes = gl.geonodes.list()
assert isinstance(nodes, list)
status = gl.geonodes.status()
assert isinstance(status, list)
@pytest.mark.gitlab_premium
def test_gitlab_license(gl):
license = gl.get_license()
assert "user_limit" in license
with pytest.raises(gitlab.GitlabLicenseError, match="The license key is invalid."):
gl.set_license("dummy key")
def test_gitlab_settings(gl):
settings = gl.settings.get()
settings.default_projects_limit = 42
settings.save()
settings = gl.settings.get()
assert settings.default_projects_limit == 42
def test_template_dockerfile(gl):
assert gl.dockerfiles.list()
dockerfile = gl.dockerfiles.get("Node")
assert dockerfile.content is not None
def test_template_gitignore(gl, get_all_kwargs):
assert gl.gitignores.list(**get_all_kwargs)
gitignore = gl.gitignores.get("Node")
assert gitignore.content is not None
def test_template_gitlabciyml(gl, get_all_kwargs):
assert gl.gitlabciymls.list(**get_all_kwargs)
gitlabciyml = gl.gitlabciymls.get("Nodejs")
assert gitlabciyml.content is not None
def test_template_license(gl):
assert gl.licenses.list()
license = gl.licenses.get(
"bsd-2-clause", project="mytestproject", fullname="mytestfullname"
)
assert "mytestfullname" in license.content
def test_hooks(gl):
hook = gl.hooks.create({"url": "http://whatever.com"})
assert hook in gl.hooks.list()
hook.delete()
assert hook not in gl.hooks.list()
def test_namespaces(gl, get_all_kwargs):
gl.auth()
current_user = gl.user.username
namespaces = gl.namespaces.list(**get_all_kwargs)
assert namespaces
namespaces = gl.namespaces.list(search=current_user, **get_all_kwargs)
assert namespaces[0].kind == "user"
namespace = gl.namespaces.get(current_user)
assert namespace.kind == "user"
namespace = gl.namespaces.exists(current_user)
assert namespace.exists
def test_notification_settings(gl):
settings = gl.notificationsettings.get()
settings.level = gitlab.const.NotificationLevel.WATCH
settings.save()
settings = gl.notificationsettings.get()
assert settings.level == gitlab.const.NotificationLevel.WATCH
def test_search(gl):
result = gl.search(scope=gitlab.const.SearchScope.USERS, search="Administrator")
assert result[0]["id"] == 1
def test_user_activities(gl):
activities = gl.user_activities.list(query_parameters={"from": "2019-01-01"})
assert isinstance(activities, list)
def test_events(gl):
events = gl.events.list()
assert isinstance(events, list)
@pytest.mark.skip
def test_features(gl):
feat = gl.features.set("foo", 30)
assert feat.name == "foo"
assert feat in gl.features.list()
feat.delete()
assert feat not in gl.features.list()
def test_pagination(gl, project):
project2 = gl.projects.create({"name": "project-page-2"})
list1 = gl.projects.list(per_page=1, page=1)
list2 = gl.projects.list(per_page=1, page=2)
assert len(list1) == 1
assert len(list2) == 1
assert list1[0].id != list2[0].id
project2.delete()
def test_rate_limits(gl):
settings = gl.settings.get()
settings.throttle_authenticated_api_enabled = True
settings.throttle_authenticated_api_requests_per_period = 1
settings.throttle_authenticated_api_period_in_seconds = 3
settings.save()
projects = []
for i in range(0, 20):
projects.append(gl.projects.create({"name": f"{str(i)}ok"}))
with pytest.raises(gitlab.GitlabCreateError) as e:
for i in range(20, 40):
projects.append(
gl.projects.create(
{"name": f"{str(i)}shouldfail"}, obey_rate_limit=False
)
)
assert "Retry later" in str(e.value)
settings.throttle_authenticated_api_enabled = False
settings.save()
[project.delete() for project in projects]
def test_list_default_warning(gl):
"""When there are more than 20 items and use default `list()` then warning is
generated"""
with pytest.warns(UserWarning, match="python-gitlab.readthedocs.io") as record:
gl.gitlabciymls.list()
assert len(record) == 1
warning = record[0]
assert __file__ == warning.filename
def test_list_page_nowarning(gl, recwarn):
"""Using `page=X` will disable the warning"""
gl.gitlabciymls.list(page=1)
assert not recwarn
def test_list_all_false_nowarning(gl, recwarn):
"""Using `all=False` will disable the warning"""
gl.gitlabciymls.list(all=False)
assert not recwarn
def test_list_all_true_nowarning(gl, get_all_kwargs, recwarn):
"""Using `get_all=True` will disable the warning"""
items = gl.gitlabciymls.list(**get_all_kwargs)
for warn in recwarn:
if issubclass(warn.category, UserWarning):
# Our warning has a link to the docs in it, make sure we don't have
# that.
assert "python-gitlab.readthedocs.io" not in str(warn.message)
assert len(items) > 20
def test_list_iterator_true_nowarning(gl, recwarn):
"""Using `iterator=True` will disable the warning"""
items = gl.gitlabciymls.list(iterator=True)
assert not recwarn
assert len(list(items)) > 20
def test_list_as_list_false_warnings(gl):
"""Using `as_list=False` will disable the UserWarning but cause a
DeprecationWarning"""
with pytest.warns(DeprecationWarning) as record:
items = gl.gitlabciymls.list(as_list=False)
assert len(record) == 1
assert len(list(items)) > 20
def test_list_with_as_list_and_iterator_raises(gl):
with pytest.raises(ValueError, match="`as_list` or `iterator`"):
gl.gitlabciymls.list(as_list=False, iterator=True)
|