2022-08-05 14:03:38 +02:00
|
|
|
# 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)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-03-06 13:30:43 +01:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import re
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
from httmock import with_httmock, urlmatch, response
|
|
|
|
from ansible_collections.community.general.tests.unit.compat import unittest
|
2022-11-02 21:42:29 +01:00
|
|
|
from ansible_collections.community.general.plugins.modules import github_repo
|
2021-03-06 13:30:43 +01:00
|
|
|
|
|
|
|
GITHUB_MINIMUM_PYTHON_VERSION = (2, 7)
|
|
|
|
|
|
|
|
|
|
|
|
@urlmatch(netloc=r'.*')
|
|
|
|
def debug_mock(url, request):
|
|
|
|
print(request.original.__dict__)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/orgs/.*', method="get")
|
2021-03-06 13:30:43 +01:00
|
|
|
def get_orgs_mock(url, request):
|
2021-04-09 06:00:21 +02:00
|
|
|
match = re.search(r"api\.github\.com(:[0-9]+)?/orgs/(?P<org>[^/]+)", request.url)
|
2021-03-06 13:30:43 +01:00
|
|
|
org = match.group("org")
|
|
|
|
|
|
|
|
# https://docs.github.com/en/rest/reference/orgs#get-an-organization
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
content = {
|
|
|
|
"login": org,
|
2021-04-09 06:00:21 +02:00
|
|
|
"url": "https://api.github.com/orgs/{0}".format(org)
|
2021-03-06 13:30:43 +01:00
|
|
|
}
|
|
|
|
content = json.dumps(content).encode("utf-8")
|
|
|
|
return response(200, content, headers, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user', method="get")
|
2021-03-06 13:30:43 +01:00
|
|
|
def get_user_mock(url, request):
|
|
|
|
# https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
content = {
|
|
|
|
"login": "octocat",
|
2021-04-09 06:00:21 +02:00
|
|
|
"url": "https://api.github.com/users/octocat"
|
2021-03-06 13:30:43 +01:00
|
|
|
}
|
|
|
|
content = json.dumps(content).encode("utf-8")
|
|
|
|
return response(200, content, headers, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/repos/.*/.*', method="get")
|
2021-03-06 13:30:43 +01:00
|
|
|
def get_repo_notfound_mock(url, request):
|
|
|
|
return response(404, "{\"message\": \"Not Found\"}", "", "Not Found", 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/repos/.*/.*', method="get")
|
2021-03-06 13:30:43 +01:00
|
|
|
def get_repo_mock(url, request):
|
|
|
|
match = re.search(
|
2021-04-09 06:00:21 +02:00
|
|
|
r"api\.github\.com(:[0-9]+)?/repos/(?P<org>[^/]+)/(?P<repo>[^/]+)", request.url)
|
2021-03-06 13:30:43 +01:00
|
|
|
org = match.group("org")
|
|
|
|
repo = match.group("repo")
|
|
|
|
|
|
|
|
# https://docs.github.com/en/rest/reference/repos#get-a-repository
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
content = {
|
|
|
|
"name": repo,
|
|
|
|
"full_name": "{0}/{1}".format(org, repo),
|
2021-04-09 06:00:21 +02:00
|
|
|
"url": "https://api.github.com/repos/{0}/{1}".format(org, repo),
|
2021-03-06 13:30:43 +01:00
|
|
|
"private": False,
|
|
|
|
"description": "This your first repo!",
|
|
|
|
"default_branch": "master",
|
|
|
|
"allow_rebase_merge": True
|
|
|
|
}
|
|
|
|
content = json.dumps(content).encode("utf-8")
|
|
|
|
return response(200, content, headers, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-11-22 07:11:26 +01:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/repos/.*/.*', method="get")
|
|
|
|
def get_private_repo_mock(url, request):
|
|
|
|
match = re.search(
|
|
|
|
r"api\.github\.com(:[0-9]+)?/repos/(?P<org>[^/]+)/(?P<repo>[^/]+)", request.url)
|
|
|
|
org = match.group("org")
|
|
|
|
repo = match.group("repo")
|
|
|
|
|
|
|
|
# https://docs.github.com/en/rest/reference/repos#get-a-repository
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
content = {
|
|
|
|
"name": repo,
|
|
|
|
"full_name": "{0}/{1}".format(org, repo),
|
|
|
|
"url": "https://api.github.com/repos/{0}/{1}".format(org, repo),
|
|
|
|
"private": True,
|
|
|
|
"description": "This your first repo!",
|
|
|
|
"default_branch": "master",
|
|
|
|
"allow_rebase_merge": True
|
|
|
|
}
|
|
|
|
content = json.dumps(content).encode("utf-8")
|
|
|
|
return response(200, content, headers, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/orgs/.*/repos', method="post")
|
2021-03-06 13:30:43 +01:00
|
|
|
def create_new_org_repo_mock(url, request):
|
|
|
|
match = re.search(
|
2021-04-09 06:00:21 +02:00
|
|
|
r"api\.github\.com(:[0-9]+)?/orgs/(?P<org>[^/]+)/repos", request.url)
|
2021-03-06 13:30:43 +01:00
|
|
|
org = match.group("org")
|
|
|
|
repo = json.loads(request.body)
|
|
|
|
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
# https://docs.github.com/en/rest/reference/repos#create-an-organization-repository
|
|
|
|
content = {
|
|
|
|
"name": repo['name'],
|
|
|
|
"full_name": "{0}/{1}".format(org, repo['name']),
|
2021-11-22 07:11:26 +01:00
|
|
|
"private": repo.get('private', False),
|
|
|
|
"description": repo.get('description')
|
2021-03-06 13:30:43 +01:00
|
|
|
}
|
|
|
|
content = json.dumps(content).encode("utf-8")
|
|
|
|
return response(201, content, headers, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/user/repos', method="post")
|
2021-03-06 13:30:43 +01:00
|
|
|
def create_new_user_repo_mock(url, request):
|
|
|
|
repo = json.loads(request.body)
|
|
|
|
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
# https://docs.github.com/en/rest/reference/repos#create-a-repository-for-the-authenticated-user
|
|
|
|
content = {
|
|
|
|
"name": repo['name'],
|
|
|
|
"full_name": "{0}/{1}".format("octocat", repo['name']),
|
2021-11-22 07:11:26 +01:00
|
|
|
"private": repo.get('private', False),
|
|
|
|
"description": repo.get('description')
|
2021-03-06 13:30:43 +01:00
|
|
|
}
|
|
|
|
content = json.dumps(content).encode("utf-8")
|
|
|
|
return response(201, content, headers, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/repos/.*/.*', method="patch")
|
2021-03-06 13:30:43 +01:00
|
|
|
def patch_repo_mock(url, request):
|
|
|
|
match = re.search(
|
2021-04-09 06:00:21 +02:00
|
|
|
r"api\.github\.com(:[0-9]+)?/repos/(?P<org>[^/]+)/(?P<repo>[^/]+)", request.url)
|
2021-03-06 13:30:43 +01:00
|
|
|
org = match.group("org")
|
|
|
|
repo = match.group("repo")
|
|
|
|
|
|
|
|
body = json.loads(request.body)
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
# https://docs.github.com/en/rest/reference/repos#update-a-repository
|
|
|
|
content = {
|
|
|
|
"name": repo,
|
|
|
|
"full_name": "{0}/{1}".format(org, repo),
|
2021-04-09 06:00:21 +02:00
|
|
|
"url": "https://api.github.com/repos/{0}/{1}".format(org, repo),
|
2021-11-22 07:11:26 +01:00
|
|
|
"private": body.get('private', False),
|
|
|
|
"description": body.get('description'),
|
2021-03-06 13:30:43 +01:00
|
|
|
"default_branch": "master",
|
|
|
|
"allow_rebase_merge": True
|
|
|
|
}
|
|
|
|
content = json.dumps(content).encode("utf-8")
|
|
|
|
return response(200, content, headers, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/repos/.*/.*', method="delete")
|
2021-03-06 13:30:43 +01:00
|
|
|
def delete_repo_mock(url, request):
|
|
|
|
# https://docs.github.com/en/rest/reference/repos#delete-a-repository
|
|
|
|
return response(204, None, None, None, 5, request)
|
|
|
|
|
|
|
|
|
2021-04-09 06:00:21 +02:00
|
|
|
@urlmatch(netloc=r'api\.github\.com(:[0-9]+)?$', path=r'/repos/.*/.*', method="delete")
|
2021-03-06 13:30:43 +01:00
|
|
|
def delete_repo_notfound_mock(url, request):
|
|
|
|
# https://docs.github.com/en/rest/reference/repos#delete-a-repository
|
|
|
|
return response(404, "{\"message\": \"Not Found\"}", "", "Not Found", 5, request)
|
|
|
|
|
|
|
|
|
|
|
|
class TestGithubRepo(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
if sys.version_info < GITHUB_MINIMUM_PYTHON_VERSION:
|
|
|
|
self.skipTest("Python %s+ is needed for PyGithub" %
|
|
|
|
",".join(map(str, GITHUB_MINIMUM_PYTHON_VERSION)))
|
|
|
|
|
|
|
|
@with_httmock(get_orgs_mock)
|
|
|
|
@with_httmock(get_repo_notfound_mock)
|
|
|
|
@with_httmock(create_new_org_repo_mock)
|
|
|
|
def test_create_new_org_repo(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": "MyOrganization",
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": "Just for fun",
|
|
|
|
"private": False,
|
2021-07-22 16:55:09 +02:00
|
|
|
"state": "present",
|
2021-11-22 07:11:26 +01:00
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
2021-03-06 13:30:43 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
self.assertEqual(result['repo']['private'], False)
|
2021-11-22 07:11:26 +01:00
|
|
|
self.assertEqual(result['repo']['description'], 'Just for fun')
|
|
|
|
|
|
|
|
@with_httmock(get_orgs_mock)
|
|
|
|
@with_httmock(get_repo_notfound_mock)
|
|
|
|
@with_httmock(create_new_org_repo_mock)
|
|
|
|
def test_create_new_org_repo_incomplete(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": "MyOrganization",
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": None,
|
|
|
|
"private": None,
|
|
|
|
"state": "present",
|
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
self.assertEqual(result['repo']['private'], False)
|
|
|
|
self.assertEqual(result['repo']['description'], None)
|
2021-03-06 13:30:43 +01:00
|
|
|
|
|
|
|
@with_httmock(get_user_mock)
|
|
|
|
@with_httmock(get_repo_notfound_mock)
|
|
|
|
@with_httmock(create_new_user_repo_mock)
|
|
|
|
def test_create_new_user_repo(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": None,
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": "Just for fun",
|
|
|
|
"private": True,
|
2021-07-22 16:55:09 +02:00
|
|
|
"state": "present",
|
2021-11-22 07:11:26 +01:00
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
2021-03-06 13:30:43 +01:00
|
|
|
})
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
self.assertEqual(result['repo']['private'], True)
|
|
|
|
|
|
|
|
@with_httmock(get_orgs_mock)
|
|
|
|
@with_httmock(get_repo_mock)
|
|
|
|
@with_httmock(patch_repo_mock)
|
|
|
|
def test_patch_existing_org_repo(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": "MyOrganization",
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": "Just for fun",
|
|
|
|
"private": True,
|
2021-07-22 16:55:09 +02:00
|
|
|
"state": "present",
|
2021-11-22 07:11:26 +01:00
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
2021-03-06 13:30:43 +01:00
|
|
|
})
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
self.assertEqual(result['repo']['private'], True)
|
|
|
|
|
2021-11-22 07:11:26 +01:00
|
|
|
@with_httmock(get_orgs_mock)
|
|
|
|
@with_httmock(get_private_repo_mock)
|
|
|
|
def test_idempotency_existing_org_private_repo(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": "MyOrganization",
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": None,
|
|
|
|
"private": None,
|
|
|
|
"state": "present",
|
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
|
|
|
})
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
self.assertEqual(result['repo']['private'], True)
|
|
|
|
self.assertEqual(result['repo']['description'], 'This your first repo!')
|
|
|
|
|
2021-03-06 13:30:43 +01:00
|
|
|
@with_httmock(get_orgs_mock)
|
|
|
|
@with_httmock(get_repo_mock)
|
|
|
|
@with_httmock(delete_repo_mock)
|
|
|
|
def test_delete_org_repo(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": "MyOrganization",
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": "Just for fun",
|
|
|
|
"private": False,
|
2021-07-22 16:55:09 +02:00
|
|
|
"state": "absent",
|
2021-11-22 07:11:26 +01:00
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
2021-03-06 13:30:43 +01:00
|
|
|
})
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
|
|
|
|
@with_httmock(get_user_mock)
|
|
|
|
@with_httmock(get_repo_mock)
|
|
|
|
@with_httmock(delete_repo_mock)
|
|
|
|
def test_delete_user_repo(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": None,
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": "Just for fun",
|
|
|
|
"private": False,
|
2021-07-22 16:55:09 +02:00
|
|
|
"state": "absent",
|
2021-11-22 07:11:26 +01:00
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
2021-03-06 13:30:43 +01:00
|
|
|
})
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
|
|
|
|
@with_httmock(get_orgs_mock)
|
|
|
|
@with_httmock(get_repo_notfound_mock)
|
|
|
|
@with_httmock(delete_repo_notfound_mock)
|
|
|
|
def test_delete_org_repo_notfound(self):
|
|
|
|
result = github_repo.run_module({
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
"access_token": "mytoken",
|
|
|
|
"organization": "MyOrganization",
|
|
|
|
"name": "myrepo",
|
|
|
|
"description": "Just for fun",
|
|
|
|
"private": True,
|
2021-07-22 16:55:09 +02:00
|
|
|
"state": "absent",
|
2021-11-22 07:11:26 +01:00
|
|
|
"api_url": "https://api.github.com",
|
|
|
|
"force_defaults": False,
|
2021-03-06 13:30:43 +01:00
|
|
|
})
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|