mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-12 00:02:36 -05:00
Add inbound_number rest and tests
This commit is contained in:
@@ -93,6 +93,7 @@ def register_blueprint(application):
|
|||||||
from app.organisation.rest import organisation_blueprint
|
from app.organisation.rest import organisation_blueprint
|
||||||
from app.dvla_organisation.rest import dvla_organisation_blueprint
|
from app.dvla_organisation.rest import dvla_organisation_blueprint
|
||||||
from app.delivery.rest import delivery_blueprint
|
from app.delivery.rest import delivery_blueprint
|
||||||
|
from app.inbound_number.rest import inbound_number_blueprint
|
||||||
from app.inbound_sms.rest import inbound_sms as inbound_sms_blueprint
|
from app.inbound_sms.rest import inbound_sms as inbound_sms_blueprint
|
||||||
from app.notifications.receive_notifications import receive_notifications_blueprint
|
from app.notifications.receive_notifications import receive_notifications_blueprint
|
||||||
from app.notifications.notifications_ses_callback import ses_callback_blueprint
|
from app.notifications.notifications_ses_callback import ses_callback_blueprint
|
||||||
@@ -134,6 +135,9 @@ def register_blueprint(application):
|
|||||||
delivery_blueprint.before_request(requires_admin_auth)
|
delivery_blueprint.before_request(requires_admin_auth)
|
||||||
application.register_blueprint(delivery_blueprint)
|
application.register_blueprint(delivery_blueprint)
|
||||||
|
|
||||||
|
inbound_number_blueprint.before_request(requires_admin_auth)
|
||||||
|
application.register_blueprint(inbound_number_blueprint, url_prefix='/inbound_number')
|
||||||
|
|
||||||
inbound_sms_blueprint.before_request(requires_admin_auth)
|
inbound_sms_blueprint.before_request(requires_admin_auth)
|
||||||
application.register_blueprint(inbound_sms_blueprint)
|
application.register_blueprint(inbound_sms_blueprint)
|
||||||
|
|
||||||
|
|||||||
0
app/inbound_number/__init__.py
Normal file
0
app/inbound_number/__init__.py
Normal file
45
app/inbound_number/rest.py
Normal file
45
app/inbound_number/rest.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
|
||||||
|
from app.dao.inbound_numbers_dao import (
|
||||||
|
dao_get_inbound_numbers,
|
||||||
|
dao_get_inbound_number_for_service,
|
||||||
|
dao_get_available_inbound_numbers,
|
||||||
|
dao_set_inbound_number_to_service,
|
||||||
|
dao_set_inbound_number_active_flag_for_service
|
||||||
|
)
|
||||||
|
from app.errors import register_errors
|
||||||
|
from app.models import InboundNumber
|
||||||
|
from app.schema_validation import validate
|
||||||
|
|
||||||
|
inbound_number_blueprint = Blueprint('inbound_number', __name__)
|
||||||
|
register_errors(inbound_number_blueprint)
|
||||||
|
|
||||||
|
|
||||||
|
@inbound_number_blueprint.route('', methods=['GET'])
|
||||||
|
def get_inbound_numbers():
|
||||||
|
inbound_numbers = [i.serialize() for i in dao_get_inbound_numbers()]
|
||||||
|
|
||||||
|
return jsonify(data=inbound_numbers)
|
||||||
|
|
||||||
|
|
||||||
|
@inbound_number_blueprint.route('/<uuid:service_id>', methods=['POST'])
|
||||||
|
def post_allocate_or_reactivate_inbound_number(service_id):
|
||||||
|
inbound_number = dao_get_inbound_number_for_service(service_id)
|
||||||
|
|
||||||
|
if not inbound_number:
|
||||||
|
available_numbers = dao_get_available_inbound_numbers()
|
||||||
|
|
||||||
|
if len(available_numbers) > 0:
|
||||||
|
dao_set_inbound_number_to_service(service_id, available_numbers[0])
|
||||||
|
return '', 204
|
||||||
|
else:
|
||||||
|
return '', 409
|
||||||
|
else:
|
||||||
|
dao_set_inbound_number_active_flag_for_service(service_id, active=True)
|
||||||
|
return '', 204
|
||||||
|
|
||||||
|
|
||||||
|
@inbound_number_blueprint.route('/<uuid:service_id>/off', methods=['POST'])
|
||||||
|
def post_deactivate_inbound_number(service_id):
|
||||||
|
dao_set_inbound_number_active_flag_for_service(service_id, active=False)
|
||||||
|
return '', 204
|
||||||
70
tests/app/inbound_number/test_rest.py
Normal file
70
tests/app/inbound_number/test_rest.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
from flask import url_for
|
||||||
|
import json
|
||||||
|
|
||||||
|
from app.models import InboundNumber
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
from tests.app.db import create_service, create_inbound_number
|
||||||
|
|
||||||
|
|
||||||
|
def test_rest_get_inbound_numbers(admin_request, sample_inbound_numbers):
|
||||||
|
result = admin_request.get('inbound_number.get_inbound_numbers')
|
||||||
|
|
||||||
|
assert len(result['data']) == len(sample_inbound_numbers)
|
||||||
|
assert result['data'] == [i.serialize() for i in sample_inbound_numbers]
|
||||||
|
|
||||||
|
|
||||||
|
def test_rest_allocate_inbound_number(admin_request, notify_db_session, sample_inbound_numbers):
|
||||||
|
service = create_service(service_name='test service')
|
||||||
|
admin_request.post(
|
||||||
|
'inbound_number.post_allocate_or_reactivate_inbound_number',
|
||||||
|
_expected_status=204,
|
||||||
|
service_id=service.id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rest_allocate_inbound_number_when_no_inbound_available_returns_409(
|
||||||
|
admin_request, notify_db_session, sample_service):
|
||||||
|
service_1 = create_service(service_name='test service 1')
|
||||||
|
create_inbound_number(number='4', provider='mmg', active=False, service_id=sample_service.id)
|
||||||
|
create_inbound_number(number='5', provider='mmg', active=True, service_id=service_1.id)
|
||||||
|
service_2 = create_service(service_name='test service 2')
|
||||||
|
|
||||||
|
admin_request.post(
|
||||||
|
'inbound_number.post_allocate_or_reactivate_inbound_number',
|
||||||
|
_expected_status=409,
|
||||||
|
service_id=service_2.id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rest_deactivate_inbound_number_for_service(admin_request, notify_db_session, sample_inbound_numbers):
|
||||||
|
service = create_service(service_name='test service 1')
|
||||||
|
create_inbound_number(number='4', provider='mmg', active=True, service_id=service.id)
|
||||||
|
|
||||||
|
admin_request.post(
|
||||||
|
'inbound_number.post_deactivate_inbound_number',
|
||||||
|
_expected_status=204,
|
||||||
|
service_id=service.id
|
||||||
|
)
|
||||||
|
|
||||||
|
inbound_number_deactivated = dao_get_inbound_number_for_service(service.id)
|
||||||
|
assert not inbound_number_deactivated.active
|
||||||
|
|
||||||
|
|
||||||
|
def test_rest_reactivate_inbound_number_for_service(admin_request, notify_db_session, sample_inbound_numbers):
|
||||||
|
service = create_service(service_name='test service 1')
|
||||||
|
create_inbound_number(number='4', provider='mmg', active=False, service_id=service.id)
|
||||||
|
|
||||||
|
admin_request.post(
|
||||||
|
'inbound_number.post_allocate_or_reactivate_inbound_number',
|
||||||
|
_expected_status=204,
|
||||||
|
service_id=service.id
|
||||||
|
)
|
||||||
|
|
||||||
|
inbound_number_reactivated = dao_get_inbound_number_for_service(service.id)
|
||||||
|
assert inbound_number_reactivated.active
|
||||||
Reference in New Issue
Block a user