mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-20 22:39:43 -04:00
fix template folder dao
This commit is contained in:
@@ -239,7 +239,7 @@
|
|||||||
"filename": "tests/app/dao/test_services_dao.py",
|
"filename": "tests/app/dao/test_services_dao.py",
|
||||||
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
|
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
|
||||||
"is_verified": false,
|
"is_verified": false,
|
||||||
"line_number": 269,
|
"line_number": 265,
|
||||||
"is_secret": false
|
"is_secret": false
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -384,5 +384,5 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"generated_at": "2024-10-14T17:28:40Z"
|
"generated_at": "2024-10-14T17:46:47Z"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ from unittest.mock import Mock
|
|||||||
import pytest
|
import pytest
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
from sqlalchemy import func, select
|
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
@@ -92,7 +91,7 @@ from tests.app.db import (
|
|||||||
|
|
||||||
def test_create_service(notify_db_session):
|
def test_create_service(notify_db_session):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -102,9 +101,8 @@ def test_create_service(notify_db_session):
|
|||||||
created_by=user,
|
created_by=user,
|
||||||
)
|
)
|
||||||
dao_create_service(service, user)
|
dao_create_service(service, user)
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
stmt = select(Service)
|
service_db = Service.query.one()
|
||||||
service_db = db.session.execute(stmt).scalars().one()
|
|
||||||
assert service_db.name == "service_name"
|
assert service_db.name == "service_name"
|
||||||
assert service_db.id == service.id
|
assert service_db.id == service.id
|
||||||
assert service_db.email_from == "email_from"
|
assert service_db.email_from == "email_from"
|
||||||
@@ -122,7 +120,7 @@ def test_create_service_with_organization(notify_db_session):
|
|||||||
organization_type=OrganizationType.STATE,
|
organization_type=OrganizationType.STATE,
|
||||||
domains=["local-authority.gov.uk"],
|
domains=["local-authority.gov.uk"],
|
||||||
)
|
)
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -132,10 +130,9 @@ def test_create_service_with_organization(notify_db_session):
|
|||||||
created_by=user,
|
created_by=user,
|
||||||
)
|
)
|
||||||
dao_create_service(service, user)
|
dao_create_service(service, user)
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
stmt = select(Service)
|
service_db = Service.query.one()
|
||||||
service_db = db.session.execute(stmt).scalars().one()
|
organization = Organization.query.get(organization.id)
|
||||||
organization = db.session.get(Organization, organization.id)
|
|
||||||
assert service_db.name == "service_name"
|
assert service_db.name == "service_name"
|
||||||
assert service_db.id == service.id
|
assert service_db.id == service.id
|
||||||
assert service_db.email_from == "email_from"
|
assert service_db.email_from == "email_from"
|
||||||
@@ -154,7 +151,7 @@ def test_fetch_service_by_id_with_api_keys(notify_db_session):
|
|||||||
organization_type=OrganizationType.STATE,
|
organization_type=OrganizationType.STATE,
|
||||||
domains=["local-authority.gov.uk"],
|
domains=["local-authority.gov.uk"],
|
||||||
)
|
)
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -164,10 +161,9 @@ def test_fetch_service_by_id_with_api_keys(notify_db_session):
|
|||||||
created_by=user,
|
created_by=user,
|
||||||
)
|
)
|
||||||
dao_create_service(service, user)
|
dao_create_service(service, user)
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
stmt = select(Service)
|
service_db = Service.query.one()
|
||||||
service_db = db.session.execute(stmt).scalars().one()
|
organization = Organization.query.get(organization.id)
|
||||||
organization = db.session.get(Organization, organization.id)
|
|
||||||
assert service_db.name == "service_name"
|
assert service_db.name == "service_name"
|
||||||
assert service_db.id == service.id
|
assert service_db.id == service.id
|
||||||
assert service_db.email_from == "email_from"
|
assert service_db.email_from == "email_from"
|
||||||
@@ -187,7 +183,7 @@ def test_fetch_service_by_id_with_api_keys(notify_db_session):
|
|||||||
|
|
||||||
def test_cannot_create_two_services_with_same_name(notify_db_session):
|
def test_cannot_create_two_services_with_same_name(notify_db_session):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
service1 = Service(
|
service1 = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from1",
|
email_from="email_from1",
|
||||||
@@ -213,7 +209,7 @@ def test_cannot_create_two_services_with_same_name(notify_db_session):
|
|||||||
|
|
||||||
def test_cannot_create_two_services_with_same_email_from(notify_db_session):
|
def test_cannot_create_two_services_with_same_email_from(notify_db_session):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
service1 = Service(
|
service1 = Service(
|
||||||
name="service_name1",
|
name="service_name1",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -239,7 +235,7 @@ def test_cannot_create_two_services_with_same_email_from(notify_db_session):
|
|||||||
|
|
||||||
def test_cannot_create_service_with_no_user(notify_db_session):
|
def test_cannot_create_service_with_no_user(notify_db_session):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -262,7 +258,7 @@ def test_should_add_user_to_service(notify_db_session):
|
|||||||
created_by=user,
|
created_by=user,
|
||||||
)
|
)
|
||||||
dao_create_service(service, user)
|
dao_create_service(service, user)
|
||||||
assert user in service_query_first().users
|
assert user in Service.query.first().users
|
||||||
new_user = User(
|
new_user = User(
|
||||||
name="Test User",
|
name="Test User",
|
||||||
email_address="new_user@digital.fake.gov",
|
email_address="new_user@digital.fake.gov",
|
||||||
@@ -271,7 +267,7 @@ def test_should_add_user_to_service(notify_db_session):
|
|||||||
)
|
)
|
||||||
save_model_user(new_user, validated_email_access=True)
|
save_model_user(new_user, validated_email_access=True)
|
||||||
dao_add_user_to_service(service, new_user)
|
dao_add_user_to_service(service, new_user)
|
||||||
assert new_user in service_query_first().users
|
assert new_user in Service.query.first().users
|
||||||
|
|
||||||
|
|
||||||
def test_dao_add_user_to_service_sets_folder_permissions(sample_user, sample_service):
|
def test_dao_add_user_to_service_sets_folder_permissions(sample_user, sample_service):
|
||||||
@@ -318,8 +314,7 @@ def test_dao_add_user_to_service_raises_error_if_adding_folder_permissions_for_a
|
|||||||
other_service_folder = create_template_folder(other_service)
|
other_service_folder = create_template_folder(other_service)
|
||||||
folder_permissions = [str(other_service_folder.id)]
|
folder_permissions = [str(other_service_folder.id)]
|
||||||
|
|
||||||
stmt = select(func.count()).select_from(ServiceUser)
|
assert ServiceUser.query.count() == 2
|
||||||
assert db.session.execute(stmt).scalar() == 2
|
|
||||||
|
|
||||||
with pytest.raises(IntegrityError) as e:
|
with pytest.raises(IntegrityError) as e:
|
||||||
dao_add_user_to_service(
|
dao_add_user_to_service(
|
||||||
@@ -331,8 +326,7 @@ def test_dao_add_user_to_service_raises_error_if_adding_folder_permissions_for_a
|
|||||||
'insert or update on table "user_folder_permissions" violates foreign key constraint'
|
'insert or update on table "user_folder_permissions" violates foreign key constraint'
|
||||||
in str(e.value)
|
in str(e.value)
|
||||||
)
|
)
|
||||||
stmt = select(func.count()).select_from(ServiceUser)
|
assert ServiceUser.query.count() == 2
|
||||||
assert db.session.execute(stmt).scalar() == 2
|
|
||||||
|
|
||||||
|
|
||||||
def test_should_remove_user_from_service(notify_db_session):
|
def test_should_remove_user_from_service(notify_db_session):
|
||||||
@@ -353,9 +347,9 @@ def test_should_remove_user_from_service(notify_db_session):
|
|||||||
)
|
)
|
||||||
save_model_user(new_user, validated_email_access=True)
|
save_model_user(new_user, validated_email_access=True)
|
||||||
dao_add_user_to_service(service, new_user)
|
dao_add_user_to_service(service, new_user)
|
||||||
assert new_user in service_query_first().users
|
assert new_user in Service.query.first().users
|
||||||
dao_remove_user_from_service(service, new_user)
|
dao_remove_user_from_service(service, new_user)
|
||||||
assert new_user not in service_query_first().users
|
assert new_user not in Service.query.first().users
|
||||||
|
|
||||||
|
|
||||||
def test_should_remove_user_from_service_exception(notify_db_session):
|
def test_should_remove_user_from_service_exception(notify_db_session):
|
||||||
@@ -388,12 +382,11 @@ def test_should_remove_user_from_service_exception(notify_db_session):
|
|||||||
def test_removing_a_user_from_a_service_deletes_their_permissions(
|
def test_removing_a_user_from_a_service_deletes_their_permissions(
|
||||||
sample_user, sample_service
|
sample_user, sample_service
|
||||||
):
|
):
|
||||||
stmt = select(Permission)
|
assert len(Permission.query.all()) == 7
|
||||||
assert len(db.session.execute(stmt).scalars().all()) == 7
|
|
||||||
|
|
||||||
dao_remove_user_from_service(sample_service, sample_user)
|
dao_remove_user_from_service(sample_service, sample_user)
|
||||||
|
|
||||||
assert db.session.execute(stmt).scalars().all() == []
|
assert Permission.query.all() == []
|
||||||
|
|
||||||
|
|
||||||
def test_removing_a_user_from_a_service_deletes_their_folder_permissions_for_that_service(
|
def test_removing_a_user_from_a_service_deletes_their_folder_permissions_for_that_service(
|
||||||
@@ -675,8 +668,8 @@ def test_removing_all_permission_returns_service_with_no_permissions(notify_db_s
|
|||||||
|
|
||||||
def test_create_service_creates_a_history_record_with_current_data(notify_db_session):
|
def test_create_service_creates_a_history_record_with_current_data(notify_db_session):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
assert service_history_query_count() == 0
|
assert Service.get_history_model().query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -685,13 +678,11 @@ def test_create_service_creates_a_history_record_with_current_data(notify_db_ses
|
|||||||
created_by=user,
|
created_by=user,
|
||||||
)
|
)
|
||||||
dao_create_service(service, user)
|
dao_create_service(service, user)
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
assert service_history_query_count() == 1
|
assert Service.get_history_model().query.count() == 1
|
||||||
|
|
||||||
service_from_db = service_query_first()
|
service_from_db = Service.query.first()
|
||||||
stmt = select(Service.get_history_model())
|
service_history = Service.get_history_model().query.first()
|
||||||
|
|
||||||
service_history = db.session.execute(stmt).scalars().first()
|
|
||||||
|
|
||||||
assert service_from_db.id == service_history.id
|
assert service_from_db.id == service_history.id
|
||||||
assert service_from_db.name == service_history.name
|
assert service_from_db.name == service_history.name
|
||||||
@@ -701,25 +692,10 @@ def test_create_service_creates_a_history_record_with_current_data(notify_db_ses
|
|||||||
assert service_from_db.created_by.id == service_history.created_by_id
|
assert service_from_db.created_by.id == service_history.created_by_id
|
||||||
|
|
||||||
|
|
||||||
def service_query_count():
|
|
||||||
stmt = select(func.count()).select_from(Service)
|
|
||||||
return db.session.execute(stmt).scalar() or 0
|
|
||||||
|
|
||||||
|
|
||||||
def service_query_first():
|
|
||||||
stmt = select(Service)
|
|
||||||
return db.session.execute(stmt).scalars().first()
|
|
||||||
|
|
||||||
|
|
||||||
def service_history_query_count():
|
|
||||||
stmt = select(func.count()).select_from(Service.get_history_model())
|
|
||||||
return db.session.execute(stmt).scalar() or 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_service_creates_a_history_record_with_current_data(notify_db_session):
|
def test_update_service_creates_a_history_record_with_current_data(notify_db_session):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
assert service_history_query_count() == 0
|
assert Service.get_history_model().query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -729,32 +705,39 @@ def test_update_service_creates_a_history_record_with_current_data(notify_db_ses
|
|||||||
)
|
)
|
||||||
dao_create_service(service, user)
|
dao_create_service(service, user)
|
||||||
|
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
assert service_query_first().version == 1
|
assert Service.query.first().version == 1
|
||||||
assert service_history_query_count() == 1
|
assert Service.get_history_model().query.count() == 1
|
||||||
|
|
||||||
service.name = "updated_service_name"
|
service.name = "updated_service_name"
|
||||||
dao_update_service(service)
|
dao_update_service(service)
|
||||||
|
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
assert service_history_query_count() == 2
|
assert Service.get_history_model().query.count() == 2
|
||||||
|
|
||||||
service_from_db = service_query_first()
|
service_from_db = Service.query.first()
|
||||||
|
|
||||||
assert service_from_db.version == 2
|
assert service_from_db.version == 2
|
||||||
|
|
||||||
stmt = select(Service.get_history_model()).filter_by(name="service_name")
|
assert (
|
||||||
assert db.session.execute(stmt).scalars().one().version == 1
|
Service.get_history_model().query.filter_by(name="service_name").one().version
|
||||||
stmt = select(Service.get_history_model()).filter_by(name="updated_service_name")
|
== 1
|
||||||
assert db.session.execute(stmt).scalars().one().version == 2
|
)
|
||||||
|
assert (
|
||||||
|
Service.get_history_model()
|
||||||
|
.query.filter_by(name="updated_service_name")
|
||||||
|
.one()
|
||||||
|
.version
|
||||||
|
== 2
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_update_service_permission_creates_a_history_record_with_current_data(
|
def test_update_service_permission_creates_a_history_record_with_current_data(
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
):
|
):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
assert service_history_query_count() == 0
|
assert Service.get_history_model().query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name="service_name",
|
name="service_name",
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -772,17 +755,17 @@ def test_update_service_permission_creates_a_history_record_with_current_data(
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
|
|
||||||
service.permissions.append(
|
service.permissions.append(
|
||||||
ServicePermission(service_id=service.id, permission=ServicePermissionType.EMAIL)
|
ServicePermission(service_id=service.id, permission=ServicePermissionType.EMAIL)
|
||||||
)
|
)
|
||||||
dao_update_service(service)
|
dao_update_service(service)
|
||||||
|
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
assert service_history_query_count() == 2
|
assert Service.get_history_model().query.count() == 2
|
||||||
|
|
||||||
service_from_db = service_query_first()
|
service_from_db = Service.query.first()
|
||||||
|
|
||||||
assert service_from_db.version == 2
|
assert service_from_db.version == 2
|
||||||
|
|
||||||
@@ -801,10 +784,10 @@ def test_update_service_permission_creates_a_history_record_with_current_data(
|
|||||||
service.permissions.remove(permission)
|
service.permissions.remove(permission)
|
||||||
dao_update_service(service)
|
dao_update_service(service)
|
||||||
|
|
||||||
assert service_query_count() == 1
|
assert Service.query.count() == 1
|
||||||
assert service_history_query_count() == 3
|
assert Service.get_history_model().query.count() == 3
|
||||||
|
|
||||||
service_from_db = service_query_first()
|
service_from_db = Service.query.first()
|
||||||
assert service_from_db.version == 3
|
assert service_from_db.version == 3
|
||||||
_assert_service_permissions(
|
_assert_service_permissions(
|
||||||
service.permissions,
|
service.permissions,
|
||||||
@@ -814,12 +797,12 @@ def test_update_service_permission_creates_a_history_record_with_current_data(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
stmt = (
|
history = (
|
||||||
select(Service.get_history_model())
|
Service.get_history_model()
|
||||||
.filter_by(name="service_name")
|
.query.filter_by(name="service_name")
|
||||||
.order_by("version")
|
.order_by("version")
|
||||||
|
.all()
|
||||||
)
|
)
|
||||||
history = db.session.execute(stmt).scalars().all()
|
|
||||||
|
|
||||||
assert len(history) == 3
|
assert len(history) == 3
|
||||||
assert history[2].version == 3
|
assert history[2].version == 3
|
||||||
@@ -827,8 +810,8 @@ def test_update_service_permission_creates_a_history_record_with_current_data(
|
|||||||
|
|
||||||
def test_create_service_and_history_is_transactional(notify_db_session):
|
def test_create_service_and_history_is_transactional(notify_db_session):
|
||||||
user = create_user()
|
user = create_user()
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
assert service_history_query_count() == 0
|
assert Service.get_history_model().query.count() == 0
|
||||||
service = Service(
|
service = Service(
|
||||||
name=None,
|
name=None,
|
||||||
email_from="email_from",
|
email_from="email_from",
|
||||||
@@ -845,8 +828,8 @@ def test_create_service_and_history_is_transactional(notify_db_session):
|
|||||||
in str(seeei)
|
in str(seeei)
|
||||||
)
|
)
|
||||||
|
|
||||||
assert service_query_count() == 0
|
assert Service.query.count() == 0
|
||||||
assert service_history_query_count() == 0
|
assert Service.get_history_model().query.count() == 0
|
||||||
|
|
||||||
|
|
||||||
def test_delete_service_and_associated_objects(notify_db_session):
|
def test_delete_service_and_associated_objects(notify_db_session):
|
||||||
@@ -863,8 +846,7 @@ def test_delete_service_and_associated_objects(notify_db_session):
|
|||||||
create_invited_user(service=service)
|
create_invited_user(service=service)
|
||||||
user.organizations = [organization]
|
user.organizations = [organization]
|
||||||
|
|
||||||
stmt = select(func.count()).select_from(ServicePermission)
|
assert ServicePermission.query.count() == len(
|
||||||
assert db.session.execute(stmt).scalar() == len(
|
|
||||||
(
|
(
|
||||||
ServicePermissionType.SMS,
|
ServicePermissionType.SMS,
|
||||||
ServicePermissionType.EMAIL,
|
ServicePermissionType.EMAIL,
|
||||||
@@ -873,41 +855,21 @@ def test_delete_service_and_associated_objects(notify_db_session):
|
|||||||
)
|
)
|
||||||
|
|
||||||
delete_service_and_all_associated_db_objects(service)
|
delete_service_and_all_associated_db_objects(service)
|
||||||
stmt = select(VerifyCode)
|
assert VerifyCode.query.count() == 0
|
||||||
assert db.session.execute(stmt).scalar() is None
|
assert ApiKey.query.count() == 0
|
||||||
stmt = select(ApiKey)
|
assert ApiKey.get_history_model().query.count() == 0
|
||||||
assert db.session.execute(stmt).scalar() is None
|
assert Template.query.count() == 0
|
||||||
stmt = select(ApiKey.get_history_model())
|
assert TemplateHistory.query.count() == 0
|
||||||
assert db.session.execute(stmt).scalar() is None
|
assert Job.query.count() == 0
|
||||||
|
assert Notification.query.count() == 0
|
||||||
stmt = select(Template)
|
assert Permission.query.count() == 0
|
||||||
assert db.session.execute(stmt).scalar() is None
|
assert User.query.count() == 0
|
||||||
|
assert InvitedUser.query.count() == 0
|
||||||
stmt = select(TemplateHistory)
|
assert Service.query.count() == 0
|
||||||
assert db.session.execute(stmt).scalar() is None
|
assert Service.get_history_model().query.count() == 0
|
||||||
|
assert ServicePermission.query.count() == 0
|
||||||
stmt = select(Job)
|
|
||||||
assert db.session.execute(stmt).scalar() is None
|
|
||||||
|
|
||||||
stmt = select(Notification)
|
|
||||||
assert db.session.execute(stmt).scalar() is None
|
|
||||||
|
|
||||||
stmt = select(Permission)
|
|
||||||
assert db.session.execute(stmt).scalar() is None
|
|
||||||
|
|
||||||
stmt = select(User)
|
|
||||||
assert db.session.execute(stmt).scalar() is None
|
|
||||||
|
|
||||||
stmt = select(InvitedUser)
|
|
||||||
assert db.session.execute(stmt).scalar() is None
|
|
||||||
stmt = select(ServicePermission)
|
|
||||||
assert db.session.execute(stmt).scalar() is None
|
|
||||||
|
|
||||||
assert service_query_count() == 0
|
|
||||||
assert service_history_query_count() == 0
|
|
||||||
# the organization hasn't been deleted
|
# the organization hasn't been deleted
|
||||||
stmt = select(func.count()).select_from(Organization)
|
assert Organization.query.count() == 1
|
||||||
assert db.session.execute(stmt).scalar() == 1
|
|
||||||
|
|
||||||
|
|
||||||
def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
||||||
@@ -925,8 +887,9 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
|||||||
|
|
||||||
dao_create_service(service_one, user)
|
dao_create_service(service_one, user)
|
||||||
assert user.id == service_one.users[0].id
|
assert user.id == service_one.users[0].id
|
||||||
stmt = select(Permission).filter_by(service=service_one, user=user)
|
test_user_permissions = Permission.query.filter_by(
|
||||||
test_user_permissions = db.session.execute(stmt).scalars().all()
|
service=service_one, user=user
|
||||||
|
).all()
|
||||||
assert len(test_user_permissions) == 7
|
assert len(test_user_permissions) == 7
|
||||||
|
|
||||||
other_user = User(
|
other_user = User(
|
||||||
@@ -946,11 +909,14 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
|||||||
dao_create_service(service_two, other_user)
|
dao_create_service(service_two, other_user)
|
||||||
|
|
||||||
assert other_user.id == service_two.users[0].id
|
assert other_user.id == service_two.users[0].id
|
||||||
stmt = select(Permission).filter_by(service=service_two, user=other_user)
|
other_user_permissions = Permission.query.filter_by(
|
||||||
other_user_permissions = db.session.execute(stmt).scalars().all()
|
service=service_two, user=other_user
|
||||||
|
).all()
|
||||||
assert len(other_user_permissions) == 7
|
assert len(other_user_permissions) == 7
|
||||||
stmt = select(Permission).filter_by(service=service_one, user=other_user)
|
|
||||||
other_user_service_one_permissions = db.session.execute(stmt).scalars().all()
|
other_user_service_one_permissions = Permission.query.filter_by(
|
||||||
|
service=service_one, user=other_user
|
||||||
|
).all()
|
||||||
assert len(other_user_service_one_permissions) == 0
|
assert len(other_user_service_one_permissions) == 0
|
||||||
|
|
||||||
# adding the other_user to service_one should leave all other_user permissions on service_two intact
|
# adding the other_user to service_one should leave all other_user permissions on service_two intact
|
||||||
@@ -960,12 +926,14 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions(
|
|||||||
|
|
||||||
dao_add_user_to_service(service_one, other_user, permissions=permissions)
|
dao_add_user_to_service(service_one, other_user, permissions=permissions)
|
||||||
|
|
||||||
stmt = select(Permission).filter_by(service=service_one, user=other_user)
|
other_user_service_one_permissions = Permission.query.filter_by(
|
||||||
other_user_service_one_permissions = db.session.execute(stmt).scalars().all()
|
service=service_one, user=other_user
|
||||||
|
).all()
|
||||||
assert len(other_user_service_one_permissions) == 2
|
assert len(other_user_service_one_permissions) == 2
|
||||||
|
|
||||||
stmt = select(Permission).filter_by(service=service_two, user=other_user)
|
other_user_service_two_permissions = Permission.query.filter_by(
|
||||||
other_user_service_two_permissions = db.session.execute(stmt).scalars().all()
|
service=service_two, user=other_user
|
||||||
|
).all()
|
||||||
assert len(other_user_service_two_permissions) == 7
|
assert len(other_user_service_two_permissions) == 7
|
||||||
|
|
||||||
|
|
||||||
@@ -988,10 +956,9 @@ def test_fetch_stats_filters_on_service(notify_db_session):
|
|||||||
|
|
||||||
def test_fetch_stats_ignores_historical_notification_data(sample_template):
|
def test_fetch_stats_ignores_historical_notification_data(sample_template):
|
||||||
create_notification_history(template=sample_template)
|
create_notification_history(template=sample_template)
|
||||||
stmt = select(Notification)
|
|
||||||
assert db.session.execute(stmt).scalar() is None
|
assert Notification.query.count() == 0
|
||||||
stmt = select(func.count()).select_from(NotificationHistory)
|
assert NotificationHistory.query.count() == 1
|
||||||
assert db.session.execute(stmt).scalar() == 1
|
|
||||||
|
|
||||||
stats = dao_fetch_todays_stats_for_service(sample_template.service_id)
|
stats = dao_fetch_todays_stats_for_service(sample_template.service_id)
|
||||||
assert len(stats) == 0
|
assert len(stats) == 0
|
||||||
@@ -1349,7 +1316,7 @@ def test_dao_fetch_todays_stats_for_all_services_can_exclude_from_test_key(
|
|||||||
def test_dao_suspend_service_with_no_api_keys(notify_db_session):
|
def test_dao_suspend_service_with_no_api_keys(notify_db_session):
|
||||||
service = create_service()
|
service = create_service()
|
||||||
dao_suspend_service(service.id)
|
dao_suspend_service(service.id)
|
||||||
service = db.session.get(Service, service.id)
|
service = Service.query.get(service.id)
|
||||||
assert not service.active
|
assert not service.active
|
||||||
assert service.name == service.name
|
assert service.name == service.name
|
||||||
assert service.api_keys == []
|
assert service.api_keys == []
|
||||||
@@ -1362,11 +1329,11 @@ def test_dao_suspend_service_marks_service_as_inactive_and_expires_api_keys(
|
|||||||
service = create_service()
|
service = create_service()
|
||||||
api_key = create_api_key(service=service)
|
api_key = create_api_key(service=service)
|
||||||
dao_suspend_service(service.id)
|
dao_suspend_service(service.id)
|
||||||
service = db.session.get(Service, service.id)
|
service = Service.query.get(service.id)
|
||||||
assert not service.active
|
assert not service.active
|
||||||
assert service.name == service.name
|
assert service.name == service.name
|
||||||
|
|
||||||
api_key = db.session.get(ApiKey, api_key.id)
|
api_key = ApiKey.query.get(api_key.id)
|
||||||
assert api_key.expiry_date == datetime(2001, 1, 1, 23, 59, 00)
|
assert api_key.expiry_date == datetime(2001, 1, 1, 23, 59, 00)
|
||||||
|
|
||||||
|
|
||||||
@@ -1377,13 +1344,13 @@ def test_dao_resume_service_marks_service_as_active_and_api_keys_are_still_revok
|
|||||||
service = create_service()
|
service = create_service()
|
||||||
api_key = create_api_key(service=service)
|
api_key = create_api_key(service=service)
|
||||||
dao_suspend_service(service.id)
|
dao_suspend_service(service.id)
|
||||||
service = db.session.get(Service, service.id)
|
service = Service.query.get(service.id)
|
||||||
assert not service.active
|
assert not service.active
|
||||||
|
|
||||||
dao_resume_service(service.id)
|
dao_resume_service(service.id)
|
||||||
assert db.session.get(Service, service.id).active
|
assert Service.query.get(service.id).active
|
||||||
|
|
||||||
api_key = db.session.get(ApiKey, api_key.id)
|
api_key = ApiKey.query.get(api_key.id)
|
||||||
assert api_key.expiry_date == datetime(2001, 1, 1, 23, 59, 00)
|
assert api_key.expiry_date == datetime(2001, 1, 1, 23, 59, 00)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from sqlalchemy import select
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.dao.service_user_dao import dao_get_service_user
|
from app.dao.service_user_dao import dao_get_service_user
|
||||||
from app.dao.template_folder_dao import (
|
from app.dao.template_folder_dao import (
|
||||||
@@ -17,5 +19,5 @@ def test_dao_delete_template_folder_deletes_user_folder_permissions(
|
|||||||
dao_update_template_folder(folder)
|
dao_update_template_folder(folder)
|
||||||
|
|
||||||
dao_delete_template_folder(folder)
|
dao_delete_template_folder(folder)
|
||||||
|
stmt = select(user_folder_permissions)
|
||||||
assert db.session.query(user_folder_permissions).all() == []
|
assert db.session.execute(stmt).scalars().all() == []
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ from datetime import datetime
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
from sqlalchemy import func, select
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
|
from app import db
|
||||||
from app.dao.templates_dao import (
|
from app.dao.templates_dao import (
|
||||||
dao_create_template,
|
dao_create_template,
|
||||||
dao_get_all_templates_for_service,
|
dao_get_all_templates_for_service,
|
||||||
@@ -17,6 +19,16 @@ from app.models import Template, TemplateHistory, TemplateRedacted
|
|||||||
from tests.app.db import create_template
|
from tests.app.db import create_template
|
||||||
|
|
||||||
|
|
||||||
|
def template_query_count():
|
||||||
|
stmt = select(func.count()).select_from(Template)
|
||||||
|
return db.session.execute(stmt).scalar or 0
|
||||||
|
|
||||||
|
|
||||||
|
def template_history_query_count():
|
||||||
|
stmt = select(func.count()).select_from(TemplateHistory)
|
||||||
|
return db.session.execute(stmt).scalar or 0
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"template_type, subject",
|
"template_type, subject",
|
||||||
[
|
[
|
||||||
@@ -37,7 +49,7 @@ def test_create_template(sample_service, sample_user, template_type, subject):
|
|||||||
template = Template(**data)
|
template = Template(**data)
|
||||||
dao_create_template(template)
|
dao_create_template(template)
|
||||||
|
|
||||||
assert Template.query.count() == 1
|
assert template_query_count() == 1
|
||||||
assert len(dao_get_all_templates_for_service(sample_service.id)) == 1
|
assert len(dao_get_all_templates_for_service(sample_service.id)) == 1
|
||||||
assert (
|
assert (
|
||||||
dao_get_all_templates_for_service(sample_service.id)[0].name
|
dao_get_all_templates_for_service(sample_service.id)[0].name
|
||||||
@@ -50,11 +62,13 @@ def test_create_template(sample_service, sample_user, template_type, subject):
|
|||||||
|
|
||||||
|
|
||||||
def test_create_template_creates_redact_entry(sample_service):
|
def test_create_template_creates_redact_entry(sample_service):
|
||||||
assert TemplateRedacted.query.count() == 0
|
stmt = select(func.count()).select_from(TemplateRedacted)
|
||||||
|
assert db.session.execute(stmt).scalar() is None
|
||||||
|
|
||||||
template = create_template(sample_service)
|
template = create_template(sample_service)
|
||||||
|
|
||||||
redacted = TemplateRedacted.query.one()
|
stmt = select(TemplateRedacted)
|
||||||
|
redacted = db.session.execute(stmt).scalars().one()
|
||||||
assert redacted.template_id == template.id
|
assert redacted.template_id == template.id
|
||||||
assert redacted.redact_personalisation is False
|
assert redacted.redact_personalisation is False
|
||||||
assert redacted.updated_by_id == sample_service.created_by_id
|
assert redacted.updated_by_id == sample_service.created_by_id
|
||||||
@@ -79,7 +93,8 @@ def test_update_template(sample_service, sample_user):
|
|||||||
|
|
||||||
|
|
||||||
def test_redact_template(sample_template):
|
def test_redact_template(sample_template):
|
||||||
redacted = TemplateRedacted.query.one()
|
stmt = select(TemplateRedacted)
|
||||||
|
redacted = db.session.execute(stmt).scalars().one()
|
||||||
assert redacted.template_id == sample_template.id
|
assert redacted.template_id == sample_template.id
|
||||||
assert redacted.redact_personalisation is False
|
assert redacted.redact_personalisation is False
|
||||||
|
|
||||||
@@ -96,7 +111,7 @@ def test_get_all_templates_for_service(service_factory):
|
|||||||
service_1 = service_factory.get("service 1", email_from="service.1")
|
service_1 = service_factory.get("service 1", email_from="service.1")
|
||||||
service_2 = service_factory.get("service 2", email_from="service.2")
|
service_2 = service_factory.get("service 2", email_from="service.2")
|
||||||
|
|
||||||
assert Template.query.count() == 2
|
assert template_query_count() == 2
|
||||||
assert len(dao_get_all_templates_for_service(service_1.id)) == 1
|
assert len(dao_get_all_templates_for_service(service_1.id)) == 1
|
||||||
assert len(dao_get_all_templates_for_service(service_2.id)) == 1
|
assert len(dao_get_all_templates_for_service(service_2.id)) == 1
|
||||||
|
|
||||||
@@ -119,7 +134,7 @@ def test_get_all_templates_for_service(service_factory):
|
|||||||
content="Template content",
|
content="Template content",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert Template.query.count() == 5
|
assert template_query_count() == 5
|
||||||
assert len(dao_get_all_templates_for_service(service_1.id)) == 3
|
assert len(dao_get_all_templates_for_service(service_1.id)) == 3
|
||||||
assert len(dao_get_all_templates_for_service(service_2.id)) == 2
|
assert len(dao_get_all_templates_for_service(service_2.id)) == 2
|
||||||
|
|
||||||
@@ -144,7 +159,7 @@ def test_get_all_templates_for_service_is_alphabetised(sample_service):
|
|||||||
service=sample_service,
|
service=sample_service,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert Template.query.count() == 3
|
assert template_query_count() == 3
|
||||||
assert (
|
assert (
|
||||||
dao_get_all_templates_for_service(sample_service.id)[0].name
|
dao_get_all_templates_for_service(sample_service.id)[0].name
|
||||||
== "Sample Template 1"
|
== "Sample Template 1"
|
||||||
@@ -171,7 +186,7 @@ def test_get_all_templates_for_service_is_alphabetised(sample_service):
|
|||||||
|
|
||||||
|
|
||||||
def test_get_all_returns_empty_list_if_no_templates(sample_service):
|
def test_get_all_returns_empty_list_if_no_templates(sample_service):
|
||||||
assert Template.query.count() == 0
|
assert template_query_count() == 0
|
||||||
assert len(dao_get_all_templates_for_service(sample_service.id)) == 0
|
assert len(dao_get_all_templates_for_service(sample_service.id)) == 0
|
||||||
|
|
||||||
|
|
||||||
@@ -257,8 +272,8 @@ def test_get_template_by_id_and_service_returns_none_if_no_template(
|
|||||||
def test_create_template_creates_a_history_record_with_current_data(
|
def test_create_template_creates_a_history_record_with_current_data(
|
||||||
sample_service, sample_user
|
sample_service, sample_user
|
||||||
):
|
):
|
||||||
assert Template.query.count() == 0
|
assert template_query_count() == 0
|
||||||
assert TemplateHistory.query.count() == 0
|
assert template_history_query_count() == 0
|
||||||
data = {
|
data = {
|
||||||
"name": "Sample Template",
|
"name": "Sample Template",
|
||||||
"template_type": TemplateType.EMAIL,
|
"template_type": TemplateType.EMAIL,
|
||||||
@@ -270,10 +285,12 @@ def test_create_template_creates_a_history_record_with_current_data(
|
|||||||
template = Template(**data)
|
template = Template(**data)
|
||||||
dao_create_template(template)
|
dao_create_template(template)
|
||||||
|
|
||||||
assert Template.query.count() == 1
|
assert template_query_count() == 1
|
||||||
|
|
||||||
template_from_db = Template.query.first()
|
stmt = select(Template)
|
||||||
template_history = TemplateHistory.query.first()
|
template_from_db = db.session.execute(stmt).scalars().first()
|
||||||
|
stmt = select(TemplateHistory)
|
||||||
|
template_history = db.session.execute(stmt).scalars().first()
|
||||||
|
|
||||||
assert template_from_db.id == template_history.id
|
assert template_from_db.id == template_history.id
|
||||||
assert template_from_db.name == template_history.name
|
assert template_from_db.name == template_history.name
|
||||||
@@ -286,8 +303,8 @@ def test_create_template_creates_a_history_record_with_current_data(
|
|||||||
def test_update_template_creates_a_history_record_with_current_data(
|
def test_update_template_creates_a_history_record_with_current_data(
|
||||||
sample_service, sample_user
|
sample_service, sample_user
|
||||||
):
|
):
|
||||||
assert Template.query.count() == 0
|
assert template_query_count() == 0
|
||||||
assert TemplateHistory.query.count() == 0
|
assert template_history_query_count() == 0
|
||||||
data = {
|
data = {
|
||||||
"name": "Sample Template",
|
"name": "Sample Template",
|
||||||
"template_type": TemplateType.EMAIL,
|
"template_type": TemplateType.EMAIL,
|
||||||
@@ -301,22 +318,26 @@ def test_update_template_creates_a_history_record_with_current_data(
|
|||||||
|
|
||||||
created = dao_get_all_templates_for_service(sample_service.id)[0]
|
created = dao_get_all_templates_for_service(sample_service.id)[0]
|
||||||
assert created.name == "Sample Template"
|
assert created.name == "Sample Template"
|
||||||
assert Template.query.count() == 1
|
assert template_query_count() == 1
|
||||||
assert Template.query.first().version == 1
|
stmt = select(Template)
|
||||||
assert TemplateHistory.query.count() == 1
|
assert db.session.execute(stmt).scalars().first().version == 1
|
||||||
|
assert template_history_query_count() == 1
|
||||||
|
|
||||||
created.name = "new name"
|
created.name = "new name"
|
||||||
dao_update_template(created)
|
dao_update_template(created)
|
||||||
|
|
||||||
assert Template.query.count() == 1
|
assert template_query_count() == 1
|
||||||
assert TemplateHistory.query.count() == 2
|
assert template_history_query_count() == 2
|
||||||
|
|
||||||
template_from_db = Template.query.first()
|
stmt = select(Template)
|
||||||
|
template_from_db = db.session.execute(stmt).scalars().first()
|
||||||
|
|
||||||
assert template_from_db.version == 2
|
assert template_from_db.version == 2
|
||||||
|
|
||||||
assert TemplateHistory.query.filter_by(name="Sample Template").one().version == 1
|
stmt = select(TemplateHistory).filter_by(name="Sample Template")
|
||||||
assert TemplateHistory.query.filter_by(name="new name").one().version == 2
|
assert db.session.execute(stmt).scalars().one().version == 1
|
||||||
|
stmt = select(TemplateHistory).filter_by(name="new name")
|
||||||
|
assert db.session.execute(stmt).scalars().one().version == 2
|
||||||
|
|
||||||
|
|
||||||
def test_get_template_history_version(sample_user, sample_service, sample_template):
|
def test_get_template_history_version(sample_user, sample_service, sample_template):
|
||||||
|
|||||||
Reference in New Issue
Block a user