mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Add create fake letter response file
- used by letter functional tests to ensure that we can create the response file necessary to trigger the update to a delivered state
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from requests import request, RequestException, HTTPError
|
from requests import request, RequestException, HTTPError
|
||||||
|
|
||||||
|
from notifications_utils.s3 import s3upload
|
||||||
|
|
||||||
|
from app import notify_celery
|
||||||
from app.models import SMS_TYPE
|
from app.models import SMS_TYPE
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.celery.process_ses_receipts_tasks import process_ses_results
|
from app.celery.process_ses_receipts_tasks import process_ses_results
|
||||||
@@ -115,6 +119,35 @@ def firetext_callback(notification_id, to):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@notify_celery.task(bind=True, name="create-fake-letter-response-file", max_retries=5, default_retry_delay=300)
|
||||||
|
def create_fake_letter_response_file(self, reference):
|
||||||
|
now = datetime.utcnow()
|
||||||
|
dvla_response_data = '{}|Sent|1|Sorted'.format(reference)
|
||||||
|
upload_file_name = 'NOTIFY.{}.RSP.TXT'.format(now.strftime('%Y%m%d%H%M%S'))
|
||||||
|
|
||||||
|
s3upload(
|
||||||
|
filedata=dvla_response_data,
|
||||||
|
region=current_app.config['AWS_REGION'],
|
||||||
|
bucket_name=current_app.config['DVLA_RESPONSE_BUCKET_NAME'],
|
||||||
|
file_location=upload_file_name
|
||||||
|
)
|
||||||
|
current_app.logger.info("Fake DVLA response file {}, content [{}], uploaded to {}, created at {}".format(
|
||||||
|
upload_file_name, dvla_response_data, current_app.config['DVLA_RESPONSE_BUCKET_NAME'], now))
|
||||||
|
|
||||||
|
# on development we can't trigger SNS callbacks so we need to manually hit the DVLA callback endpoint
|
||||||
|
if current_app.config['NOTIFY_ENVIRONMENT'] == 'development':
|
||||||
|
make_request('letter', 'dvla', _fake_sns_s3_callback(upload_file_name), None)
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_sns_s3_callback(filename):
|
||||||
|
message_contents = '{"Records":[{"s3":{"object":{"key":"%s"}}}]}' % (filename) # noqa
|
||||||
|
return json.dumps({
|
||||||
|
"Type": "Notification",
|
||||||
|
"MessageId": "some-message-id",
|
||||||
|
"Message": message_contents
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def ses_notification_callback(reference):
|
def ses_notification_callback(reference):
|
||||||
ses_message_body = {
|
ses_message_body = {
|
||||||
'delivery': {
|
'delivery': {
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import uuid
|
import uuid
|
||||||
from unittest.mock import ANY
|
from unittest.mock import ANY
|
||||||
|
|
||||||
|
from flask import current_app, json
|
||||||
|
from freezegun import freeze_time
|
||||||
import pytest
|
import pytest
|
||||||
from flask import json
|
import requests_mock
|
||||||
|
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.celery.research_mode_tasks import (
|
from app.celery.research_mode_tasks import (
|
||||||
@@ -11,7 +13,9 @@ from app.celery.research_mode_tasks import (
|
|||||||
mmg_callback,
|
mmg_callback,
|
||||||
firetext_callback,
|
firetext_callback,
|
||||||
ses_notification_callback,
|
ses_notification_callback,
|
||||||
|
create_fake_letter_response_file,
|
||||||
)
|
)
|
||||||
|
from tests.conftest import set_config_values
|
||||||
|
|
||||||
|
|
||||||
def test_make_mmg_callback(notify_api, rmock):
|
def test_make_mmg_callback(notify_api, rmock):
|
||||||
@@ -102,3 +106,65 @@ def test_failure_firetext_callback(phone_number):
|
|||||||
'time': '2016-03-10 14:17:00',
|
'time': '2016-03-10 14:17:00',
|
||||||
'reference': '1234'
|
'reference': '1234'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2018-01-25 14:00:00")
|
||||||
|
def test_create_fake_letter_response_file_uploads_response_file_s3(
|
||||||
|
notify_api, mocker):
|
||||||
|
mock_s3upload = mocker.patch('app.celery.research_mode_tasks.s3upload')
|
||||||
|
filename = 'NOTIFY.20180125140000.RSP.TXT'
|
||||||
|
|
||||||
|
with requests_mock.Mocker() as request_mock:
|
||||||
|
request_mock.post(
|
||||||
|
'http://localhost:6011/notifications/letter/dvla',
|
||||||
|
content=b'{}',
|
||||||
|
status_code=200
|
||||||
|
)
|
||||||
|
|
||||||
|
create_fake_letter_response_file('random-ref')
|
||||||
|
|
||||||
|
mock_s3upload.assert_called_once_with(
|
||||||
|
filedata='random-ref|Sent|1|Sorted',
|
||||||
|
region=current_app.config['AWS_REGION'],
|
||||||
|
bucket_name=current_app.config['DVLA_RESPONSE_BUCKET_NAME'],
|
||||||
|
file_location=filename
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@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.s3upload')
|
||||||
|
filename = 'NOTIFY.20180125140000.RSP.TXT'
|
||||||
|
|
||||||
|
with set_config_values(notify_api, {
|
||||||
|
'NOTIFY_ENVIRONMENT': 'development'
|
||||||
|
}):
|
||||||
|
with requests_mock.Mocker() as request_mock:
|
||||||
|
request_mock.post(
|
||||||
|
'http://localhost:6011/notifications/letter/dvla',
|
||||||
|
content=b'{}',
|
||||||
|
status_code=200
|
||||||
|
)
|
||||||
|
|
||||||
|
create_fake_letter_response_file('random-ref')
|
||||||
|
|
||||||
|
assert request_mock.last_request.json() == {
|
||||||
|
"Type": "Notification",
|
||||||
|
"MessageId": "some-message-id",
|
||||||
|
"Message": '{"Records":[{"s3":{"object":{"key":"' + filename + '"}}}]}'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@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.s3upload')
|
||||||
|
|
||||||
|
with set_config_values(notify_api, {
|
||||||
|
'NOTIFY_ENVIRONMENT': 'preview'
|
||||||
|
}):
|
||||||
|
with requests_mock.Mocker() as request_mock:
|
||||||
|
create_fake_letter_response_file('random-ref')
|
||||||
|
|
||||||
|
assert request_mock.last_request is None
|
||||||
|
|||||||
Reference in New Issue
Block a user