Remove unused users/carte_controller

This commit is contained in:
Paul Chavard 2018-11-27 15:46:22 +01:00
parent b015d1ca27
commit 37a7e26fc3
9 changed files with 0 additions and 549 deletions

View file

@ -1,78 +0,0 @@
class Users::CarteController < UsersController
before_action only: [:show] do
authorized_routes? self.class
end
def show
@dossier = current_user_dossier
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to url_for(root_path)
end
def save
geo_json = clean_json_latlngs(params[:selection])
dossier = current_user_dossier
dossier.quartier_prioritaires.each(&:destroy)
dossier.cadastres.each(&:destroy)
if geo_json.present?
ModuleApiCartoService.save_qp!(dossier, geo_json)
ModuleApiCartoService.save_cadastre!(dossier, geo_json)
end
dossier.update!(json_latlngs: geo_json)
redirect_to brouillon_dossier_path(dossier)
end
def zones
@dossier = current_user_dossier
@data = {}
geo_json = JSON.parse(params.fetch(:selection, '[]'))
if geo_json.first == ["error", "TooManyPolygons"]
@error = true
else
if @dossier.procedure.module_api_carto.quartiers_prioritaires?
quartiers_prioritaires = ModuleApiCartoService.generate_qp(geo_json)
@dossier.quartier_prioritaires.build(quartiers_prioritaires)
@data[:quartiersPrioritaires] = quartiers_prioritaires
end
if @dossier.procedure.module_api_carto.cadastre?
cadastres = ModuleApiCartoService.generate_cadastre(geo_json)
@dossier.cadastres.build(cadastres)
@data[:cadastres] = cadastres
end
end
end
def self.route_authorization
{
states: [Dossier.states.fetch(:brouillon), Dossier.states.fetch(:en_construction)],
api_carto: true
}
end
private
def clean_json_latlngs(json_latlngs)
# a polygon must contain at least 4 points
# https://tools.ietf.org/html/rfc7946#section-3.1.6
if json_latlngs.present?
multipolygone = JSON.parse(json_latlngs)
if multipolygone.first == ["error", "TooManyPolygons"]
[].to_json
else
multipolygone.reject! { |polygone| polygone.count < 4 }
if multipolygone.present?
multipolygone.to_json
end
end
end
end
end

View file

@ -1,35 +0,0 @@
class UsersController < ApplicationController
before_action :authenticate_user!
def index
redirect_to root_path
end
def current_user_dossier(dossier_id = nil)
dossier_id ||= params[:dossier_id] || params[:id]
dossier = Dossier.find(dossier_id)
if !current_user.owns_or_invite?(dossier)
raise ActiveRecord::RecordNotFound
end
dossier
end
def authorized_routes?(controller)
if !UserRoutesAuthorizationService.authorized_route?(controller, current_user_dossier)
redirect_to_root_path 'Le statut de votre dossier n\'autorise pas cette URL'
end
rescue ActiveRecord::RecordNotFound
redirect_to_root_path 'Vous navez pas accès à ce dossier.'
end
private
def redirect_to_root_path(message)
flash.alert = message
redirect_to url_for root_path
end
end

View file

