| OLD | NEW |
| 1 import os | 1 import os |
| 2 import shutil | 2 import shutil |
| 3 import pytest | 3 import pytest |
| 4 import zipfile |
| 5 |
| 4 from wsgi_intercept import urllib3_intercept | 6 from wsgi_intercept import urllib3_intercept |
| 5 from wsgi_intercept import add_wsgi_intercept | 7 from wsgi_intercept import add_wsgi_intercept |
| 6 from wsgi_intercept import remove_wsgi_intercept | 8 from wsgi_intercept import remove_wsgi_intercept |
| 7 | 9 |
| 8 from crowdin_mock_api import app | 10 from crowdin_mock_api import CrowdinMock |
| 9 from cms.bin import translate | 11 from cms.bin import translate |
| 10 | 12 |
| 11 | 13 |
| 12 @pytest.fixture(scope='module') | 14 @pytest.fixture(scope='module') |
| 13 def expect_requests(): | 15 def expect_requests(): |
| 14 return [ | 16 return [ |
| 15 ('info?key=test_key&json=1', ''), | 17 ('info?key=test_key&json=1', ''), |
| 16 ('supported-languages?key=test_key&json=1', ''), | 18 ('supported-languages?key=test_key&json=1', ''), |
| 17 ('add-file?key=test_key&json=1', 'translate.json'), | 19 ('add-file?key=test_key&json=1', '.json'), |
| 18 ('upload-translation?key=test_key&json=1', 'simple'), | 20 ('upload-translation?key=test_key&json=1', 'simple'), |
| 19 ('delete-file?key=test_key&json=1', 'translate.json'), | 21 ('delete-file?key=test_key&json=1', '.json'), |
| 20 ('delete-file?key=test_key&json=1', 'translate.json'), | 22 ('delete-file?key=test_key&json=1', '.json'), |
| 23 ('delete-file?key=test_key&json=1', '.json'), |
| 24 ('delete-file?key=test_key&json=1', '.json'), |
| 21 ('delete-directory?key=test_key&json=1', ''), | 25 ('delete-directory?key=test_key&json=1', ''), |
| 22 ('delete-directory?key=test_key&json=1', ''), | 26 ('delete-directory?key=test_key&json=1', ''), |
| 23 ('export?key=test_key&json=1', ''), | 27 ('export?key=test_key&json=1', ''), |
| 24 ('download/all.zip?key=test_key', ''), | 28 ('download/all.zip?key=test_key', ''), |
| 25 ] | 29 ] |
| 26 | 30 |
| 27 | 31 |
| 28 @pytest.fixture(scope='module') | 32 @pytest.fixture(scope='module') |
| 29 def make_crowdin_zip(temp_site): | 33 def api_zip(temp_site, request): |
| 30 zip_name = os.path.join('tests', 'all') | 34 zip_name = 'all' |
| 31 input_dir = os.path.join(temp_site, 'locales') | 35 input_dir = os.path.join(temp_site, 'locales') |
| 32 shutil.make_archive(zip_name, 'zip', input_dir) | 36 shutil.make_archive(zip_name, 'zip', input_dir) |
| 33 yield None | 37 yield zipfile.ZipFile(zip_name + '.zip') |
| 34 os.remove(zip_name + '.zip') | 38 os.remove(zip_name + '.zip') |
| 35 | 39 |
| 36 | 40 |
| 37 @pytest.fixture() | 41 @pytest.fixture(scope='session') |
| 38 def make_intercept(scope='module'): | 42 def mock_api(temp_site): |
| 43 api = CrowdinMock(os.path.join(temp_site, 'locales')) |
| 44 api.run() |
| 39 host = 'api.crowdin.com' | 45 host = 'api.crowdin.com' |
| 40 port = 443 | 46 port = 443 |
| 41 urllib3_intercept.install() | 47 urllib3_intercept.install() |
| 42 add_wsgi_intercept(host, port, lambda: app.wsgi_app) | 48 add_wsgi_intercept(host, port, lambda: api.app.wsgi_app) |
| 43 yield None | 49 yield api |
| 44 remove_wsgi_intercept() | 50 remove_wsgi_intercept() |
| 45 | 51 |
| 46 | 52 |
| 47 def test_sync(temp_site, make_intercept, make_crowdin_zip, expect_requests): | 53 def test_sync(temp_site, mock_api, api_zip, expect_requests): |
| 48 translate.crowdin_sync(temp_site, 'test_key') | 54 translate.crowdin_sync(temp_site, 'test_key') |
| 49 for (url, data), (expect_url, expect_data) in zip(app.request_log, | 55 for (url, data), (expect_url, expect_data) in zip(mock_api.app.request_log, |
| 50 expect_requests): | 56 expect_requests): |
| 51 assert expect_url in url | 57 assert expect_url in url |
| 52 assert expect_data in data | 58 assert expect_data in data |
| OLD | NEW |