add new test module for the public API

This commit is contained in:
Hans-Christoph Steiner 2023-03-07 21:44:59 +01:00
parent 723873456d
commit cfe399888b
No known key found for this signature in database
GPG Key ID: 3E177817BA1B9BFA
1 changed files with 74 additions and 0 deletions

74
tests/api.TestCase Executable file
View File

@ -0,0 +1,74 @@
#!/usr/bin/env python3
import inspect
import os
import sys
import unittest
from unittest import mock
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
class ApiTest(unittest.TestCase):
"""Test the public API in the base "fdroidserver" module
This is mostly a smokecheck to make sure the public API as
declared in fdroidserver/__init__.py is working. The functions
are all implemented in other modules, with their own tests.
"""
def setUp(self):
self.basedir = os.path.join(localmodule, 'tests')
os.chdir(self.basedir)
def test_download_repo_index_no_fingerprint(self):
with self.assertRaises(fdroidserver.VerificationException):
fdroidserver.download_repo_index("http://example.org")
@mock.patch('fdroidserver.net.http_get')
def test_download_repo_index_url_parsing(self, mock_http_get):
"""Test whether it is trying to download the right file
This passes the URL back via the etag return value just as a
hack to check which URL was actually attempted.
"""
mock_http_get.side_effect = lambda url, etag, timeout: (None, url)
repo_url = 'https://example.org/fdroid/repo'
index_url = 'https://example.org/fdroid/repo/index-v1.jar'
for url in (repo_url, index_url):
_ignored, etag_set_to_url = fdroidserver.download_repo_index(
url, verify_fingerprint=False
)
self.assertEqual(index_url, etag_set_to_url)
@mock.patch('fdroidserver.net.http_get')
def test_download_repo_index_v1_url_parsing(self, mock_http_get):
"""Test whether it is trying to download the right file
This passes the URL back via the etag return value just as a
hack to check which URL was actually attempted.
"""
mock_http_get.side_effect = lambda url, etag, timeout: (None, url)
repo_url = 'https://example.org/fdroid/repo'
index_url = 'https://example.org/fdroid/repo/index-v1.jar'
for url in (repo_url, index_url):
_ignored, etag_set_to_url = fdroidserver.download_repo_index_v1(
url, verify_fingerprint=False
)
self.assertEqual(index_url, etag_set_to_url)
if __name__ == "__main__":
newSuite = unittest.TestSuite()
newSuite.addTest(unittest.makeSuite(ApiTest))
unittest.main(failfast=False)