Skip to content

Commit 323ab3c

Browse files
nejchJohnVillalovos
authored andcommitted
test: add tests and clean up usage for new enums
1 parent d652133 commit 323ab3c

File tree

12 files changed

+84
-45
lines changed

12 files changed

+84
-45
lines changed

docs/gl_objects/access_requests.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Create an access request::
4343
Approve an access request::
4444

4545
ar.approve() # defaults to DEVELOPER level
46-
ar.approve(access_level=gitlab.const.AccessLevel.MAINTAINER.value) # explicitly set access level
46+
ar.approve(access_level=gitlab.const.AccessLevel.MAINTAINER) # explicitly set access level
4747

4848
Deny (delete) an access request::
4949

docs/gl_objects/groups.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Remove a group::
8080

8181
Share/unshare the group with a group::
8282

83-
group.share(group2.id, gitlab.const.AccessLevel.DEVELOPER.value)
83+
group.share(group2.id, gitlab.const.AccessLevel.DEVELOPER)
8484
group.unshare(group2.id)
8585

8686
Import / Export
@@ -284,11 +284,11 @@ Get a member of a group, including members inherited through ancestor groups::
284284
Add a member to the group::
285285

286286
member = group.members.create({'user_id': user_id,
287-
'access_level': gitlab.const.AccessLevel.GUEST.value})
287+
'access_level': gitlab.const.AccessLevel.GUEST})
288288

289289
Update a member (change the access level)::
290290

291-
member.access_level = gitlab.const.AccessLevel.DEVELOPER.value
291+
member.access_level = gitlab.const.AccessLevel.DEVELOPER
292292
member.save()
293293

294294
Remove a member from the group::
@@ -316,7 +316,7 @@ LDAP group links
316316

317317
Add an LDAP group link to an existing GitLab group::
318318

319-
group.add_ldap_group_link(ldap_group_cn, gitlab.const.AccessLevel.DEVELOPER.value, 'ldapmain')
319+
group.add_ldap_group_link(ldap_group_cn, gitlab.const.AccessLevel.DEVELOPER, 'ldapmain')
320320

321321
Remove a link::
322322

docs/gl_objects/notifications.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ Get the notifications settings::
4747
Update the notifications settings::
4848

4949
# use a predefined level
50-
settings.level = gitlab.const.NotificationLevel.WATCH.value
50+
settings.level = gitlab.const.NotificationLevel.WATCH
5151

5252
# create a custom setup
53-
settings.level = gitlab.const.NotificationLevel.CUSTOM.value
53+
settings.level = gitlab.const.NotificationLevel.CUSTOM
5454
settings.save() # will create additional attributes, but not mandatory
5555

5656
settings.new_merge_request = True

docs/gl_objects/projects.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ Create a snippet::
511511
'file_name': 'foo.py',
512512
'code': 'import gitlab',
513513
'visibility_level':
514-
gitlab.const.Visibility.PRIVATE.value})
514+
gitlab.const.Visibility.PRIVATE})
515515

516516
Update a snippet::
517517

@@ -577,11 +577,11 @@ Get a member of a project, including members inherited through ancestor groups::
577577
Add a project member::
578578

579579
member = project.members.create({'user_id': user.id, 'access_level':
580-
gitlab.const.AccessLevel.DEVELOPER.value})
580+
gitlab.const.AccessLevel.DEVELOPER})
581581

582582
Modify a project member (change the access level)::
583583

584-
member.access_level = gitlab.const.AccessLevel.MAINTAINER.value
584+
member.access_level = gitlab.const.AccessLevel.MAINTAINER
585585
member.save()
586586

587587
Remove a member from the project team::
@@ -592,7 +592,7 @@ Remove a member from the project team::
592592

593593
Share/unshare the project with a group::
594594

595-
project.share(group.id, gitlab.const.AccessLevel.DEVELOPER.value)
595+
project.share(group.id, gitlab.const.AccessLevel.DEVELOPER)
596596
project.unshare(group.id)
597597

598598
Project hooks

docs/gl_objects/protected_branches.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Create a protected branch::
3131

3232
p_branch = project.protectedbranches.create({
3333
'name': '*-stable',
34-
'merge_access_level': gitlab.const.AccessLevel.DEVELOPER.value,
35-
'push_access_level': gitlab.const.AccessLevel.MAINTAINER.value
34+
'merge_access_level': gitlab.const.AccessLevel.DEVELOPER,
35+
'push_access_level': gitlab.const.AccessLevel.MAINTAINER
3636
})
3737

3838
Create a protected branch with more granular access control::
@@ -41,7 +41,7 @@ Create a protected branch with more granular access control::
4141
'name': '*-stable',
4242
'allowed_to_push': [{"user_id": 99}, {"user_id": 98}],
4343
'allowed_to_merge': [{"group_id": 653}],
44-
'allowed_to_unprotect': [{"access_level": gitlab.const.AccessLevel.MAINTAINER.value}]
44+
'allowed_to_unprotect': [{"access_level": gitlab.const.AccessLevel.MAINTAINER}]
4545
})
4646

