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_does_not_send_request_when_inbound_sms_does_not_exist(
            self, notify_api, sample_service):
        inbound_api = create_service_inbound_api(service=sample_service)

        with requests_mock.Mocker() as request_mock:
            request_mock.post(inbound_api.url, json={}, status_code=200)
            with pytest.raises(SQLAlchemyError):
                send_inbound_sms_to_service(inbound_sms_id=uuid.uuid4(),
                                            service_id=sample_service.id)

        assert request_mock.call_count == 0
    def test_does_not_send_request_when_inbound_api_does_not_exist(
            self, notify_api, sample_service, mocker):
        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")
        mocked = mocker.patch("requests.request")

        send_inbound_sms_to_service(inbound_sms.id, inbound_sms.service_id)

        assert mocked.call_count == 0
    def test_does_not_retry_if_request_returns_404(self, notify_api,
                                                   sample_service, mocker):
        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")

        mocked = mocker.patch(
            'app.celery.service_callback_tasks.send_inbound_sms_to_service.retry'
        )

        with requests_mock.Mocker() as request_mock:
            request_mock.post(inbound_api.url, json={}, status_code=404)
            send_inbound_sms_to_service(inbound_sms.id, inbound_sms.service_id)

        assert mocked.call_count == 0
    def test_retries_if_request_throws_unknown(self, notify_api,
                                               sample_service, mocker):
        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")

        mocked = mocker.patch(
            'app.celery.service_callback_tasks.send_inbound_sms_to_service.retry'
        )
        mocker.patch("app.celery.service_callback_tasks.request",
                     side_effect=RequestException())

        send_inbound_sms_to_service(inbound_sms.id, inbound_sms.service_id)

        assert mocked.call_count == 1
        assert mocked.call_args[1]['queue'] == 'retry-tasks'