mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
[WIP] Added endpoints under /job for creating, updating and reading
notification status.
This commit is contained in:
@@ -14,9 +14,9 @@ def save_notification(notification, update_dict={}):
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def get_notification(job_id, notification_id):
|
||||
return Notification.query.filter_by(job_id=job_id, id=notification_id).one()
|
||||
def get_notification(service_id, job_id, notification_id):
|
||||
return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one()
|
||||
|
||||
|
||||
def get_notifications_by_job(job_id):
|
||||
return Notification.query.filter_by(job_id=job_id).all()
|
||||
def get_notifications(service_id, job_id):
|
||||
return Notification.query.filter_by(service_id=service_id, job_id=job_id).all()
|
||||
|
||||
@@ -8,7 +8,6 @@ from flask import (
|
||||
current_app
|
||||
)
|
||||
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.exc import DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
@@ -18,10 +17,19 @@ from app.dao.jobs_dao import (
|
||||
get_jobs_by_service
|
||||
)
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
save_notification,
|
||||
get_notification,
|
||||
get_notifications
|
||||
)
|
||||
|
||||
from app.schemas import (
|
||||
job_schema,
|
||||
jobs_schema,
|
||||
job_schema_load_json
|
||||
job_schema_load_json,
|
||||
notification_status_schema,
|
||||
notifications_status_schema,
|
||||
notification_status_schema_load_json
|
||||
)
|
||||
|
||||
job = Blueprint('job', __name__, url_prefix='/service/<service_id>/job')
|
||||
@@ -72,6 +80,55 @@ def update_job(service_id, job_id):
|
||||
return jsonify(data=job_schema.dump(job).data), 200
|
||||
|
||||
|
||||
@job.route('/<job_id>/notification', methods=['POST'])
|
||||
def create_notification_for_job(service_id, job_id):
|
||||
|
||||
# TODO assert service_id == payload service id
|
||||
# and same for job id
|
||||
notification, errors = notification_status_schema.load(request.get_json())
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
try:
|
||||
save_notification(notification)
|
||||
except Exception as e:
|
||||
return jsonify(result="error", message=str(e)), 500
|
||||
return jsonify(data=notification_status_schema.dump(notification).data), 201
|
||||
|
||||
|
||||
@job.route('/<job_id>/notification', methods=['GET'])
|
||||
@job.route('/<job_id>/notification/<notification_id>')
|
||||
def get_notification_for_job(service_id, job_id, notification_id=None):
|
||||
if notification_id:
|
||||
try:
|
||||
notification = get_notification(service_id, job_id, notification_id)
|
||||
data, errors = notification_status_schema.dump(notification)
|
||||
return jsonify(data=data)
|
||||
except DataError:
|
||||
return jsonify(result="error", message="Invalid notification id"), 400
|
||||
except NoResultFound:
|
||||
return jsonify(result="error", message="Notification not found"), 404
|
||||
else:
|
||||
notifications = get_notifications(service_id, job_id)
|
||||
data, errors = notifications_status_schema.dump(notifications)
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@job.route('/<job_id>/notification/<notification_id>', methods=['PUT'])
|
||||
def update_notification_for_job(service_id, job_id, notification_id):
|
||||
|
||||
notification = get_notification(service_id, job_id, notification_id)
|
||||
update_dict, errors = notification_status_schema_load_json.load(request.get_json())
|
||||
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
try:
|
||||
save_notification(notification, update_dict=update_dict)
|
||||
except Exception as e:
|
||||
return jsonify(result="error", message=str(e)), 400
|
||||
|
||||
return jsonify(data=job_schema.dump(notification).data), 200
|
||||
|
||||
|
||||
def _enqueue_job(job):
|
||||
aws_region = current_app.config['AWS_REGION']
|
||||
queue_name = current_app.config['NOTIFY_JOB_QUEUE']
|
||||
|
||||
@@ -129,6 +129,12 @@ class EmailNotificationSchema(NotificationSchema):
|
||||
body = fields.Str(load_from="message", dump_to='message', required=True)
|
||||
|
||||
|
||||
class NotificationStatusSchema(BaseSchema):
|
||||
|
||||
class Meta:
|
||||
model = models.Notification
|
||||
|
||||
|
||||
user_schema = UserSchema()
|
||||
user_schema_load_json = UserSchema(load_json=True)
|
||||
users_schema = UserSchema(many=True)
|
||||
@@ -148,3 +154,6 @@ request_verify_code_schema = RequestVerifyCodeSchema()
|
||||
sms_admin_notification_schema = SmsAdminNotificationSchema()
|
||||
sms_template_notification_schema = SmsTemplateNotificationSchema()
|
||||
email_notification_schema = EmailNotificationSchema()
|
||||
notification_status_schema = NotificationStatusSchema()
|
||||
notifications_status_schema = NotificationStatusSchema(many=True)
|
||||
notification_status_schema_load_json = NotificationStatusSchema(load_json=True)
|
||||
|
||||
@@ -5,7 +5,7 @@ from app.models import Notification
|
||||
from app.dao.notifications_dao import (
|
||||
save_notification,
|
||||
get_notification,
|
||||
get_notifications_by_job
|
||||
get_notifications
|
||||
)
|
||||
|
||||
|
||||
@@ -39,7 +39,9 @@ def test_save_notification(notify_db, notify_db_session, sample_template, sample
|
||||
|
||||
|
||||
def test_get_notification_for_job(notify_db, notify_db_session, sample_notification):
|
||||
notifcation_from_db = get_notification(sample_notification.job_id, sample_notification.id)
|
||||
notifcation_from_db = get_notification(sample_notification.service.id,
|
||||
sample_notification.job_id,
|
||||
sample_notification.id)
|
||||
assert sample_notification == notifcation_from_db
|
||||
|
||||
|
||||
@@ -53,7 +55,7 @@ def test_get_all_notifications_for_job(notify_db, notify_db_session, sample_job)
|
||||
template=sample_job.template,
|
||||
job=sample_job)
|
||||
|
||||
notifcations_from_db = get_notifications_by_job(sample_job.id)
|
||||
notifcations_from_db = get_notifications(sample_job.service.id, sample_job.id)
|
||||
assert len(notifcations_from_db) == 5
|
||||
|
||||
|
||||
@@ -61,10 +63,10 @@ def test_update_notification(notify_db, notify_db_session, sample_notification):
|
||||
assert sample_notification.status == 'sent'
|
||||
|
||||
update_dict = {
|
||||
'id': sample_notification.id,
|
||||
'service': sample_notification.service,
|
||||
'template': sample_notification.template,
|
||||
'job': sample_notification.job,
|
||||
'id': str(sample_notification.id),
|
||||
'service': str(sample_notification.service.id),
|
||||
'template': sample_notification.template.id,
|
||||
'job': str(sample_notification.job.id),
|
||||
'status': 'failed'
|
||||
}
|
||||
|
||||
|
||||
@@ -163,6 +163,130 @@ def test_get_update_job_status(notify_api,
|
||||
assert resp_json['data']['status'] == 'in progress'
|
||||
|
||||
|
||||
def test_get_notification(notify_api, notify_db, notify_db_session, sample_notification):
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
path = url_for('job.get_notification_for_job',
|
||||
service_id=sample_notification.service.id,
|
||||
job_id=sample_notification.job.id,
|
||||
notification_id=sample_notification.id)
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_notification.service.id,
|
||||
path=path,
|
||||
method='GET')
|
||||
|
||||
headers = [('Content-Type', 'application/json'), auth_header]
|
||||
response = client.get(path, headers=headers)
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert str(sample_notification.id) == resp_json['data']['id']
|
||||
assert str(sample_notification.service.id) == resp_json['data']['service']
|
||||
assert sample_notification.template.id == resp_json['data']['template']
|
||||
assert str(sample_notification.job.id) == resp_json['data']['job']
|
||||
assert sample_notification.status == resp_json['data']['status']
|
||||
|
||||
|
||||
def test_get_notifications(notify_api, notify_db, notify_db_session, sample_job):
|
||||
|
||||
from tests.app.conftest import sample_notification
|
||||
for i in range(0, 5):
|
||||
sample_notification(notify_db,
|
||||
notify_db_session,
|
||||
service=sample_job.service,
|
||||
template=sample_job.template,
|
||||
job=sample_job)
|
||||
|
||||
service_id = str(sample_job.service.id)
|
||||
job_id = str(sample_job.id)
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
path = url_for('job.get_notification_for_job',
|
||||
service_id=service_id,
|
||||
job_id=job_id)
|
||||
|
||||
auth_header = create_authorization_header(service_id=service_id,
|
||||
path=path,
|
||||
method='GET')
|
||||
|
||||
headers = [('Content-Type', 'application/json'), auth_header]
|
||||
response = client.get(path, headers=headers)
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert len(resp_json['data']) == 5
|
||||
|
||||
|
||||
def test_add_notification(notify_api, notify_db, notify_db_session, sample_job):
|
||||
|
||||
notificaton_id = uuid.uuid4()
|
||||
to = '+44709123456'
|
||||
data = {
|
||||
'id': str(notificaton_id),
|
||||
'to': to,
|
||||
'job': str(sample_job.id),
|
||||
'service': str(sample_job.service.id),
|
||||
'template': sample_job.template.id
|
||||
}
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
path = url_for('job.create_notification_for_job',
|
||||
service_id=sample_job.service.id,
|
||||
job_id=sample_job.id)
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_job.service.id,
|
||||
path=path,
|
||||
method='POST',
|
||||
request_body=json.dumps(data))
|
||||
|
||||
headers = [('Content-Type', 'application/json'), auth_header]
|
||||
|
||||
response = client.post(path, headers=headers, data=json.dumps(data))
|
||||
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert data['id'] == resp_json['data']['id']
|
||||
assert data['to'] == resp_json['data']['to']
|
||||
assert data['service'] == resp_json['data']['service']
|
||||
assert data['template'] == resp_json['data']['template']
|
||||
assert data['job'] == resp_json['data']['job']
|
||||
assert 'sent' == resp_json['data']['status']
|
||||
|
||||
|
||||
def test_update_notification(notify_api, notify_db, notify_db_session, sample_notification):
|
||||
|
||||
assert sample_notification.status == 'sent'
|
||||
|
||||
update_data = {
|
||||
'id': str(sample_notification.id),
|
||||
'to': sample_notification.to,
|
||||
'job': str(sample_notification.job.id),
|
||||
'service': str(sample_notification.service.id),
|
||||
'template': sample_notification.template.id,
|
||||
'status': 'failed'
|
||||
}
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
path = url_for('job.update_notification_for_job',
|
||||
service_id=sample_notification.service.id,
|
||||
job_id=sample_notification.job.id,
|
||||
notification_id=sample_notification.id)
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_notification.service.id,
|
||||
path=path,
|
||||
method='PUT',
|
||||
request_body=json.dumps(update_data))
|
||||
|
||||
headers = [('Content-Type', 'application/json'), auth_header]
|
||||
|
||||
response = client.put(path, headers=headers, data=json.dumps(update_data))
|
||||
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert update_data['id'] == resp_json['data']['id']
|
||||
assert 'failed' == resp_json['data']['status']
|
||||
|
||||
|
||||
def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
|
||||
for i in range(number_of_jobs):
|
||||
create_job(notify_db, notify_db_session, service=template.service,
|
||||
|
||||
Reference in New Issue
Block a user