mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
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:
@@ -4,6 +4,7 @@ from flask import current_app
|
||||
|
||||
import pytz
|
||||
from boto3 import client, resource
|
||||
import botocore
|
||||
|
||||
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
||||
|
||||
@@ -18,6 +19,17 @@ def get_s3_object(bucket_name, file_location):
|
||||
return s3.Object(bucket_name, file_location)
|
||||
|
||||
|
||||
def file_exists(bucket_name, file_location):
|
||||
try:
|
||||
# try and access metadata of object
|
||||
get_s3_object(bucket_name, file_location).metadata
|
||||
return True
|
||||
except botocore.exceptions.ClientError as e:
|
||||
if e.response['ResponseMetadata']['HTTPStatusCode'] == 404:
|
||||
return False
|
||||
raise
|
||||
|
||||
|
||||
def get_job_location(service_id, job_id):
|
||||
return (
|
||||
current_app.config['CSV_UPLOAD_BUCKET_NAME'],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
|
||||
from flask import current_app
|
||||
@@ -7,6 +7,7 @@ from requests import request, RequestException, HTTPError
|
||||
from notifications_utils.s3 import s3upload
|
||||
|
||||
from app import notify_celery
|
||||
from app.aws.s3 import file_exists
|
||||
from app.models import SMS_TYPE
|
||||
from app.config import QueueNames
|
||||
from app.celery.process_ses_receipts_tasks import process_ses_results
|
||||
@@ -123,7 +124,18 @@ def firetext_callback(notification_id, to):
|
||||
def create_fake_letter_response_file(self, reference):
|
||||
now = datetime.utcnow()
|
||||
dvla_response_data = '{}|Sent|0|Sorted'.format(reference)
|
||||
upload_file_name = 'NOTIFY-{}-RSP.TXT'.format(now.strftime('%Y%m%d%H%M%S'))
|
||||
|
||||
# try and find a filename that hasn't been taken yet - going back in time 60 seconds
|
||||
for i in range(30):
|
||||
upload_file_name = 'NOTIFY-{}-RSP.TXT'.format((now - timedelta(seconds=i)).strftime('%Y%m%d%H%M%S'))
|
||||
if not file_exists(current_app.config['DVLA_RESPONSE_BUCKET_NAME'], upload_file_name):
|
||||
break
|
||||
else:
|
||||
raise ValueError(
|
||||
'cant create fake letter response file for {} - too many files for that time already exist on s3'.format(
|
||||
reference
|
||||
)
|
||||
)
|
||||
|
||||
s3upload(
|
||||
filedata=dvla_response_data,
|
||||
|
||||
Reference in New Issue
Block a user