4747
Delete a protected branch::

docs/gl_objects/search.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,30 @@ Examples
4646
Search for issues matching a specific string::
4747

4848
# global search
49-
gl.search(gitlab.const.SearchScope.ISSUES.value, 'regression')
49+
gl.search(gitlab.const.SearchScope.ISSUES, 'regression')
5050

5151
# group search
5252
group = gl.groups.get('mygroup')
53-
group.search(gitlab.const.SearchScope.ISSUES.value, 'regression')
53+
group.search(gitlab.const.SearchScope.ISSUES, 'regression')
5454

5555
# project search
5656
project = gl.projects.get('myproject')
57-
project.search(gitlab.const.SearchScope.ISSUES.value, 'regression')
57+
project.search(gitlab.const.SearchScope.ISSUES, 'regression')
5858

5959
The ``search()`` methods implement the pagination support::
6060

6161
# get lists of 10 items, and start at page 2
62-
gl.search(gitlab.const.SearchScope.ISSUES.value, search_str, page=2, per_page=10)
62+
gl.search(gitlab.const.SearchScope.ISSUES, search_str, page=2, per_page=10)
6363

6464
# get a generator that will automatically make required API calls for
6565
# pagination
66-
for item in gl.search(gitlab.const.SearchScope.ISSUES.value, search_str, iterator=True):
66+
for item in gl.search(gitlab.const.SearchScope.ISSUES, search_str, iterator=True):
6767
do_something(item)
6868

6969
The search API doesn't return objects, but dicts. If you need to act on
7070
objects, you need to create them explicitly::
7171

72-
for item in gl.search(gitlab.const.SearchScope.ISSUES.value, search_str, iterator=True):
72+
for item in gl.search(gitlab.const.SearchScope.ISSUES, search_str, iterator=True):
7373
issue_project = gl.projects.get(item['project_id'], lazy=True)
7474
issue = issue_project.issues.get(item['iid'])
7575
issue.state = 'closed'

docs/gl_objects/snippets.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Create a snippet::
4444

4545
Update the snippet attributes::
4646

47-
snippet.visibility_level = gitlab.const.Visibility.PUBLIC.value
47+
snippet.visibility_level = gitlab.const.Visibility.PUBLIC
4848
snippet.save()
4949

5050
To update a snippet code you need to create a ``ProjectSnippet`` object::

tests/functional/api/test_gitlab.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ def test_namespaces(gl):
125125

126126
def test_notification_settings(gl):
127127
settings = gl.notificationsettings.get()
128-
settings.level = gitlab.const.NOTIFICATION_LEVEL_WATCH
128+
settings.level = gitlab.const.NotificationLevel.WATCH
129129
settings.save()
130130

131131
settings = gl.notificationsettings.get()
132-
assert settings.level == gitlab.const.NOTIFICATION_LEVEL_WATCH
132+
assert settings.level == gitlab.const.NotificationLevel.WATCH
133133

134134

135135
def test_search(gl):
136-
result = gl.search(scope=gitlab.const.SEARCH_SCOPE_USERS, search="Administrator")
136+
result = gl.search(scope=gitlab.const.SearchScope.USERS, search="Administrator")
137137
assert result[0]["id"] == 1
138138

139139

tests/functional/api/test_groups.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ def test_groups(gl):
4343
assert group4 in filtered_groups
4444

4545
group1.members.create(
46-
{"access_level": gitlab.const.OWNER_ACCESS, "user_id": user.id}
46+
{"access_level": gitlab.const.AccessLevel.OWNER, "user_id": user.id}
4747
)
4848
group1.members.create(
49-
{"access_level": gitlab.const.GUEST_ACCESS, "user_id": user2.id}
49+
{"access_level": gitlab.const.AccessLevel.GUEST, "user_id": user2.id}
5050
)
5151
group2.members.create(
52-
{"access_level": gitlab.const.OWNER_ACCESS, "user_id": user2.id}
52+
{"access_level": gitlab.const.AccessLevel.OWNER, "user_id": user2.id}
5353
)
5454

55-
group4.share(group1.id, gitlab.const.DEVELOPER_ACCESS)
56-
group4.share(group2.id, gitlab.const.MAINTAINER_ACCESS)
55+
group4.share(group1.id, gitlab.const.AccessLevel.DEVELOPER)
56+
group4.share(group2.id, gitlab.const.AccessLevel.MAINTAINER)
5757
# Reload group4 to have updated shared_with_groups
5858
group4 = gl.groups.get(group4.id)
5959
assert len(group4.shared_with_groups) == 2
@@ -71,7 +71,7 @@ def test_groups(gl):
7171

