Projet

Général

Profil

0001-sms-ensure-country-code-and-prefix-are-numbers-61813.patch

Nicolas Roche, 16 février 2022 12:52

Télécharger (3,89 ko)

Voir les différences:

Subject: [PATCH] sms: ensure country code and prefix are numbers (#61813)

 passerelle/sms/models.py | 11 +++++++++--
 tests/test_sms.py        | 10 +++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)
passerelle/sms/models.py
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16
import logging
17 17
import re
18 18

  
19 19
from django.conf.urls import url
20 20
from django.contrib.postgres.fields import ArrayField
21
from django.core.validators import RegexValidator
21 22
from django.db import models
22 23
from django.urls import reverse
23 24
from django.utils import six
24 25
from django.utils.module_loading import import_string
25 26
from django.utils.translation import ugettext_lazy as _
26 27

  
27 28
from passerelle.base.models import BaseResource
28 29
from passerelle.compat import json_loads
......
61 62
    category = _('SMS Providers')
62 63
    documentation_url = (
63 64
        'https://doc-publik.entrouvert.com/admin-fonctionnel/les-tutos/configuration-envoi-sms/'
64 65
    )
65 66

  
66 67
    _can_send_messages_description = _('Sending messages is limited to the following API users:')
67 68

  
68 69
    default_country_code = models.CharField(
69
        verbose_name=_('Default country code'), max_length=3, default=u'33'
70
        verbose_name=_('Default country code'),
71
        max_length=3,
72
        default=u'33',
73
        validators=[RegexValidator('^[0-9]*$', _('The country must only contain numbers'))],
70 74
    )
71 75
    default_trunk_prefix = models.CharField(
72
        verbose_name=_('Default trunk prefix'), max_length=2, default=u'0'
76
        verbose_name=_('Default trunk prefix'),
77
        max_length=2,
78
        default=u'0',
79
        validators=[RegexValidator('^[0-9]*$', _('The trunk prefix must only contain numbers'))],
73 80
    )  # Yeah France first !
74 81
    max_message_length = models.IntegerField(
75 82
        _('Maximum message length'), help_text=_('Messages over this limit will be truncated.'), default=2000
76 83
    )
77 84

  
78 85
    manager_view_template_name = 'passerelle/manage/messages_service_view.html'
79 86

  
80 87
    FR_METRO = 'fr-metro'
tests/test_sms.py
615 615
    assert (
616 616
        _('no')
617 617
        in [x.text for x in resp.html.find_all('p') if x.text.startswith(_('Allow premium rate numbers'))][0]
618 618
    )
619 619

  
620 620
    path = '/manage/%s/%s/edit' % (connector.get_connector_slug(), connector.slug)
621 621
    resp = app.get(path)
622 622
    resp.form['authorized'] = []
623
    resp.form['default_country_code'] = '+33'
624
    resp.form['default_trunk_prefix'] = 'x'
623 625
    resp = resp.form.submit()
624 626
    assert resp.html.find('div', {'class': 'errornotice'}).p.text == 'There were errors processing your form.'
625
    assert resp.html.find('div', {'class': 'error'}).text.strip() == 'This field is required.'
627
    assert [x.text.strip() for x in resp.html.find_all('div', {'class': 'error'})] == [
628
        'The country must only contain numbers',
629
        'The trunk prefix must only contain numbers',
630
        'This field is required.',
631
    ]
626 632
    resp.form['authorized'] = [SMSResource.FR_METRO, SMSResource.FR_DOMTOM]
633
    resp.form['default_country_code'] = '33'
634
    resp.form['default_trunk_prefix'] = '0'
627 635
    resp = resp.form.submit()
628 636
    resp = resp.follow()
629 637
    assert (
630 638
        _('France mainland (+33 [67])')
631 639
        in [x.text for x in resp.html.find_all('p') if x.text.startswith(_('Authorized Countries'))][0]
632 640
    )
633 641

  
634 642
    path = '/%s/%s/send/' % (connector.get_connector_slug(), connector.slug)
635
-