GitLab group and project access token modules (#7964)

* Adding gitlab group and project acess token modules

* Documentation corrections and recreate option change

* Documentation corrections

* Correcting documentation for return objects
This commit is contained in:
Zoran Krleza 2024-02-25 19:44:49 +01:00 committed by GitHub
parent 6cafd3bed7
commit f6d0b35bb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1402 additions and 0 deletions

View file

@ -284,6 +284,36 @@ def resp_delete_group(url, request):
return response(204, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/access_tokens", method="get")
def resp_list_group_access_tokens(url, request):
headers = {'content-type': 'application/json'}
content = ('[{"user_id" : 1, "scopes" : ["api"], "name" : "token1", "expires_at" : "2021-01-31",'
'"id" : 1, "active" : false, "created_at" : "2021-01-20T22:11:48.151Z", "revoked" : true,'
'"access_level": 40},{"user_id" : 2, "scopes" : ["api"], "name" : "token2", "expires_at" : "2021-02-31",'
'"id" : 2, "active" : true, "created_at" : "2021-02-20T22:11:48.151Z", "revoked" : false,'
'"access_level": 40}]')
content = content.encode("utf-8")
return response(200, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/access_tokens", method="post")
def resp_create_group_access_tokens(url, request):
headers = {'content-type': 'application/json'}
content = ('{"user_id" : 1, "scopes" : ["api"], "name" : "token1", "expires_at" : "2021-01-31",'
'"id" : 1, "active" : false, "created_at" : "2021-01-20T22:11:48.151Z", "revoked" : true,'
'"access_level": 40, "token": "Der423FErcdv35qEEWc"}')
content = content.encode("utf-8")
return response(201, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/access_tokens/1", method="delete")
def resp_revoke_group_access_tokens(url, request):
headers = {'content-type': 'application/json'}
content = ('')
content = content.encode("utf-8")
return response(204, content, headers, None, 5, request)
'''
GROUP MEMBER API
'''
@ -534,6 +564,36 @@ def resp_delete_protected_branch(url, request):
return response(204, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1/access_tokens", method="get")
def resp_list_project_access_tokens(url, request):
headers = {'content-type': 'application/json'}
content = ('[{"user_id" : 1, "scopes" : ["api"], "name" : "token1", "expires_at" : "2021-01-31",'
'"id" : 1, "active" : false, "created_at" : "2021-01-20T22:11:48.151Z", "revoked" : true,'
'"access_level": 40},{"user_id" : 2, "scopes" : ["api"], "name" : "token2", "expires_at" : "2021-02-31",'
'"id" : 2, "active" : true, "created_at" : "2021-02-20T22:11:48.151Z", "revoked" : false,'
'"access_level": 40}]')
content = content.encode("utf-8")
return response(200, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1/access_tokens", method="post")
def resp_create_project_access_tokens(url, request):
headers = {'content-type': 'application/json'}
content = ('{"user_id" : 1, "scopes" : ["api"], "name" : "token1", "expires_at" : "2021-01-31",'
'"id" : 1, "active" : false, "created_at" : "2021-01-20T22:11:48.151Z", "revoked" : true,'
'"access_level": 40, "token": "Der423FErcdv35qEEWc"}')
content = content.encode("utf-8")
return response(201, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1/access_tokens/1", method="delete")
def resp_revoke_project_access_tokens(url, request):
headers = {'content-type': 'application/json'}
content = ('')
content = content.encode("utf-8")
return response(204, content, headers, None, 5, request)
'''
HOOK API
'''

View file

@ -0,0 +1,107 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2023, Zoran Krleza (zoran.krleza@true-north.hr)
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
import gitlab
from ansible_collections.community.general.plugins.modules.gitlab_group_access_token import GitLabGroupAccessToken
# python-gitlab 3.1+ is needed for python-gitlab access tokens api
PYTHON_GITLAB_MINIMAL_VERSION = (3, 1)
def python_gitlab_version_match_requirement():
return tuple(map(int, gitlab.__version__.split('.'))) >= PYTHON_GITLAB_MINIMAL_VERSION
def _dummy(x):
"""Dummy function. Only used as a placeholder for toplevel definitions when the test is going
to be skipped anyway"""
return x
pytestmark = []
try:
from .gitlab import (GitlabModuleTestCase,
resp_get_user,
resp_get_group,
resp_list_group_access_tokens,
resp_create_group_access_tokens,
resp_revoke_group_access_tokens)
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
resp_list_group_access_tokens = _dummy
resp_create_group_access_tokens = _dummy
resp_revoke_group_access_tokens = _dummy
resp_get_user = _dummy
resp_get_group = _dummy
# Unit tests requirements
try:
from httmock import with_httmock # noqa
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing"))
with_httmock = _dummy
class TestGitlabGroupAccessToken(GitlabModuleTestCase):
@with_httmock(resp_get_user)
def setUp(self):
super(TestGitlabGroupAccessToken, self).setUp()
if not python_gitlab_version_match_requirement():
self.skipTest("python-gitlab %s+ is needed for gitlab_group_access_token" % ",".join(map(str, PYTHON_GITLAB_MINIMAL_VERSION)))
self.moduleUtil = GitLabGroupAccessToken(module=self.mock_module, gitlab_instance=self.gitlab_instance)
@with_httmock(resp_get_group)
@with_httmock(resp_list_group_access_tokens)
def test_find_access_token(self):
group = self.gitlab_instance.groups.get(1)
self.assertIsNotNone(group)
rvalue = self.moduleUtil.find_access_token(group, "token1")
self.assertEqual(rvalue, False)
self.assertIsNotNone(self.moduleUtil.access_token_object)
@with_httmock(resp_get_group)
@with_httmock(resp_list_group_access_tokens)
def test_find_access_token_negative(self):
groups = self.gitlab_instance.groups.get(1)
self.assertIsNotNone(groups)
rvalue = self.moduleUtil.find_access_token(groups, "nonexisting")
self.assertEqual(rvalue, False)
self.assertIsNone(self.moduleUtil.access_token_object)
@with_httmock(resp_get_group)
@with_httmock(resp_create_group_access_tokens)
def test_create_access_token(self):
groups = self.gitlab_instance.groups.get(1)
self.assertIsNotNone(groups)
rvalue = self.moduleUtil.create_access_token(groups, {'name': "tokenXYZ", 'scopes': ["api"], 'access_level': 20, 'expires_at': "2024-12-31"})
self.assertEqual(rvalue, True)
self.assertIsNotNone(self.moduleUtil.access_token_object)
@with_httmock(resp_get_group)
@with_httmock(resp_list_group_access_tokens)
@with_httmock(resp_revoke_group_access_tokens)
def test_revoke_access_token(self):
groups = self.gitlab_instance.groups.get(1)
self.assertIsNotNone(groups)
rvalue = self.moduleUtil.find_access_token(groups, "token1")
self.assertEqual(rvalue, False)
self.assertIsNotNone(self.moduleUtil.access_token_object)
rvalue = self.moduleUtil.revoke_access_token()
self.assertEqual(rvalue, True)

View file

@ -0,0 +1,107 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2023, Zoran Krleza (zoran.krleza@true-north.hr)
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
import gitlab
from ansible_collections.community.general.plugins.modules.gitlab_project_access_token import GitLabProjectAccessToken
# python-gitlab 3.1+ is needed for python-gitlab access tokens api
PYTHON_GITLAB_MINIMAL_VERSION = (3, 1)
def python_gitlab_version_match_requirement():
return tuple(map(int, gitlab.__version__.split('.'))) >= PYTHON_GITLAB_MINIMAL_VERSION
def _dummy(x):
"""Dummy function. Only used as a placeholder for toplevel definitions when the test is going
to be skipped anyway"""
return x
pytestmark = []
try:
from .gitlab import (GitlabModuleTestCase,
resp_get_user,
resp_get_project,
resp_list_project_access_tokens,
resp_create_project_access_tokens,
resp_revoke_project_access_tokens)
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
resp_list_project_access_tokens = _dummy
resp_create_project_access_tokens = _dummy
resp_revoke_project_access_tokens = _dummy
resp_get_user = _dummy
resp_get_project = _dummy
# Unit tests requirements
try:
from httmock import with_httmock # noqa
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing"))
with_httmock = _dummy
class TestGitlabProjectAccessToken(GitlabModuleTestCase):
@with_httmock(resp_get_user)
def setUp(self):
super(TestGitlabProjectAccessToken, self).setUp()
if not python_gitlab_version_match_requirement():
self.skipTest("python-gitlab %s+ is needed for gitlab_project_access_token" % ",".join(map(str, PYTHON_GITLAB_MINIMAL_VERSION)))
self.moduleUtil = GitLabProjectAccessToken(module=self.mock_module, gitlab_instance=self.gitlab_instance)
@with_httmock(resp_get_project)
@with_httmock(resp_list_project_access_tokens)
def test_find_access_token(self):
project = self.gitlab_instance.projects.get(1)
self.assertIsNotNone(project)
rvalue = self.moduleUtil.find_access_token(project, "token1")
self.assertEqual(rvalue, False)
self.assertIsNotNone(self.moduleUtil.access_token_object)
@with_httmock(resp_get_project)
@with_httmock(resp_list_project_access_tokens)
def test_find_access_token_negative(self):
project = self.gitlab_instance.projects.get(1)
self.assertIsNotNone(project)
rvalue = self.moduleUtil.find_access_token(project, "nonexisting")
self.assertEqual(rvalue, False)
self.assertIsNone(self.moduleUtil.access_token_object)
@with_httmock(resp_get_project)
@with_httmock(resp_create_project_access_tokens)
def test_create_access_token(self):
project = self.gitlab_instance.projects.get(1)
self.assertIsNotNone(project)
rvalue = self.moduleUtil.create_access_token(project, {'name': "tokenXYZ", 'scopes': ["api"], 'access_level': 20, 'expires_at': "2024-12-31"})
self.assertEqual(rvalue, True)
self.assertIsNotNone(self.moduleUtil.access_token_object)
@with_httmock(resp_get_project)
@with_httmock(resp_list_project_access_tokens)
@with_httmock(resp_revoke_project_access_tokens)
def test_revoke_access_token(self):
project = self.gitlab_instance.projects.get(1)
self.assertIsNotNone(project)
rvalue = self.moduleUtil.find_access_token(project, "token1")
self.assertEqual(rvalue, False)
self.assertIsNotNone(self.moduleUtil.access_token_object)
rvalue = self.moduleUtil.revoke_access_token()
self.assertEqual(rvalue, True)