diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 3d6402da2..4503d90c2 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -39,7 +39,14 @@ from app.dao.invited_user_dao import save_invited_user from app.dao.provider_rates_dao import create_provider_rates from app.clients.sms.firetext import FiretextClient from tests import create_authorization_header -from tests.app.db import create_user, create_template, create_notification, create_api_key, create_inbound_number +from tests.app.db import ( + create_user, + create_template, + create_notification, + create_service, + create_api_key, + create_inbound_number +) @pytest.yield_fixture @@ -985,9 +992,10 @@ def sample_provider_rate(notify_db, notify_db_session, valid_from=None, rate=Non @pytest.fixture def sample_inbound_numbers(notify_db, notify_db_session, sample_service): + service = create_service(service_name='sample service 2') inbound_numbers = [] inbound_numbers.append(create_inbound_number(number='1', provider='mmg')) - inbound_numbers.append(create_inbound_number(number='2', provider='mmg', active=False)) + inbound_numbers.append(create_inbound_number(number='2', provider='mmg', active=False, service_id=service.id)) inbound_numbers.append(create_inbound_number(number='3', provider='firetext', service_id=sample_service.id)) return inbound_numbers @@ -1043,8 +1051,11 @@ def admin_request(client): data=json.dumps(_data), headers=[('Content-Type', 'application/json'), create_authorization_header()] ) - json_resp = json.loads(resp.get_data(as_text=True)) - assert resp.status_code == _expected_status, json_resp + if resp.get_data(as_text=True): + json_resp = json.loads(resp.get_data(as_text=True)) + else: + json_resp = None + assert resp.status_code == _expected_status return json_resp @staticmethod diff --git a/tests/app/dao/test_inbound_numbers_dao.py b/tests/app/dao/test_inbound_numbers_dao.py index 333ea5d10..40b2db478 100644 --- a/tests/app/dao/test_inbound_numbers_dao.py +++ b/tests/app/dao/test_inbound_numbers_dao.py @@ -5,11 +5,12 @@ from app.dao.inbound_numbers_dao import ( dao_get_inbound_numbers, dao_get_available_inbound_numbers, dao_get_inbound_number_for_service, - dao_set_inbound_number_to_service + dao_set_inbound_number_to_service, + dao_set_inbound_number_active_flag_for_service ) from app.models import InboundNumber -from tests.app.db import create_service +from tests.app.db import create_service, create_inbound_number def test_get_inbound_numbers(notify_db, notify_db_session, sample_inbound_numbers): @@ -26,7 +27,7 @@ def test_get_available_inbound_numbers(notify_db, notify_db_session, sample_inbo assert res[0] == sample_inbound_numbers[0] -def test_allocate_inbound_number_to_service(notify_db, notify_db_session, sample_inbound_numbers): +def test_set_service_id_on_inbound_number(notify_db, notify_db_session, sample_inbound_numbers): service = create_service(service_name='test service') numbers = dao_get_available_inbound_numbers() @@ -38,9 +39,34 @@ def test_allocate_inbound_number_to_service(notify_db, notify_db_session, sample assert res[0].service_id == service.id -def test_allocating_a_service_twice_will_raise_an_error(notify_db, notify_db_session, sample_inbound_numbers): - from tests.app.db import create_inbound_number - create_inbound_number(number='4', provider='mmg') +def test_after_setting_service_id_that_inbound_number_is_unavailable( + notify_db, notify_db_session, sample_inbound_numbers): + service = create_service(service_name='test service') + numbers = dao_get_available_inbound_numbers() + + assert len(numbers) == 1 + + dao_set_inbound_number_to_service(service.id, numbers[0]) + + res = dao_get_available_inbound_numbers() + + assert len(res) == 0 + + +def test_get_inbound_number_for_service(notify_db, notify_db_session, sample_inbound_numbers): + service = create_service(service_name='test service') + inbound_number = create_inbound_number(number='4') + + dao_set_inbound_number_to_service(service.id, inbound_number) + + res = dao_get_inbound_number_for_service(service.id) + + assert res.service_id == service.id + + +def test_setting_a_service_twice_will_raise_an_error(notify_db, notify_db_session): + create_inbound_number(number='1') + create_inbound_number(number='2') service = create_service(service_name='test service') numbers = dao_get_available_inbound_numbers() @@ -49,15 +75,19 @@ def test_allocating_a_service_twice_will_raise_an_error(notify_db, notify_db_ses with pytest.raises(IntegrityError) as e: dao_set_inbound_number_to_service(service.id, numbers[1]) - res = InboundNumber.query.filter(InboundNumber.service_id == service.id).all() + res = dao_get_inbound_number_for_service(service.id) - assert len(res) == 1 - assert res[0].service_id == service.id + assert res.service_id == service.id assert 'duplicate key value violates unique constraint' in str(e.value) -def test_get_inbound_number_for_service(notify_db, notify_db_session, sample_inbound_numbers, sample_service): - res = dao_get_inbound_number_for_service(sample_service.id) +@pytest.mark.parametrize("active", [True, False]) +def test_set_inbound_number_active_flag(notify_db, notify_db_session, sample_service, active): + inbound_number = create_inbound_number(number='1') + dao_set_inbound_number_to_service(sample_service.id, inbound_number) - assert len(res) == 1 - assert res[0].service_id == sample_service.id + dao_set_inbound_number_active_flag_for_service(sample_service.id, active=active) + + inbound_number = dao_get_inbound_number_for_service(sample_service.id) + + assert inbound_number.active is active