"""Test configuration file. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest. """ import application from owslogger import flask_logger import pytest from images import config from images.constants import header as header_constants from images.constants import workstation as workstation_constants from tests.utils import db from tests.utils import http @pytest.fixture def client(): """Return flask test client. Returns: flask client: Flask test client. """ return application.app.test_client() @pytest.fixture def header(): """Return header dict fixture. Returns: dict: Header dict. """ return http.get_valid_header_dict() @pytest.fixture def test_schema(request): """Fixture that creates the test DB schema. Creates before a test case runs and tears the schema down after the test case has finished. Indvidual test cases use factories to seed data. Args: request(_pytest.python.SubRequest): a sub request for handling getting a fixture from a test function/fixture. """ db.create_import_asset_table() db.create_import_asset_detail_table() db.create_image_assets_table() db.create_release_cover_images_table() def db_fixture_teardown(): db.drop_import_asset_table() db.drop_import_asset_detail_table() db.drop_image_assets_table() db.drop_release_cover_images_table() request.addfinalizer(db_fixture_teardown) @pytest.fixture def context(): """Return flask app context, including a mock logger. Returns: AppContext: flask app context, including a mock logger. """ class MockLog(): """Mock Logging Class.""" def error(self): pass def info(self): pass def warning(self): pass context = application.app.app_context() context.g.ows = flask_logger.Ows() context.g.ows.log = MockLog return context @pytest.fixture def header_with_php_session_cookie(): """Return header dict fixture with php session cookie.""" cookies = '{}=somerandostring'.format( workstation_constants.PHP_SESSION_COOKIE_KEY) return http.get_valid_header_dict_with_cookie(cookies) @pytest.fixture def filename(): """The filename to be used in tests to match the Workstation response.""" return 'something.tiff' @pytest.fixture def asset_code_string(): """Asset code string as generated by Workstation.""" return '885686291092_5190217' @pytest.fixture def import_asset_id(): """Import asset ID as generated by Workstation.""" return '182698' @pytest.fixture def vendor_id(): """The Vendor ID as returned by Workstation for the current session.""" return '7123' @pytest.fixture def token(): """The token as returned by Workstation.""" return '52c08e2c8d87b595c49c' @pytest.fixture def image_id(filename): """The image ID returned by Workstation.""" return filename @pytest.fixture def upload_filename(asset_code_string): """The upload filename as generated by worksation. Uses asset code string. Not to be confused with the real filename. """ return '{}.tiff'.format(asset_code_string) @pytest.fixture def valid_post_image_data(filename): """Valid data to make post image requests for images microservice.""" return {'filename': filename, 'id': filename} @pytest.fixture def valid_post_image_response( filename, upload_filename, import_asset_id, vendor_id, token, asset_code_string): """Valid response from microservice post image endpoint.""" file_description = '{0}|{1}|{2}|{3}|{4}'.format( upload_filename, import_asset_id, vendor_id, token, asset_code_string) print(file_description) return { 'file_description': file_description, 'id': filename, 'upload_filename': upload_filename, 'import_asset_id': import_asset_id} @pytest.fixture def workstation_response_body( filename, asset_code_string, upload_filename, import_asset_id, vendor_id, token, image_id): """Return a faked Workstation response using the filename and folder.""" return {image_id: { 'upload_filename': upload_filename, 'import_asset_id': import_asset_id, 'vendor_id': vendor_id, 'token': token, 'id': image_id, 'audio_asset_code': [asset_code_string]}} @pytest.fixture def meta_not_found_workstation_response_body( filename, asset_code_string, upload_filename, import_asset_id, vendor_id, token, image_id): """Return a faked Workstation response using the filename and folder.""" return {image_id: { 'upload_filename': 'meta_not_found', 'import_asset_id': import_asset_id, 'vendor_id': vendor_id, 'token': token, 'id': 'meta_not_found', 'audio_asset_code': ['meta_not_found']}} @pytest.fixture def prepare_for_bulk_upload_message( upload_filename, import_asset_id, vendor_id, token, filename, image_id, asset_code_string): """The message returned from the model layer.""" return { 'upload_filename': upload_filename, 'import_asset_id': import_asset_id, 'vendor_id': vendor_id, 'token': token, 'id': image_id, 'original_filename': filename, 'asset_code': asset_code_string} @pytest.fixture def test_account_type(): """Return a subaccount_id for testing.""" return 'vendor' @pytest.fixture def image_upc(): """Fake image upc for testing.""" return 123456789012 @pytest.fixture def upc(): """Fixture for upc.""" return 123456789012 @pytest.fixture def expected_path(): """Fixture to return complete image url.""" return ('https://images.qaorch.com/release/large_cover/123456/789/' '123456789012.jpg') @pytest.fixture def account_info(): """Fixture to return account information.""" return {'vendor_id': 18088, 'subaccount_id': 0} @pytest.fixture def import_asset_detail_id(): """Fake import asset id for testing.""" return 123456 @pytest.fixture def expected_url(): """Fake image assset url for testing.""" return ''.join([ config.IMAGES_URL, '/release/large_cover/123456/789/123456789012.jpg']) @pytest.fixture def valid_headers(test_account_type): """Return valid account headers.""" return { header_constants.GRASS_ACCOUNT_TYPE: test_account_type, header_constants.GRASS_ACCOUNT_ID: 18088} @pytest.fixture def not_valid_headers(test_account_type): """Return invalid account headers.""" return { header_constants.GRASS_ACCOUNT_TYPE: test_account_type, header_constants.GRASS_ACCOUNT_ID: 99999} @pytest.fixture def ows_product_response_body(): """Return response from ows_product get product by upc.""" return { 'subaccount_id': 0, 'vendor_id': 18088, 'product_type_id': 1, 'distribution_format_id': 1, 'project_id': None, 'product_id': 618813, 'upc': 123456789012}