mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:15:19 -05:00
Add a document download client
Allows uploading documents to the Document Download API. The client is configured with an API host and auth token. There's no need for a flag to disable the client in the test environments at the moment since the upload is only triggered by a specific payload which would only be sent with an explicit goal of using document download.
This commit is contained in:
@@ -16,6 +16,7 @@ from werkzeug.local import LocalProxy
|
||||
|
||||
from app.celery.celery import NotifyCelery
|
||||
from app.clients import Clients
|
||||
from app.clients.document_download import DocumentDownloadClient
|
||||
from app.clients.email.aws_ses import AwsSesClient
|
||||
from app.clients.sms.firetext import FiretextClient
|
||||
from app.clients.sms.loadtesting import LoadtestingClient
|
||||
@@ -39,6 +40,7 @@ deskpro_client = DeskproClient()
|
||||
statsd_client = StatsdClient()
|
||||
redis_store = RedisClient()
|
||||
performance_platform_client = PerformancePlatformClient()
|
||||
document_download_client = DocumentDownloadClient()
|
||||
|
||||
clients = Clients()
|
||||
|
||||
@@ -71,6 +73,7 @@ def create_app(application):
|
||||
encryption.init_app(application)
|
||||
redis_store.init_app(application)
|
||||
performance_platform_client.init_app(application)
|
||||
document_download_client.init_app(application)
|
||||
clients.init_app(sms_clients=[firetext_client, mmg_client, loadtest_client], email_clients=[aws_ses_client])
|
||||
|
||||
register_blueprint(application)
|
||||
|
||||
27
app/clients/document_download.py
Normal file
27
app/clients/document_download.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import base64
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class DocumentDownloadClient:
|
||||
|
||||
def init_app(self, app):
|
||||
self.api_host = app.config['DOCUMENT_DOWNLOAD_API_HOST']
|
||||
self.auth_token = app.config['DOCUMENT_DOWNLOAD_API_KEY']
|
||||
|
||||
def get_upload_url(self, service_id):
|
||||
return "{}/services/{}/documents".format(self.api_host, service_id)
|
||||
|
||||
def upload_document(self, service_id, file_contents):
|
||||
response = requests.post(
|
||||
self.get_upload_url(service_id),
|
||||
headers={
|
||||
'Authorization': "Bearer {}".format(self.auth_token),
|
||||
},
|
||||
files={
|
||||
'document': base64.b64decode(file_contents)
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json()['document']['url']
|
||||
@@ -318,6 +318,9 @@ class Config(object):
|
||||
TEMPLATE_PREVIEW_API_HOST = os.environ.get('TEMPLATE_PREVIEW_API_HOST', 'http://localhost:6013')
|
||||
TEMPLATE_PREVIEW_API_KEY = os.environ.get('TEMPLATE_PREVIEW_API_KEY', 'my-secret-key')
|
||||
|
||||
DOCUMENT_DOWNLOAD_API_HOST = os.environ.get('DOCUMENT_DOWNLOAD_API_HOST', 'http://localhost:7000')
|
||||
DOCUMENT_DOWNLOAD_API_KEY = os.environ.get('DOCUMENT_DOWNLOAD_API_KEY', 'auth-token')
|
||||
|
||||
LETTER_PROCESSING_DEADLINE = time(17, 30)
|
||||
|
||||
MMG_URL = "https://api.mmg.co.uk/json/api.php"
|
||||
|
||||
37
tests/app/clients/test_document_download.py
Normal file
37
tests/app/clients/test_document_download.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import requests
|
||||
import requests_mock
|
||||
import pytest
|
||||
|
||||
from app.clients.document_download import DocumentDownloadClient
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def document_download(client, mocker):
|
||||
client = DocumentDownloadClient()
|
||||
current_app = mocker.Mock(config={
|
||||
'DOCUMENT_DOWNLOAD_API_HOST': 'https://document-download',
|
||||
'DOCUMENT_DOWNLOAD_API_KEY': 'test-key'
|
||||
})
|
||||
client.init_app(current_app)
|
||||
return client
|
||||
|
||||
|
||||
def test_get_upload_url(document_download):
|
||||
assert document_download.get_upload_url('service-id') == 'https://document-download/services/service-id/documents'
|
||||
|
||||
|
||||
def test_upload_document(document_download):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
request_mock.post('https://document-download/services/service-id/documents', json={
|
||||
'document': {'url': 'https://document-download/services/service-id/documents/uploaded-url'}
|
||||
}, status_code=201)
|
||||
|
||||
resp = document_download.upload_document('service-id', 'abababab')
|
||||
|
||||
assert resp == 'https://document-download/services/service-id/documents/uploaded-url'
|
||||
|
||||
|
||||
def test_should_raise_for_status(document_download):
|
||||
with pytest.raises(requests.HTTPError), requests_mock.Mocker() as request_mock:
|
||||
request_mock.post('https://document-download/services/service-id/documents', json={}, status_code=403)
|
||||
document_download.upload_document('service-id', 'abababab')
|
||||
Reference in New Issue
Block a user