2018-07-11 17:02:49 +01:00
import json
import uuid
2024-02-16 16:30:17 -05:00
from app . enums import NotificationType
2018-07-11 17:02:49 +01:00
from app . models import ServiceDataRetention
2021-08-04 15:12:09 +01:00
from tests import create_admin_authorization_header
2018-07-11 17:02:49 +01:00
from tests . app . db import create_service_data_retention
2018-07-13 15:18:27 +01:00
def test_get_service_data_retention ( client , sample_service ) :
2019-06-03 17:27:08 +01:00
sms_data_retention = create_service_data_retention ( service = sample_service )
2023-08-29 14:54:30 -07:00
email_data_retention = create_service_data_retention (
2024-02-21 11:27:38 -05:00
service = sample_service ,
notification_type = NotificationType . EMAIL ,
days_of_retention = 10 ,
2023-08-29 14:54:30 -07:00
)
2018-07-13 15:18:27 +01:00
response = client . get (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id !s} /data-retention " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
2018-07-13 15:18:27 +01:00
)
assert response . status_code == 200
json_response = json . loads ( response . get_data ( as_text = True ) )
2023-03-02 20:20:31 -05:00
assert len ( json_response ) == 2
2024-02-09 11:42:53 -05:00
assert json_response [ 0 ] == sms_data_retention . serialize ( )
assert json_response [ 1 ] == email_data_retention . serialize ( )
2018-07-13 15:18:27 +01:00
def test_get_service_data_retention_returns_empty_list ( client , sample_service ) :
response = client . get (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id !s} /data-retention " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
2018-07-13 15:18:27 +01:00
)
assert response . status_code == 200
assert len ( json . loads ( response . get_data ( as_text = True ) ) ) == 0
2018-08-13 16:44:24 +01:00
def test_get_data_retention_for_service_notification_type ( client , sample_service ) :
2019-06-03 17:27:08 +01:00
data_retention = create_service_data_retention ( service = sample_service )
2023-08-29 14:54:30 -07:00
response = client . get (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id } /data-retention/ "
f " notification-type/ { NotificationType . SMS } " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
)
2018-08-13 16:44:24 +01:00
assert response . status_code == 200
assert json . loads ( response . get_data ( as_text = True ) ) == data_retention . serialize ( )
2018-07-13 15:18:27 +01:00
def test_get_service_data_retention_by_id ( client , sample_service ) :
2019-06-03 17:27:08 +01:00
sms_data_retention = create_service_data_retention ( service = sample_service )
2023-08-29 14:54:30 -07:00
create_service_data_retention (
2024-02-16 16:30:17 -05:00
service = sample_service ,
notification_type = NotificationType . EMAIL ,
days_of_retention = 10 ,
2023-08-29 14:54:30 -07:00
)
create_service_data_retention (
2024-02-16 16:30:17 -05:00
service = sample_service ,
notification_type = NotificationType . LETTER ,
days_of_retention = 30 ,
2023-08-29 14:54:30 -07:00
)
2018-07-13 15:18:27 +01:00
response = client . get (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id !s} /data-retention/ { sms_data_retention . id } " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
2018-07-13 15:18:27 +01:00
)
assert response . status_code == 200
assert json . loads ( response . get_data ( as_text = True ) ) == sms_data_retention . serialize ( )
2023-08-29 14:54:30 -07:00
def test_get_service_data_retention_by_id_returns_none_when_no_data_retention_exists (
client , sample_service
) :
2018-07-13 15:18:27 +01:00
response = client . get (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id !s} /data-retention/ { uuid . uuid4 ( ) } " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
2018-07-13 15:18:27 +01:00
)
assert response . status_code == 200
assert json . loads ( response . get_data ( as_text = True ) ) == { }
2018-07-11 17:02:49 +01:00
def test_create_service_data_retention ( client , sample_service ) :
2024-02-16 16:30:17 -05:00
data = { " notification_type " : NotificationType . SMS , " days_of_retention " : 3 }
2018-07-11 17:02:49 +01:00
response = client . post (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id !s} /data-retention " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
data = json . dumps ( data ) ,
2018-07-11 17:02:49 +01:00
)
assert response . status_code == 201
2023-08-29 14:54:30 -07:00
json_resp = json . loads ( response . get_data ( as_text = True ) ) [ " result " ]
2018-07-11 17:02:49 +01:00
results = ServiceDataRetention . query . all ( )
assert len ( results ) == 1
data_retention = results [ 0 ]
assert json_resp == data_retention . serialize ( )
2023-08-29 14:54:30 -07:00
def test_create_service_data_retention_returns_400_when_notification_type_is_invalid (
client ,
) :
data = { " notification_type " : " unknown " , " days_of_retention " : 3 }
2018-07-11 17:02:49 +01:00
response = client . post (
2024-02-16 16:30:17 -05:00
f " /service/ { uuid . uuid4 ( ) !s} /data-retention " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
data = json . dumps ( data ) ,
2018-07-11 17:02:49 +01:00
)
json_resp = json . loads ( response . get_data ( as_text = True ) )
assert response . status_code == 400
2023-08-29 14:54:30 -07:00
assert json_resp [ " errors " ] [ 0 ] [ " error " ] == " ValidationError "
assert (
json_resp [ " errors " ] [ 0 ] [ " message " ]
2024-02-22 17:06:18 -05:00
== f " notification_type unknown is not one of [ { ' , ' . join ( [ f ' < { type ( e ) . __name__ } . { e . name } : { e . value } > ' for e in ( NotificationType . SMS , NotificationType . EMAIL ) ] ) } ] "
2023-08-29 14:54:30 -07:00
)
2018-07-11 17:02:49 +01:00
def test_create_service_data_retention_returns_400_when_data_retention_for_notification_type_already_exists (
2023-08-29 14:54:30 -07:00
client , sample_service
2018-07-11 17:02:49 +01:00
) :
2019-06-03 17:27:08 +01:00
create_service_data_retention ( service = sample_service )
2024-02-16 16:30:17 -05:00
data = { " notification_type " : NotificationType . SMS , " days_of_retention " : 3 }
2018-07-11 17:02:49 +01:00
response = client . post (
2024-02-16 16:30:17 -05:00
f " /service/ { uuid . uuid4 ( ) !s} /data-retention " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
data = json . dumps ( data ) ,
2018-07-11 17:02:49 +01:00
)
assert response . status_code == 400
json_resp = json . loads ( response . get_data ( as_text = True ) )
2023-08-29 14:54:30 -07:00
assert json_resp [ " result " ] == " error "
assert (
json_resp [ " message " ]
== " Service already has data retention for sms notification type "
)
2018-07-11 17:02:49 +01:00
def test_modify_service_data_retention ( client , sample_service ) :
2019-06-03 17:27:08 +01:00
data_retention = create_service_data_retention ( service = sample_service )
2023-08-29 14:54:30 -07:00
data = { " days_of_retention " : 3 }
2018-07-11 17:02:49 +01:00
response = client . post (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id } /data-retention/ { data_retention . id } " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
data = json . dumps ( data ) ,
2018-07-11 17:02:49 +01:00
)
assert response . status_code == 204
2023-08-29 14:54:30 -07:00
assert response . get_data ( as_text = True ) == " "
2018-07-11 17:02:49 +01:00
2023-08-29 14:54:30 -07:00
def test_modify_service_data_retention_returns_400_when_data_retention_does_not_exist (
client , sample_service
) :
data = { " days_of_retention " : 3 }
2018-07-11 17:02:49 +01:00
response = client . post (
2024-02-16 16:30:17 -05:00
f " /service/ { sample_service . id } /data-retention/ { uuid . uuid4 ( ) } " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
data = json . dumps ( data ) ,
2018-07-11 17:02:49 +01:00
)
assert response . status_code == 404
def test_modify_service_data_retention_returns_400_when_data_is_invalid ( client ) :
2023-08-29 14:54:30 -07:00
data = { " bad_key " : 3 }
2018-07-11 17:02:49 +01:00
response = client . post (
2024-02-16 16:30:17 -05:00
f " /service/ { uuid . uuid4 ( ) } /data-retention/ { uuid . uuid4 ( ) } " ,
2023-08-29 14:54:30 -07:00
headers = [
( " Content-Type " , " application/json " ) ,
create_admin_authorization_header ( ) ,
] ,
data = json . dumps ( data ) ,
2018-07-11 17:02:49 +01:00
)
assert response . status_code == 400