From f990c09e801ec36e61cdb367aa0cd6eac875f741 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 10 Mar 2020 16:45:02 +0100 Subject: [PATCH 2/2] base: add maximum sms length option (#39654) --- passerelle/base/models.py | 3 +++ tests/test_sms.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/passerelle/base/models.py b/passerelle/base/models.py index de688a12..68211a5f 100644 --- a/passerelle/base/models.py +++ b/passerelle/base/models.py @@ -908,6 +908,8 @@ class SMSResource(models.Model): _can_send_messages_description = _('Sending messages is limited to the following API users:') + max_message_length = models.IntegerField(_('Maximum message length'), default=160) + @classmethod def clean_numbers(cls, destinations, default_country_code='33', default_trunk_prefix='0'): # Yeah France first ! @@ -945,6 +947,7 @@ class SMSResource(models.Model): 'to is not a list of strings' except (ValueError, AssertionError) as e: raise APIError('Payload error: %s' % e) + data['message'] = data['message'][:self.max_message_length] logging.info('sending message %r to %r with sending number %r', data['message'], data['to'], data['from']) if 'nostop' in request.GET: diff --git a/tests/test_sms.py b/tests/test_sms.py index 8386a18b..1b41c3b8 100644 --- a/tests/test_sms.py +++ b/tests/test_sms.py @@ -1,7 +1,9 @@ +import mock import pytest from django.contrib.contenttypes.models import ContentType +from passerelle.apps.ovh.models import OVHSMSGateway from passerelle.base.models import ApiUser, AccessRight, SMSResource from test_manager import login, admin_user @@ -70,3 +72,19 @@ def test_manage_views(admin_user, app, connector): resp = app.get(url) assert 'Endpoints' in resp.text assert 'accessright/add' in resp.text + + +@pytest.mark.parametrize('connector', [OVHSMSGateway], indirect=True) +def test_sms_max_message_length(app, connector): + path = '/%s/%s/send/' % (connector.get_connector_slug(), connector.slug) + + message_above_limit = 'a' * (connector.max_message_length + 1) + payload = { + 'message': message_above_limit, + 'from': '+33699999999', + 'to': ['+33688888888'], + } + with mock.patch.object(OVHSMSGateway, 'send_msg') as send_function: + send_function.return_value = {} + result = app.post_json(path, params=payload) + assert send_function.call_args[0][0] == 'a' * connector.max_message_length -- 2.20.1