mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-08 19:50:30 -07:00
parent
6b20aad2b6
commit
458648f8fb
4 changed files with 114 additions and 40 deletions
|
@ -1,7 +1,6 @@
|
||||||
# Copyright (c) 2024-2025, Austin Lucas Lake <git@austinlucaslake.com>
|
# Copyright (c) Ansible Project
|
||||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# 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
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
unsupported
|
|
||||||
azp/posix/1
|
azp/posix/1
|
||||||
destructive
|
destructive
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
# and should not be used as examples of how to write Ansible roles #
|
# and should not be used as examples of how to write Ansible roles #
|
||||||
####################################################################
|
####################################################################
|
||||||
|
|
||||||
# Test code for the github_gpg_key module.
|
# Test code for the github_issue module.
|
||||||
#
|
#
|
||||||
# Copyright (c) 2024-2025, Austin Lucas Lake <git@austinlucaslake.com>
|
# Copyright (c) 2024-2025, Austin Lucas Lake <git@austinlucaslake.com>
|
||||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
@ -13,18 +13,16 @@
|
||||||
github_gpg_key:
|
github_gpg_key:
|
||||||
token: "{{ token }}"
|
token: "{{ token }}"
|
||||||
name: "{{ name }}"
|
name: "{{ name }}"
|
||||||
armored_public_key: "{{ armored_public_key }}"
|
armored_gpg_key: "{{ armored_gpg_key }}"
|
||||||
register: key1
|
register: created_gpg_key
|
||||||
|
|
||||||
- name: Delete GPG key
|
- name: Delete GPG key
|
||||||
github_gpg_key:
|
github_gpg_key:
|
||||||
state: absent
|
state: absent
|
||||||
token: "{{ token }}"
|
token: "{{ token }}"
|
||||||
gpg_key_id: "{{ gpg_key_id }}"
|
name: "{{ name }}"
|
||||||
register: key2
|
register: deleted_gpg_key
|
||||||
|
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- key1.created_key.name == key2.deleted_key.name
|
- created_gpg_key.id == deleted_gpg_key[0].id
|
||||||
- key1.created_key.key_id == key2.deleted_key.key_id
|
|
||||||
- key1.created_key.raw_key == key2.deleted_key.raw_key
|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
---
|
---
|
||||||
# Copyright (c) 2024-2025, Austin Lucas Lake <git@austinlucaslake.com>
|
# Copyright (c) Ansible Project
|
||||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# 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
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
token: # TODO
|
token: # TODO
|
||||||
name: My GPG Key
|
name: # TODO
|
||||||
armored_public_key: # TODO
|
armored_public_key: # TODO
|
||||||
gpg_key_id: # TODO
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# Copyright (c) 2024-2025, Austin Lucas Lake <git@austinlucaslake.com>
|
# Copyright (c) 2024-2025, Austin Lucas Lake, <git@austinlucaslake.com>
|
||||||
# Based on tests/unit/plugins/modules/test_github_repo.py by Ansible Project
|
# Based on tests/unit/plugins/modules/test_github_repo.py by Ansible Project
|
||||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# 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
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
@ -9,6 +9,7 @@ __metaclass__ = type
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
from httmock import with_httmock, urlmatch, response
|
from httmock import with_httmock, urlmatch, response
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||||
from ansible_collections.community.general.plugins.modules import github_gpg_key
|
from ansible_collections.community.general.plugins.modules import github_gpg_key
|
||||||
|
|
||||||
|
@ -20,6 +21,48 @@ def debug_mock(url, request):
|
||||||
print(request.original.__dict__)
|
print(request.original.__dict__)
|
||||||
|
|
||||||
|
|
||||||
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/gpg_keys', method="get")
|
||||||
|
def list_gpg_keys_mock(url, request):
|
||||||
|
# https://docs.github.com/en/rest/reference/repos#get-a-repository
|
||||||
|
headers = {'content-type': 'application/json'}
|
||||||
|
content = [{
|
||||||
|
"id": 3,
|
||||||
|
"name": "Octocat's GPG Key",
|
||||||
|
"primary_key_id": 2,
|
||||||
|
"key_id": "3262EFF25BA0D270",
|
||||||
|
"public_key": "xsBNBFayYZ...",
|
||||||
|
"emails": [{
|
||||||
|
"email": "octocat@users.noreply.github.com",
|
||||||
|
"verified": True
|
||||||
|
}],
|
||||||
|
"subkeys": [{
|
||||||
|
"id": 4,
|
||||||
|
"primary_key_id": 3,
|
||||||
|
"key_id": "4A595D4C72EE49C7",
|
||||||
|
"public_key": "zsBNBFayYZ...",
|
||||||
|
"emails": [],
|
||||||
|
"can_sign": False,
|
||||||
|
"can_encrypt_comms": True,
|
||||||
|
"can_encrypt_storage": True,
|
||||||
|
"can_certify": False,
|
||||||
|
"created_at": "2016-03-24T11:31:04-06:00",
|
||||||
|
"expires_at": "2016-03-24T11:31:04-07:00",
|
||||||
|
"revoked": False
|
||||||
|
}],
|
||||||
|
"can_sign": True,
|
||||||
|
"can_encrypt_comms": False,
|
||||||
|
"can_encrypt_storage": False,
|
||||||
|
"can_certify": True,
|
||||||
|
"created_at": "2016-03-24T11:31:04-06:00",
|
||||||
|
"expires_at": "2016-03-24T11:31:04-07:00",
|
||||||
|
"revoked": False,
|
||||||
|
"raw_key": "string"
|
||||||
|
}]
|
||||||
|
|
||||||
|
content = json.dumps(content).encode("utf-8")
|
||||||
|
return response(200, content, headers, None, 5, request)
|
||||||
|
|
||||||
|
|
||||||
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/gpg_keys', method="post")
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/gpg_keys', method="post")
|
||||||
def create_gpg_key_mock(url, request):
|
def create_gpg_key_mock(url, request):
|
||||||
gpg_key = json.loads(request.body)
|
gpg_key = json.loads(request.body)
|
||||||
|
@ -30,7 +73,7 @@ def create_gpg_key_mock(url, request):
|
||||||
# https://docs.github.com/en/rest/reference/repos#create-a-repository-for-the-authenticated-user
|
# https://docs.github.com/en/rest/reference/repos#create-a-repository-for-the-authenticated-user
|
||||||
content = {
|
content = {
|
||||||
"id": 3,
|
"id": 3,
|
||||||
"name": gpg_key.get("name"),
|
"name": gpg_key["name"],
|
||||||
"primary_key_id": 2,
|
"primary_key_id": 2,
|
||||||
"key_id": "3262EFF25BA0D270",
|
"key_id": "3262EFF25BA0D270",
|
||||||
"public_key": "xsBNBFayYZ...",
|
"public_key": "xsBNBFayYZ...",
|
||||||
|
@ -68,42 +111,77 @@ def create_gpg_key_mock(url, request):
|
||||||
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/gpg_keys/.*', method="delete")
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/gpg_keys/.*', method="delete")
|
||||||
def delete_gpg_key_mock(url, request):
|
def delete_gpg_key_mock(url, request):
|
||||||
# https://docs.github.com/en/rest/users/gpg-keys#delete-a-gpg-key-for-the-authenticated-user
|
# https://docs.github.com/en/rest/users/gpg-keys#delete-a-gpg-key-for-the-authenticated-user
|
||||||
return response(200, None, None, None, 5, request)
|
return response(204, None, None, None, 5, request)
|
||||||
|
|
||||||
|
|
||||||
class TestGithubGPGKey(unittest.TestCase):
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/gpg_keys/.*', method="delete")
|
||||||
|
def delete_gpg_key_notfound_mock(url, request):
|
||||||
|
# https://docs.github.com/en/rest/users/gpg-keys#delete-a-gpg-key-for-the-authenticated-user
|
||||||
|
return response(404, "{\"message\": \"Not Found\"}", "", "Not Found", 5, request)
|
||||||
|
|
||||||
|
|
||||||
|
class TestGithubRepo(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.token = "github_access_token"
|
self.module = AnsibleModule(
|
||||||
self.name = "GPG public key"
|
argument_spec=dict(
|
||||||
self.armored_public_key = "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\n\nMy ASCII-armored GPG public key\r\n-----END PGP PUBLIC KEY BLOCK-----"
|
state=dict(type='str', default='present', choice=['present', 'absent']),
|
||||||
self.gpg_key_id = 123456789
|
token=dict(type='str', required=True, no_log=True),
|
||||||
|
name=dict(type='str', required=True),
|
||||||
@with_httmock(create_gpg_key_mock)
|
armored_public_key=dict(type='str', no_log=True),
|
||||||
def test_create_gpg_key(self):
|
force=dict(type='bool', default=True)
|
||||||
result = github_gpg_key.run_module(
|
),
|
||||||
params=dict(
|
supports_check_mode=True,
|
||||||
state="present",
|
required_if=[
|
||||||
token=self.token,
|
['state', 'present', ['armored_public_key']],
|
||||||
name=self.name,
|
]
|
||||||
armored_public_key=self.armored_public_key
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self.assertEqual(result['changed'], True)
|
|
||||||
self.assertEqual(result['key']['name'], self.name)
|
|
||||||
self.assertEqual(result['key']['raw_key'], self.armored_public_key)
|
|
||||||
|
|
||||||
@with_httmock(delete_gpg_key_mock)
|
@with_httmock(list_gpg_keys_mock)
|
||||||
def test_delete_gpg_key(self):
|
@with_httmock(create_gpg_key_mock)
|
||||||
|
def test_create_gpg_key_repo(self):
|
||||||
result = github_gpg_key.run_module(
|
result = github_gpg_key.run_module(
|
||||||
module=self.module,
|
module=self.module,
|
||||||
params=dict(
|
params=dict(
|
||||||
state="absent",
|
token="github_access_token",
|
||||||
token=self.token,
|
name="GPG public key",
|
||||||
gpg_key_id=self.gpg_key_id
|
armored_public_key="-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\n\nMy ASCII-armored GPG public key\r\n-----END PGP PUBLIC KEY BLOCK-----",
|
||||||
|
state="present",
|
||||||
|
force=True
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(result['changed'], True)
|
self.assertEqual(result['changed'], True)
|
||||||
self.assertEqual(result['key']['gpg_key_id'], self.gpg_key_id)
|
self.assertEqual(result['key']['name'], 'GPG public key')
|
||||||
|
|
||||||
|
@with_httmock(list_gpg_keys_mock)
|
||||||
|
@with_httmock(delete_gpg_key_mock)
|
||||||
|
def test_delete_user_repo(self):
|
||||||
|
result = github_gpg_key.run_module(
|
||||||
|
module=self.module,
|
||||||
|
params=dict(
|
||||||
|
token="github_access_token",
|
||||||
|
name="GPG public key",
|
||||||
|
armored_public_key="-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\n\nMy ASCII-armored GPG public key\r\n-----END PGP PUBLIC KEY BLOCK-----",
|
||||||
|
state="absent",
|
||||||
|
force=True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(result['changed'], True)
|
||||||
|
|
||||||
|
@with_httmock(list_gpg_keys_mock)
|
||||||
|
@with_httmock(delete_gpg_key_notfound_mock)
|
||||||
|
def test_delete_gpg_key_notfound(self):
|
||||||
|
result = github_gpg_key.run_module(
|
||||||
|
module=self.module,
|
||||||
|
params=dict(
|
||||||
|
token="github_access_token",
|
||||||
|
name="GPG public key",
|
||||||
|
armored_public_key="-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\n\nMy ASCII-armored GPG public key\r\n-----END PGP PUBLIC KEY BLOCK-----",
|
||||||
|
state="absent",
|
||||||
|
force=True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(result['changed'], False)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue