From a1626bdd1a804ba412c28e831731fe0aea7ef7e1 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 26 Nov 2020 06:34:12 +0100 Subject: [PATCH] phonecalls: pass callee to redirect_url (#48850) --- passerelle/apps/phonecalls/models.py | 16 +++++++++++++++- tests/test_phonecalls.py | 10 ++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/passerelle/apps/phonecalls/models.py b/passerelle/apps/phonecalls/models.py index 5e06592b..75c94690 100644 --- a/passerelle/apps/phonecalls/models.py +++ b/passerelle/apps/phonecalls/models.py @@ -15,12 +15,13 @@ # along with this program. If not, see . import json +from urllib.parse import urlparse, urlunparse from django.contrib.postgres.fields import JSONField from django.db import models from django.utils.timezone import now, timedelta, make_naive from django.utils.translation import ugettext_lazy as _ -from django.http import HttpResponseRedirect, HttpResponse +from django.http import HttpResponseRedirect, HttpResponse, QueryDict from django.shortcuts import render from passerelle.base.models import BaseResource @@ -46,6 +47,17 @@ class PhoneCalls(BaseResource): class Meta: verbose_name = _('Phone Calls') + @classmethod + def add_callee_to_url(self, url, callee): + parsed_url = urlparse(url) + if parsed_url.query: + query = QueryDict(parsed_url.query, mutable=True) + for key in query: + if query[key] == 'callee': + query[key] = callee + url = urlunparse(parsed_url._replace(query=query.urlencode())) + return url + @endpoint(name='call-start', description=_('Notify a call start'), perm='can_access', @@ -71,6 +83,8 @@ class PhoneCalls(BaseResource): response = {'data': new_call.json()} redirect_url = self.redirect_url + if redirect_url: + redirect_url = self.add_callee_to_url(redirect_url, callee) # redirect to agent's portal if redirect and redirect_url: diff --git a/tests/test_phonecalls.py b/tests/test_phonecalls.py index d1a573ea..1dce91a0 100644 --- a/tests/test_phonecalls.py +++ b/tests/test_phonecalls.py @@ -235,3 +235,13 @@ def test_phonecalls_start_newtab(app, phonecalls): assert resp.content_type == 'text/html' assert 'window.open("https://portail\\u002Dagent.publik/")' in resp.text assert Call.objects.filter(callee='42', caller='0612345678').count() == 1 + + +def test_phonecalls_add_callee(app, phonecalls): + phonecalls.redirect_url = 'https://portail-agent.publik/?callee=callee' + phonecalls.save() + + start_endpoint = utils.generic_endpoint_url('phonecalls', 'call-start', slug=phonecalls.slug) + resp = app.get(start_endpoint, status=302, params={ + 'apikey': '123', 'callee': '42', 'caller': '0612345678', 'redirect': '1'}) + assert resp.location == 'https://portail-agent.publik/?callee=42' -- 2.29.2