def test_check_service_sms_sender_id_where_service_id_is_not_found(sample_service, fake_uuid): sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456') with pytest.raises(BadRequestError) as e: check_service_sms_sender_id(fake_uuid, sms_sender.id, SMS_TYPE) assert e.value.status_code == 400 assert e.value.message == 'sms_sender_id {} does not exist in database for service id {}' \ .format(sms_sender.id, fake_uuid)
def test_post_https_request_to_service(self, notify_api, sample_service): inbound_api = create_service_inbound_api( # nosec service=sample_service, url="https://some.service.gov.uk/", bearer_token="something_unique") inbound_sms = create_inbound_sms(service=sample_service, notify_number="0751421", user_number="447700900111", provider_date=datetime(2017, 6, 20), content="Here is some content") sms_sender = create_service_sms_sender(service=sample_service, sms_sender="0751421") expected_data = { "id": str(inbound_sms.id), "source_number": inbound_sms.user_number, "destination_number": inbound_sms.notify_number, "message": inbound_sms.content, "date_received": inbound_sms.provider_date.strftime(DATETIME_FORMAT), "sms_sender_id": str(sms_sender.id) } with requests_mock.Mocker() as request_mock: request_mock.post(inbound_api.url, json={}, status_code=200) send_inbound_sms_to_service(inbound_sms.id, inbound_sms.service_id) assert request_mock.call_count == 1 assert request_mock.request_history[0].url == inbound_api.url assert request_mock.request_history[0].method == 'POST' assert request_mock.request_history[0].text == json.dumps( expected_data) assert request_mock.request_history[0].headers[ "Content-type"] == "application/json" assert request_mock.request_history[0].headers[ "Authorization"] == "Bearer {}".format(inbound_api.bearer_token)
def test_post_sms_notification_uses_sms_sender_id_reply_to( client, sample_template_with_placeholders, mocker): sms_sender = create_service_sms_sender( service=sample_template_with_placeholders.service, sms_sender='6502532222') mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') data = { 'phone_number': '+16502532222', 'template_id': str(sample_template_with_placeholders.id), 'personalisation': { ' Name': 'Jo' }, 'sms_sender_id': str(sms_sender.id) } auth_header = create_authorization_header( service_id=sample_template_with_placeholders.service_id) response = client.post(path='/v2/notifications/sms', data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 201 resp_json = json.loads(response.get_data(as_text=True)) assert validate(resp_json, post_sms_response) == resp_json assert resp_json['content']['from_number'] == '+16502532222' notifications = Notification.query.all() assert len(notifications) == 1 assert notifications[0].reply_to_text == '+16502532222' mocked.assert_called_once_with([resp_json['id']], queue='send-sms-tasks')
def test_notification_reply_to_text_is_original_value_if_sender_is_changed_after_post_notification( client, sample_template, mocker): sms_sender = create_service_sms_sender(service=sample_template.service, sms_sender='123456', is_default=False) mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') data = { 'phone_number': '+16502532222', 'template_id': str(sample_template.id), 'sms_sender_id': str(sms_sender.id) } auth_header = create_authorization_header( service_id=sample_template.service_id) response = client.post(path='/v2/notifications/sms', data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) dao_update_service_sms_sender(service_id=sample_template.service_id, service_sms_sender_id=sms_sender.id, is_default=sms_sender.is_default, sms_sender='updated') assert response.status_code == 201 notifications = Notification.query.all() assert len(notifications) == 1 assert notifications[0].reply_to_text == '123456'
def test_dao_get_service_sms_senders_id_raises_exception_with_archived_sms_sender( notify_db_session): service = create_service() archived_sms_sender = create_service_sms_sender(service=service, sms_sender="second", is_default=False, archived=True) with pytest.raises(expected_exception=SQLAlchemyError): dao_get_service_sms_sender_by_id( service_id=service.id, service_sms_sender_id=archived_sms_sender.id)
def test_send_sms_should_use_service_sms_sender(sample_service, sample_template, mocker): mocker.patch("app.aws_sns_client.send_sms", return_value="message_id_from_sns") sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456", is_default=False) db_notification = create_notification(template=sample_template, reply_to_text=sms_sender.sms_sender) send_to_providers.send_sms_to_provider( db_notification, ) app.aws_sns_client.send_sms.assert_called_once_with(to=ANY, content=ANY, reference=ANY, sender=sms_sender.sms_sender)
def test_dao_get_sms_senders_by_service_id_does_not_return_archived_senders( notify_db_session): service = create_service() archived_sms_sender = create_service_sms_sender(service=service, sms_sender="second", is_default=False, archived=True) results = dao_get_sms_senders_by_service_id(service_id=service.id) assert len(results) == 1 assert archived_sms_sender not in results
def test_get_service_sms_sender_by_id(admin_request, notify_db_session): service_sms_sender = create_service_sms_sender(service=create_service(), sms_sender='1235', is_default=False) response_json = admin_request.get( 'service_sms_sender.get_service_sms_sender_by_id', service_id=service_sms_sender.service_id, sms_sender_id=service_sms_sender.id, _expected_status=200) assert response_json == service_sms_sender.serialize()
def test_send_sms_should_use_service_sms_sender(sample_service, sample_template, mock_sms_client): sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456', is_default=False) db_notification = create_notification(template=sample_template, reply_to_text=sms_sender.sms_sender) send_to_providers.send_sms_to_provider(db_notification, ) mock_sms_client.send_sms.assert_called_once_with( to=ANY, content=ANY, reference=ANY, sender=sms_sender.sms_sender)
def test_send_one_off_sms_notification_should_use_default_service_reply_to_text( sample_service, celery_mock): template = create_template(service=sample_service, template_type=SMS_TYPE) sample_service.service_sms_senders[0].is_default = False create_service_sms_sender(service=sample_service, sms_sender='07123123456', is_default=True) data = { 'to': '07111111111', 'template_id': str(template.id), 'created_by': str(sample_service.created_by_id), } notification_id = send_one_off_notification(service_id=sample_service.id, post_data=data) notification = Notification.query.get(notification_id['id']) celery_mock.assert_called_once_with(notification=notification, research_mode=False, queue=None) assert notification.reply_to_text == "447123123456"
def test_send_one_off_sms_notification_should_use_default_service_reply_to_text( sample_service, celery_mock): template = create_template(service=sample_service, template_type=SMS_TYPE) sample_service.service_sms_senders[0].is_default = False create_service_sms_sender(service=sample_service, sms_sender="6502532222", is_default=True) data = { "to": "6502532223", "template_id": str(template.id), "created_by": str(sample_service.created_by_id), } notification_id = send_one_off_notification(service_id=sample_service.id, post_data=data) notification = Notification.query.get(notification_id["id"]) celery_mock.assert_called_once_with(notification=notification, research_mode=False, queue=None) assert notification.reply_to_text == "+16502532222"
def test_delete_service_sms_sender_can_archive_sms_sender( admin_request, notify_db_session): service = create_service() service_sms_sender = create_service_sms_sender(service=service, sms_sender='5678', is_default=False) admin_request.post( 'service_sms_sender.delete_service_sms_sender', service_id=service.id, sms_sender_id=service_sms_sender.id, ) assert service_sms_sender.archived is True
def test_get_service_sms_senders_for_service(admin_request, notify_db_session): service_sms_sender = create_service_sms_sender(service=create_service(), sms_sender='second', is_default=False) response_json = admin_request.get( 'service_sms_sender.get_service_sms_senders_for_service', service_id=service_sms_sender.service_id, _expected_status=200) assert len(response_json) == 2 assert response_json[0]['is_default'] assert response_json[0]['sms_sender'] == current_app.config['FROM_NUMBER'] assert not response_json[1]['is_default'] assert response_json[1]['sms_sender'] == 'second'
def test_send_sms_should_use_service_sms_sender(sample_service, sample_template, mocker): res = Mock(content=json.dumps({'sid': 1})) mocker.patch('app.twilio_client.send_sms', return_value=res) sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456', is_default=False) db_notification = create_notification(template=sample_template, reply_to_text=sms_sender.sms_sender) send_to_providers.send_sms_to_provider(db_notification, ) app.twilio_client.send_sms.assert_called_once_with( to=ANY, content=ANY, reference=ANY, sender=sms_sender.sms_sender)
def test_send_sms_should_use_service_sms_sender(sample_service, sample_template, mocker): mocker.patch('app.telstra_sms_client.send_sms', return_value=[ 'MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', NOTIFICATION_SENDING ]) sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456', is_default=False) db_notification = create_notification(template=sample_template, sms_sender_id=sms_sender.id, reply_to_text=sms_sender.sms_sender) send_to_providers.send_sms_to_provider(db_notification, ) app.telstra_sms_client.send_sms.assert_called_once_with( to=ANY, content=ANY, reference=ANY, sender=sms_sender.sms_sender)
def test_update_service_sms_sender(admin_request, notify_db_session): service = create_service() service_sms_sender = create_service_sms_sender(service=service, sms_sender='1235', is_default=False) response_json = admin_request.post( 'service_sms_sender.update_service_sms_sender', service_id=service.id, sms_sender_id=service_sms_sender.id, _data={ "sms_sender": 'second', "is_default": False, }, _expected_status=200) assert response_json['sms_sender'] == 'second' assert not response_json['inbound_number_id'] assert not response_json['is_default']
def test_update_service_sms_sender_does_not_allow_sender_update_for_inbound_number( admin_request, notify_db_session): service = create_service() inbound_number = create_inbound_number('12345', service_id=service.id) service_sms_sender = create_service_sms_sender( service=service, sms_sender='1235', is_default=False, inbound_number_id=inbound_number.id) payload = { "sms_sender": 'second', "is_default": True, "inbound_number_id": str(inbound_number.id) } admin_request.post('service_sms_sender.update_service_sms_sender', service_id=service.id, sms_sender_id=service_sms_sender.id, _data=payload, _expected_status=400)
def test_post_sms_notification_with_archived_reply_to_id_returns_400(client, sample_template, mocker): archived_sender = create_service_sms_sender( sample_template.service, '12345', is_default=False, archived=True) mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') data = { "phone_number": '+447700900855', "template_id": sample_template.id, 'sms_sender_id': archived_sender.id } auth_header = create_authorization_header(service_id=sample_template.service_id) response = client.post( path="v2/notifications/sms", data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 400 resp_json = json.loads(response.get_data(as_text=True)) assert 'sms_sender_id {} does not exist in database for service id {}'. \ format(archived_sender.id, sample_template.service_id) in resp_json['errors'][0]['message'] assert 'BadRequestError' in resp_json['errors'][0]['error']
def test_check_reply_to_sms_type(sample_service): sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456') assert check_reply_to(sample_service.id, sms_sender.id, SMS_TYPE) == '123456'
def test_check_service_sms_sender_id_where_sms_sender_id_is_found(sample_service): sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456') assert check_service_sms_sender_id(sample_service.id, sms_sender.id, SMS_TYPE) == '123456'