Skip to content

Commit

Permalink
test: add tests and clean up usage for new enums
Browse files Browse the repository at this point in the history
  • Loading branch information
nejch authored and JohnVillalovos committed Jun 27, 2022
1 parent d652133 commit 323ab3c
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 45 deletions.
2 changes: 1 addition & 1 deletion docs/gl_objects/access_requests.rst
Expand Up @@ -43,7 +43,7 @@ Create an access request::
Approve an access request::

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

Deny (delete) an access request::

Expand Down
8 changes: 4 additions & 4 deletions docs/gl_objects/groups.rst
Expand Up @@ -80,7 +80,7 @@ Remove a group::

Share/unshare the group with a group::

group.share(group2.id, gitlab.const.AccessLevel.DEVELOPER.value)
group.share(group2.id, gitlab.const.AccessLevel.DEVELOPER)
group.unshare(group2.id)

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

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

Update a member (change the access level)::

member.access_level = gitlab.const.AccessLevel.DEVELOPER.value
member.access_level = gitlab.const.AccessLevel.DEVELOPER
member.save()

Remove a member from the group::
Expand Down Expand Up @@ -316,7 +316,7 @@ LDAP group links

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

group.add_ldap_group_link(ldap_group_cn, gitlab.const.AccessLevel.DEVELOPER.value, 'ldapmain')
group.add_ldap_group_link(ldap_group_cn, gitlab.const.AccessLevel.DEVELOPER, 'ldapmain')

Remove a link::

Expand Down
4 changes: 2 additions & 2 deletions docs/gl_objects/notifications.rst
Expand Up @@ -47,10 +47,10 @@ Get the notifications settings::
Update the notifications settings::

# use a predefined level
settings.level = gitlab.const.NotificationLevel.WATCH.value
settings.level = gitlab.const.NotificationLevel.WATCH

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

settings.new_merge_request = True
Expand Down
8 changes: 4 additions & 4 deletions docs/gl_objects/projects.rst
Expand Up @@ -511,7 +511,7 @@ Create a snippet::
'file_name': 'foo.py',
'code': 'import gitlab',
'visibility_level':
gitlab.const.Visibility.PRIVATE.value})
gitlab.const.Visibility.PRIVATE})

Update a snippet::

Expand Down Expand Up @@ -577,11 +577,11 @@ Get a member of a project, including members inherited through ancestor groups::
Add a project member::

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

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

member.access_level = gitlab.const.AccessLevel.MAINTAINER.value
member.access_level = gitlab.const.AccessLevel.MAINTAINER
member.save()

Remove a member from the project team::
Expand All @@ -592,7 +592,7 @@ Remove a member from the project team::

Share/unshare the project with a group::

project.share(group.id, gitlab.const.AccessLevel.DEVELOPER.value)
project.share(group.id, gitlab.const.AccessLevel.DEVELOPER)
project.unshare(group.id)

Project hooks
Expand Down
6 changes: 3 additions & 3 deletions docs/gl_objects/protected_branches.rst
Expand Up @@ -31,8 +31,8 @@ Create a protected branch::

p_branch = project.protectedbranches.create({
'name': '*-stable',
'merge_access_level': gitlab.const.AccessLevel.DEVELOPER.value,
'push_access_level': gitlab.const.AccessLevel.MAINTAINER.value
'merge_access_level': gitlab.const.AccessLevel.DEVELOPER,
'push_access_level': gitlab.const.AccessLevel.MAINTAINER
})

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

Delete a protected branch::
Expand Down
12 changes: 6 additions & 6 deletions docs/gl_objects/search.rst
Expand Up @@ -46,30 +46,30 @@ Examples
Search for issues matching a specific string::

# global search
gl.search(gitlab.const.SearchScope.ISSUES.value, 'regression')
gl.search(gitlab.const.SearchScope.ISSUES, 'regression')

# group search
group = gl.groups.get('mygroup')
group.search(gitlab.const.SearchScope.ISSUES.value, 'regression')
group.search(gitlab.const.SearchScope.ISSUES, 'regression')

# project search
project = gl.projects.get('myproject')
project.search(gitlab.const.SearchScope.ISSUES.value, 'regression')
project.search(gitlab.const.SearchScope.ISSUES, 'regression')

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

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

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

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

for item in gl.search(gitlab.const.SearchScope.ISSUES.value, search_str, iterator=True):
for item in gl.search(gitlab.const.SearchScope.ISSUES, search_str, iterator=True):
issue_project = gl.projects.get(item['project_id'], lazy=True)
issue = issue_project.issues.get(item['iid'])
issue.state = 'closed'
Expand Down
2 changes: 1 addition & 1 deletion docs/gl_objects/snippets.rst
Expand Up @@ -44,7 +44,7 @@ Create a snippet::

Update the snippet attributes::

snippet.visibility_level = gitlab.const.Visibility.PUBLIC.value
snippet.visibility_level = gitlab.const.Visibility.PUBLIC
snippet.save()

To update a snippet code you need to create a ``ProjectSnippet`` object::
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/api/test_gitlab.py
Expand Up @@ -125,15 +125,15 @@ def test_namespaces(gl):

