From cfe399888b25200b7ffdba7ffc6760bdff68b19c Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 7 Mar 2023 21:44:59 +0100 Subject: [PATCH] add new test module for the public API --- tests/api.TestCase | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 tests/api.TestCase diff --git a/tests/api.TestCase b/tests/api.TestCase new file mode 100755 index 00000000..c51dabf5 --- /dev/null +++ b/tests/api.TestCase @@ -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)