"""conftest.py. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ import application from owsresponse import response import pytest from unittest.mock import MagicMock from blacklist_manager.constants import header from blacklist_manager.utils import pagination from tests.unit.utils import db @pytest.fixture def mock_app(): """Mock app.""" with application.app.app_context() as context: context.g.request_context = MagicMock() yield context @pytest.fixture def client(): """Return flask test client. Returns: flask client: flask test client """ return application.app.test_client() @pytest.fixture def db_fixture(): """Create tables in test database.""" db.create_all() yield db.drop_all() @pytest.fixture def valid_blacklist_reasons_response(): """Return a valid blacklist reasons response.""" data = [ { 'id': 3, 'reason': 'Valid Reason', 'added_by': 532, 'alert': 'Alert of reason', 'contact': 'schauhan@theorchard.com', 'date_added': '2018-02-02', 'date_updated': '2018-02-02' } ] return data @pytest.fixture def valid_blacklist_word_data(): """Return a valid blacklist word data.""" return { 'id': 1, 'word': 'Blacklist Word', 'notes': 'Blacklist Notes', 'vendor_id': 123, 'blacklist_reason': { 'id': 3, 'reason': 'Valid Reason' }, 'added_by': { 'id': 532, 'name': 'test user' }, 'date_added': '2018-09-02', 'added_by_identity': 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' } @pytest.fixture def valid_blacklist_word_data2(): """Return a valid blacklist word data.""" return { 'id': 32, 'word': 'abc', 'notes': 'abc notes', 'vendor_id': None, 'blacklist_reason': { 'id': 243323, 'reason': 'reasonable reason' }, 'added_by': { 'id': 6499, 'name': 'test2 user2' }, 'date_added': '2019-10-01', 'added_by_identity': None } @pytest.fixture def valid_blacklist_words_response( valid_blacklist_word_data, valid_blacklist_word_data2): """Return a valid packaging response.""" def _valid_blacklist_words_response( items=( 'valid_blacklist_word_data', 'valid_blacklist_word_data2', ) ): all_items = { 'valid_blacklist_word_data': valid_blacklist_word_data, 'valid_blacklist_word_data2': valid_blacklist_word_data2, } data = { 'items': [ value for key, value in all_items.items() if key in items], 'pagination': { 'type': 'standard', 'page_offset': None, 'page_limit': None } } return data return _valid_blacklist_words_response @pytest.fixture def valid_blacklist_words_response_with_count(valid_blacklist_words_response): """Return a valid blacklist_words response with count.""" def _valid_blacklist_words_response_with_count( items=( 'valid_blacklist_word_data', 'valid_blacklist_word_data2', ) ): return { **valid_blacklist_words_response(items), 'pagination': { 'count': len(items), 'page_limit': None, 'page_offset': None, 'type': 'standard', } } return _valid_blacklist_words_response_with_count @pytest.fixture def make_customer_record(): """Make customer record.""" def _make_customer_record(name): return {'name': name, 'orders': []} return _make_customer_record @pytest.fixture def create_blacklist_reason_response(): """Return a blacklist reason record id.""" return {'id': 1} @pytest.fixture def blacklist_reason_response(): """Return a blacklist reason record id.""" return {'id': 3} @pytest.fixture def blacklist_error_reason_response(): """Return a Json with error that indicates an extra field.""" return { 'additionalProperties': { 'validator': 'additionalProperties', 'validator_value': 'false', 'message': 'Additional properties are not allowed ("extra" was unexpected)' } } @pytest.fixture def blacklist_error_reason_data(): """Return a dict of blacklist reason.""" return { 'reason': 'Some blacklisted reason', 'extra': 'extra field' } @pytest.fixture def blacklist_update_reason_data(): """Return a dict of blacklist reason.""" return { 'reason': 'test reason', } @pytest.fixture def blacklist_reason_data(): """Return a dict of blacklist reason.""" return { 'reason': 'Valid Reason', 'added_by': 532, 'alert': 'Alert of reason', 'contact': 'schauhan@theorchard.com' } @pytest.fixture def create_blacklist_error_reason_response(): """Return an error.""" return { 'additionalProperties': { 'validator': 'additionalProperties', 'validator_value': 'false', 'message': 'Additional properties are not allowed ("extra" was unexpected)' } } @pytest.fixture def grass_headers(): """Return grass headers.""" return { header.GRASS_ACCOUNT_TYPE: header.GRASS_ACCOUNT_TYPE_VENDOR, header.GRASS_ACCOUNT_ID: 1 } @pytest.fixture def valid_blacklist_words_response_handler( valid_blacklist_words_response): """Return a valid blacklist word response.""" return response.Response( message=valid_blacklist_words_response(), status=200) @pytest.fixture def fixture_pagination(): """Create Pagination named tuple.""" return pagination.Pagination(1, 100) @pytest.fixture def fixture_vendor(): """Fixture for vendor.""" return 'my_vendor' @pytest.fixture def fixture_grass_account(fixture_vendor): """Create GrassAccount named tuple.""" return ('type', fixture_vendor) @pytest.fixture def fixture_error_response(): """Create a response.Response fixture for generic error.""" return response.create_error_response( code='error_code', message='error_message') @pytest.fixture def fixture_ok_response(): """Create a response.Response fixture for generic ok.""" return response.Response(200) @pytest.fixture def valid_oa_headers(): """Return valid OA request headers. Returns: (dict): header dict """ data = { header.CORRELATION_ID: 'test', header.ORCHARD_USER_ID: 'oa:562'} return data @pytest.fixture def blacklist_word_response(): """Return a blacklist word id.""" return {'id': 1} @pytest.fixture def blacklist_error_word_response(): """Return an error.""" return { 'additionalProperties': { 'validator': 'additionalProperties', 'validator_value': 'false', 'message': 'Additional properties are not allowed ("extra" was unexpected)' } } @pytest.fixture def blacklist_word_data(): """Return a dict of blacklist word.""" return { 'word': 'Some blacklisted word', 'notes': 'Some notes for given blacklisted word', 'blacklist_reason_id': 3, 'added_by': 562 } @pytest.fixture def blacklist_update_word_data(): """Return a dict of blacklist word.""" return { 'word': 'Some blacklisted word', 'notes': 'Some notes for given blacklisted word', 'blacklist_reason_id': 3 } @pytest.fixture def blacklist_error_word_data(): """Return a dict of blacklist word.""" return { 'word': 'Some blacklisted word', 'notes': 'Some notes for given blacklisted word', 'blacklist_reason_id': 3, 'added_by': 562, 'extra': 'extra field' } @pytest.fixture def example_blacklist_data_hidden_chars(): """Return a dict of a blacklist word with hidden utf-8 characters.""" return { # word has a leading zero width space character 'A bad word' 'word': u'\N{ZERO WIDTH SPACE}A bad word'.encode('utf-8').decode('utf-8'), 'notes': 'Some notes for given blacklisted word', 'blacklist_reason_id': 3, 'added_by': 562, 'extra': 'extra field' } @pytest.fixture def example_blacklist_data_no_hidden_chars(): """Return a dict of a blacklist word with no hidden utf-8 characters.""" return { 'word': 'ハッピー', 'notes': 'Some notes for given blacklisted word', 'blacklist_reason_id': 3, 'added_by': 562, 'extra': 'extra field' } @pytest.fixture def example_data_to_validate(): """Return some data to validate against the example validation schema. Returns: data (dict): a dict containing example data that validates against the example schema """ return { 'apple': 1234, 'orange': '007686986', 'banana': 'nine', 'mango': 'Mexican' } @pytest.fixture def example_validation_schema(): """Return example JSON Draft3 Schema for testing the validator. Returns: schema (dict): JSON Draft3 Validation Schema """ return { '$schema': 'http://json-schema.org/draft-03/schema', 'properties': { 'apple': { 'required': True, 'type': 'integer', 'blank': True }, 'orange': { 'required': True, 'type': 'string', 'maxLength': 255 }, 'banana': { 'type': 'string', 'required': True, 'blank': False, 'maxLength': 10 }, 'mango': { 'required': True, 'type': 'string' } }, 'additionalProperties': False, 'required': True, 'type': 'object' } @pytest.fixture def fixture_product_data(): """Fixture response for product data.""" return { 'product_id': 1, } @pytest.fixture def fixture_product_data_sql(): """Fixture response for product data sql query.""" return { 'release_name': 'random', } @pytest.fixture def valid_response_for_release_validation(): """Return a valid blacklist word response.""" data = { 'items': [ { 'word': 'Blacklist Word', 'reason_id': 3 }, { 'word': 'abc', 'reason_id': 243323 } ] } return data @pytest.fixture def valid_list_of_reasons_for_release_validation(): """Return a valid blacklist word response.""" data = { 3: { 'reason': 'Valid Reason', 'alert': 'Alert of reason', 'contact': 'schauhan@theorchard.com', }, 243323: { 'reason': 'reasonable reason', 'alert': 'abc alert', 'contact': 'asdf@theorchard.com', } } return data @pytest.fixture def valid_blacklist_words_response_for_blacklist_word_match(): """Return a valid blacklist word response.""" data = { 'items': [ { 'word': 'reina', 'reason_id': 3, 'matched_fields': [ 'release_artist_name', 'track_artist_name' ] } ] } return data @pytest.fixture def validation_error_constructor_data(): """Fixture response for product data.""" return { 'matched_blacklist_words': ['reina'], 'items': [ { 'word': 'reina', 'reason': 'Valid Reason', 'alert': 'Alert of reason', 'contact': 'schauhan@theorchard.com', 'matched_field': 'release_artist_name' }, { 'word': 'reina', 'reason': 'Valid Reason', 'alert': 'Alert of reason', 'contact': 'schauhan@theorchard.com', 'matched_field': 'track_artist_name' } ] } @pytest.fixture def fetched_product_details_data(): """Fixture response for product data fetched as SQL query.""" return { 'release_name': 'copy test 01_22_15_56', 'label': '2018 New Test Records', 'format': 'Full Length', 'vendor_catalog_number': 'JDTSTAUD', 'vendor_release_identifier': None, 'c_line': '2020 James Records', 'artist_name': 'James D', 'artist_names': 'James D', 'track_names': 'File_example_wav_10mg,File_example_wav_5mg', 'versions': '', 'p_lines': '2020 James Records', 'vendor_track_identifiers': '', 'meta_languages': 'ENG', 'track_artist_names': 'James D', 'company': 'Non-Mechanical M/USD', 'subaccount_name': None } @pytest.fixture def error_correction_data(): """Fixture response for error correction data.""" return { 'release_correction_id': 1, 'release_id': 1, 'status': 'active', 'items': [ { 'release_correction_detail_id': 1, 'table_name': 'releases', 'field_name': 'performer', 'key_id': 1, 'key_value': [ { 'role': 'performer', 'artist_name': 'performer name' } ] }, { 'release_correction_detail_id': 2, 'table_name': 'releases', 'field_name': 'release_name', 'key_id': 1, 'key_value': 'New release Name abc' } ] } @pytest.fixture def error_correction_data_with_vendor_release_identifier(): """Fixture response for error correction data.""" return { 'release_correction_id': 1, 'release_id': 1, 'status': 'active', 'items': [ { 'release_correction_detail_id': 1, 'table_name': 'releases', 'field_name': 'performer', 'key_id': 1, 'key_value': [ { 'role': 'performer', 'artist_name': 'performer name' } ] }, { 'release_correction_detail_id': 2, 'table_name': 'releases', 'field_name': 'release_name', 'key_id': 1, 'key_value': 'New release Name abc' }, { 'release_correction_detail_id': 3, 'table_name': 'releases', 'field_name': 'vendor_release_identifier', 'key_id': 1, 'key_value': 'New vendor release identifier' } ] } @pytest.fixture def blacklist_words_for_validation_items_store_blocked(): """Fixture response items for get_blacklist_words_for_validation.""" return [ {'reason_id': 1, 'word': 'James'}, {'reason_id': 1, 'word': 'James D'}, {'reason_id': 2, 'word': 'Test'}, {'reason_id': 1, 'word': 'Length'}, {'reason_id': 2, 'word': 'Example'}, {'reason_id': 1, 'word': 'ENG'}, {'reason_id': 2, 'word': 'Mechanical'}, {'reason_id': 1, 'word': 'Testing'}, {'reason_id': 2, 'word': 'Subaccount'}, {'reason_id': 1, 'word': 'Person'}, {'reason_id': 2, 'word': 'AB'}, {'reason_id': 1, 'word': 'Record'}, {'reason_id': 2, 'word': 'New'}, {'reason_id': 1, 'word': 'Identifier'}, {'reason_id': 2, 'word': 'Line'}, {'reason_id': 1, 'word': 'BICYCLE'}, {'reason_id': 2, 'word': 'Vendor'} ] @pytest.fixture def blacklist_words_for_validation_items(): """Fixture response items for get_blacklist_words_for_validation.""" return [ {'reason_id': 3, 'word': 'James'}, {'reason_id': 4, 'word': 'Test'}, {'reason_id': 5, 'word': 'Length'}, {'reason_id': 6, 'word': 'Example'}, {'reason_id': 7, 'word': 'ENG'}, {'reason_id': 8, 'word': 'Mechanical'}, {'reason_id': 9, 'word': 'Testing'}, {'reason_id': 10, 'word': 'Subaccount'}, {'reason_id': 11, 'word': 'Person'}, {'reason_id': 12, 'word': 'AB'}, {'reason_id': 13, 'word': 'Record'}, {'reason_id': 14, 'word': 'New'}, {'reason_id': 15, 'word': 'Identifier'}, {'reason_id': 16, 'word': 'Line'}, {'reason_id': 17, 'word': 'BICYCLE'}, {'reason_id': 18, 'word': 'Vendor'} ] @pytest.fixture def blacklist_words_for_validation_items_no_matches(): """Fixture response items for get_blacklist_words_for_validation.""" return [ {'reason_id': 3, 'word': 'foo'}, {'reason_id': 4, 'word': 'bar'}, {'reason_id': 5, 'word': 'baz'}, ] @pytest.fixture def blacklist_words_for_validation_items_single_character_edge_case(): """Fixture response items for get_blacklist_words_for_validation.""" return [ {'reason_id': 1, 'word': 'E'} ] @pytest.fixture def fetch_product_details_by_id_result(): """Fixture response for fetch_product_details_by_id.""" return { 'artist_name': 'James D', 'artist_names': 'Person A', 'c_line': '2020 C-Line Records', 'company': 'Non-Mechanical M/USD', 'format': 'Full Length', 'label': '2018 New Test Records', 'meta_languages': 'ENG', 'p_lines': '2020 P-Line Records', 'release_name': 'Test Product', 'subaccount_name': 'ABC Subaccount', 'track_artist_names': 'Person AB', 'track_names': 'Testing Track 1,Testing Track 2', 'vendor_catalog_number': 'BICYCLE', 'vendor_release_identifier': 'Vendor-Release-Identifier-1', 'vendor_track_identifiers': 'Vendor-Track-Identifier-1', 'versions': 'Example Version 1,Example Version 2' }