def test_notification_settings(gl):
settings = gl.notificationsettings.get()
settings.level = gitlab.const.NOTIFICATION_LEVEL_WATCH
settings.level = gitlab.const.NotificationLevel.WATCH
settings.save()

settings = gl.notificationsettings.get()
assert settings.level == gitlab.const.NOTIFICATION_LEVEL_WATCH
assert settings.level == gitlab.const.NotificationLevel.WATCH


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


Expand Down
16 changes: 8 additions & 8 deletions tests/functional/api/test_groups.py
Expand Up @@ -43,17 +43,17 @@ def test_groups(gl):
assert group4 in filtered_groups

group1.members.create(
{"access_level": gitlab.const.OWNER_ACCESS, "user_id": user.id}
{"access_level": gitlab.const.AccessLevel.OWNER, "user_id": user.id}
)
group1.members.create(
{"access_level": gitlab.const.GUEST_ACCESS, "user_id": user2.id}
{"access_level": gitlab.const.AccessLevel.GUEST, "user_id": user2.id}
)
group2.members.create(
{"access_level": gitlab.const.OWNER_ACCESS, "user_id": user2.id}
{"access_level": gitlab.const.AccessLevel.OWNER, "user_id": user2.id}
)

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

membership = memberships1[0]
assert membership.source_type == "Namespace"
assert membership.access_level == gitlab.const.OWNER_ACCESS
assert membership.access_level == gitlab.const.AccessLevel.OWNER

project_memberships = user.memberships.list(type="Project")
assert len(project_memberships) == 0
Expand All @@ -95,10 +95,10 @@ def test_groups(gl):
assert len(group1.members.list()) == 2
assert len(group1.members_all.list())
member = group1.members.get(user2.id)
member.access_level = gitlab.const.OWNER_ACCESS
member.access_level = gitlab.const.AccessLevel.OWNER
member.save()
member = group1.members.get(user2.id)
assert member.access_level == gitlab.const.OWNER_ACCESS
assert member.access_level == gitlab.const.AccessLevel.OWNER

gl.auth()
group2.members.delete(gl.user.id)
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/api/test_projects.py
Expand Up @@ -3,6 +3,7 @@
import pytest

import gitlab
from gitlab.const import AccessLevel
from gitlab.v4.objects.projects import ProjectStorage


Expand Down Expand Up @@ -36,6 +37,17 @@ def test_create_project(gl, user):
sudo_project.delete()


def test_project_members(user, project):
member = project.members.create(
{"user_id": user.id, "access_level": AccessLevel.DEVELOPER}
)
assert member in project.members.list()
assert member.access_level == 30

member.delete()
assert member not in project.members.list()


def test_project_badges(project):
badge_image = "http://example.com"
badge_link = "http://example/img.svg"
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/objects/test_members.py
Expand Up @@ -4,6 +4,7 @@
import pytest
import responses

from gitlab.const import AccessLevel
from gitlab.v4.objects import GroupBillableMember

billable_members_content = [
Expand All @@ -21,6 +22,19 @@
]


@pytest.fixture
def resp_create_group_member(no_content):
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/groups/1/members",
json={"id": 1, "username": "jane_doe", "access_level": 30},
content_type="application/json",
status=201,
)
yield rsps


@pytest.fixture
def resp_list_billable_group_members():
with responses.RequestsMock() as rsps:
Expand All @@ -47,6 +61,11 @@ def resp_delete_billable_group_member(no_content):
yield rsps


def test_create_group_member(group, resp_create_group_member):
member = group.members.create({"user_id": 1, "access_level": AccessLevel.DEVELOPER})
assert member.access_level == 30


def test_list_group_billable_members(group, resp_list_billable_group_members):
billable_members = group.billable_members.list()
assert isinstance(billable_members, list)
Expand Down
34 changes: 21 additions & 13 deletions tests/unit/test_gitlab.py
Expand Up @@ -19,7 +19,6 @@
import copy
import logging
import pickle
import warnings
from http.client import HTTPConnection

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


def test_gitlab_deprecated_const():
with warnings.catch_warnings(record=True) as caught_warnings:
gitlab.NO_ACCESS
assert len(caught_warnings) == 1
warning = caught_warnings[0]
assert isinstance(warning.message, DeprecationWarning)
message = str(caught_warnings[0].message)
assert "deprecated" in message
assert "gitlab.const.NO_ACCESS" in message
def test_gitlab_deprecated_global_const_warns():
with pytest.deprecated_call(
match="'gitlab.NO_ACCESS' is deprecated.*constants in the 'gitlab.const'"
) as record:
no_access = gitlab.NO_ACCESS

with warnings.catch_warnings(record=True) as caught_warnings:
gitlab.const.NO_ACCESS
assert len(caught_warnings) == 0
assert len(record) == 1
assert no_access == 0


def test_gitlab_enum_const_does_not_warn(recwarn):
no_access = gitlab.const.AccessLevel.NO_ACCESS

assert not recwarn
assert no_access == 0


def test_gitlab_plain_const_does_not_warn(recwarn):
no_access = gitlab.const.NO_ACCESS

assert not recwarn
assert no_access == 0

0 comments on commit 323ab3c

Please sign in to comment.