create fake letter response files with variables timestamps

when a test letter is created on dev or preview, we upload a file to
the dvla ftp response bucket, to test that our integration with s3
works. s3 triggers an sns notification, which we pick up, and then we
download the file and mark the letters it mentions as delivered.

However, if two tests run at the same time, they'll create the same
file on s3. One will just overwrite the next, and the first letter will
never move into delivered - this was causing functional tests to
intermittently fail.

This commit makes the test letter task check if the file exists - if it
does, it moves back one second and tries again. It tries this thirty
times before giving up.
This commit is contained in:
Leo Hemsted
2018-07-12 16:53:10 +01:00
parent 6e87b36303
commit 0cfed3f514
3 changed files with 62 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import uuid
from unittest.mock import ANY
from unittest.mock import ANY, call
from flask import current_app, json
from freezegun import freeze_time
@@ -111,6 +111,7 @@ def test_failure_firetext_callback(phone_number):
@freeze_time("2018-01-25 14:00:00")
def test_create_fake_letter_response_file_uploads_response_file_s3(
notify_api, mocker):
mocker.patch('app.celery.research_mode_tasks.file_exists', return_value=False)
mock_s3upload = mocker.patch('app.celery.research_mode_tasks.s3upload')
filename = 'NOTIFY-20180125140000-RSP.TXT'
@@ -134,6 +135,7 @@ def test_create_fake_letter_response_file_uploads_response_file_s3(
@freeze_time("2018-01-25 14:00:00")
def test_create_fake_letter_response_file_calls_dvla_callback_on_development(
notify_api, mocker):
mocker.patch('app.celery.research_mode_tasks.file_exists', return_value=False)
mocker.patch('app.celery.research_mode_tasks.s3upload')
filename = 'NOTIFY-20180125140000-RSP.TXT'
@@ -159,6 +161,7 @@ def test_create_fake_letter_response_file_calls_dvla_callback_on_development(
@freeze_time("2018-01-25 14:00:00")
def test_create_fake_letter_response_file_does_not_call_dvla_callback_on_preview(
notify_api, mocker):
mocker.patch('app.celery.research_mode_tasks.file_exists', return_value=False)
mocker.patch('app.celery.research_mode_tasks.s3upload')
with set_config_values(notify_api, {
@@ -168,3 +171,35 @@ def test_create_fake_letter_response_file_does_not_call_dvla_callback_on_preview
create_fake_letter_response_file('random-ref')
assert request_mock.last_request is None
@freeze_time("2018-01-25 14:00:30")
def test_create_fake_letter_response_file_tries_to_create_files_with_other_filenames(notify_api, mocker):
mock_file_exists = mocker.patch('app.celery.research_mode_tasks.file_exists', side_effect=[True, True, False])
mock_s3upload = mocker.patch('app.celery.research_mode_tasks.s3upload')
create_fake_letter_response_file('random-ref')
assert mock_file_exists.mock_calls == [
call('test.notify.com-ftp', 'NOTIFY-20180125140030-RSP.TXT'),
call('test.notify.com-ftp', 'NOTIFY-20180125140029-RSP.TXT'),
call('test.notify.com-ftp', 'NOTIFY-20180125140028-RSP.TXT'),
]
mock_s3upload.assert_called_once_with(
filedata=ANY,
region=ANY,
bucket_name=ANY,
file_location='NOTIFY-20180125140028-RSP.TXT'
)
@freeze_time("2018-01-25 14:00:30")
def test_create_fake_letter_response_file_gives_up_after_thirty_times(notify_api, mocker):
mock_file_exists = mocker.patch('app.celery.research_mode_tasks.file_exists', return_value=True)
mock_s3upload = mocker.patch('app.celery.research_mode_tasks.s3upload')
with pytest.raises(ValueError):
create_fake_letter_response_file('random-ref')
assert len(mock_file_exists.mock_calls) == 30
assert not mock_s3upload.called