base: add maximum sms length option (#39654)

This commit is contained in:
Valentin Deniaud 2020-03-10 16:45:02 +01:00
parent 560a4ee386
commit 038b4b709d
7 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2020-03-10 14:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('choosit', '0008_auto_20181118_0807'),
]
operations = [
migrations.AddField(
model_name='choositsmsgateway',
name='max_message_length',
field=models.IntegerField(default=160, verbose_name='Maximum message length'),
),
]

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2020-03-10 14:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mobyt', '0007_auto_20181118_0807'),
]
operations = [
migrations.AddField(
model_name='mobytsmsgateway',
name='max_message_length',
field=models.IntegerField(default=160, verbose_name='Maximum message length'),
),
migrations.AlterField(
model_name='mobytsmsgateway',
name='quality',
field=models.CharField(choices=[('l', 'sms direct'), ('ll', 'sms low-cost'), ('n', 'sms top')], default='l', max_length=4, verbose_name='Message quality'),
),
]

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2020-03-10 14:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('orange', '0006_remove_orangesmsgateway_log_level'),
]
operations = [
migrations.AddField(
model_name='orangesmsgateway',
name='max_message_length',
field=models.IntegerField(default=160, verbose_name='Maximum message length'),
),
migrations.AlterField(
model_name='orangesmsgateway',
name='keystore',
field=models.FileField(blank=True, help_text='Certificate and private key in PEM format', null=True, upload_to='orange', verbose_name='Keystore'),
),
]

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2020-03-10 14:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ovh', '0007_auto_20181118_0807'),
]
operations = [
migrations.AddField(
model_name='ovhsmsgateway',
name='max_message_length',
field=models.IntegerField(default=160, verbose_name='Maximum message length'),
),
]

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2020-03-10 14:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('oxyd', '0007_auto_20181118_0807'),
]
operations = [
migrations.AddField(
model_name='oxydsmsgateway',
name='max_message_length',
field=models.IntegerField(default=160, verbose_name='Maximum message length'),
),
]

View File

@ -908,6 +908,8 @@ class SMSResource(BaseResource):
_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(BaseResource):
'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:

View File

@ -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