|
| 1 | +import pytest |
| 2 | +import requests |
| 3 | + |
| 4 | +from tests.helper import TestHelpers |
| 5 | +from processes import S3Process |
| 6 | + |
| 7 | + |
| 8 | +class TestS3Process: |
| 9 | + @classmethod |
| 10 | + def setup_class(cls): |
| 11 | + TestHelpers.setEnvVars() |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def teardown_class(cls): |
| 15 | + TestHelpers.clearEnvVars() |
| 16 | + |
| 17 | + @pytest.fixture |
| 18 | + def test_instance(self, mocker): |
| 19 | + class TestS3Process(S3Process): |
| 20 | + def __init__(self, process, customFile, ingestPeriod): |
| 21 | + self.bucket = 'testBucket' |
| 22 | + |
| 23 | + return TestS3Process('TestProcess', 'testFile', 'testDate') |
| 24 | + |
| 25 | + @pytest.fixture |
| 26 | + def test_file_message(self): |
| 27 | + return """ |
| 28 | + { |
| 29 | + "fileData": { |
| 30 | + "fileURL": "testSourceURL", |
| 31 | + "bucketPath": "testBucketPath.epub" |
| 32 | + } |
| 33 | + } |
| 34 | + """ |
| 35 | + |
| 36 | + def test_run_process(self, test_instance, mocker): |
| 37 | + mock_process_files = mocker.patch.object(S3Process, 'process_files') |
| 38 | + mock_save_records = mocker.patch.object(S3Process, 'saveRecords') |
| 39 | + mock_commit_changes = mocker.patch.object(S3Process, 'commitChanges') |
| 40 | + mock_file_process = mocker.MagicMock() |
| 41 | + mock_process = mocker.patch('processes.s3Files.Process') |
| 42 | + mock_process.return_value = mock_file_process |
| 43 | + |
| 44 | + test_instance.runProcess() |
| 45 | + |
| 46 | + mock_process_files.assert_called_once |
| 47 | + mock_save_records.assert_called_once |
| 48 | + mock_commit_changes.assert_called_once |
| 49 | + assert mock_process.call_count == 4 |
| 50 | + assert mock_file_process.start.call_count == 4 |
| 51 | + assert mock_file_process.join.call_count == 4 |
| 52 | + |
| 53 | + def test_process_files(self, test_file_message, mocker): |
| 54 | + mock_sleep = mocker.patch('processes.s3Files.sleep') |
| 55 | + |
| 56 | + mock_s3 = mocker.MagicMock() |
| 57 | + mock_s3_manager = mocker.patch('processes.s3Files.S3Manager') |
| 58 | + mock_s3_manager.return_value = mock_s3 |
| 59 | + |
| 60 | + mock_rabbit_mq = mocker.MagicMock() |
| 61 | + mock_rabbit_mq_manager = mocker.patch('processes.s3Files.RabbitMQManager') |
| 62 | + mock_rabbit_mq_manager.return_value = mock_rabbit_mq |
| 63 | + mock_message_propse = mocker.MagicMock() |
| 64 | + mock_message_propse.delivery_tag = 'rabbitMQTag' |
| 65 | + mock_rabbit_mq.getMessageFromQueue.side_effect = [ |
| 66 | + (mock_message_propse, {}, test_file_message), |
| 67 | + (None, None, None), |
| 68 | + (None, None, None), |
| 69 | + (None, None, None), |
| 70 | + (None, None, None) |
| 71 | + ] |
| 72 | + |
| 73 | + mock_get_file_contents = mocker.patch.object(S3Process, 'get_file_contents') |
| 74 | + mock_get_file_contents.return_value = 'testFileBytes' |
| 75 | + |
| 76 | + mock_generate_webpub = mocker.patch.object(S3Process, 'generate_webpub') |
| 77 | + mock_generate_webpub.return_value = 'testWebpubJson' |
| 78 | + |
| 79 | + S3Process.process_files() |
| 80 | + |
| 81 | + assert mock_rabbit_mq.getMessageFromQueue.call_count == 4 |
| 82 | + mock_rabbit_mq.getMessageFromQueue.assert_called_with('test_file_queue') |
| 83 | + |
| 84 | + mock_sleep.assert_has_calls([ |
| 85 | + mocker.call(30), mocker.call(60), mocker.call(90) |
| 86 | + ]) |
| 87 | + |
| 88 | + mock_generate_webpub.assert_called_once_with('testBucketPath', 'test_aws_bucket') |
| 89 | + |
| 90 | + mock_s3.putObjectInBucket.assert_has_calls([ |
| 91 | + mocker.call('testFileBytes', 'testBucketPath.epub', 'test_aws_bucket'), |
| 92 | + mocker.call('testWebpubJson', 'testBucketPath/manifest.json', 'test_aws_bucket') |
| 93 | + ]) |
| 94 | + mock_rabbit_mq.acknowledgeMessageProcessed.assert_called_once_with('rabbitMQTag') |
| 95 | + |
| 96 | + def test_get_file_contents_success(self, test_instance, mocker): |
| 97 | + mock_get_request = mocker.patch.object(requests, 'get') |
| 98 | + mock_response = mocker.MagicMock() |
| 99 | + mock_response.status_code = 200 |
| 100 | + mock_response.iter_content.return_value = [b'e', b'p', b'u', b'b'] |
| 101 | + mock_get_request.return_value = mock_response |
| 102 | + |
| 103 | + test_file = test_instance.get_file_contents('testURL') |
| 104 | + |
| 105 | + assert test_file == b'epub' |
| 106 | + mock_get_request.assert_called_once_with( |
| 107 | + 'testURL', |
| 108 | + stream=True, |
| 109 | + timeout=15, |
| 110 | + headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)'} |
| 111 | + ) |
| 112 | + |
| 113 | + def test_get_file_contents_error(self, test_instance, mocker): |
| 114 | + mock_get_request = mocker.patch.object(requests, 'get') |
| 115 | + mock_response = mocker.MagicMock() |
| 116 | + mock_response.status_code = 500 |
| 117 | + mock_get_request.return_value = mock_response |
| 118 | + |
| 119 | + with pytest.raises(Exception): |
| 120 | + test_instance.get_file_contents('testURL') |
| 121 | + |
| 122 | + def test_generate_webpub_success(self, mocker): |
| 123 | + mock_get_request = mocker.patch.object(requests, 'get') |
| 124 | + mock_response = mocker.MagicMock(content='testWebpub') |
| 125 | + mock_get_request.return_value = mock_response |
| 126 | + |
| 127 | + test_webpub = S3Process.generate_webpub('testRoot', 'testBucket') |
| 128 | + |
| 129 | + assert test_webpub == 'testWebpub' |
| 130 | + |
| 131 | + mock_get_request.assert_called_once_with( |
| 132 | + 'test_conversion_url/api/https%3A%2F%2FtestBucket.s3.amazonaws.com%2FtestRoot%2FMETA-INF%2Fcontainer.xml', |
| 133 | + timeout=15 |
| 134 | + ) |
| 135 | + |
| 136 | + def test_generate_webpub_error(self, mocker): |
| 137 | + mock_get_request = mocker.patch.object(requests, 'get') |
| 138 | + mock_response = mocker.MagicMock(content='testWebpub') |
| 139 | + mock_response.raise_for_status.side_effect = Exception |
| 140 | + mock_get_request.return_value = mock_response |
| 141 | + |
| 142 | + test_webpub = S3Process.generate_webpub('testRoot', 'testBucket') |
| 143 | + |
| 144 | + assert test_webpub == None |
| 145 | + |
| 146 | + mock_get_request.assert_called_once_with( |
| 147 | + 'test_conversion_url/api/https%3A%2F%2FtestBucket.s3.amazonaws.com%2FtestRoot%2FMETA-INF%2Fcontainer.xml', |
| 148 | + timeout=15 |
| 149 | + ) |
0 commit comments