File: test_api.py

package info (click to toggle)
fdroidserver 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,948 kB
  • sloc: python: 34,139; xml: 2,186; sh: 1,362; java: 293; makefile: 54; javascript: 23
file content (103 lines) | stat: -rwxr-xr-x 4,032 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3

import os
import shutil
import unittest
from pathlib import Path
from unittest import mock

import fdroidserver
from fdroidserver import common, signindex
from .shared_test_code import GP_FINGERPRINT, mkdtemp


basedir = Path(__file__).parent


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):
        os.chdir(basedir)

        self._td = mkdtemp()
        self.testdir = self._td.name

        common.config = None
        config = common.read_config()
        config['jarsigner'] = common.find_sdk_tools_cmd('jarsigner')
        common.config = config
        signindex.config = config

    def tearDown(self):
        self._td.cleanup()

    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)

    @mock.patch('fdroidserver.net.download_using_mirrors')
    def test_download_repo_index_v2(self, mock_download_using_mirrors):
        """Basically a copy of IndexTest.test_download_repo_index_v2"""
        mock_download_using_mirrors.side_effect = lambda mirrors: os.path.join(
            self.testdir, 'repo', os.path.basename(mirrors[0]['url'])
        )
        os.chdir(self.testdir)
        signindex.config['keystore'] = os.path.join(basedir, 'keystore.jks')
        os.mkdir('repo')
        shutil.copy(basedir / 'repo' / 'entry.json', 'repo')
        shutil.copy(basedir / 'repo' / 'index-v2.json', 'repo')
        signindex.sign_index('repo', 'entry.json')
        repo_url = 'https://fake.url/fdroid/repo'
        entry_url = 'https://fake.url/fdroid/repo/entry.jar'
        index_url = 'https://fake.url/fdroid/repo/index-v2.json'
        fingerprint_url = 'https://fake.url/fdroid/repo?fingerprint=' + GP_FINGERPRINT
        slash_url = 'https://fake.url/fdroid/repo//?fingerprint=' + GP_FINGERPRINT
        for url in (repo_url, entry_url, index_url, fingerprint_url, slash_url):
            data, _ignored = fdroidserver.download_repo_index_v2(
                url, verify_fingerprint=False
            )
            self.assertEqual(['repo', 'packages'], list(data))
            self.assertEqual(
                'My First F-Droid Repo Demo', data['repo']['name']['en-US']
            )