@ -1,28 +1,4 @@
class ModuleApiCartoService
def self.save_qp!(dossier, json_latlngs)
if dossier.procedure.module_api_carto.quartiers_prioritaires?
qp_list = generate_qp(JSON.parse(json_latlngs))
qp_list.each do |qp|
qp[:dossier_id] = dossier.id
qp[:geometry] = qp[:geometry].to_json
QuartierPrioritaire.create(qp)
end
end
end
def self.save_cadastre!(dossier, json_latlngs)
if dossier.procedure.module_api_carto.cadastre?
cadastre_list = generate_cadastre JSON.parse(json_latlngs)
cadastre_list.each do |cadastre|
cadastre[:dossier_id] = dossier.id
cadastre[:geometry] = cadastre[:geometry].to_json
Cadastre.create(cadastre)
end
end
end
def self.generate_qp(coordinates)
coordinates.flat_map do |coordinate|
ApiCarto::QuartiersPrioritairesAdapter.new(

View file

@ -1,8 +0,0 @@
class UserRoutesAuthorizationService
def self.authorized_route?(controller, dossier)
auth = controller.route_authorization
auth[:states].include?(dossier.state) &&
(auth[:api_carto].nil? ? true : auth[:api_carto] == dossier.use_legacy_carto?)
end
end

View file

@ -1,242 +0,0 @@
shared_examples 'carte_controller_spec' do
describe 'GET #show' do
describe 'before_action authorized_routes?' do
context 'when dossiers procedure have api carto actived' do
context 'when dossier does not have a valid state' do
before do
dossier.state = Dossier.states.fetch(:en_instruction)
dossier.save
get :show, params: { dossier_id: dossier.id }
end
it { is_expected.to redirect_to root_path }
end
end
context 'when dossiers procedure does not have api carto actived' do
let(:dossier) { create(:dossier) }
before do
get :show, params: { dossier_id: dossier.id }
end
it { is_expected.to redirect_to(root_path) }
end
end
context 'user is not connected' do
before do
sign_out user
end
it 'redirects to users/sign_in' do
get :show, params: { dossier_id: dossier.id }
expect(response).to redirect_to('/users/sign_in')
end
end
it 'returns http success if carto is activated' do
get :show, params: { dossier_id: dossier.id }
expect(response).to have_http_status(:success)
end
context 'when procedure not have activate api carto' do
it 'redirection on user dossier list' do
get :show, params: { 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, params: { dossier_id: bad_dossier_id }
expect(response).to redirect_to(root_path)
end
end
it_behaves_like "not owner of dossier", :show
end
describe 'POST #save' do
context 'it cleans json_latlngs' do
let(:dossier) { create(:dossier, state: Dossier.states.fetch(:en_construction)) }
let(:json_latlngs) { multipolygon.to_json }
before do
post :save, params: { dossier_id: dossier.id, selection: json_latlngs }
dossier.reload
end
context 'when json_latlngs is invalid' do
let(:multipolygon) do
[
[
{ lat: 1, lng: 1 },
{ lat: 1, lng: 2 },
{ lat: 1, lng: 1 }
]
]
end
it { expect(dossier.json_latlngs).to be_nil }
end
context 'when json_latlngs is valid' do
let(:multipolygon) do
[
[
{ lat: 1, lng: 1 },
{ lat: 1, lng: 2 },
{ lat: 2, lng: 2 },
{ lat: 1, lng: 1 }
]
]
end
it { expect(dossier.json_latlngs).to eq(json_latlngs) }
end
end
context 'En train de modifier la localisation' do
let(:dossier) { create(:dossier, state: Dossier.states.fetch(:en_construction)) }
before do
post :save, params: { dossier_id: dossier.id, selection: '' }
end
it 'Redirection vers le formulaire de la procedure' do
expect(response).to redirect_to(brouillon_dossier_path(dossier))
end
end
describe 'Save quartier prioritaire' do
let(:module_api_carto) { create(:module_api_carto, :with_quartiers_prioritaires) }
before do
allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter)
.to receive(:results)
.and_return([{ :code => "QPCODE1234", :nom => "QP de test", :commune => "Paris", :geometry => { :type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } }])
post :save, params: { dossier_id: dossier.id, selection: json_latlngs }
end
context 'when json_latlngs params is empty' do
context 'when dossier have quartier prioritaire in database' do
let!(:dossier) { create(:dossier, :with_two_quartier_prioritaires) }
before do
dossier.reload
end
context 'when value is empty' do
let(:json_latlngs) { '' }
it { expect(dossier.quartier_prioritaires.size).to eq(0) }
end
context 'when value is empty array' do
let(:json_latlngs) { '[]' }
it { expect(dossier.quartier_prioritaires.size).to eq(0) }
end
end
end
context 'when json_latlngs params is informed' do
let(:json_latlngs) { '[[{"lat":48.87442541960633,"lng":2.3859214782714844},{"lat":48.87273183590832,"lng":2.3850631713867183},{"lat":48.87081237174292,"lng":2.3809432983398438},{"lat":48.8712640169951,"lng":2.377510070800781},{"lat":48.87510283703279,"lng":2.3778533935546875},{"lat":48.87544154230615,"lng":2.382831573486328},{"lat":48.87442541960633,"lng":2.3859214782714844}]]' }
it { expect(dossier.quartier_prioritaires.size).to eq(1) }
describe 'Quartier Prioritaire' do
subject { QuartierPrioritaire.last }
it { expect(subject.code).to eq('QPCODE1234') }
it { expect(subject.commune).to eq('Paris') }
it { expect(subject.nom).to eq('QP de test') }
it { expect(subject.dossier_id).to eq(dossier.id) }
end
end
end
describe 'Save cadastre' do
let(:module_api_carto) { create(:module_api_carto, :with_cadastre) }
before do
allow_any_instance_of(ApiCarto::CadastreAdapter)
.to receive(:results)
.and_return([{ :surface_intersection => "0.0006", :surface_parcelle => 11252.692583090324, :numero => "0013", :feuille => 1, :section => "CD", :code_dep => "30", :nom_com => "Le Grau-du-Roi", :code_com => "133", :code_arr => "000", :geometry => { :type => "MultiPolygon", :coordinates => [[[[4.134084, 43.5209193], [4.1346615, 43.5212035], [4.1346984, 43.521189], [4.135096, 43.5213848], [4.1350839, 43.5214122], [4.1352697, 43.521505], [4.1356278, 43.5211065], [4.1357402, 43.5207188], [4.1350935, 43.5203936], [4.135002, 43.5204366], [4.1346051, 43.5202412], [4.134584, 43.5202472], [4.1345572, 43.5202551], [4.134356, 43.5203137], [4.1342488, 43.5203448], [4.134084, 43.5209193]]]] } }])
post :save, params: { dossier_id: dossier.id, selection: json_latlngs }
end
context 'when json_latlngs params is empty' do
context 'when dossier have cadastres in database' do
let!(:dossier) { create(:dossier, :with_two_cadastres) }
before do
dossier.reload
end
context 'when value is empty' do
let(:json_latlngs) { '' }
it { expect(dossier.cadastres.size).to eq(0) }
end
context 'when value is empty array' do
let(:json_latlngs) { '[]' }
it { expect(dossier.cadastres.size).to eq(0) }
end
end
end
context 'when json_latlngs params is informed' do
let(:json_latlngs) { '[[{"lat":48.87442541960633,"lng":2.3859214782714844},{"lat":48.87273183590832,"lng":2.3850631713867183},{"lat":48.87081237174292,"lng":2.3809432983398438},{"lat":48.8712640169951,"lng":2.377510070800781},{"lat":48.87510283703279,"lng":2.3778533935546875},{"lat":48.87544154230615,"lng":2.382831573486328},{"lat":48.87442541960633,"lng":2.3859214782714844}]]' }
it { expect(dossier.cadastres.size).to eq(1) }
describe 'Cadastre' do
subject { Cadastre.last }
it { expect(subject.surface_intersection).to eq('0.0006') }
it { expect(subject.surface_parcelle).to eq(11252.6925830903) }
it { expect(subject.numero).to eq('0013') }
it { expect(subject.feuille).to eq(1) }
it { expect(subject.section).to eq('CD') }
it { expect(subject.code_dep).to eq('30') }
it { expect(subject.nom_com).to eq('Le Grau-du-Roi') }
it { expect(subject.code_com).to eq('133') }
it { expect(subject.code_arr).to eq('000') }
it { expect(subject.geometry).to eq({ "type" => "MultiPolygon", "coordinates" => [[[[4.134084, 43.5209193], [4.1346615, 43.5212035], [4.1346984, 43.521189], [4.135096, 43.5213848], [4.1350839, 43.5214122], [4.1352697, 43.521505], [4.1356278, 43.5211065], [4.1357402, 43.5207188], [4.1350935, 43.5203936], [4.135002, 43.5204366], [4.1346051, 43.5202412], [4.134584, 43.5202472], [4.1345572, 43.5202551], [4.134356, 43.5203137], [4.1342488, 43.5203448], [4.134084, 43.5209193]]]] }) }
end
end
end
end
describe 'POST #zones' do
let(:module_api_carto) { create(:module_api_carto, :with_quartiers_prioritaires) }
render_views
before do
allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter)
.to receive(:results)
.and_return([{ :code => "QPCODE1234", :geometry => { :type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } }])
post :zones, params: { dossier_id: dossier.id, selection: json_latlngs.to_json }, format: 'js'
end
context 'when coordinates are empty' do
let(:json_latlngs) { [] }
it 'Quartier Prioritaire Adapter does not call' do
expect(response.body).to include("DS.cartoDrawZones({\"quartiersPrioritaires\":[]});")
end
end
context 'when coordinates are informed' do
let(:json_latlngs) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]] }
it { expect(response.body).not_to be_nil }
it { expect(response.body).to include('QPCODE1234') }
it { expect(response.body).to include('MultiPolygon') }
it { expect(response.body).to include('[2.38715792094576,48.8723062632126]') }
end
end
end

View file

@ -1,36 +0,0 @@
require 'spec_helper'
require 'controllers/users/carte_controller_shared_example'
RSpec.describe Users::CarteController, type: :controller do
let(:bad_adresse) { 'babouba' }
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, procedure: procedure) }
let(:owner_user) { dossier.user }
let(:invite_by_user) { create :user, email: 'invite@plop.com' }
let(:dossier_with_no_carto) { create(:dossier) }
let!(:etablissement) { create(:etablissement, dossier: dossier) }
let(:bad_dossier_id) { Dossier.count + 1000 }
let(:adresse) { etablissement.geo_adresse }
before do
create :invite, dossier: dossier, user: invite_by_user, email: invite_by_user.email
sign_in user
end
context 'when sign in user is the owner' do
let(:user) { owner_user }
it_should_behave_like "carte_controller_spec"
end
context 'when sign in user is an invite by owner' do
let(:user) { invite_by_user }
it_should_behave_like "carte_controller_spec"
end
end

View file

@ -1,43 +0,0 @@
shared_examples 'current_user_dossier_spec' do
context 'when no dossier_id is filled' do
it { expect { subject.current_user_dossier }.to raise_error ActiveRecord::RecordNotFound }
end
context 'when dossier_id is given as a param' do
context 'when dossier id is valid' do
it 'returns current user dossier' do
expect(subject.current_user_dossier dossier.id).to eq(dossier)
end
end
context 'when dossier id is incorrect' do
it { expect { subject.current_user_dossier 666 }.to raise_error ActiveRecord::RecordNotFound }
end
end
context 'when no params[] is given' do
context 'when dossier id is valid' do
before do
subject.params[:dossier_id] = dossier.id
end
it 'returns current user dossier' do
expect(subject.current_user_dossier).to eq(dossier)
end
end
context 'when dossier id is incorrect' do
it { expect { subject.current_user_dossier }.to raise_error ActiveRecord::RecordNotFound }
end
context 'when dossier_id is given as a param' do
before do
subject.params[:dossier_id] = 1
end
it 'returns dossier with the id on params past' do
expect(subject.current_user_dossier dossier.id).to eq(dossier)
end
end
end
end

