"""Tests for iTunes connector.""" import subprocess import pytest from availability import config from availability import exceptions from availability.connectors.stores import itunes def test_query_store_subprocess_success(mocker): """Test successful subprocess call while querying remote store.""" itmstr_response = b'foo' expected_response = 'foo' product_id = '98143907070931' mock_cmd = ['/bin/false', '--foo'] mock_get_itunes_command = mocker.patch( 'availability.connectors.stores.itunes._get_itunes_command', return_value=mock_cmd) mock_process = mocker.Mock() mock_process.communicate.return_value = (itmstr_response, b'stderr') mock_process.returncode = 0 mock_popen = mocker.patch( 'availability.connectors.stores.itunes.subprocess.Popen', return_value=mock_process) assert itunes.query_store(product_id) == expected_response assert mock_get_itunes_command.call_count == 1 assert mock_get_itunes_command.call_args == mocker.call(product_id) assert mock_process.communicate.call_count == 1 assert mock_process.communicate.call_args == mocker.call( timeout=config.ITUNES_TIMEOUT) assert mock_popen.call_count == 1 assert mock_popen.call_args == mocker.call( mock_cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def test_query_store_subprocess_error(mocker): """Test unsuccessful subprocess call while querying remote store.""" itmstr_response = b'foo' product_id = '98143907070931' mock_cmd = ['/bin/false', '--foo'] mock_get_itunes_command = mocker.patch( 'availability.connectors.stores.itunes._get_itunes_command', return_value=mock_cmd) mock_process = mocker.Mock() mock_process.communicate.return_value = (itmstr_response, b'stderr') mock_process.returncode = 1 mock_popen = mocker.patch( 'availability.connectors.stores.itunes.subprocess.Popen', return_value=mock_process) with pytest.raises(exceptions.StoreRequestError): itunes.query_store(product_id) assert mock_get_itunes_command.call_count == 1 assert mock_get_itunes_command.call_args == mocker.call(product_id) assert mock_process.communicate.call_count == 1 assert mock_process.communicate.call_args == mocker.call( timeout=config.ITUNES_TIMEOUT) assert mock_popen.call_count == 1 assert mock_popen.call_args == mocker.call( mock_cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)