1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-11-14 11:00:10 +01:00
fdroidserver/tests/checkupdates.TestCase

239 lines
9.1 KiB
Plaintext
Raw Normal View History

2019-07-23 17:28:19 +02:00
#!/usr/bin/env python3
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
import logging
import optparse
import os
import sys
import unittest
from unittest import mock
from pathlib import Path
2019-07-23 17:28:19 +02:00
localmodule = Path(__file__).resolve().parent.parent
print('localmodule: ' + str(localmodule))
2019-07-23 17:28:19 +02:00
if localmodule not in sys.path:
2021-06-07 17:27:30 +02:00
sys.path.insert(0, str(localmodule))
2019-07-23 17:28:19 +02:00
import fdroidserver.checkupdates
import fdroidserver.metadata
from fdroidserver.exception import FDroidException
2019-07-23 17:28:19 +02:00
class CheckupdatesTest(unittest.TestCase):
'''fdroidserver/checkupdates.py'''
2019-07-23 17:28:19 +02:00
def setUp(self):
logging.basicConfig(level=logging.DEBUG)
self.basedir = localmodule / 'tests'
self.tmpdir = localmodule / '.testfiles'
self.tmpdir.mkdir(exist_ok=True)
# TODO: Python3.6: Accepts a path-like object.
os.chdir(str(self.basedir))
2019-07-23 17:28:19 +02:00
def test_autoupdatemode_no_suffix(self):
fdroidserver.checkupdates.options = mock.Mock()
fdroidserver.checkupdates.options.auto = 'bleh'
fdroidserver.checkupdates.config = {}
app = fdroidserver.metadata.App()
app.id = 'loop.starts.shooting'
app.metadatapath = 'metadata/' + app.id + '.yml'
app.CurrentVersion = '1.1.8-fdroid'
app.CurrentVersionCode = 10108
app.UpdateCheckMode = 'HTTP'
app.AutoUpdateMode = 'Version %v'
build = fdroidserver.metadata.Build()
build.versionCode = app.CurrentVersionCode
build.versionName = app.CurrentVersion
app['Builds'].append(build)
with mock.patch(
'fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)
):
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
with mock.patch('subprocess.call', lambda cmd: 0):
fdroidserver.checkupdates.checkupdates_app(app)
build = app['Builds'][-1]
self.assertEqual(build.versionName, '1.1.9')
self.assertEqual(build.commit, '1.1.9')
def test_autoupdatemode_suffix(self):
fdroidserver.checkupdates.options = mock.Mock()
fdroidserver.checkupdates.options.auto = 'bleh'
fdroidserver.checkupdates.config = {}
app = fdroidserver.metadata.App()
app.id = 'loop.starts.shooting'
app.metadatapath = 'metadata/' + app.id + '.yml'
app.CurrentVersion = '1.1.8-fdroid'
app.CurrentVersionCode = 10108
app.UpdateCheckMode = 'HTTP'
app.AutoUpdateMode = r'Version +.%c-fdroid v%v_%c'
build = fdroidserver.metadata.Build()
build.versionCode = app.CurrentVersionCode
build.versionName = app.CurrentVersion
app['Builds'].append(build)
with mock.patch(
'fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)
):
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
with mock.patch('subprocess.call', lambda cmd: 0):
fdroidserver.checkupdates.checkupdates_app(app)
build = app['Builds'][-1]
self.assertEqual(build.versionName, '1.1.9.10109-fdroid')
self.assertEqual(build.commit, 'v1.1.9_10109')
2019-07-23 17:28:19 +02:00
def test_checkupdates_app_http(self):
fdroidserver.checkupdates.options = mock.Mock()
fdroidserver.checkupdates.options.auto = 'bleh'
fdroidserver.checkupdates.config = {}
app = fdroidserver.metadata.App()
app.id = 'loop.starts.shooting'
app.metadatapath = 'metadata/' + app.id + '.yml'
app.CurrentVersionCode = 10108
app.UpdateCheckMode = 'HTTP'
app.UpdateCheckData = 'mock'
with mock.patch(
'fdroidserver.checkupdates.check_http', lambda app: (None, 'bla')
):
2019-07-23 17:28:19 +02:00
fdroidserver.checkupdates.checkupdates_app(app)
with mock.patch(
'fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)
):
with mock.patch(
'fdroidserver.metadata.write_metadata', mock.Mock()
) as wrmock:
2019-07-23 17:28:19 +02:00
with mock.patch('subprocess.call', lambda cmd: 0):
fdroidserver.checkupdates.checkupdates_app(app)
wrmock.assert_called_with(app.metadatapath, app)
def test_checkupdates_app_tags(self):
fdroidserver.checkupdates.options = mock.Mock()
fdroidserver.checkupdates.options.auto = 'bleh'
fdroidserver.checkupdates.config = {}
app = fdroidserver.metadata.App()
app.id = 'loop.starts.shooting'
app.metadatapath = 'metadata/' + app.id + '.yml'
app.CurrentVersion = '1.1.8'
app.CurrentVersionCode = 10108
app.UpdateCheckMode = 'Tags'
app.AutoUpdateMode = 'Version'
build = fdroidserver.metadata.Build()
build.versionCode = app.CurrentVersionCode
build.versionName = app.CurrentVersion
app['Builds'].append(build)
with mock.patch(
'fdroidserver.checkupdates.check_tags',
lambda app, pattern: (None, 'bla', None),
):
fdroidserver.checkupdates.checkupdates_app(app)
with mock.patch(
'fdroidserver.checkupdates.check_tags',
lambda app, pattern: ('1.1.9', 10109, 'v1.1.9'),
):
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
with mock.patch('subprocess.call', lambda cmd: 0):
fdroidserver.checkupdates.checkupdates_app(app)
build = app['Builds'][-1]
self.assertEqual(build.versionName, '1.1.9')
self.assertEqual(build.commit, 'v1.1.9')
2019-07-23 20:45:04 +02:00
def test_check_http(self):
fdroidserver.checkupdates.options = mock.Mock()
app = fdroidserver.metadata.App()
app.id = 'loop.starts.shooting'
app.metadatapath = 'metadata/' + app.id + '.yml'
app.CurrentVersionCode = 10108
app.UpdateCheckMode = 'HTTP'
app.UpdateCheckData = 'https://a.net/b.txt|c(.*)|https://d.net/e.txt|v(.*)'
app.UpdateCheckIgnore = 'beta'
2019-07-23 20:45:04 +02:00
respmock = mock.Mock()
respmock.read = lambda: 'v1.1.9\nc10109'.encode('utf-8')
with mock.patch('urllib.request.urlopen', lambda a, b, c: respmock):
vername, vercode = fdroidserver.checkupdates.check_http(app)
self.assertEqual(vername, '1.1.9')
self.assertEqual(vercode, '10109')
def test_check_http_blocks_unknown_schemes(self):
app = fdroidserver.metadata.App()
for scheme in ('file', 'ssh', 'http', ';pwn'):
app.id = scheme
faked = scheme + '://fake.url/for/testing/scheme'
app.UpdateCheckData = faked + '|ignored|' + faked + '|ignored'
app.metadatapath = 'metadata/' + app.id + '.yml'
vername, vercode = fdroidserver.checkupdates.check_http(app)
self.assertIsNone(vername)
self.assertTrue(FDroidException.__name__ in vercode)
def test_check_http_ignore(self):
fdroidserver.checkupdates.options = mock.Mock()
app = fdroidserver.metadata.App()
app.id = 'loop.starts.shooting'
app.metadatapath = 'metadata/' + app.id + '.yml'
app.CurrentVersionCode = 10108
app.UpdateCheckMode = 'HTTP'
app.UpdateCheckData = 'https://a.net/b.txt|c(.*)|https://d.net/e.txt|v(.*)'
app.UpdateCheckIgnore = 'beta'
respmock = mock.Mock()
respmock.read = lambda: 'v1.1.9-beta\nc10109'.encode('utf-8')
with mock.patch('urllib.request.urlopen', lambda a, b, c: respmock):
vername, vercode = fdroidserver.checkupdates.check_http(app)
self.assertEqual(vername, None)
self.assertEqual(vercode, 'Version 1.1.9-beta is ignored')
2021-05-18 10:21:05 +02:00
def test_check_tags_data(self):
fdroidserver.checkupdates.options = mock.Mock()
app = fdroidserver.metadata.App()
app.id = 'loop.starts.shooting'
app.metadatapath = 'metadata/' + app.id + '.yml'
app.RepoType = 'git'
app.CurrentVersionCode = 10108
app.UpdateCheckMode = 'Tags'
app.UpdateCheckData = 'b.txt|c(.*)|e.txt|v(.*)'
vcs = mock.Mock()
vcs.latesttags.return_value = ['1.1.8', '1.1.9']
with mock.patch(
'pathlib.Path.read_text', lambda a: 'v1.1.9\nc10109'
2021-05-18 10:21:05 +02:00
) as _ignored, mock.patch('fdroidserver.common.getvcs', return_value=vcs):
_ignored # silence the linters
vername, vercode, _tag = fdroidserver.checkupdates.check_tags(app, None)
2021-05-18 10:21:05 +02:00
self.assertEqual(vername, '1.1.9')
self.assertEqual(vercode, '10109')
2019-07-23 17:28:19 +02:00
if __name__ == "__main__":
# TODO: Python3.6: Accept path-like object.
os.chdir(str(Path(__file__).parent))
2019-07-23 17:28:19 +02:00
parser = optparse.OptionParser()
parser.add_option(
"-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
2019-07-23 17:28:19 +02:00
(fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite()
newSuite.addTest(unittest.makeSuite(CheckupdatesTest))
2019-07-23 17:28:19 +02:00
unittest.main(failfast=False)