Files
notifications-api/tests/app/clients/test_reach.py
Ben Thorner 7d92a0869a Remove per-client SMS exception classes
In response to: [^1].

The stacktrace conveys the same and more information. We don't do
anything different for each exception class, so there's no value
in having three of them over one exception.

I did think about DRYing-up the duplicate exception behaviour into
the base class one. This isn't ideal because the base class would
be making assumptions about how inheriting classes make requests,
which might change with future providers. Although it might be nice
to have more info in the top-level message, we'll still get it in
the stacktrace e.g.

    ValueError: Expected 'code' to be '0'
    During handling of the above exception, another exception occurred:
    app.clients.sms.SmsClientResponseException: SMS client error (Invalid response JSON)

    requests.exceptions.ReadTimeout
    During handling of the above exception, another exception occurred:
    app.clients.sms.SmsClientResponseException: SMS client error (Request failed)

[^1]: https://github.com/alphagov/notifications-api/pull/3493#discussion_r837363717
2022-03-30 13:38:50 +01:00

85 lines
3.0 KiB
Python

import pytest
import requests_mock
from requests.exceptions import ConnectTimeout, ReadTimeout
from app import reach_client
from app.clients.sms import SmsClientResponseException
# TODO: tests for get_reach_responses
def test_try_send_sms_successful_returns_reach_response(notify_api, mocker):
to = content = reference = 'foo'
response_dict = {} # TODO
with requests_mock.Mocker() as request_mock:
request_mock.post('https://example.com/reach', json=response_dict, status_code=200)
response = reach_client.try_send_sms(to, content, reference, False, 'sender')
# response_json = response.json()
assert response.status_code == 200
# TODO: assertions
def test_try_send_sms_calls_reach_correctly(notify_api, mocker):
to = '+447234567890'
content = 'my message'
reference = 'my reference'
response_dict = {} # TODO
with requests_mock.Mocker() as request_mock:
request_mock.post('https://example.com/reach', json=response_dict, status_code=200)
reach_client.try_send_sms(to, content, reference, False, 'sender')
assert request_mock.call_count == 1
assert request_mock.request_history[0].url == 'https://example.com/reach'
assert request_mock.request_history[0].method == 'POST'
# request_args = request_mock.request_history[0].json()
# TODO: assertions
def test_try_send_sms_raises_if_reach_rejects(notify_api, mocker):
to = content = reference = 'foo'
response_dict = {
'Error': 206,
'Description': 'Some kind of error'
}
with pytest.raises(SmsClientResponseException) as exc, requests_mock.Mocker() as request_mock:
request_mock.post('https://example.com/reach', json=response_dict, status_code=400)
reach_client.try_send_sms(to, content, reference, False, 'sender')
assert "Request failed" in str(exc)
def test_try_send_sms_raises_if_reach_fails_to_return_json(notify_api, mocker):
to = content = reference = 'foo'
response_dict = 'NOT AT ALL VALID JSON {"key" : "value"}}'
with pytest.raises(SmsClientResponseException) as exc, requests_mock.Mocker() as request_mock:
request_mock.post('https://example.com/reach', text=response_dict, status_code=200)
reach_client.try_send_sms(to, content, reference, False, 'sender')
assert 'Invalid response JSON' in str(exc.value)
def test_try_send_sms_raises_if_reach_rejects_with_connect_timeout(rmock):
to = content = reference = 'foo'
with pytest.raises(SmsClientResponseException) as exc:
rmock.register_uri('POST', 'https://example.com/reach', exc=ConnectTimeout)
reach_client.try_send_sms(to, content, reference, False, 'sender')
assert 'Request failed' in str(exc.value)
def test_try_send_sms_raises_if_reach_rejects_with_read_timeout(rmock):
to = content = reference = 'foo'
with pytest.raises(SmsClientResponseException) as exc:
rmock.register_uri('POST', 'https://example.com/reach', exc=ReadTimeout)
reach_client.try_send_sms(to, content, reference, False, 'sender')
assert 'Request failed' in str(exc.value)