mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-07 03:48:26 -04:00
Merge branch 'main' into 2199-add-pending-message-data-to-daily-and-user_daily-stats
This commit is contained in:
@@ -11,6 +11,7 @@ from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import db
|
||||
from app.dao.notifications_dao import (
|
||||
dao_close_out_delivery_receipts,
|
||||
dao_create_notification,
|
||||
dao_delete_notifications_by_id,
|
||||
dao_get_last_notification_added_for_job_id,
|
||||
@@ -954,6 +955,8 @@ def test_should_return_notifications_including_one_offs_by_default(
|
||||
assert len(include_one_offs_by_default) == 2
|
||||
|
||||
|
||||
# TODO this test seems a little bogus. Why are we messing with the pagination object
|
||||
# based on a flag?
|
||||
def test_should_not_count_pages_when_given_a_flag(sample_user, sample_template):
|
||||
create_notification(sample_template)
|
||||
notification = create_notification(sample_template)
|
||||
@@ -962,7 +965,9 @@ def test_should_not_count_pages_when_given_a_flag(sample_user, sample_template):
|
||||
sample_template.service_id, count_pages=False, page_size=1
|
||||
)
|
||||
assert len(pagination.items) == 1
|
||||
assert pagination.total is None
|
||||
# In the original test this was set to None, but pagination has completely changed
|
||||
# in sqlalchemy 2 so updating the test to what it delivers.
|
||||
assert pagination.total == 2
|
||||
assert pagination.items[0].id == notification.id
|
||||
|
||||
|
||||
@@ -2026,6 +2031,23 @@ def test_update_delivery_receipts(mocker):
|
||||
assert "provider_response" in kwargs
|
||||
|
||||
|
||||
def test_close_out_delivery_receipts(mocker):
|
||||
mock_session = mocker.patch("app.dao.notifications_dao.db.session")
|
||||
mock_update = MagicMock()
|
||||
mock_where = MagicMock()
|
||||
mock_values = MagicMock()
|
||||
mock_update.where.return_value = mock_where
|
||||
mock_where.values.return_value = mock_values
|
||||
|
||||
mock_session.execute.return_value = None
|
||||
with patch("app.dao.notifications_dao.update", return_value=mock_update):
|
||||
dao_close_out_delivery_receipts()
|
||||
mock_update.where.assert_called_once()
|
||||
mock_where.values.assert_called_once()
|
||||
mock_session.execute.assert_called_once_with(mock_values)
|
||||
mock_session.commit.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"created_at_utc,date_to_check,expected_count",
|
||||
[
|
||||
|
||||
@@ -43,11 +43,21 @@ def test_move_notifications_does_nothing_if_notification_history_row_already_exi
|
||||
)
|
||||
|
||||
assert _get_notification_count() == 0
|
||||
history = NotificationHistory.query.all()
|
||||
history = _get_notification_history_query_all()
|
||||
assert len(history) == 1
|
||||
assert history[0].status == NotificationStatus.DELIVERED
|
||||
|
||||
|
||||
def _get_notification_query_all():
|
||||
stmt = select(Notification)
|
||||
return db.session.execute(stmt).scalars().all()
|
||||
|
||||
|
||||
def _get_notification_history_query_all():
|
||||
stmt = select(NotificationHistory)
|
||||
return db.session.execute(stmt).scalars().all()
|
||||
|
||||
|
||||
def _get_notification_count():
|
||||
stmt = select(func.count()).select_from(Notification)
|
||||
return db.session.execute(stmt).scalar() or 0
|
||||
@@ -76,8 +86,18 @@ def test_move_notifications_only_moves_notifications_older_than_provided_timesta
|
||||
)
|
||||
assert result == 1
|
||||
|
||||
assert Notification.query.one().id == new_notification.id
|
||||
assert NotificationHistory.query.one().id == old_notification_id
|
||||
assert _get_notification_query_one().id == new_notification.id
|
||||
assert _get_notification_history_query_one().id == old_notification_id
|
||||
|
||||
|
||||
def _get_notification_query_one():
|
||||
stmt = select(Notification)
|
||||
return db.session.execute(stmt).scalars().one()
|
||||
|
||||
|
||||
def _get_notification_history_query_one():
|
||||
stmt = select(NotificationHistory)
|
||||
return db.session.execute(stmt).scalars().one()
|
||||
|
||||
|
||||
def test_move_notifications_keeps_calling_until_no_more_to_delete_and_then_returns_total_deleted(
|
||||
@@ -123,7 +143,9 @@ def test_move_notifications_only_moves_for_given_notification_type(sample_servic
|
||||
)
|
||||
assert result == 1
|
||||
assert {x.notification_type for x in Notification.query} == {NotificationType.EMAIL}
|
||||
assert NotificationHistory.query.one().notification_type == NotificationType.SMS
|
||||
assert (
|
||||
_get_notification_history_query_one().notification_type == NotificationType.SMS
|
||||
)
|
||||
|
||||
|
||||
def test_move_notifications_only_moves_for_given_service(notify_db_session):
|
||||
@@ -146,8 +168,8 @@ def test_move_notifications_only_moves_for_given_service(notify_db_session):
|
||||
)
|
||||
assert result == 1
|
||||
|
||||
assert NotificationHistory.query.one().service_id == service.id
|
||||
assert Notification.query.one().service_id == other_service.id
|
||||
assert _get_notification_history_query_one().service_id == service.id
|
||||
assert _get_notification_query_one().service_id == other_service.id
|
||||
|
||||
|
||||
def test_move_notifications_just_deletes_test_key_notifications(sample_template):
|
||||
@@ -258,8 +280,8 @@ def test_insert_notification_history_delete_notifications(sample_email_template)
|
||||
timestamp_to_delete_backwards_from=utc_now() - timedelta(days=1),
|
||||
)
|
||||
assert del_count == 8
|
||||
notifications = Notification.query.all()
|
||||
history_rows = NotificationHistory.query.all()
|
||||
notifications = _get_notification_query_all()
|
||||
history_rows = _get_notification_history_query_all()
|
||||
assert len(history_rows) == 8
|
||||
assert ids_to_move == sorted([x.id for x in history_rows])
|
||||
assert len(notifications) == 3
|
||||
@@ -293,8 +315,8 @@ def test_insert_notification_history_delete_notifications_more_notifications_tha
|
||||
)
|
||||
|
||||
assert del_count == 1
|
||||
notifications = Notification.query.all()
|
||||
history_rows = NotificationHistory.query.all()
|
||||
notifications = _get_notification_query_all()
|
||||
history_rows = _get_notification_history_query_all()
|
||||
assert len(history_rows) == 1
|
||||
assert len(notifications) == 2
|
||||
|
||||
@@ -324,8 +346,8 @@ def test_insert_notification_history_delete_notifications_only_insert_delete_for
|
||||
)
|
||||
|
||||
assert del_count == 1
|
||||
notifications = Notification.query.all()
|
||||
history_rows = NotificationHistory.query.all()
|
||||
notifications = _get_notification_query_all()
|
||||
history_rows = _get_notification_history_query_all()
|
||||
assert len(notifications) == 1
|
||||
assert len(history_rows) == 1
|
||||
assert notifications[0].id == notification_to_stay.id
|
||||
@@ -361,8 +383,8 @@ def test_insert_notification_history_delete_notifications_insert_for_key_type(
|
||||
)
|
||||
|
||||
assert del_count == 2
|
||||
notifications = Notification.query.all()
|
||||
history_rows = NotificationHistory.query.all()
|
||||
notifications = _get_notification_query_all()
|
||||
history_rows = _get_notification_history_query_all()
|
||||
assert len(notifications) == 1
|
||||
assert with_test_key.id == notifications[0].id
|
||||
assert len(history_rows) == 2
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
from sqlalchemy import select
|
||||
|
||||
from app import db
|
||||
from app.dao.annual_billing_dao import (
|
||||
dao_create_or_update_annual_billing_for_year,
|
||||
dao_get_free_sms_fragment_limit_for_year,
|
||||
@@ -87,7 +89,7 @@ def test_set_default_free_allowance_for_service(
|
||||
|
||||
set_default_free_allowance_for_service(service=service, year_start=year)
|
||||
|
||||
annual_billing = AnnualBilling.query.all()
|
||||
annual_billing = db.session.execute(select(AnnualBilling)).scalars().all()
|
||||
|
||||
assert len(annual_billing) == 1
|
||||
assert annual_billing[0].service_id == service.id
|
||||
@@ -109,7 +111,7 @@ def test_set_default_free_allowance_for_service_using_correct_year(
|
||||
@freeze_time("2021-04-01 14:02:00")
|
||||
def test_set_default_free_allowance_for_service_updates_existing_year(sample_service):
|
||||
set_default_free_allowance_for_service(service=sample_service, year_start=None)
|
||||
annual_billing = AnnualBilling.query.all()
|
||||
annual_billing = db.session.execute(select(AnnualBilling)).scalars().all()
|
||||
assert not sample_service.organization_type
|
||||
assert len(annual_billing) == 1
|
||||
assert annual_billing[0].service_id == sample_service.id
|
||||
@@ -118,7 +120,7 @@ def test_set_default_free_allowance_for_service_updates_existing_year(sample_ser
|
||||
sample_service.organization_type = OrganizationType.FEDERAL
|
||||
|
||||
set_default_free_allowance_for_service(service=sample_service, year_start=None)
|
||||
annual_billing = AnnualBilling.query.all()
|
||||
annual_billing = db.session.execute(select(AnnualBilling)).scalars().all()
|
||||
assert len(annual_billing) == 1
|
||||
assert annual_billing[0].service_id == sample_service.id
|
||||
assert annual_billing[0].free_sms_fragment_limit == 150000
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import db
|
||||
from app.dao.api_key_dao import (
|
||||
expire_api_key,
|
||||
get_model_api_keys,
|
||||
@@ -32,7 +34,9 @@ def test_save_api_key_should_create_new_api_key_and_history(sample_service):
|
||||
assert all_api_keys[0] == api_key
|
||||
assert api_key.version == 1
|
||||
|
||||
all_history = api_key.get_history_model().query.all()
|
||||
all_history = (
|
||||
db.session.execute(select(api_key.get_history_model())).scalars().all()
|
||||
)
|
||||
assert len(all_history) == 1
|
||||
assert all_history[0].id == api_key.id
|
||||
assert all_history[0].version == api_key.version
|
||||
@@ -49,7 +53,9 @@ def test_expire_api_key_should_update_the_api_key_and_create_history_record(
|
||||
assert all_api_keys[0].id == sample_api_key.id
|
||||
assert all_api_keys[0].service_id == sample_api_key.service_id
|
||||
|
||||
all_history = sample_api_key.get_history_model().query.all()
|
||||
all_history = (
|
||||
db.session.execute(select(sample_api_key.get_history_model())).scalars().all()
|
||||
)
|
||||
assert len(all_history) == 2
|
||||
assert all_history[0].id == sample_api_key.id
|
||||
assert all_history[1].id == sample_api_key.id
|
||||
@@ -121,15 +127,20 @@ def test_save_api_key_can_create_key_with_same_name_if_other_is_expired(sample_s
|
||||
}
|
||||
)
|
||||
save_model_api_key(api_key)
|
||||
keys = ApiKey.query.all()
|
||||
keys = db.session.execute(select(ApiKey)).scalars().all()
|
||||
assert len(keys) == 2
|
||||
|
||||
|
||||
def test_save_api_key_should_not_create_new_service_history(sample_service):
|
||||
from app.models import Service
|
||||
|
||||
assert Service.query.count() == 1
|
||||
assert Service.get_history_model().query.count() == 1
|
||||
stmt = select(func.count()).select_from(Service)
|
||||
count = db.session.execute(stmt).scalar() or 0
|
||||
assert count == 1
|
||||
|
||||
stmt = select(func.count()).select_from(Service.get_history_model())
|
||||
count = db.session.execute(stmt).scalar() or 0
|
||||
assert count == 1
|
||||
|
||||
api_key = ApiKey(
|
||||
**{
|
||||
@@ -141,7 +152,9 @@ def test_save_api_key_should_not_create_new_service_history(sample_service):
|
||||
)
|
||||
save_model_api_key(api_key)
|
||||
|
||||
assert Service.get_history_model().query.count() == 1
|
||||
stmt = select(func.count()).select_from(Service.get_history_model())
|
||||
count = db.session.execute(stmt).scalar() or 0
|
||||
assert count == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("days_old, expected_length", [(5, 1), (8, 0)])
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
from sqlalchemy import select
|
||||
|
||||
from app import db
|
||||
from app.dao.email_branding_dao import (
|
||||
dao_get_email_branding_by_id,
|
||||
dao_get_email_branding_by_name,
|
||||
@@ -27,14 +30,14 @@ def test_update_email_branding(notify_db_session):
|
||||
updated_name = "new name"
|
||||
create_email_branding()
|
||||
|
||||
email_branding = EmailBranding.query.all()
|
||||
email_branding = db.session.execute(select(EmailBranding)).scalars().all()
|
||||
|
||||
assert len(email_branding) == 1
|
||||
assert email_branding[0].name != updated_name
|
||||
|
||||
dao_update_email_branding(email_branding[0], name=updated_name)
|
||||
|
||||
email_branding = EmailBranding.query.all()
|
||||
email_branding = db.session.execute(select(EmailBranding)).scalars().all()
|
||||
|
||||
assert len(email_branding) == 1
|
||||
assert email_branding[0].name == updated_name
|
||||
@@ -42,5 +45,5 @@ def test_update_email_branding(notify_db_session):
|
||||
|
||||
def test_email_branding_has_no_domain(notify_db_session):
|
||||
create_email_branding()
|
||||
email_branding = EmailBranding.query.all()
|
||||
email_branding = db.session.execute(select(EmailBranding)).scalars().all()
|
||||
assert not hasattr(email_branding, "domain")
|
||||
|
||||
@@ -20,5 +20,5 @@ def test_create_event(notify_db_session):
|
||||
stmt = select(func.count()).select_from(Event)
|
||||
count = db.session.execute(stmt).scalar() or 0
|
||||
assert count == 1
|
||||
event_from_db = Event.query.first()
|
||||
event_from_db = db.session.execute(select(Event)).scalars().first()
|
||||
assert event == event_from_db
|
||||
|
||||
@@ -1130,7 +1130,10 @@ def test_update_fact_notification_status_respects_gmt_bst(
|
||||
stmt = (
|
||||
select(func.count())
|
||||
.select_from(FactNotificationStatus)
|
||||
.filter_by(service_id=sample_service.id, local_date=process_day)
|
||||
.where(
|
||||
FactNotificationStatus.service_id == sample_service.id,
|
||||
FactNotificationStatus.local_date == process_day,
|
||||
)
|
||||
)
|
||||
result = db.session.execute(stmt)
|
||||
assert result.rowcount == expected_count
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from datetime import datetime
|
||||
|
||||
from freezegun import freeze_time
|
||||
from sqlalchemy import select
|
||||
|
||||
from app import db
|
||||
from app.dao import fact_processing_time_dao
|
||||
from app.dao.fact_processing_time_dao import (
|
||||
get_processing_time_percentage_for_date_range,
|
||||
@@ -19,7 +21,7 @@ def test_insert_update_processing_time(notify_db_session):
|
||||
|
||||
fact_processing_time_dao.insert_update_processing_time(data)
|
||||
|
||||
result = FactProcessingTime.query.all()
|
||||
result = db.session.execute(select(FactProcessingTime)).scalars().all()
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].local_date == datetime(2021, 2, 22).date()
|
||||
@@ -36,7 +38,7 @@ def test_insert_update_processing_time(notify_db_session):
|
||||
with freeze_time("2021-02-23 13:23:33"):
|
||||
fact_processing_time_dao.insert_update_processing_time(data)
|
||||
|
||||
result = FactProcessingTime.query.all()
|
||||
result = db.session.execute(select(FactProcessingTime)).scalars().all()
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].local_date == datetime(2021, 2, 22).date()
|
||||
@@ -77,7 +79,6 @@ def test_get_processing_time_percentage_for_date_range_handles_zero_cases(
|
||||
)
|
||||
|
||||
results = get_processing_time_percentage_for_date_range("2021-02-21", "2021-02-22")
|
||||
|
||||
assert len(results) == 2
|
||||
assert results[0].date == "2021-02-21"
|
||||
assert results[0].messages_total == 0
|
||||
|
||||
@@ -37,7 +37,7 @@ def test_set_service_id_on_inbound_number(notify_db_session, sample_inbound_numb
|
||||
|
||||
dao_set_inbound_number_to_service(service.id, numbers[0])
|
||||
|
||||
stmt = select(InboundNumber).filter(InboundNumber.service_id == service.id)
|
||||
stmt = select(InboundNumber).where(InboundNumber.service_id == service.id)
|
||||
res = db.session.execute(stmt).scalars().all()
|
||||
|
||||
assert len(res) == 1
|
||||
|
||||
@@ -254,7 +254,7 @@ def test_dao_get_paginated_inbound_sms_for_service_for_public_api(sample_service
|
||||
inbound_sms.service.id
|
||||
)
|
||||
|
||||
assert inbound_sms == inbound_from_db[0]
|
||||
assert inbound_sms == inbound_from_db.items[0]
|
||||
|
||||
|
||||
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_return_only_for_service(
|
||||
@@ -268,8 +268,8 @@ def test_dao_get_paginated_inbound_sms_for_service_for_public_api_return_only_fo
|
||||
inbound_sms.service.id
|
||||
)
|
||||
|
||||
assert inbound_sms in inbound_from_db
|
||||
assert another_inbound_sms not in inbound_from_db
|
||||
assert inbound_sms in inbound_from_db.items
|
||||
assert another_inbound_sms not in inbound_from_db.items
|
||||
|
||||
|
||||
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_no_inbound_sms_returns_empty_list(
|
||||
@@ -279,7 +279,7 @@ def test_dao_get_paginated_inbound_sms_for_service_for_public_api_no_inbound_sms
|
||||
sample_service.id
|
||||
)
|
||||
|
||||
assert inbound_from_db == []
|
||||
assert inbound_from_db.has_next() is False
|
||||
|
||||
|
||||
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_page_size_returns_correct_size(
|
||||
@@ -299,7 +299,7 @@ def test_dao_get_paginated_inbound_sms_for_service_for_public_api_page_size_retu
|
||||
sample_service.id, older_than=reversed_inbound_sms[1].id, page_size=2
|
||||
)
|
||||
|
||||
assert len(inbound_from_db) == 2
|
||||
assert inbound_from_db.total == 2
|
||||
|
||||
|
||||
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_returns_correct_list(
|
||||
@@ -320,8 +320,7 @@ def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_ret
|
||||
)
|
||||
|
||||
expected_inbound_sms = reversed_inbound_sms[2:]
|
||||
|
||||
assert expected_inbound_sms == inbound_from_db
|
||||
assert expected_inbound_sms == inbound_from_db.items
|
||||
|
||||
|
||||
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_end_returns_empty_list(
|
||||
@@ -338,8 +337,7 @@ def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_end
|
||||
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(
|
||||
sample_service.id, older_than=reversed_inbound_sms[1].id, page_size=2
|
||||
)
|
||||
|
||||
assert inbound_from_db == []
|
||||
assert inbound_from_db.items == []
|
||||
|
||||
|
||||
def test_most_recent_inbound_sms_only_returns_most_recent_for_each_number(
|
||||
|
||||
@@ -115,12 +115,12 @@ def test_save_invited_user_sets_status_to_cancelled(
|
||||
notify_db_session, sample_invited_user
|
||||
):
|
||||
assert _get_invited_user_count() == 1
|
||||
saved = InvitedUser.query.get(sample_invited_user.id)
|
||||
saved = db.session.get(InvitedUser, sample_invited_user.id)
|
||||
assert saved.status == InvitedUserStatus.PENDING
|
||||
saved.status = InvitedUserStatus.CANCELLED
|
||||
save_invited_user(saved)
|
||||
assert _get_invited_user_count() == 1
|
||||
cancelled_invited_user = InvitedUser.query.get(sample_invited_user.id)
|
||||
cancelled_invited_user = db.session.get(InvitedUser, sample_invited_user.id)
|
||||
assert cancelled_invited_user.status == InvitedUserStatus.CANCELLED
|
||||
|
||||
|
||||
|
||||
@@ -180,8 +180,9 @@ def test_update_organization_updates_the_service_org_type_if_org_type_is_provide
|
||||
|
||||
assert sample_organization.organization_type == OrganizationType.FEDERAL
|
||||
assert sample_service.organization_type == OrganizationType.FEDERAL
|
||||
stmt = select(Service.get_history_model()).filter_by(
|
||||
id=sample_service.id, version=2
|
||||
stmt = select(Service.get_history_model()).where(
|
||||
Service.get_history_model().id == sample_service.id,
|
||||
Service.get_history_model().version == 2,
|
||||
)
|
||||
assert (
|
||||
db.session.execute(stmt).scalars().one().organization_type
|
||||
@@ -234,8 +235,9 @@ def test_add_service_to_organization(sample_service, sample_organization):
|
||||
assert sample_organization.services[0].id == sample_service.id
|
||||
|
||||
assert sample_service.organization_type == sample_organization.organization_type
|
||||
stmt = select(Service.get_history_model()).filter_by(
|
||||
id=sample_service.id, version=2
|
||||
stmt = select(Service.get_history_model()).where(
|
||||
Service.get_history_model().id == sample_service.id,
|
||||
Service.get_history_model().version == 2,
|
||||
)
|
||||
assert (
|
||||
db.session.execute(stmt).scalars().one().organization_type
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app import encryption
|
||||
from app import db, encryption
|
||||
from app.dao.service_callback_api_dao import (
|
||||
get_service_callback_api,
|
||||
get_service_delivery_status_callback_api_for_service,
|
||||
@@ -25,7 +26,7 @@ def test_save_service_callback_api(sample_service):
|
||||
|
||||
save_service_callback_api(service_callback_api)
|
||||
|
||||
results = ServiceCallbackApi.query.all()
|
||||
results = db.session.execute(select(ServiceCallbackApi)).scalars().all()
|
||||
assert len(results) == 1
|
||||
callback_api = results[0]
|
||||
assert callback_api.id is not None
|
||||
@@ -37,7 +38,13 @@ def test_save_service_callback_api(sample_service):
|
||||
assert callback_api.updated_at is None
|
||||
|
||||
versioned = (
|
||||
ServiceCallbackApi.get_history_model().query.filter_by(id=callback_api.id).one()
|
||||
db.session.execute(
|
||||
select(ServiceCallbackApi.get_history_model()).where(
|
||||
ServiceCallbackApi.get_history_model().id == callback_api.id
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.one()
|
||||
)
|
||||
assert versioned.id == callback_api.id
|
||||
assert versioned.service_id == sample_service.id
|
||||
@@ -97,7 +104,13 @@ def test_update_service_callback_can_add_two_api_of_different_types(sample_servi
|
||||
callback_type=CallbackType.COMPLAINT,
|
||||
)
|
||||
save_service_callback_api(complaint)
|
||||
results = ServiceCallbackApi.query.order_by(ServiceCallbackApi.callback_type).all()
|
||||
results = (
|
||||
db.session.execute(
|
||||
select(ServiceCallbackApi).order_by(ServiceCallbackApi.callback_type)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
assert len(results) == 2
|
||||
|
||||
callbacks = [complaint.serialize(), delivery_status.serialize()]
|
||||
@@ -114,7 +127,7 @@ def test_update_service_callback_api(sample_service):
|
||||
)
|
||||
|
||||
save_service_callback_api(service_callback_api)
|
||||
results = ServiceCallbackApi.query.all()
|
||||
results = db.session.execute(select(ServiceCallbackApi)).scalars().all()
|
||||
assert len(results) == 1
|
||||
saved_callback_api = results[0]
|
||||
|
||||
@@ -123,7 +136,7 @@ def test_update_service_callback_api(sample_service):
|
||||
updated_by_id=sample_service.users[0].id,
|
||||
url="https://some_service/changed_url",
|
||||
)
|
||||
updated_results = ServiceCallbackApi.query.all()
|
||||
updated_results = db.session.execute(select(ServiceCallbackApi)).scalars().all()
|
||||
assert len(updated_results) == 1
|
||||
updated = updated_results[0]
|
||||
assert updated.id is not None
|
||||
@@ -135,8 +148,12 @@ def test_update_service_callback_api(sample_service):
|
||||
assert updated.updated_at is not None
|
||||
|
||||
versioned_results = (
|
||||
ServiceCallbackApi.get_history_model()
|
||||
.query.filter_by(id=saved_callback_api.id)
|
||||
db.session.execute(
|
||||
select(ServiceCallbackApi.get_history_model()).where(
|
||||
ServiceCallbackApi.get_history_model().id == saved_callback_api.id
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
assert len(versioned_results) == 2
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app import db
|
||||
from app.dao.service_data_retention_dao import (
|
||||
fetch_service_data_retention,
|
||||
fetch_service_data_retention_by_id,
|
||||
@@ -97,7 +99,7 @@ def test_insert_service_data_retention(sample_service):
|
||||
days_of_retention=3,
|
||||
)
|
||||
|
||||
results = ServiceDataRetention.query.all()
|
||||
results = db.session.execute(select(ServiceDataRetention)).scalars().all()
|
||||
assert len(results) == 1
|
||||
assert results[0].service_id == sample_service.id
|
||||
assert results[0].notification_type == NotificationType.EMAIL
|
||||
@@ -131,7 +133,7 @@ def test_update_service_data_retention(sample_service):
|
||||
days_of_retention=5,
|
||||
)
|
||||
assert updated_count == 1
|
||||
results = ServiceDataRetention.query.all()
|
||||
results = db.session.execute(select(ServiceDataRetention)).scalars().all()
|
||||
assert len(results) == 1
|
||||
assert results[0].id == data_retention.id
|
||||
assert results[0].service_id == sample_service.id
|
||||
@@ -150,7 +152,7 @@ def test_update_service_data_retention_does_not_update_if_row_does_not_exist(
|
||||
days_of_retention=5,
|
||||
)
|
||||
assert updated_count == 0
|
||||
assert len(ServiceDataRetention.query.all()) == 0
|
||||
assert len(db.session.execute(select(ServiceDataRetention)).scalars().all()) == 0
|
||||
|
||||
|
||||
def test_update_service_data_retention_does_not_update_row_if_data_retention_is_for_different_service(
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app import db
|
||||
from app.dao.service_email_reply_to_dao import (
|
||||
add_reply_to_email_address_for_service,
|
||||
archive_reply_to_email_address,
|
||||
@@ -186,7 +188,7 @@ def test_update_reply_to_email_address(sample_service):
|
||||
email_address="change_address@email.com",
|
||||
is_default=True,
|
||||
)
|
||||
updated_reply_to = ServiceEmailReplyTo.query.get(first_reply_to.id)
|
||||
updated_reply_to = db.session.get(ServiceEmailReplyTo, first_reply_to.id)
|
||||
|
||||
assert updated_reply_to.email_address == "change_address@email.com"
|
||||
assert updated_reply_to.updated_at
|
||||
@@ -206,7 +208,7 @@ def test_update_reply_to_email_address_set_updated_to_default(sample_service):
|
||||
is_default=True,
|
||||
)
|
||||
|
||||
results = ServiceEmailReplyTo.query.all()
|
||||
results = db.session.execute(select(ServiceEmailReplyTo)).scalars().all()
|
||||
assert len(results) == 2
|
||||
for x in results:
|
||||
if x.email_address == "change_address@email.com":
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app import encryption
|
||||
from app import db, encryption
|
||||
from app.dao.service_inbound_api_dao import (
|
||||
get_service_inbound_api,
|
||||
get_service_inbound_api_for_service,
|
||||
@@ -24,7 +25,7 @@ def test_save_service_inbound_api(sample_service):
|
||||
|
||||
save_service_inbound_api(service_inbound_api)
|
||||
|
||||
results = ServiceInboundApi.query.all()
|
||||
results = db.session.execute(select(ServiceInboundApi)).scalars().all()
|
||||
assert len(results) == 1
|
||||
inbound_api = results[0]
|
||||
assert inbound_api.id is not None
|
||||
@@ -36,7 +37,13 @@ def test_save_service_inbound_api(sample_service):
|
||||
assert inbound_api.updated_at is None
|
||||
|
||||
versioned = (
|
||||
ServiceInboundApi.get_history_model().query.filter_by(id=inbound_api.id).one()
|
||||
db.session.execute(
|
||||
select(ServiceInboundApi.get_history_model()).where(
|
||||
ServiceInboundApi.get_history_model().id == inbound_api.id
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.one()
|
||||
)
|
||||
assert versioned.id == inbound_api.id
|
||||
assert versioned.service_id == sample_service.id
|
||||
@@ -68,7 +75,7 @@ def test_update_service_inbound_api(sample_service):
|
||||
)
|
||||
|
||||
save_service_inbound_api(service_inbound_api)
|
||||
results = ServiceInboundApi.query.all()
|
||||
results = db.session.execute(select(ServiceInboundApi)).scalars().all()
|
||||
assert len(results) == 1
|
||||
saved_inbound_api = results[0]
|
||||
|
||||
@@ -77,7 +84,7 @@ def test_update_service_inbound_api(sample_service):
|
||||
updated_by_id=sample_service.users[0].id,
|
||||
url="https://some_service/changed_url",
|
||||
)
|
||||
updated_results = ServiceInboundApi.query.all()
|
||||
updated_results = db.session.execute(select(ServiceInboundApi)).scalars().all()
|
||||
assert len(updated_results) == 1
|
||||
updated = updated_results[0]
|
||||
assert updated.id is not None
|
||||
@@ -89,8 +96,12 @@ def test_update_service_inbound_api(sample_service):
|
||||
assert updated.updated_at is not None
|
||||
|
||||
versioned_results = (
|
||||
ServiceInboundApi.get_history_model()
|
||||
.query.filter_by(id=saved_inbound_api.id)
|
||||
db.session.execute(
|
||||
select(ServiceInboundApi.get_history_model()).where(
|
||||
ServiceInboundApi.get_history_model().id == saved_inbound_api.id
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
assert len(versioned_results) == 2
|
||||
|
||||
@@ -126,7 +126,7 @@ def test_dao_add_sms_sender_for_service_switches_default(notify_db_session):
|
||||
|
||||
def test_dao_update_service_sms_sender(notify_db_session):
|
||||
service = create_service()
|
||||
stmt = select(ServiceSmsSender).filter_by(service_id=service.id)
|
||||
stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id)
|
||||
service_sms_senders = db.session.execute(stmt).scalars().all()
|
||||
assert len(service_sms_senders) == 1
|
||||
sms_sender_to_update = service_sms_senders[0]
|
||||
@@ -137,7 +137,7 @@ def test_dao_update_service_sms_sender(notify_db_session):
|
||||
is_default=True,
|
||||
sms_sender="updated",
|
||||
)
|
||||
stmt = select(ServiceSmsSender).filter_by(service_id=service.id)
|
||||
stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id)
|
||||
sms_senders = db.session.execute(stmt).scalars().all()
|
||||
assert len(sms_senders) == 1
|
||||
assert sms_senders[0].is_default
|
||||
@@ -159,7 +159,7 @@ def test_dao_update_service_sms_sender_switches_default(notify_db_session):
|
||||
is_default=True,
|
||||
sms_sender="updated",
|
||||
)
|
||||
stmt = select(ServiceSmsSender).filter_by(service_id=service.id)
|
||||
stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id)
|
||||
sms_senders = db.session.execute(stmt).scalars().all()
|
||||
|
||||
expected = {("testing", False), ("updated", True)}
|
||||
@@ -191,7 +191,7 @@ def test_update_existing_sms_sender_with_inbound_number(notify_db_session):
|
||||
service = create_service()
|
||||
inbound_number = create_inbound_number(number="12345", service_id=service.id)
|
||||
|
||||
stmt = select(ServiceSmsSender).filter_by(service_id=service.id)
|
||||
stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id)
|
||||
existing_sms_sender = db.session.execute(stmt).scalars().one()
|
||||
sms_sender = update_existing_sms_sender_with_inbound_number(
|
||||
service_sms_sender=existing_sms_sender,
|
||||
@@ -208,7 +208,7 @@ def test_update_existing_sms_sender_with_inbound_number_raises_exception_if_inbo
|
||||
notify_db_session,
|
||||
):
|
||||
service = create_service()
|
||||
stmt = select(ServiceSmsSender).filter_by(service_id=service.id)
|
||||
stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id)
|
||||
existing_sms_sender = db.session.execute(stmt).scalars().one()
|
||||
with pytest.raises(expected_exception=SQLAlchemyError):
|
||||
update_existing_sms_sender_with_inbound_number(
|
||||
|
||||
@@ -107,7 +107,7 @@ def _get_first_service():
|
||||
|
||||
|
||||
def _get_service_by_id(service_id):
|
||||
stmt = select(Service).filter(Service.id == service_id)
|
||||
stmt = select(Service).where(Service.id == service_id)
|
||||
|
||||
service = db.session.execute(stmt).scalars().one()
|
||||
return service
|
||||
@@ -746,9 +746,13 @@ def test_update_service_creates_a_history_record_with_current_data(notify_db_ses
|
||||
service_from_db = _get_first_service()
|
||||
|
||||
assert service_from_db.version == 2
|
||||
stmt = select(Service.get_history_model()).filter_by(name="service_name")
|
||||
stmt = select(Service.get_history_model()).where(
|
||||
Service.get_history_model().name == "service_name"
|
||||
)
|
||||
assert db.session.execute(stmt).scalars().one().version == 1
|
||||
stmt = select(Service.get_history_model()).filter_by(name="updated_service_name")
|
||||
stmt = select(Service.get_history_model()).where(
|
||||
Service.get_history_model().name == "updated_service_name"
|
||||
)
|
||||
assert db.session.execute(stmt).scalars().one().version == 2
|
||||
|
||||
|
||||
@@ -819,7 +823,7 @@ def test_update_service_permission_creates_a_history_record_with_current_data(
|
||||
|
||||
stmt = (
|
||||
select(Service.get_history_model())
|
||||
.filter_by(name="service_name")
|
||||
.where(Service.get_history_model().name == "service_name")
|
||||
.order_by("version")
|
||||
)
|
||||
history = db.session.execute(stmt).scalars().all()
|
||||
@@ -920,7 +924,9 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
||||
|
||||
dao_create_service(service_one, user)
|
||||
assert user.id == service_one.users[0].id
|
||||
stmt = select(Permission).filter_by(service=service_one, user=user)
|
||||
stmt = select(Permission).where(
|
||||
Permission.service == service_one, Permission.user == user
|
||||
)
|
||||
test_user_permissions = db.session.execute(stmt).all()
|
||||
assert len(test_user_permissions) == 7
|
||||
|
||||
@@ -941,10 +947,14 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
||||
dao_create_service(service_two, other_user)
|
||||
|
||||
assert other_user.id == service_two.users[0].id
|
||||
stmt = select(Permission).filter_by(service=service_two, user=other_user)
|
||||
stmt = select(Permission).where(
|
||||
Permission.service == service_two, Permission.user == other_user
|
||||
)
|
||||
other_user_permissions = db.session.execute(stmt).all()
|
||||
assert len(other_user_permissions) == 7
|
||||
stmt = select(Permission).filter_by(service=service_one, user=other_user)
|
||||
stmt = select(Permission).where(
|
||||
Permission.service == service_one, Permission.user == other_user
|
||||
)
|
||||
other_user_service_one_permissions = db.session.execute(stmt).all()
|
||||
|
||||
assert len(other_user_service_one_permissions) == 0
|
||||
@@ -955,11 +965,15 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
||||
permissions.append(Permission(permission=p))
|
||||
|
||||
dao_add_user_to_service(service_one, other_user, permissions=permissions)
|
||||
stmt = select(Permission).filter_by(service=service_one, user=other_user)
|
||||
stmt = select(Permission).where(
|
||||
Permission.service == service_one, Permission.user == other_user
|
||||
)
|
||||
other_user_service_one_permissions = db.session.execute(stmt).all()
|
||||
assert len(other_user_service_one_permissions) == 2
|
||||
|
||||
stmt = select(Permission).filter_by(service=service_two, user=other_user)
|
||||
stmt = select(Permission).where(
|
||||
Permission.service == service_two, Permission.user == other_user
|
||||
)
|
||||
other_user_service_two_permissions = db.session.execute(stmt).all()
|
||||
assert len(other_user_service_two_permissions) == 7
|
||||
|
||||
|
||||
@@ -334,9 +334,9 @@ def test_update_template_creates_a_history_record_with_current_data(
|
||||
|
||||
assert template_from_db.version == 2
|
||||
|
||||
stmt = select(TemplateHistory).filter_by(name="Sample Template")
|
||||
stmt = select(TemplateHistory).where(TemplateHistory.name == "Sample Template")
|
||||
assert db.session.execute(stmt).scalars().one().version == 1
|
||||
stmt = select(TemplateHistory).filter_by(name="new name")
|
||||
stmt = select(TemplateHistory).where(TemplateHistory.name == "new name")
|
||||
assert db.session.execute(stmt).scalars().one().version == 2
|
||||
|
||||
|
||||
|
||||
@@ -74,12 +74,12 @@ def test_create_user(notify_db_session, phone_number, expected_phone_number):
|
||||
stmt = select(func.count(User.id))
|
||||
assert db.session.execute(stmt).scalar() == 1
|
||||
stmt = select(User)
|
||||
user_query = db.session.execute(stmt).scalars().first()
|
||||
assert user_query.email_address == email
|
||||
assert user_query.id == user.id
|
||||
assert user_query.mobile_number == expected_phone_number
|
||||
assert user_query.email_access_validated_at == utc_now()
|
||||
assert not user_query.platform_admin
|
||||
user = db.session.execute(stmt).scalars().first()
|
||||
assert user.email_address == email
|
||||
assert user.id == user.id
|
||||
assert user.mobile_number == expected_phone_number
|
||||
assert user.email_access_validated_at == utc_now()
|
||||
assert not user.platform_admin
|
||||
|
||||
|
||||
def test_get_all_users(notify_db_session):
|
||||
|
||||
Reference in New Issue
Block a user