mirror of
https://github.com/f-droid/fdroidserver.git
synced 2025-09-13 22:42:29 +03:00
🎡 add unit tests for github.py
add unittests for our github api calls
This commit is contained in:
parent
1b19293ab0
commit
44b0af933d
3 changed files with 175 additions and 3 deletions
|
@ -85,7 +85,7 @@ class GithubApi:
|
||||||
with urllib.request.urlopen(req) as resp:
|
with urllib.request.urlopen(req) as resp:
|
||||||
refs = json.load(resp)
|
refs = json.load(resp)
|
||||||
for ref in refs:
|
for ref in refs:
|
||||||
r = ref['ref']
|
r = ref.get('ref', '')
|
||||||
if r.startswith('refs/tags/'):
|
if r.startswith('refs/tags/'):
|
||||||
tags.append(r[10:])
|
tags.append(r[10:])
|
||||||
return tags
|
return tags
|
||||||
|
|
164
tests/github.TestCase
Executable file
164
tests/github.TestCase
Executable file
|
@ -0,0 +1,164 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import inspect
|
||||||
|
import optparse
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest.mock
|
||||||
|
import testcommon
|
||||||
|
|
||||||
|
localmodule = os.path.realpath(
|
||||||
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
|
||||||
|
)
|
||||||
|
print('localmodule: ' + localmodule)
|
||||||
|
if localmodule not in sys.path:
|
||||||
|
sys.path.insert(0, localmodule)
|
||||||
|
|
||||||
|
import fdroidserver.github
|
||||||
|
import fdroidserver.common
|
||||||
|
|
||||||
|
|
||||||
|
class GithubApiTest(unittest.TestCase):
|
||||||
|
def test__init(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
self.assertEqual(api._api_token, 'faketoken')
|
||||||
|
self.assertEqual(api._repo_path, 'fakerepopath')
|
||||||
|
|
||||||
|
def test__req(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
r = api._req('https://fakeurl', data='fakedata')
|
||||||
|
self.assertEqual(r.full_url, 'https://fakeurl')
|
||||||
|
self.assertEqual(r.data, "fakedata")
|
||||||
|
self.assertDictEqual(
|
||||||
|
r.headers,
|
||||||
|
{
|
||||||
|
'Accept': 'application/vnd.github+json',
|
||||||
|
'Authorization': 'Bearer faketoken',
|
||||||
|
'X-github-api-version': '2022-11-28',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_list_released_tags(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
uomock = testcommon.mock_urlopen(
|
||||||
|
body='[{"tag_name": "fake"}, {"tag_name": "double_fake"}]'
|
||||||
|
)
|
||||||
|
with unittest.mock.patch("urllib.request.urlopen", uomock):
|
||||||
|
result = api.list_released_tags()
|
||||||
|
self.assertListEqual(result, ['fake', 'double_fake'])
|
||||||
|
|
||||||
|
def test_list_unreleased_tags(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
|
||||||
|
api.list_all_tags = unittest.mock.Mock(return_value=[1, 2, 3, 4])
|
||||||
|
api.list_released_tags = unittest.mock.Mock(return_value=[1, 2])
|
||||||
|
|
||||||
|
result = api.list_unreleased_tags()
|
||||||
|
|
||||||
|
self.assertListEqual(result, [3, 4])
|
||||||
|
|
||||||
|
def test_tag_exists(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
uomock = testcommon.mock_urlopen(body='[{"ref": "refs/tags/fake_tag"}]')
|
||||||
|
with unittest.mock.patch("urllib.request.urlopen", uomock):
|
||||||
|
result = api.tag_exists('fake_tag')
|
||||||
|
self.assertTrue(result)
|
||||||
|
|
||||||
|
def test_tag_exists_failure(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
|
||||||
|
uomock = testcommon.mock_urlopen(body='[{"error": "failure"}]')
|
||||||
|
|
||||||
|
with unittest.mock.patch("urllib.request.urlopen", uomock):
|
||||||
|
success = api.tag_exists('fake_tag')
|
||||||
|
|
||||||
|
self.assertFalse(success)
|
||||||
|
|
||||||
|
def test_list_all_tags(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
|
||||||
|
uomock = testcommon.mock_urlopen(
|
||||||
|
body='[{"ref": "refs/tags/fake"}, {"ref": "refs/tags/double_fake"}]'
|
||||||
|
)
|
||||||
|
|
||||||
|
with unittest.mock.patch("urllib.request.urlopen", uomock):
|
||||||
|
result = api.list_all_tags()
|
||||||
|
|
||||||
|
self.assertListEqual(result, ['fake', 'double_fake'])
|
||||||
|
|
||||||
|
def test_create_release(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
|
||||||
|
uomock = testcommon.mock_urlopen(body='{"id": "fakeid"}')
|
||||||
|
api.tag_exists = lambda x: True
|
||||||
|
api._create_release_asset = unittest.mock.Mock()
|
||||||
|
|
||||||
|
with unittest.mock.patch("urllib.request.urlopen", uomock):
|
||||||
|
success = api.create_release('faketag', ['file_a', 'file_b'])
|
||||||
|
self.assertTrue(success)
|
||||||
|
|
||||||
|
req = uomock.call_args_list[0][0][0]
|
||||||
|
self.assertEqual(1, len(uomock.call_args_list))
|
||||||
|
self.assertEqual(2, len(uomock.call_args_list[0]))
|
||||||
|
self.assertEqual(1, len(uomock.call_args_list[0][0]))
|
||||||
|
self.assertEqual(
|
||||||
|
req.full_url,
|
||||||
|
'https://api.github.com/repos/fakerepopath/releases',
|
||||||
|
)
|
||||||
|
self.assertEqual(req.data, b'{"tag_name": "faketag"}')
|
||||||
|
self.assertListEqual(
|
||||||
|
api._create_release_asset.call_args_list,
|
||||||
|
[
|
||||||
|
unittest.mock.call('fakeid', 'file_a'),
|
||||||
|
unittest.mock.call('fakeid', 'file_b'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
def test__create_release_asset(self):
|
||||||
|
api = fdroidserver.github.GithubApi('faketoken', 'fakerepopath')
|
||||||
|
uomock = testcommon.mock_urlopen()
|
||||||
|
|
||||||
|
with unittest.mock.patch(
|
||||||
|
'fdroidserver.github.open',
|
||||||
|
unittest.mock.mock_open(read_data=b"fake_content"),
|
||||||
|
), unittest.mock.patch("urllib.request.urlopen", uomock):
|
||||||
|
success = api._create_release_asset('fake_id', 'fake_file')
|
||||||
|
|
||||||
|
self.assertTrue(success)
|
||||||
|
|
||||||
|
req = uomock.call_args_list[0][0][0]
|
||||||
|
self.assertEqual(1, len(uomock.call_args_list))
|
||||||
|
self.assertEqual(2, len(uomock.call_args_list[0]))
|
||||||
|
self.assertEqual(1, len(uomock.call_args_list[0][0]))
|
||||||
|
self.assertEqual(
|
||||||
|
req.full_url,
|
||||||
|
'https://uploads.github.com/repos/fakerepopath/releases/fake_id/assets?name=fake_file',
|
||||||
|
)
|
||||||
|
self.assertDictEqual(
|
||||||
|
req.headers,
|
||||||
|
{
|
||||||
|
"Accept": "application/vnd.github+json",
|
||||||
|
'Authorization': 'Bearer faketoken',
|
||||||
|
'Content-type': 'application/octet-stream',
|
||||||
|
'X-github-api-version': '2022-11-28',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(req.data, b'fake_content')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
os.chdir(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
parser = optparse.OptionParser()
|
||||||
|
parser.add_option(
|
||||||
|
"-v",
|
||||||
|
"--verbose",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Spew out even more information than normal",
|
||||||
|
)
|
||||||
|
(fdroidserver.common.options, args) = parser.parse_args(["--verbose"])
|
||||||
|
|
||||||
|
newSuite = unittest.TestSuite()
|
||||||
|
newSuite.addTest(unittest.makeSuite(GithubApiTest))
|
||||||
|
unittest.main(failfast=False)
|
|
@ -19,9 +19,9 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
import unittest.mock
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest import mock
|
|
||||||
|
|
||||||
|
|
||||||
class TmpCwd:
|
class TmpCwd:
|
||||||
|
@ -84,5 +84,13 @@ def parse_args_for_test(parser, args):
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if arg[0] == '-':
|
if arg[0] == '-':
|
||||||
flags.append(flags)
|
flags.append(flags)
|
||||||
with mock.patch('sys.argv', flags):
|
with unittest.mock.patch('sys.argv', flags):
|
||||||
parse_args(parser)
|
parse_args(parser)
|
||||||
|
|
||||||
|
|
||||||
|
def mock_urlopen(status=200, body=None):
|
||||||
|
resp = unittest.mock.MagicMock()
|
||||||
|
resp.getcode.return_value = status
|
||||||
|
resp.read.return_value = body
|
||||||
|
resp.__enter__.return_value = resp
|
||||||
|
return unittest.mock.Mock(return_value=resp)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue