2016-09-27 13:51:43 +01:00
|
|
|
import uuid
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from tests import create_authorization_header
|
|
|
|
|
|
|
|
|
|
from app.models import (
|
2020-07-28 10:22:13 +01:00
|
|
|
ServiceGuestList,
|
2016-09-27 13:51:43 +01:00
|
|
|
MOBILE_TYPE, EMAIL_TYPE)
|
|
|
|
|
|
2020-07-28 11:28:19 +01:00
|
|
|
from app.dao.service_guest_list_dao import dao_add_and_commit_guest_list_contacts
|
2016-09-27 13:51:43 +01:00
|
|
|
|
|
|
|
|
|
2020-08-03 17:11:58 +01:00
|
|
|
def test_get_guest_list_returns_data(client, sample_service_guest_list):
|
2020-07-28 11:22:19 +01:00
|
|
|
service_id = sample_service_guest_list.service_id
|
2016-09-27 13:51:43 +01:00
|
|
|
|
2020-08-03 17:11:58 +01:00
|
|
|
response = client.get(f'service/{service_id}/guest-list', headers=[create_authorization_header()])
|
2016-09-27 13:51:43 +01:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert json.loads(response.get_data(as_text=True)) == {
|
2020-07-28 11:22:19 +01:00
|
|
|
'email_addresses': [sample_service_guest_list.recipient],
|
2016-09-27 13:51:43 +01:00
|
|
|
'phone_numbers': []
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-27 14:16:35 +01:00
|
|
|
|
2020-07-28 11:22:19 +01:00
|
|
|
def test_get_guest_list_separates_emails_and_phones(client, sample_service):
|
2020-07-28 10:18:47 +01:00
|
|
|
dao_add_and_commit_guest_list_contacts([
|
2020-07-28 10:22:13 +01:00
|
|
|
ServiceGuestList.from_string(sample_service.id, EMAIL_TYPE, 'service@example.com'),
|
|
|
|
|
ServiceGuestList.from_string(sample_service.id, MOBILE_TYPE, '07123456789'),
|
|
|
|
|
ServiceGuestList.from_string(sample_service.id, MOBILE_TYPE, '+1800-555-555'),
|
2016-09-27 13:51:43 +01:00
|
|
|
])
|
|
|
|
|
|
2020-07-14 16:26:32 +01:00
|
|
|
response = client.get('service/{}/guest-list'.format(sample_service.id), headers=[create_authorization_header()])
|
2016-09-27 13:51:43 +01:00
|
|
|
assert response.status_code == 200
|
2017-09-04 17:24:41 +01:00
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert json_resp['email_addresses'] == ['service@example.com']
|
|
|
|
|
assert sorted(json_resp['phone_numbers']) == sorted(['+1800-555-555', '07123456789'])
|
2016-09-27 13:51:43 +01:00
|
|
|
|
|
|
|
|
|
2020-07-28 11:22:19 +01:00
|
|
|
def test_get_guest_list_404s_with_unknown_service_id(client):
|
2020-07-14 16:26:32 +01:00
|
|
|
path = 'service/{}/guest-list'.format(uuid.uuid4())
|
2016-09-27 13:51:43 +01:00
|
|
|
|
|
|
|
|
response = client.get(path, headers=[create_authorization_header()])
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
assert json_resp['message'] == 'No result found'
|
|
|
|
|
|
|
|
|
|
|
2020-07-28 11:22:19 +01:00
|
|
|
def test_get_guest_list_returns_no_data(client, sample_service):
|
2020-07-14 16:26:32 +01:00
|
|
|
path = 'service/{}/guest-list'.format(sample_service.id)
|
2016-09-27 13:51:43 +01:00
|
|
|
|
|
|
|
|
response = client.get(path, headers=[create_authorization_header()])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert json.loads(response.get_data(as_text=True)) == {'email_addresses': [], 'phone_numbers': []}
|
|
|
|
|
|
|
|
|
|
|
2020-08-03 17:11:58 +01:00
|
|
|
def test_update_guest_list_replaces_old_guest_list(client, sample_service_guest_list):
|
2016-09-27 13:51:43 +01:00
|
|
|
data = {
|
|
|
|
|
'email_addresses': ['foo@bar.com'],
|
|
|
|
|
'phone_numbers': ['07123456789']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = client.put(
|
2020-08-03 17:11:58 +01:00
|
|
|
f'service/{sample_service_guest_list.service_id}/guest-list',
|
2016-09-27 13:51:43 +01:00
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 204
|
2020-07-28 11:22:19 +01:00
|
|
|
guest_list = ServiceGuestList.query.order_by(ServiceGuestList.recipient).all()
|
|
|
|
|
assert len(guest_list) == 2
|
|
|
|
|
assert guest_list[0].recipient == '07123456789'
|
|
|
|
|
assert guest_list[1].recipient == 'foo@bar.com'
|
2016-09-27 13:51:43 +01:00
|
|
|
|
2016-09-27 14:16:35 +01:00
|
|
|
|
2020-07-28 11:22:19 +01:00
|
|
|
def test_update_guest_list_doesnt_remove_old_guest_list_if_error(client, sample_service_guest_list):
|
2016-09-27 13:51:43 +01:00
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'email_addresses': [''],
|
|
|
|
|
'phone_numbers': ['07123456789']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = client.put(
|
2020-07-28 11:22:19 +01:00
|
|
|
'service/{}/guest-list'.format(sample_service_guest_list.service_id),
|
2016-09-27 13:51:43 +01:00
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
assert json.loads(response.get_data(as_text=True)) == {
|
|
|
|
|
'result': 'error',
|
2020-07-28 10:23:22 +01:00
|
|
|
'message': 'Invalid guest list: "" is not a valid email address or phone number'
|
2016-09-27 13:51:43 +01:00
|
|
|
}
|
2020-07-28 11:22:19 +01:00
|
|
|
guest_list = ServiceGuestList.query.one()
|
|
|
|
|
assert guest_list.id == sample_service_guest_list.id
|