View file

@ -1,30 +0,0 @@
require 'spec_helper'
require 'controllers/users_controller_shared_example'
describe UsersController, type: :controller do
describe '#current_user_dossier' do
let(:owner_user) { create(:user) }
let(:invite_user) { create :user, email: 'invite@plop.com' }
let(:not_invite_user) { create :user, email: 'not_invite@plop.com' }
let(:dossier) { create(:dossier, user: owner_user) }
context 'when user is the owner' do
before do
sign_in owner_user
end
it_should_behave_like "current_user_dossier_spec"
end
context 'when user is invite by the owner' do
before do
create :invite, email: invite_user.email, dossier: dossier, user: invite_user
sign_in invite_user
end
it_should_behave_like "current_user_dossier_spec"
end
end
end

View file

@ -1,53 +0,0 @@
require 'spec_helper'
describe UserRoutesAuthorizationService do
describe '#authorize_route?' do
let(:module_api_carto) { create :module_api_carto, use_api_carto: use_api_carto }
let(:procedure) { create :procedure, module_api_carto: module_api_carto }
let(:dossier) { create :dossier, procedure: procedure, state: state }
let(:use_api_carto) { false }
subject { described_class.authorized_route? controller, dossier }
describe 'carte' do
let(:controller) { Users::CarteController }
context 'when use_api_carto is false' do
describe 'brouillon' do
let(:state) { Dossier.states.fetch(:brouillon) }
it { is_expected.to be_falsey }
end
describe 'en_construction' do
let(:state) { Dossier.states.fetch(:en_construction) }
it { is_expected.to be_falsey }
end
describe 'accepte' do
let(:state) { Dossier.states.fetch(:accepte) }
it { is_expected.to be_falsey }
end
end
context 'when use_api_carto is true' do
let(:use_api_carto) { true }
describe 'brouillon' do
let(:state) { Dossier.states.fetch(:brouillon) }
it { is_expected.to be_truthy }
end
describe 'en_construction' do
let(:state) { Dossier.states.fetch(:en_construction) }
it { is_expected.to be_truthy }
end
describe 'accepte' do
let(:state) { Dossier.states.fetch(:accepte) }
it { is_expected.to be_falsey }
end
end
end
end
end