From b17d3877671bd744b75ce70bbe4867fa9187e7da Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 7 May 2019 17:31:34 +0200 Subject: [PATCH 2/2] franceconnect: maps FC gender to Publik title (#32876) --- hobo/franceconnect/views.py | 57 +++++++++++++++++++++-------- tests/test_franceconnect.py | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 tests/test_franceconnect.py diff --git a/hobo/franceconnect/views.py b/hobo/franceconnect/views.py index 216e112..68f8629 100644 --- a/hobo/franceconnect/views.py +++ b/hobo/franceconnect/views.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import json + from django.core.urlresolvers import reverse_lazy from django.views.generic import RedirectView, FormView @@ -30,6 +32,21 @@ def get_variable(name): }) return variable +PLATFORMS = { + 'test': { + 'A2_FC_AUTHORIZE_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/authorize', + 'A2_FC_TOKEN_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/token', + 'A2_FC_USERINFO_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/userinfo', + 'A2_FC_LOGOUT_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/logout', + }, + 'prod': { + 'A2_FC_AUTHORIZE_URL': 'https://app.franceconnect.gouv.fr/api/v1/authorize', + 'A2_FC_TOKEN_URL': 'https://app.franceconnect.gouv.fr/api/v1/token', + 'A2_FC_USERINFO_URL': 'https://app.franceconnect.gouv.fr/api/v1/userinfo', + 'A2_FC_LOGOUT_URL': 'https://app.franceconnect.gouv.fr/api/v1/logout', + } +} + class HomeView(FormView): template_name = 'hobo/franceconnect_home.html' @@ -50,22 +67,7 @@ class HomeView(FormView): return initial def form_valid(self, form): - platforms = { - 'test': { - 'A2_FC_AUTHORIZE_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/authorize', - 'A2_FC_TOKEN_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/token', - 'A2_FC_USERINFO_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/userinfo', - 'A2_FC_LOGOUT_URL': 'https://fcp.integ01.dev-franceconnect.fr/api/v1/logout', - }, - 'prod': { - 'A2_FC_AUTHORIZE_URL': 'https://app.franceconnect.gouv.fr/api/v1/authorize', - 'A2_FC_TOKEN_URL': 'https://app.franceconnect.gouv.fr/api/v1/token', - 'A2_FC_USERINFO_URL': 'https://app.franceconnect.gouv.fr/api/v1/userinfo', - 'A2_FC_LOGOUT_URL': 'https://app.franceconnect.gouv.fr/api/v1/logout', - } - } - - for key, value in platforms[form.cleaned_data['platform']].items(): + for key, value in PLATFORMS[form.cleaned_data['platform']].items(): variable = get_variable(key) variable.value = value variable.save() @@ -82,6 +84,29 @@ class HomeView(FormView): variable.value = 'true' variable.save() + variable = get_variable('A2_FC_USER_INFO_MAPPINGS') + variable.value = json.dumps({ + 'last_name': { + 'ref': 'family_name', + 'verified': True, + }, + 'first_name': { + 'ref': 'given_name', + 'verified': True, + }, + 'title': { + 'ref': 'gender', + 'translation': 'simple', + 'translation_simple': { + 'male': 'Monsieur', + 'female': 'Madame', + }, + 'verified': True, + }, + 'email': 'email', + }) + variable.save() + return super(HomeView, self).form_valid(form) def get_context_data(self, **kwargs): diff --git a/tests/test_franceconnect.py b/tests/test_franceconnect.py new file mode 100644 index 0000000..767f0b4 --- /dev/null +++ b/tests/test_franceconnect.py @@ -0,0 +1,73 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2019 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 . + +from test_manager import login + +from hobo.environment.models import Variable, Authentic +from hobo.franceconnect.views import PLATFORMS + + +def test_franceconnect(app, admin_user): + Authentic.objects.create(title='bar', slug='bar', base_url='http://bar.example.net') + login(app) + + assert Variable.objects.filter(name__startswith='SETTING_A2_FC').count() == 0 + + response = app.get('/franceconnect/') + + assert Variable.objects.filter(name__startswith='SETTING_A2_FC').count() == 4 + assert Variable.objects.filter(name__startswith='SETTING_A2_FC_ENABLE', value='true').count() == 0 + + response = response.click('Enable') + + assert Variable.objects.filter(name__startswith='SETTING_A2_FC').count() == 4 + assert Variable.objects.filter(name__startswith='SETTING_A2_FC_ENABLE', value='true').count() == 0 + + response = response.form.submit().follow() + + assert Variable.objects.filter(name__startswith='SETTING_A2_FC').count() == 4 + assert Variable.objects.filter(name__startswith='SETTING_A2_FC_ENABLE', value='true').count() == 1 + + response.form.set('platform', 'prod') + response.form.set('client_id', 'xyz') + response.form.set('client_secret', '1234') + response = response.form.submit().follow() + + assert Variable.objects.filter(name__startswith='SETTING_A2_FC').count() == 9 + + for key, value in PLATFORMS['prod'].items(): + assert Variable.objects.filter(name='SETTING_' + key, value=value).count() == 1 + + assert Variable.objects.get(name='SETTING_A2_FC_USER_INFO_MAPPINGS').json == { + "last_name": { + "ref": "family_name", + "verified": True + }, + "first_name": { + "ref": "given_name", + "verified": True, + }, + "title": { + "ref": "gender", + "translation": "simple", + "translation_simple": { + "male": "Monsieur", + "female": "Madame" + }, + "verified": True + }, + "email": "email" + } -- 2.20.1