orange: add an option to pass sender name (#56345)

This commit is contained in:
Nicolas Roche 2021-08-24 16:10:55 +02:00
parent 7766719cf2
commit 2d7ef66d4a
3 changed files with 57 additions and 6 deletions

View File

@ -0,0 +1,22 @@
# Generated by Django 2.2.19 on 2021-08-24 14:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('orange', '0009_auto_20210202_1304'),
]
operations = [
migrations.AddField(
model_name='orangesmsgateway',
name='provide_sender',
field=models.BooleanField(
default=False,
help_text='Sender name must be allowed by orange',
verbose_name='Use sender configurated with Publik',
),
),
]

View File

@ -49,6 +49,12 @@ class OrangeSMSGateway(SMSResource):
password = models.CharField(verbose_name=_('Password'), max_length=64)
groupname = models.CharField(verbose_name=_('Group'), max_length=64)
provide_sender = models.BooleanField(
_('Use sender configurated with Publik'),
default=False,
help_text=_('Sender name must be allowed by orange'),
)
class Meta:
verbose_name = _('Orange')
db_table = 'sms_orange'
@ -79,7 +85,7 @@ class OrangeSMSGateway(SMSResource):
raise APIError('Group name not found: ' + self.groupname)
return group_id
def diffusion(self, access_token, group_id, destinations, message):
def diffusion(self, access_token, group_id, destinations, message, sender):
headers = {
'content-type': 'application/json',
'authorization': 'Bearer %s' % access_token,
@ -89,6 +95,8 @@ class OrangeSMSGateway(SMSResource):
'msisdns': destinations,
'smsParam': {'encoding': 'GSM7', 'body': message},
}
if self.provide_sender:
payload['smsParam']['senderName'] = sender
response = self.requests.post(URL_DIFFUSION % group_id, json=payload, headers=headers)
if response.status_code != 201:
raise OrangeError('Orange fails to send SMS: %s, %s' % (response.status_code, response.text))
@ -99,5 +107,5 @@ class OrangeSMSGateway(SMSResource):
access_token = self.get_access_token()
group_id = self.group_id_from_name(access_token)
response = self.diffusion(access_token, group_id, destinations, text)
response = self.diffusion(access_token, group_id, destinations, text, sender)
return response

View File

@ -30,7 +30,7 @@ NETLOC = 'contact-everyone.orange-business.com'
JSON_HEADERS = {'content-type': 'application/json'}
PAYLOAD = {
'message': 'hello',
'from': '+33699999999',
'from': 'john',
'to': ['+33688888888', '+33677777777'],
}
@ -59,6 +59,7 @@ def response_diffusion_ok(url, request):
assert request.headers['authorization'] == 'Bearer my_token'
request_body = json.loads(force_text(request.body))
assert request_body['smsParam']['body'] == PAYLOAD['message']
assert 'senderName' not in request_body['smsParam'].keys()
'33688888888' in request_body['msisdns'][0]
'33677777777' in request_body['msisdns'][1]
content = json.dumps({'status': "I'm ok"})
@ -147,13 +148,13 @@ def test_group_id_from_name(app, connector):
def test_diffusion(app, connector):
orange = OrangeSMSGateway()
with httmock.HTTMock(response_diffusion_ok):
resp = orange.diffusion('my_token', 'gid2', PAYLOAD['to'], PAYLOAD['message'])
resp = orange.diffusion('my_token', 'gid2', PAYLOAD['to'], PAYLOAD['message'], PAYLOAD['from'])
assert resp['status'] == "I'm ok"
# not 201
with pytest.raises(OrangeError, match='Orange fails to send SMS'):
with httmock.HTTMock(response_500):
orange.diffusion('my_token', 'gid2', PAYLOAD['to'], PAYLOAD['message'])
orange.diffusion('my_token', 'gid2', PAYLOAD['to'], PAYLOAD['message'], PAYLOAD['from'])
# not json
@httmock.urlmatch(netloc=NETLOC)
@ -162,7 +163,27 @@ def test_diffusion(app, connector):
with pytest.raises(OrangeError, match='Orange returned Invalid JSON content'):
with httmock.HTTMock(mocked_response):
orange.diffusion('my_token', 'gid2', PAYLOAD['to'], PAYLOAD['message'])
orange.diffusion('my_token', 'gid2', PAYLOAD['to'], PAYLOAD['message'], PAYLOAD['from'])
# sender name not allowed
@httmock.urlmatch(netloc=NETLOC)
def mocked_response(url, request):
request_body = json.loads(force_text(request.body))
assert request_body['smsParam']['senderName'] == 'john'
error_response = [
{
'code': 'SenderNameNotAllowed',
'field': 'smsParam.senderName',
'message': 'The given sender name is not allowed.',
}
]
return httmock.response(400, json.dumps(error_response))
orange.provide_sender = True
orange.save()
with pytest.raises(OrangeError, match='The given sender name is not allowed.'):
with httmock.HTTMock(mocked_response):
orange.diffusion('my_token', 'gid2', PAYLOAD['to'], PAYLOAD['message'], PAYLOAD['from'])
def test_send_msg(app, connector):