2020-03-09 10:11:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
|
|
|
|
# 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
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-06-24 21:50:36 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-11-02 21:42:29 +01:00
|
|
|
from ansible_collections.community.general.plugins.modules.gitlab_deploy_key import GitLabDeployKey
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2020-03-24 09:27:28 +01:00
|
|
|
from .gitlab import (GitlabModuleTestCase,
|
2020-03-25 12:43:51 +01:00
|
|
|
python_version_match_requirement,
|
|
|
|
resp_get_project, resp_find_project_deploy_key,
|
|
|
|
resp_create_project_deploy_key, resp_delete_project_deploy_key)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# GitLab module requirements
|
|
|
|
if python_version_match_requirement():
|
|
|
|
from gitlab.v4.objects import ProjectKey
|
|
|
|
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_get_project = _dummy
|
|
|
|
resp_find_project_deploy_key = _dummy
|
|
|
|
resp_create_project_deploy_key = _dummy
|
|
|
|
resp_delete_project_deploy_key = _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 TestGitlabDeployKey(GitlabModuleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(TestGitlabDeployKey, self).setUp()
|
|
|
|
|
|
|
|
self.moduleUtil = GitLabDeployKey(module=self.mock_module, gitlab_instance=self.gitlab_instance)
|
|
|
|
|
|
|
|
@with_httmock(resp_get_project)
|
|
|
|
@with_httmock(resp_find_project_deploy_key)
|
|
|
|
def test_deploy_key_exist(self):
|
|
|
|
project = self.gitlab_instance.projects.get(1)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
rvalue = self.moduleUtil.exists_deploy_key(project, "Public key")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, True)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
rvalue = self.moduleUtil.exists_deploy_key(project, "Private key")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, False)
|
|
|
|
|
|
|
|
@with_httmock(resp_get_project)
|
|
|
|
@with_httmock(resp_create_project_deploy_key)
|
|
|
|
def test_create_deploy_key(self):
|
|
|
|
project = self.gitlab_instance.projects.get(1)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
deploy_key = self.moduleUtil.create_deploy_key(project, {"title": "Public key",
|
|
|
|
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM"
|
|
|
|
"4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxc"
|
|
|
|
"KDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfD"
|
|
|
|
"zpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(type(deploy_key), ProjectKey)
|
|
|
|
self.assertEqual(deploy_key.title, "Public key")
|
|
|
|
|
|
|
|
@with_httmock(resp_get_project)
|
|
|
|
@with_httmock(resp_find_project_deploy_key)
|
|
|
|
@with_httmock(resp_create_project_deploy_key)
|
|
|
|
def test_update_deploy_key(self):
|
|
|
|
project = self.gitlab_instance.projects.get(1)
|
2021-11-16 13:01:32 +01:00
|
|
|
deploy_key = self.moduleUtil.find_deploy_key(project, "Public key")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
changed, newDeploy_key = self.moduleUtil.update_deploy_key(deploy_key, {"title": "Private key"})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(changed, True)
|
|
|
|
self.assertEqual(type(newDeploy_key), ProjectKey)
|
|
|
|
self.assertEqual(newDeploy_key.title, "Private key")
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
changed, newDeploy_key = self.moduleUtil.update_deploy_key(deploy_key, {"title": "Private key"})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(changed, False)
|
|
|
|
self.assertEqual(newDeploy_key.title, "Private key")
|
|
|
|
|
|
|
|
@with_httmock(resp_get_project)
|
|
|
|
@with_httmock(resp_find_project_deploy_key)
|
|
|
|
@with_httmock(resp_delete_project_deploy_key)
|
|
|
|
def test_delete_deploy_key(self):
|
|
|
|
project = self.gitlab_instance.projects.get(1)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
self.moduleUtil.exists_deploy_key(project, "Public key")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
rvalue = self.moduleUtil.delete_deploy_key()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, None)
|