From 11596ade651d7ee13f9c8f056ec22bfe54d5eb14 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 8 Dec 2015 11:18:49 +0100 Subject: [PATCH] - QP not research if module_api_carto have not activated quartiers_prioritaires - Carte page is not accessible if module_api_cart have not activated use_api_carto --- app/assets/javascripts/carte.js | 24 ++++++--- app/controllers/users/carte_controller.rb | 6 +++ .../admin/procedures/_informations.html.haml | 5 +- app/views/users/carte/show.html.haml | 11 ++-- config/locales/fr.yml | 1 + .../users/carte_controller_spec.rb | 22 ++++++-- spec/factories/module_api_carto.rb | 2 +- spec/factories/procedure.rb | 6 ++- .../drawing_a_zone_with_freedraw_spec.rb | 52 ++++++++++++++----- 9 files changed, 95 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/carte.js b/app/assets/javascripts/carte.js index 2ac4c81a3..b31eef33e 100644 --- a/app/assets/javascripts/carte.js +++ b/app/assets/javascripts/carte.js @@ -17,10 +17,7 @@ function initCarto() { layers: [OSM] }); - freeDraw = new L.FreeDraw({ - //mode: L.FreeDraw.MODES.CREATE - }); - + freeDraw = new L.FreeDraw(); map.addLayer(freeDraw); if ($("#json_latlngs").val() != '' && $("#json_latlngs").val() != '[]') { @@ -35,13 +32,16 @@ function initCarto() { add_event_freeDraw(); - display_qp(JSON.parse($("#quartier_prioritaires").val())); + if (qp_active()) + display_qp(JSON.parse($("#quartier_prioritaires").val())); } function add_event_freeDraw() { freeDraw.on('markers', function (e) { $("#json_latlngs").val(JSON.stringify(e.latLngs)); - display_qp(get_qp(e.latLngs)['quartier_prioritaires']); + + if (qp_active()) + display_qp(get_qp(e.latLngs)); }); $("#new").on('click', function (e) { @@ -71,7 +71,14 @@ function get_position() { return position; } +function qp_active() { + return $("#map.qp").length > 0 +} + function get_qp(coordinates) { + if (!qp_active()) + return; + var qp; $.ajax({ @@ -84,10 +91,13 @@ function get_qp(coordinates) { qp = data }); - return qp; + return qp['quartier_prioritaires']; } function display_qp(qp_list) { + if (!qp_active()) + return; + qp_array = jsObject_to_array(qp_list); $("#qp_list ul").html(''); diff --git a/app/controllers/users/carte_controller.rb b/app/controllers/users/carte_controller.rb index c03d89b53..39d322aec 100644 --- a/app/controllers/users/carte_controller.rb +++ b/app/controllers/users/carte_controller.rb @@ -3,6 +3,12 @@ class Users::CarteController < UsersController def show @dossier = current_user_dossier + + unless @dossier.procedure.module_api_carto.use_api_carto + flash.alert = t('errors.messages.dossier_map_not_activated') + redirect_to url_for(root_path) + end + rescue ActiveRecord::RecordNotFound flash.alert = t('errors.messages.dossier_not_found') redirect_to url_for(root_path) diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index f415078fa..c7b107cd3 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -23,5 +23,8 @@ Quartiers Prioritaires %li %label - = ff.check_box :cadastre + = ff.check_box :cadastre, disabled: :disabled Cadastre + %i + %strong + (bientôt disponible) diff --git a/app/views/users/carte/show.html.haml b/app/views/users/carte/show.html.haml index 6e4ab507c..f68a588b4 100644 --- a/app/views/users/carte/show.html.haml +++ b/app/views/users/carte/show.html.haml @@ -12,10 +12,13 @@ %br %br .row - #map.qp - #qp_list - %h3 Quartiers prioritaites - %ul + - if @dossier.procedure.module_api_carto.quartiers_prioritaires + #map.qp + #qp_list + %h3 Quartiers prioritaites + %ul + -else + #map = form_tag(url_for({controller: :carte, action: :save, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST') do %br diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 8d369a1ed..f6a6b4019 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -91,6 +91,7 @@ fr: one: "1 erreur a empêché ce(tte) %{resource} d'être sauvegardé(e) :" other: "%{count} erreurs ont empêché ce(tte) %{resource} d'être sauvegardé(e) :" dossier_not_found: "Le dossier n'existe pas ou vous n'y avez pas accès." + dossier_map_not_activated: "Le dossier n'a pas accès à la cartographie." invalid_siret: "Le siret est incorrect" france_connect: connexion: "Erreur lors de la connexion à France Connect." diff --git a/spec/controllers/users/carte_controller_spec.rb b/spec/controllers/users/carte_controller_spec.rb index 01e03c2f2..aa4538cc3 100644 --- a/spec/controllers/users/carte_controller_spec.rb +++ b/spec/controllers/users/carte_controller_spec.rb @@ -3,7 +3,10 @@ require 'spec_helper' RSpec.describe Users::CarteController, type: :controller do let(:bad_adresse) { 'babouba' } - let(:dossier) { create(:dossier, :with_user, :with_procedure) } + let(:procedure) { create(:procedure, :with_api_carto) } + let(:dossier) { create(:dossier, :with_user, procedure: procedure) } + + let(:dossier_with_no_carto) { create(:dossier, :with_user, :with_procedure) } let!(:entreprise) { create(:entreprise, dossier: dossier) } let!(:etablissement) { create(:etablissement, dossier: dossier) } let(:bad_dossier_id) { Dossier.count + 1000 } @@ -25,14 +28,23 @@ RSpec.describe Users::CarteController, type: :controller do end end - it 'returns http success' do + it 'returns http success if carto is activated' do get :show, dossier_id: dossier.id expect(response).to have_http_status(:success) end - it 'redirection vers la liste des dossiers du user si dossier ID n\'existe pas' do - get :show, dossier_id: bad_dossier_id - expect(response).to redirect_to(root_path) + context 'when procedure not have activate api carto' do + it 'redirection on user dossier list' do + get :show, dossier_id: dossier_with_no_carto.id + expect(response).to redirect_to(root_path) + end + end + + context 'when dossier id not exist' do + it 'redirection on user dossier list' do + get :show, dossier_id: bad_dossier_id + expect(response).to redirect_to(root_path) + end end it_behaves_like "not owner of dossier", :show diff --git a/spec/factories/module_api_carto.rb b/spec/factories/module_api_carto.rb index a3145cc2d..742bea6e6 100644 --- a/spec/factories/module_api_carto.rb +++ b/spec/factories/module_api_carto.rb @@ -8,7 +8,7 @@ FactoryGirl.define do use_api_carto true end - trait :with_qp do + trait :with_quartiers_prioritaires do use_api_carto true quartiers_prioritaires true end diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index aac684e7a..e5c5af3a2 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -5,8 +5,10 @@ FactoryGirl.define do description "Demande de subvention à l'intention des associations" after(:build) do |procedure, _evaluator| - module_api_carto = create(:module_api_carto) - procedure.module_api_carto = module_api_carto + if procedure.module_api_carto.nil? + module_api_carto = create(:module_api_carto) + procedure.module_api_carto = module_api_carto + end end trait :with_api_carto do diff --git a/spec/features/users/drawing_a_zone_with_freedraw_spec.rb b/spec/features/users/drawing_a_zone_with_freedraw_spec.rb index 03f63879a..373788e1f 100644 --- a/spec/features/users/drawing_a_zone_with_freedraw_spec.rb +++ b/spec/features/users/drawing_a_zone_with_freedraw_spec.rb @@ -2,7 +2,9 @@ require 'spec_helper' feature 'drawing a zone with freedraw' do let(:user) { create(:user) } - let(:dossier) { create(:dossier, :with_procedure, :with_entreprise, user: user) } + let(:module_api_carto) { create(:module_api_carto, :with_api_carto) } + let(:procedure) { create(:procedure, module_api_carto: module_api_carto) } + let(:dossier) { create(:dossier, :with_entreprise, procedure: procedure, user: user) } context 'when user is not logged in' do before do @@ -22,26 +24,48 @@ feature 'drawing a zone with freedraw' do end end - scenario 'he is redirected to carte page' do - expect(page).to have_css('.content #map') + context 'when procedure have api carto activated' do + scenario 'he is redirected to carte page' do + expect(page).to have_css('.content #map') + end end - context 'when draw a zone on #map', js: true do - before do - allow_any_instance_of(Users::CarteController). - to receive(:generate_qp). - and_return({"QPCODE1234" => { :code => "QPCODE1234", :nom => "Quartier de test", :commune => "Paris", :geometry => { :type=>"MultiPolygon", :coordinates=>[[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] }}}) + context 'when procedure does not have api carto activated' do + let(:module_api_carto) { create(:module_api_carto) } - page.execute_script('freeDraw.fire("markers", {latLngs: []});') - wait_for_ajax + scenario 'he is redirect to user dossiers index' do + expect(page).to have_css('#users_index') end - scenario 'QP name is present on page' do - expect(page).to have_content('Quartier de test') + scenario 'alert message is present' do + expect(page).to have_content('Le dossier n\'a pas accès à la cartographie') end + end - scenario 'Commune is present on page' do - expect(page).to have_content('Paris') + context 'when draw a zone on #map', js: true do + context 'when module quartiers prioritaires is activated' do + let(:module_api_carto) { create(:module_api_carto, :with_quartiers_prioritaires) } + + before do + allow_any_instance_of(Users::CarteController). + to receive(:generate_qp). + and_return({"QPCODE1234" => {:code => "QPCODE1234", :nom => "Quartier de test", :commune => "Paris", :geometry => {:type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]]}}}) + + page.execute_script('freeDraw.fire("markers", {latLngs: []});') + wait_for_ajax + end + + scenario 'div #map .qp is present' do + expect(page).to have_css('.content #map.qp') + end + + scenario 'QP name is present on page' do + expect(page).to have_content('Quartier de test') + end + + scenario 'Commune is present on page' do + expect(page).to have_content('Paris') + end end end end