OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-2016 Eyeo GmbH |
| 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 import io |
| 17 import ConfigParser |
| 18 import subprocess |
| 19 import urllib2 |
| 20 import zipfile |
| 21 |
| 22 import pytest |
| 23 |
| 24 from sitescripts.subscriptions.bin.updateMalwareDomainsList import main |
| 25 |
| 26 CONF_SECTION = 'subscriptionDownloads' |
| 27 |
| 28 |
| 29 @pytest.fixture(autouse=True) |
| 30 def md_repo(tmpdir): |
| 31 """A mock of our malware domains repo""" |
| 32 repo_dir = tmpdir.mkdir('md_repo') |
| 33 repo_dir.join('foo').write('foo') |
| 34 subprocess.check_call(['hg', 'init'], cwd=repo_dir.strpath) |
| 35 subprocess.check_call(['hg', 'commit', '-q', '-m', 'foo', '-A'], |
| 36 cwd=repo_dir.strpath) |
| 37 return repo_dir |
| 38 |
| 39 |
| 40 @pytest.fixture(autouse=True) |
| 41 def config(mocker, md_repo): |
| 42 """A mock of sitescripts config""" |
| 43 config = ConfigParser.ConfigParser() |
| 44 config.add_section(CONF_SECTION) |
| 45 config.set(CONF_SECTION, 'malwaredomains_repository', md_repo.strpath) |
| 46 config.set(CONF_SECTION, 'malwaredomains_mirrors', 'good') |
| 47 module = 'sitescripts.subscriptions.bin.updateMalwareDomainsList' |
| 48 mocker.patch(module + '.get_config', lambda: config) |
| 49 return config |
| 50 |
| 51 |
| 52 @pytest.fixture(autouse=True) |
| 53 def urlopen(mocker): |
| 54 """Mock urlopen function""" |
| 55 real_urlopen = urllib2.urlopen |
| 56 |
| 57 def mock_urlopen(url): |
| 58 if url.startswith('good'): |
| 59 zf_data = io.BytesIO() |
| 60 with zipfile.ZipFile(zf_data, 'w') as zf: |
| 61 zf.writestr('justdomains', 'success\n') |
| 62 return io.BytesIO(zf_data.getvalue()) |
| 63 if url.startswith('bad'): |
| 64 raise urllib2.HTTPError('Bad', '42', 'No good', [], None) |
| 65 if url.startswith('ugly'): |
| 66 raise urllib2.URLError('Ugly') |
| 67 return real_urlopen(url) |
| 68 |
| 69 mocker.patch('urllib2.urlopen', mock_urlopen) |
| 70 |
| 71 |
| 72 def test_good(md_repo): |
| 73 main() |
| 74 subprocess.check_call(['hg', 'up'], cwd=md_repo.strpath) |
| 75 result = md_repo.join('malwaredomains_full.txt').read() |
| 76 assert 'success' in result |
| 77 |
| 78 |
| 79 def test_bad(md_repo, config): |
| 80 config.set(CONF_SECTION, 'malwaredomains_mirrors', 'bad') |
| 81 try: |
| 82 main() |
| 83 except SystemExit as exc: |
| 84 err_lines = str(exc).splitlines() |
| 85 assert len(err_lines) == 3 |
| 86 assert 'Failed to fetch bad/files/justdomains.zip' in err_lines[2] |
| 87 assert '42: No good' in err_lines[2] |
| 88 |
| 89 |
| 90 def test_ugly(md_repo, config): |
| 91 config.set(CONF_SECTION, 'malwaredomains_mirrors', 'bad\nugly') |
| 92 try: |
| 93 main() |
| 94 except SystemExit as exc: |
| 95 err_lines = str(exc).splitlines() |
| 96 assert len(err_lines) == 4 |
| 97 assert 'Failed to fetch bad/files/justdomains.zip' in err_lines[2] |
| 98 assert 'Failed to fetch ugly/files/justdomains.zip' in err_lines[3] |
| 99 assert 'Ugly' in err_lines[3] |
OLD | NEW |