New dao and endpoints to create and update service data retention.

This commit is contained in:
Rebecca Law
2018-07-11 17:02:49 +01:00
parent e4cc90e585
commit e2a1dfeb31
7 changed files with 302 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
from datetime import datetime
from app import db
from app.dao.dao_utils import transactional
from app.models import ServiceDataRetention
def get_service_data_retention(service_id):
data_retention = ServiceDataRetention.query.filter_by(service_id=service_id).all()
return data_retention
@transactional
def insert_service_data_retention(service_id, notification_type, days_of_retention):
new_data_retention = ServiceDataRetention(service_id=service_id,
notification_type=notification_type,
days_of_retention=days_of_retention)
db.session.add(new_data_retention)
return new_data_retention
@transactional
def update_service_data_retention(service_data_retention_id, service_id, days_of_retention):
updated_count = ServiceDataRetention.query.filter(
ServiceDataRetention.id == service_data_retention_id,
ServiceDataRetention.service_id == service_id
).update(
{
"days_of_retention": days_of_retention,
"updated_at": datetime.utcnow()
}
)
return updated_count

View File

@@ -1859,3 +1859,14 @@ class ServiceDataRetention(db.Model):
__table_args__ = (
UniqueConstraint('service_id', 'notification_type', name='uix_service_data_retention'),
)
def serialize(self):
return {
"id": str(self.id),
"service_id": str(self.service_id),
"service_name": self.service.name,
"notification_type": self.notification_type,
"days_of_retention": self.days_of_retention,
"created_at": self.created_at.strftime(DATETIME_FORMAT),
"updated_at": self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None,
}

View File

