mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
Implemented the post email notifications endpoint for v2
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from flask import json
|
||||
|
||||
from app.models import Notification
|
||||
from tests import create_authorization_header
|
||||
|
||||
@@ -102,3 +102,55 @@ def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, s
|
||||
assert error_resp['errors'] == [{'error': 'ValidationError',
|
||||
'message': "template_id is a required property"
|
||||
}]
|
||||
|
||||
|
||||
def test_post_email_notification_returns_201(client, sample_email_template, mocker):
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
data = {
|
||||
"reference": "reference from caller",
|
||||
"email_address": sample_email_template.service.users[0].email_address,
|
||||
"template_id": sample_email_template.id,
|
||||
}
|
||||
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
|
||||
response = client.post(
|
||||
path="v2/notifications/email",
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert response.status_code == 201
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
notification_id = notifications[0].id
|
||||
assert resp_json['id'] is not None
|
||||
assert resp_json['reference'] is None
|
||||
assert resp_json['content']['body'] == sample_email_template.content
|
||||
assert resp_json['content']['subject'] == sample_email_template.subject
|
||||
assert resp_json['content']['from_email'] == sample_email_template.service.email_from
|
||||
assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri']
|
||||
assert resp_json['template']['id'] == str(sample_email_template.id)
|
||||
assert resp_json['template']['version'] == sample_email_template.version
|
||||
assert 'v2/templates/{}'.format(sample_email_template.id) in resp_json['template']['uri']
|
||||
assert mocked.called
|
||||
|
||||
|
||||
def test_post_email_notification_returns_404_and_missing_template(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
"email_address": sample_service.users[0].email_address,
|
||||
'template_id': str(uuid.uuid4())
|
||||
}
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
|
||||
response = client.post(
|
||||
path='/v2/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.headers['Content-type'] == 'application/json'
|
||||
|
||||
error_json = json.loads(response.get_data(as_text=True))
|
||||
assert error_json['status_code'] == 400
|
||||
assert error_json['errors'] == [{"error": "BadRequestError",
|
||||
"message": 'Template not found'}]
|
||||
|
||||
Reference in New Issue
Block a user