from moto import mock_s3 import boto import responses from bulkperformancerights.logic import converter from bulkperformancerights import config expected_json_path = 'ows-xlsx/json/vendor_1/foo.json' expected_xlsx_path = 'ows-xlsx/xlsx/vendor_1/foo.xlsx' user_type = 'vendor' user_id = 1 @mock_s3 @responses.activate def test_xlsx_to_json(monkeypatch): monkeypatch.setattr('bulkperformancerights.config.xlsx_to_json_base_url', 'http://xlsxtojson/') responses.add(responses.POST, '{}xlsxtojson'.format(config.xlsx_to_json_base_url), body=expected_json_path, status=200) conn = boto.connect_s3() bucket = conn.create_bucket(config.bucket_name) file_name = 'foo.xlsx' managerights_key = boto.s3.key.Key(bucket) managerights_key.key = '{}{}{}'.format(config.prefix, 'vendor/1/', file_name) managerights_key.set_contents_from_string('foo') convert_xlsx_response = converter.xlsx_to_json(bucket, file_name, user_type, user_id) assert convert_xlsx_response.status_code == 200 @mock_s3 @responses.activate def test_json_to_xlsx(monkeypatch): monkeypatch.setattr('bulkperformancerights.config.xlsx_to_json_base_url', 'http://xlsxtojson/') xlsx_polling_wait = 'bulkperformancerights.logic.converter.polling_wait' monkeypatch.setattr(xlsx_polling_wait, 1) json_to_xlsx_url = '{}jsontoxlsx'.format(config.xlsx_to_json_base_url) conn = boto.connect_s3() bucket = conn.create_bucket(config.bucket_name) json_key = boto.s3.key.Key(bucket) json_filename = 'foo.json' monkeypatch.setattr('bulkperformancerights.config.xlsx_to_json_base_url', 'http://xlsxtojson/') json_key.key = '{}json/{}{}'.format(config.xlsx_prefix, 'vendor_1/', json_filename) json_key.set_contents_from_string('foo') xlsx_key = boto.s3.key.Key(bucket) xlsx_key.key = '{}xlsx/{}{}'.format(config.xlsx_prefix, 'vendor_1/', 'foo.xlsx') xlsx_key.set_contents_from_string('foo') responses.add(responses.POST, json_to_xlsx_url, body=expected_json_path, status=200) responses.add(responses.GET, '{}jsontoxlsx/status/{}'.format(config.xlsx_to_json_base_url, 'vendor/1'), body='{"job_status": "SUCCESS",\ "xlsx_path": "ows-xlsx/xlsx/vendor_1/foo.xlsx"}', status=200) xlsx_path = converter.json_to_xlsx(bucket, 'foo_template', json_filename, 'vendor', 1) assert xlsx_path == 'foo.xlsx'