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_fetch_service_inbound_api(client, sample_service): service_inbound_api = create_service_inbound_api(service=sample_service) response = client.get("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), headers=[create_authorization_header()]) assert response.status_code == 200 assert json.loads(response.get_data(as_text=True))["data"] == service_inbound_api.serialize()
def test_fetch_service_inbound_api(admin_request, sample_service): service_inbound_api = create_service_inbound_api(service=sample_service) response = admin_request.get( 'service_callback.fetch_service_inbound_api', service_id=sample_service.id, inbound_api_id=service_inbound_api.id, ) assert response["data"] == service_inbound_api.serialize()
def test_get_service_inbound_api_for_service(sample_service): service_inbound_api = create_service_inbound_api(service=sample_service) result = get_service_inbound_api_for_service(sample_service.id) assert result.id == service_inbound_api.id assert result.url == service_inbound_api.url assert result.bearer_token == service_inbound_api.bearer_token assert result.created_at == service_inbound_api.created_at assert result.updated_at == service_inbound_api.updated_at assert result.updated_by_id == service_inbound_api.updated_by_id
def test_get_inbound_sms_returns_200_when_service_has_callbacks( client, sample_service ): create_service_inbound_api( service=sample_service, url="https://inbound.example.com", ) create_service_callback_api( service=sample_service, url="https://inbound.example.com", ) auth_header = create_authorization_header(service_id=sample_service.id) response = client.get( path='/v2/received-text-messages', headers=[('Content-Type', 'application/json'), auth_header], ) assert response.status_code == 200
def test_delete_service_inbound_api(admin_request, sample_service): service_inbound_api = create_service_inbound_api(sample_service) response = admin_request.delete( 'service_callback.remove_service_inbound_api', service_id=sample_service.id, inbound_api_id=service_inbound_api.id, ) assert response is None assert ServiceInboundApi.query.count() == 0
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_update_service_inbound_api_updates_bearer_token(client, sample_service): service_inbound_api = create_service_inbound_api(service=sample_service, bearer_token="some_super_secret") data = { "bearer_token": "different_token", "updated_by_id": str(sample_service.users[0].id) } response = client.post("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), data=json.dumps(data), headers=[('Content-Type', 'application/json'), create_authorization_header()]) assert response.status_code == 200 assert service_inbound_api.bearer_token == "different_token"
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'
def test_update_service_inbound_api_updates_bearer_token( admin_request, sample_service): service_inbound_api = create_service_inbound_api( service=sample_service, bearer_token="some_super_secret") data = { "bearer_token": "different_token", "updated_by_id": str(sample_service.users[0].id) } admin_request.post('service_callback.update_service_inbound_api', service_id=sample_service.id, inbound_api_id=service_inbound_api.id, _data=data) assert service_inbound_api.bearer_token == "different_token"
def test_update_service_inbound_api_updates_url(client, sample_service): service_inbound_api = create_service_inbound_api(service=sample_service, url="https://original_url.com") data = { "url": "https://another_url.com", "updated_by_id": str(sample_service.users[0].id) } response = client.post("/service/{}/inbound-api/{}".format(sample_service.id, service_inbound_api.id), data=json.dumps(data), headers=[('Content-Type', 'application/json'), create_authorization_header()]) assert response.status_code == 200 resp_json = json.loads(response.get_data(as_text=True))["data"] assert resp_json["url"] == "https://another_url.com" assert service_inbound_api.url == "https://another_url.com"
def test_update_service_inbound_api_updates_url(admin_request, sample_service): service_inbound_api = create_service_inbound_api( service=sample_service, url="https://original_url.com") data = { "url": "https://another_url.com", "updated_by_id": str(sample_service.users[0].id) } response = admin_request.post( 'service_callback.update_service_inbound_api', service_id=sample_service.id, inbound_api_id=service_inbound_api.id, _data=data) assert response["data"]["url"] == "https://another_url.com" assert service_inbound_api.url == "https://another_url.com"
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