7272
membership = memberships1[0]
7373
assert membership.source_type == "Namespace"
74-
assert membership.access_level == gitlab.const.OWNER_ACCESS
74+
assert membership.access_level == gitlab.const.AccessLevel.OWNER
7575

7676
project_memberships = user.memberships.list(type="Project")
7777
assert len(project_memberships) == 0
@@ -95,10 +95,10 @@ def test_groups(gl):
9595
assert len(group1.members.list()) == 2
9696
assert len(group1.members_all.list())
9797
member = group1.members.get(user2.id)
98-
member.access_level = gitlab.const.OWNER_ACCESS
98+
member.access_level = gitlab.const.AccessLevel.OWNER
9999
member.save()
100100
member = group1.members.get(user2.id)
101-
assert member.access_level == gitlab.const.OWNER_ACCESS
101+
assert member.access_level == gitlab.const.AccessLevel.OWNER
102102

103103
gl.auth()
104104
group2.members.delete(gl.user.id)

tests/functional/api/test_projects.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
import gitlab
6+
from gitlab.const import AccessLevel
67
from gitlab.v4.objects.projects import ProjectStorage
78

89

@@ -36,6 +37,17 @@ def test_create_project(gl, user):
3637
sudo_project.delete()
3738

3839

40+
def test_project_members(user, project):
41+
member = project.members.create(
42+
{"user_id": user.id, "access_level": AccessLevel.DEVELOPER}
43+
)
44+
assert member in project.members.list()
45+
assert member.access_level == 30
46+
47+
member.delete()
48+
assert member not in project.members.list()
49+
50+
3951
def test_project_badges(project):
4052
badge_image = "http://example.com"
4153
badge_link = "http://example/img.svg"

tests/unit/objects/test_members.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
import responses
66

7+
from gitlab.const import AccessLevel
78
from gitlab.v4.objects import GroupBillableMember
89

910
billable_members_content = [
@@ -21,6 +22,19 @@
2122
]
2223

2324

25+
@pytest.fixture
26+
def resp_create_group_member(no_content):
27+
with responses.RequestsMock() as rsps:
28+
rsps.add(
29+
method=responses.POST,
30+
url="http://localhost/api/v4/groups/1/members",
31+
json={"id": 1, "username": "jane_doe", "access_level": 30},
32+
content_type="application/json",
33+
status=201,
34+
)
35+
yield rsps
36+
37+
2438
@pytest.fixture
2539
def resp_list_billable_group_members():
2640
with responses.RequestsMock() as rsps:
@@ -47,6 +61,11 @@ def resp_delete_billable_group_member(no_content):
4761
yield rsps
4862

4963

64+
def test_create_group_member(group, resp_create_group_member):
65+
member = group.members.create({"user_id": 1, "access_level": AccessLevel.DEVELOPER})
66+
assert member.access_level == 30
67+
68+
5069
def test_list_group_billable_members(group, resp_list_billable_group_members):
5170
billable_members = group.billable_members.list()
5271
assert isinstance(billable_members, list)

tests/unit/test_gitlab.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import copy
2020
import logging
2121
import pickle
22-
import warnings
2322
from http.client import HTTPConnection
2423

2524
import pytest
@@ -321,16 +320,25 @@ def test_gitlab_user_agent(kwargs, expected_agent):
321320
assert gl.headers["User-Agent"] == expected_agent
322321

323322

324-
def test_gitlab_deprecated_const():
325-
with warnings.catch_warnings(record=True) as caught_warnings:
326-
gitlab.NO_ACCESS
327-
assert len(caught_warnings) == 1
328-
warning = caught_warnings[0]
329-
assert isinstance(warning.message, DeprecationWarning)
330-
message = str(caught_warnings[0].message)
331-
assert "deprecated" in message
332-
assert "gitlab.const.NO_ACCESS" in message
323+
def test_gitlab_deprecated_global_const_warns():
324+
with pytest.deprecated_call(
325+
match="'gitlab.NO_ACCESS' is deprecated.*constants in the 'gitlab.const'"
326+
) as record:
327+
no_access = gitlab.NO_ACCESS
333328

334-
with warnings.catch_warnings(record=True) as caught_warnings:
335-
gitlab.const.NO_ACCESS
336-
assert len(caught_warnings) == 0
329+
assert len(record) == 1
330+
assert no_access == 0
331+
332+
333+
def test_gitlab_enum_const_does_not_warn(recwarn):
334+
no_access = gitlab.const.AccessLevel.NO_ACCESS
335+
336+
assert not recwarn
337+
assert no_access == 0
338+
339+
340+
def test_gitlab_plain_const_does_not_warn(recwarn):
341+
no_access = gitlab.const.NO_ACCESS
342+
343+
assert not recwarn
344+
assert no_access == 0

0 commit comments

Comments
 (0)