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