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') or url.startswith('ugly'): |
| 64 raise urllib2.URLError('Bad') |
| 65 return real_urlopen(url) |
| 66 |
| 67 mocker.patch('urllib2.urlopen', mock_urlopen) |
| 68 |
| 69 |
| 70 def test_good(md_repo): |
| 71 main() |
| 72 subprocess.check_call(['hg', 'up'], cwd=md_repo.strpath) |
| 73 result = md_repo.join('malwaredomains_full.txt').read() |
| 74 assert 'success' in result |
| 75 |
| 76 |
| 77 def test_bad(md_repo, config): |
| 78 config.set(CONF_SECTION, 'malwaredomains_mirrors', 'bad') |
| 79 try: |
| 80 main() |
| 81 except SystemExit as exc: |
| 82 err_lines = str(exc).splitlines() |
| 83 assert len(err_lines) == 3 |
| 84 assert 'Failed to fetch bad/files/justdomains.zip' in err_lines[2] |
| 85 |
| 86 |
| 87 def test_ugly(md_repo, config): |
| 88 config.set(CONF_SECTION, 'malwaredomains_mirrors', 'bad\nugly') |
| 89 try: |
| 90 main() |
| 91 except SystemExit as exc: |
| 92 err_lines = str(exc).splitlines() |
| 93 assert len(err_lines) == 4 |
| 94 assert 'Failed to fetch bad/files/justdomains.zip' in err_lines[2] |
| 95 assert 'Failed to fetch ugly/files/justdomains.zip' in err_lines[3] |
OLD | NEW |