mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
correct service_whitelist dao/rest functionality
additionally added dao tests
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
from sqlalchemy import or_
|
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.models import ServiceWhitelist
|
from app.models import ServiceWhitelist
|
||||||
|
|
||||||
|
|
||||||
def dao_fetch_service_whitelist(service_id):
|
def dao_fetch_service_whitelist(service_id):
|
||||||
return ServiceWhitelist.query().filter(ServiceWhitelist.service_id == service_id).all()
|
return ServiceWhitelist.query.filter(ServiceWhitelist.service_id == service_id).all()
|
||||||
|
|
||||||
|
|
||||||
def dao_add_whitelisted_contact(obj):
|
def dao_add_and_commit_whitelisted_contacts(objs):
|
||||||
db.session.add(obj)
|
db.session.add_all(objs)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def dao_remove_service_whitelist(service_id):
|
||||||
|
return ServiceWhitelist.query.filter(ServiceWhitelist.service_id == service_id).delete()
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ from app.dao.services_dao import (
|
|||||||
)
|
)
|
||||||
from app.dao.service_whitelist_dao import (
|
from app.dao.service_whitelist_dao import (
|
||||||
dao_fetch_service_whitelist,
|
dao_fetch_service_whitelist,
|
||||||
dao_add_whitelisted_contact
|
dao_add_and_commit_whitelisted_contacts,
|
||||||
|
dao_remove_service_whitelist
|
||||||
)
|
)
|
||||||
from app.dao import notifications_dao
|
from app.dao import notifications_dao
|
||||||
from app.dao.provider_statistics_dao import get_fragment_count
|
from app.dao.provider_statistics_dao import get_fragment_count
|
||||||
@@ -278,14 +279,8 @@ def get_whitelist(service_id):
|
|||||||
whitelist = dao_fetch_service_whitelist(service_id)
|
whitelist = dao_fetch_service_whitelist(service_id)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'emails': [
|
'emails': [item.email_address for item in whitelist if item.email_address is not None],
|
||||||
{'id': item.id, 'email_address': item.email_address}
|
'mobile_numbers': [item.mobile_number for item in whitelist if item.mobile_number is not None]
|
||||||
for item in whitelist if item.email_address is not None
|
|
||||||
],
|
|
||||||
'mobile_numbers': [
|
|
||||||
{'id': item.id, 'mobile_number': item.mobile_number}
|
|
||||||
for item in whitelist if item.mobile_number is not None
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -293,5 +288,7 @@ def get_whitelist(service_id):
|
|||||||
def update_whitelist(service_id):
|
def update_whitelist(service_id):
|
||||||
# todo: make this transactional
|
# todo: make this transactional
|
||||||
dao_remove_service_whitelist(service_id)
|
dao_remove_service_whitelist(service_id)
|
||||||
for contact in request.get_json():
|
|
||||||
dao_add_whitelisted_contact(ServiceWhitelist.from_string(contact))
|
whitelist_objs = [ServiceWhitelist.from_string(service_id, contact) for contact in request.get_json()]
|
||||||
|
|
||||||
|
dao_add_and_commit_whitelisted_contacts(whitelist_objs)
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from app.models import (
|
|||||||
ProviderStatistics,
|
ProviderStatistics,
|
||||||
ProviderDetails,
|
ProviderDetails,
|
||||||
NotificationStatistics,
|
NotificationStatistics,
|
||||||
|
ServiceWhitelist,
|
||||||
KEY_TYPE_NORMAL, KEY_TYPE_TEST, KEY_TYPE_TEAM)
|
KEY_TYPE_NORMAL, KEY_TYPE_TEST, KEY_TYPE_TEAM)
|
||||||
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
|
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
|
||||||
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
|
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
|
||||||
@@ -862,3 +863,18 @@ def already_registered_template(notify_db,
|
|||||||
template = Template(**data)
|
template = Template(**data)
|
||||||
db.session.add(template)
|
db.session.add(template)
|
||||||
return template
|
return template
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def sample_service_whitelist(notify_db, notify_db_session, service=None, email_address=None, mobile_number=None):
|
||||||
|
if service is None:
|
||||||
|
service = sample_service(notify_db, notify_db_session)
|
||||||
|
|
||||||
|
if not email_address and not mobile_number:
|
||||||
|
email_address = 'whitelisted_user@digital.gov.uk'
|
||||||
|
|
||||||
|
whitelisted_user = ServiceWhitelist.from_string(service.id, email_address or mobile_number)
|
||||||
|
|
||||||
|
notify_db.session.add(whitelisted_user)
|
||||||
|
notify_db.session.commit()
|
||||||
|
return whitelisted_user
|
||||||
|
|||||||
25
tests/app/dao/test_service_whitelist_dao.py
Normal file
25
tests/app/dao/test_service_whitelist_dao.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import uuid
|
||||||
|
|
||||||
|
from app.models import ServiceWhitelist
|
||||||
|
from app.dao.service_whitelist_dao import (#
|
||||||
|
dao_fetch_service_whitelist,
|
||||||
|
dao_add_and_commit_whitelisted_contacts,
|
||||||
|
dao_remove_service_whitelist
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_service_whitelist_gets_whitelists(sample_service_whitelist):
|
||||||
|
whitelist = dao_fetch_service_whitelist(sample_service_whitelist.service_id)
|
||||||
|
assert len(whitelist) == 1
|
||||||
|
assert whitelist[0].id == sample_service_whitelist.id
|
||||||
|
|
||||||
|
def test_fetch_service_whitelist_ignores_other_service(sample_service_whitelist):
|
||||||
|
assert len(dao_fetch_service_whitelist(uuid.uuid4())) == 0
|
||||||
|
|
||||||
|
def test_add_and_commit_whitelisted_contacts_saves_data(sample_service):
|
||||||
|
whitelist = ServiceWhitelist.from_string(sample_service.id, 'foo@example.com')
|
||||||
|
dao_add_and_commit_whitelisted_contacts([whitelist])
|
||||||
|
|
||||||
|
db_contents = ServiceWhitelist.query.all()
|
||||||
|
assert len(db_contents) == 1
|
||||||
|
assert db_contents[0].id == whitelist.id
|
||||||
Reference in New Issue
Block a user