import copy from unittest.mock import MagicMock from unittest.mock import patch from ows_accounting import config from ows_accounting.constants import error from ows_accounting.logic import attachment from ows_accounting.response import Response from ows_accounting.utils import s3 @patch('ows_accounting.logic.attachment.attachment_model') def test_get_attachments(mock_attachment_model, mock_attachments_quarter): """Test if attachments are retrieved from model.""" expected_items = copy.deepcopy(mock_attachments_quarter) mock_attachment_model.get_attachments.return_value = Response( mock_attachments_quarter) result = attachment.get_attachments(10303, 'vendor', [224, 225, 226]) mock_attachment_model.get_attachments.assert_any_call( 'L', 10303, [224, 225, 226]) assert result.status == 200 assert result.message == { 'items': expected_items, 'pagination': {'type': 'none', 'total_records': 3}} def test_get_attachments_fail(): """Test if attachments should not be retrieved from model.""" result = attachment.get_attachments(10303, 'subaccount', [224, 225, 226]) assert result.status == 403 assert result.errors == { 'code': error.ERROR_CODE_INVALID_REQUEST, 'message': error.ERROR_MESSAGE_INVALID_ACCOUNT} @patch('ows_accounting.logic.attachment.attachment_model') def test_get_attachment(mock_model_obj, monkeypatch): """Test the get_attachment method.""" monkeypatch.setattr( s3, 'object_exists', MagicMock(return_value=True)) monkeypatch.setattr( s3, 'get_presigned_url', MagicMock(return_value='http://test_presigned_url')) mock_model_obj.get_attachment.return_value = Response( message={'file_name': 'attachment_doc.pdf', 'period_id': '217', 'bucket_name': 'test_bucket', 'file_key': 'L10303_217_attachment_doc.pdf' }, status=200) response = attachment.get_attachment( '10303', 'vendor', [217], 'attachment_doc.pdf') s3.get_presigned_url.assert_any_call( 'test_bucket', 'L10303_217_attachment_doc.pdf', config.PRESIGNED_URL_EXPIRES) mock_model_obj.get_attachment.assert_any_call( 'L', '10303', [217], 'attachment_doc.pdf') assert response.status == 200 @patch('ows_accounting.logic.attachment.attachment_model') def test_get_attachment_object_not_found(mock_model_obj, monkeypatch): """Test get_attachment method with object not found.""" monkeypatch.setattr( s3, 'object_exists', MagicMock(return_value=False)) monkeypatch.setattr( s3, 'get_presigned_url', MagicMock(return_value='http://test_presigned_url')) mock_model_obj.get_attachment.return_value = Response( status=400, message=error.ERROR_MESSAGE_INVALID_ATTACHMENT_PERIOD) response = attachment.get_attachment( '10303', 'vendor', [224], 'test.txt') assert not s3.get_presigned_url.called assert response.status == 404