qualification: get only backoffice submission available forms (#24495)

This commit is contained in:
Serghei Mihai 2018-06-19 11:33:04 +02:00
parent 2c3f3cda40
commit 27d62bf7ff
4 changed files with 70 additions and 8 deletions

View File

@ -0,0 +1,56 @@
# welco - multichannel request processing
# Copyright (C) 2018 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import mock
import pytest
from django.test import override_settings
from welco.forms import QualificationForm
pytestmark = pytest.mark.django_db
KNOWN_SERVICES = {
'wcs': {
'eservices': {
'url': 'http://localhost/',
'title': 'Eservices',
'orig': 'welco'
}
}
}
@mock.patch('welco.utils.requests.get')
def test_get_qualification(mocked_get, client):
with override_settings(KNOWN_SERVICES=KNOWN_SERVICES):
forms = mock.Mock()
forms.json.return_value = {'data': [{'category': 'Test',
'authentication_required': False,
'description': '',
'title': 'Test form',
'slug': 'test-form'}],
'err': 0}
mocked_get.return_value = forms
user = mock.Mock()
user.saml_identifiers = mock.Mock()
user.saml_identifiers.exists.return_value = True
user.saml_identifiers.first.return_value = mock.Mock(name_id='nameid')
form = QualificationForm(user)
assert 'backoffice-submission=on' in mocked_get.call_args[0][0]
assert 'NameID=nameid' in mocked_get.call_args[0][0]

View File

@ -23,7 +23,10 @@ from .utils import get_wcs_options
class QualificationForm(forms.Form):
formdef_reference = forms.CharField(label=_('Associated Form'))
def __init__(self, *args, **kwargs):
def __init__(self, user, *args, **kwargs):
super(QualificationForm, self).__init__(*args, **kwargs)
formdef_references = get_wcs_options('api/formdefs/')
params = {'backoffice-submission': 'on'}
if hasattr(user, 'saml_identifiers') and user.saml_identifiers.exists():
params['NameID'] = user.saml_identifiers.first().name_id
formdef_references = get_wcs_options('api/formdefs/', params=params)
self.fields['formdef_reference'].widget = forms.Select(choices=formdef_references)

View File

@ -28,6 +28,7 @@ import urlparse
from django.conf import settings
from django.core.cache import cache
from django.http import HttpResponse, HttpResponseBadRequest
from django.utils.http import urlencode
def sign_url(url, key, algo='sha256', timestamp=None, nonce=None):
parsed = urlparse.urlparse(url)
@ -59,13 +60,15 @@ def sign_string(s, key, algo='sha256', timedelta=30):
def get_wcs_services():
return settings.KNOWN_SERVICES.get('wcs')
def get_wcs_json(wcs_url, path, wcs_site):
def get_wcs_json(wcs_url, path, wcs_site, params={}):
if not wcs_url.endswith('/'):
wcs_url += '/'
url = wcs_url + path
url = wcs_url + path + '?orig=%s' % wcs_site.get('orig')
if params:
url += '&' + urlencode(params)
response_json = cache.get(url)
if response_json is None:
signed_url = sign_url(url + '?orig=%s' % wcs_site.get('orig'), wcs_site.get('secret'))
signed_url = sign_url(url, wcs_site.get('secret'))
response_json = requests.get(signed_url, headers={'accept': 'application/json'},
timeout=10).json()
if not isinstance(response_json, dict):
@ -73,11 +76,11 @@ def get_wcs_json(wcs_url, path, wcs_site):
cache.set(url, response_json)
return response_json
def get_wcs_options(url, condition=None):
def get_wcs_options(url, condition=None, params={}):
categories = {}
for wcs_key, wcs_site in get_wcs_services().iteritems():
site_title = wcs_site.get('title')
response_json = get_wcs_json(wcs_site.get('url'), url, wcs_site)
response_json = get_wcs_json(wcs_site.get('url'), url, wcs_site, params)
if type(response_json) is dict:
response_json = response_json.get('data')
for element in response_json:

View File

@ -77,7 +77,7 @@ class Qualification(TemplateView):
def get_context_data(self, **kwargs):
context = super(Qualification, self).get_context_data(**kwargs)
context['form'] = QualificationForm()
context['form'] = QualificationForm(self.request.user)
context['source_type'] = self.request.GET['source_type']
source_type = ContentType.objects.get(id=self.request.GET['source_type'])
context['source_type_name'] = source_type.model