diff --git a/app/main/uploader.py b/app/main/uploader.py index 8c87fe3c9..a4b5c2102 100644 --- a/app/main/uploader.py +++ b/app/main/uploader.py @@ -7,7 +7,6 @@ def s3upload(upload_id, service_id, filedata, region): bucket_name = 'service-{}-notify'.format(service_id) contents = '\n'.join(filedata['data']) - bucket = s3.Bucket(bucket_name) exists = True try: s3.meta.client.head_bucket(Bucket=bucket_name) @@ -20,13 +19,15 @@ def s3upload(upload_id, service_id, filedata, region): s3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={'LocationConstraint': region}) - key = s3.Object(bucket_name, upload_id) + upload_file_name = "{}.csv".format(upload_id) + key = s3.Object(bucket_name, upload_file_name) key.put(Body=contents, ServerSideEncryption='AES256') def s3download(service_id, upload_id): s3 = resource('s3') bucket_name = 'service-{}-notify'.format(service_id) - key = s3.Object(bucket_name, upload_id) + upload_file_name = "{}.csv".format(upload_id) + key = s3.Object(bucket_name, upload_file_name) contents = key.get()['Body'].read().decode('utf-8') return contents diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index 69ab377cb..2b9121748 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -26,7 +26,7 @@ class JobApiClient(BaseAPIClient): "service": service_id, "template": template_id, "original_file_name": original_file_name, - "bucket_name": "service-{}-{}-notify".format(service_id, job_id), + "bucket_name": "service-{}-notify".format(service_id), "file_name": "{}.csv".format(job_id) } diff --git a/tests/app/main/notify_client/test_job_client.py b/tests/app/main/notify_client/test_job_client.py new file mode 100644 index 000000000..1333c530d --- /dev/null +++ b/tests/app/main/notify_client/test_job_client.py @@ -0,0 +1,27 @@ +from app.notify_client.job_api_client import JobApiClient + + +def test_client_creates_job_data_correctly(mocker): + import uuid + job_id = str(uuid.uuid4()) + service_id = str(uuid.uuid4()) + template_id = 1 + original_file_name = 'test.csv' + + expected_data = { + "id": job_id, + "service": service_id, + "template": template_id, + "original_file_name": original_file_name, + "bucket_name": "service-{}-notify".format(service_id), + "file_name": "{}.csv".format(job_id) + } + + expected_url = '/service/{}/job'.format(service_id) + + client = JobApiClient() + mock_post = mocker.patch('app.notify_client.job_api_client.JobApiClient.post') + + client.create_job(job_id, service_id, template_id, original_file_name) + + mock_post.assert_called_once_with(url=expected_url, data=expected_data) diff --git a/tests/app/main/views/test_sms.py b/tests/app/main/views/test_sms.py index 98635d609..274892af1 100644 --- a/tests/app/main/views/test_sms.py +++ b/tests/app/main/views/test_sms.py @@ -184,4 +184,3 @@ def test_create_job_should_call_api(app_, assert response.status_code == 200 mock_create_job.assert_called_with(job_id, service_id, template_id, original_file_name) - assert job_data['bucket_name'] == "service-{}-{}-notify".format(service_id, job_id) diff --git a/tests/conftest.py b/tests/conftest.py index 93cf43486..b4829be78 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -433,7 +433,7 @@ def mock_create_job(mocker, job_data): job_data['id'] = job_id job_data['service'] = service_id job_data['template'] = template_id - job_data['bucket_name'] = 'service-{}-{}-notify'.format(service_id, job_id) + job_data['bucket_name'] = 'service-{}-notify'.format(job_id) job_data['original_file_name'] = file_name job_data['file_name'] = '{}.csv'.format(job_id) return job_data