@@ -24,6 +24,7 @@ from app.dao.fact_notification_status_dao import (
)
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
from app.dao.organisation_dao import dao_get_organisation_by_service_id
from app.dao.service_data_retention_dao import insert_service_data_retention, update_service_data_retention
from app.dao.service_sms_sender_dao import (
archive_sms_sender,
dao_add_sms_sender_for_service,
@@ -77,6 +78,10 @@ from app.errors import (
from app.models import Service, EmailBranding
from app.schema_validation import validate
from app.service import statistics
from app.service.service_data_retention_schema import (
add_service_data_retention_request,
update_service_data_retention_request
)
from app.service.service_senders_schema import (
add_service_email_reply_to_request,
add_service_letter_contact_block_request,
@@ -754,6 +759,42 @@ def is_service_name_unique():
return jsonify(result=result), 200
@service_blueprint.route('/<uuid:service_id>/data-retention', methods=['POST'])
def create_service_data_retention(service_id):
form = validate(request.get_json(), add_service_data_retention_request)
try:
new_data_retention = insert_service_data_retention(
service_id=service_id,
notification_type=form.get("notification_type"),
days_of_retention=form.get("days_of_retention")
)
except IntegrityError:
raise InvalidRequest(
message="Service already has data retention for {} notification type".format(form.get("notification_type")),
status_code=400
)
return jsonify(result=new_data_retention.serialize()), 201
@service_blueprint.route('/<uuid:service_id>/data-retention/<uuid:data_retention_id>', methods=['POST'])
def modify_service_data_retention(service_id, data_retention_id):
form = validate(request.get_json(), update_service_data_retention_request)
update_count = update_service_data_retention(
service_data_retention_id=data_retention_id,
service_id=service_id,
days_of_retention=form.get("days_of_retention")
)
if update_count == 0:
raise InvalidRequest(
message="The service data retention for id: {} was not found for service: {}".format(data_retention_id,
service_id),
status_code=404)
return '', 204
def check_request_args(request):
service_id = request.args.get('service_id')
name = request.args.get('name', None)

View File

@@ -0,0 +1,23 @@
add_service_data_retention_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST service data retention schema",
"title": "Add service data retention for notification type api",
"type": "object",
"properties": {
"days_of_retention": {"type": "integer"},
"notification_type": {"enum": ["sms", "letter", "email"]},
},
"required": ["days_of_retention", "notification_type"]
}
update_service_data_retention_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST service data retention schema",
"title": "Update service data retention for notification type api",
"type": "object",
"properties": {
"days_of_retention": {"type": "integer"},
},
"required": ["days_of_retention"]
}

View File

@@ -0,0 +1,78 @@
import uuid
from datetime import datetime
import pytest
from sqlalchemy.exc import IntegrityError
from app.dao.service_data_retention_dao import insert_service_data_retention, update_service_data_retention
from app.models import ServiceDataRetention
def test_insert_service_data_retention(sample_service):
insert_service_data_retention(
service_id=sample_service.id,
notification_type='email',
days_of_retention=3
)
results = ServiceDataRetention.query.all()
assert len(results) == 1
assert results[0].service_id == sample_service.id
assert results[0].notification_type == 'email'
assert results[0].days_of_retention == 3
assert results[0].created_at.date() == datetime.utcnow().date()
def test_insert_service_data_retention_throws_unique_constraint(sample_service):
insert_service_data_retention(service_id=sample_service.id,
notification_type='email',
days_of_retention=3
)
with pytest.raises(expected_exception=IntegrityError):
insert_service_data_retention(service_id=sample_service.id,
notification_type='email',
days_of_retention=5
)
def test_update_service_data_retention(sample_service):
data_retention = insert_service_data_retention(service_id=sample_service.id,
notification_type='sms',
days_of_retention=3
)
updated_count = update_service_data_retention(service_data_retention_id=data_retention.id,
service_id=sample_service.id,
days_of_retention=5
)
assert updated_count == 1
results = ServiceDataRetention.query.all()
assert len(results) == 1
assert results[0].id == data_retention.id
assert results[0].service_id == sample_service.id
assert results[0].notification_type == 'sms'
assert results[0].days_of_retention == 5
assert results[0].created_at.date() == datetime.utcnow().date()
assert results[0].updated_at.date() == datetime.utcnow().date()
def test_update_service_data_retention_does_not_update_if_row_does_not_exist(sample_service):
updated_count = update_service_data_retention(
service_data_retention_id=uuid.uuid4(),
service_id=sample_service.id,
days_of_retention=5
)
assert updated_count == 0
assert len(ServiceDataRetention.query.all()) == 0
def test_update_service_data_retention_does_not_update_row_if_data_retention_is_for_different_service(
sample_service
):
data_retention = insert_service_data_retention(service_id=sample_service.id,
notification_type='email',
days_of_retention=3
)
updated_count = update_service_data_retention(service_data_retention_id=data_retention.id,
service_id=uuid.uuid4(),
days_of_retention=5)
assert updated_count == 0

View File

@@ -3,6 +3,7 @@ import uuid
from app import db
from app.dao.jobs_dao import dao_create_job
from app.dao.service_data_retention_dao import insert_service_data_retention
from app.dao.service_inbound_api_dao import save_service_inbound_api
from app.dao.service_callback_api_dao import save_service_callback_api
from app.dao.service_sms_sender_dao import update_existing_sms_sender_with_inbound_number, dao_update_service_sms_sender
@@ -35,7 +36,7 @@ from app.models import (
InvitedOrganisationUser,
FactBilling,
FactNotificationStatus,
Complaint
Complaint,
)
from app.dao.users_dao import save_model_user
from app.dao.notifications_dao import (
@@ -663,3 +664,16 @@ def ses_notification_callback():
'dd426d95ee9390147a5624348ee.pem",' \
'\n "UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&S' \
'subscriptionArn=arn:aws:sns:eu-west-1:302763885840:preview-emails:d6aad3ef-83d6-4cf3-a470-54e2e75916da"\n}'
def create_service_data_retention(
service_id,
notification_type='sms',
days_of_retention=3
):
data_retention = insert_service_data_retention(
service_id=service_id,
notification_type=notification_type,
days_of_retention=days_of_retention
)
return data_retention

View File

@@ -0,0 +1,100 @@
import json
import uuid
from app.models import ServiceDataRetention
from tests import create_authorization_header
from tests.app.db import create_service_data_retention
def test_create_service_data_retention(client, sample_service):
data = {
"notification_type": 'sms',
"days_of_retention": 3
}
response = client.post(
'/service/{}/data-retention'.format(str(sample_service.id)),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps(data)
)
assert response.status_code == 201
json_resp = json.loads(response.get_data(as_text=True))['result']
results = ServiceDataRetention.query.all()
assert len(results) == 1
data_retention = results[0]
assert json_resp == data_retention.serialize()
def test_create_service_data_retention_returns_400_when_notification_type_is_invalid(client):
data = {
"notification_type": 'unknown',
"days_of_retention": 3
}
response = client.post(
'/service/{}/data-retention'.format(str(uuid.uuid4())),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps(data)
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert json_resp['errors'][0]['error'] == 'ValidationError'
assert json_resp['errors'][0]['message'] == 'notification_type unknown is not one of [sms, letter, email]'
def test_create_service_data_retention_returns_400_when_data_retention_for_notification_type_already_exists(
client, sample_service
):
create_service_data_retention(service_id=sample_service.id)
data = {
"notification_type": "sms",
"days_of_retention": 3
}
response = client.post(
'/service/{}/data-retention'.format(str(uuid.uuid4())),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps(data)
)
assert response.status_code == 400
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Service already has data retention for sms notification type'
def test_modify_service_data_retention(client, sample_service):
data_retention = create_service_data_retention(service_id=sample_service.id)
data = {
"days_of_retention": 3
}
response = client.post(
'/service/{}/data-retention/{}'.format(sample_service.id, data_retention.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps(data)
)
assert response.status_code == 204
assert response.get_data(as_text=True) == ''
def test_modify_service_data_retention_returns_400_when_data_retention_does_not_exist(client, sample_service):
data = {
"days_of_retention": 3
}
response = client.post(
'/service/{}/data-retention/{}'.format(sample_service.id, uuid.uuid4()),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps(data)
)
assert response.status_code == 404
def test_modify_service_data_retention_returns_400_when_data_is_invalid(client):
data = {
"bad_key": 3
}
response = client.post(
'/service/{}/data-retention/{}'.format(uuid.uuid4(), uuid.uuid4()),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps(data)
)
assert response.status_code == 400