From 0b8c385de1706df258f864f3313d142d808d800e Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 23 Sep 2016 10:45:12 +0100 Subject: [PATCH] add remove_whitelist tests --- app/service/rest.py | 1 - tests/app/conftest.py | 4 ++- tests/app/dao/test_service_whitelist_dao.py | 27 ++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 25577d7bb..9679110b9 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -287,7 +287,6 @@ def get_whitelist(service_id): @service_blueprint.route('//whitelist', methods=['POST']) def update_whitelist(service_id): - # todo: make this transactional dao_remove_service_whitelist(service_id) whitelist_objs = [ServiceWhitelist.from_string(service_id, contact) for contact in request.get_json()] diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 0f9c66af7..43ac08458 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -130,9 +130,11 @@ def sample_service(notify_db, user=None, restricted=False, limit=1000, - email_from="sample.service"): + email_from=None): if user is None: user = sample_user(notify_db, notify_db_session) + if email_from is None: + email_from = service_name.lower().replace(' ', '.') data = { 'name': service_name, 'message_limit': limit, diff --git a/tests/app/dao/test_service_whitelist_dao.py b/tests/app/dao/test_service_whitelist_dao.py index 8b429d249..2fdd7ae6f 100644 --- a/tests/app/dao/test_service_whitelist_dao.py +++ b/tests/app/dao/test_service_whitelist_dao.py @@ -2,12 +2,13 @@ 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 ) +from tests.app.conftest import sample_service as create_service + def test_fetch_service_whitelist_gets_whitelists(sample_service_whitelist): whitelist = dao_fetch_service_whitelist(sample_service_whitelist.service_id) @@ -21,8 +22,32 @@ def test_fetch_service_whitelist_ignores_other_service(sample_service_whitelist) 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 + + +def test_remove_service_whitelist_only_removes_for_my_service(notify_db, notify_db_session): + service_1 = create_service(notify_db, notify_db_session, service_name="service 1") + service_2 = create_service(notify_db, notify_db_session, service_name="service 2") + dao_add_and_commit_whitelisted_contacts([ + ServiceWhitelist.from_string(service_1.id, 'service1@example.com'), + ServiceWhitelist.from_string(service_2.id, 'service2@example.com') + ]) + + dao_remove_service_whitelist(service_1.id) + + assert service_1.whitelist == [] + assert len(service_2.whitelist) == 1 + + +def test_remove_service_whitelist_does_not_commit(notify_db, sample_service_whitelist): + dao_remove_service_whitelist(sample_service_whitelist.service_id) + + # since dao_remove_service_whitelist doesn't commit, we can still rollback its changes + notify_db.session.rollback() + + assert ServiceWhitelist.query.count() == 1