2015-09-28 18:55:16 +02:00
require 'spec_helper'
RSpec . describe Users :: CarteController , type : :controller do
let ( :bad_adresse ) { 'babouba' }
2016-01-18 12:03:18 +01:00
let ( :module_api_carto ) { create ( :module_api_carto , :with_api_carto ) }
let ( :procedure ) { create ( :procedure , module_api_carto : module_api_carto ) }
2016-01-25 15:54:21 +01:00
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
2015-12-08 11:18:49 +01:00
2016-02-04 14:08:35 +01:00
let ( :dossier_with_no_carto ) { create ( :dossier ) }
2015-09-28 18:55:16 +02:00
let! ( :entreprise ) { create ( :entreprise , dossier : dossier ) }
let! ( :etablissement ) { create ( :etablissement , dossier : dossier ) }
let ( :bad_dossier_id ) { Dossier . count + 1000 }
2016-01-07 11:47:52 +01:00
let ( :adresse ) { etablissement . geo_adresse }
2015-09-28 18:55:16 +02:00
2015-10-09 14:43:19 +02:00
before do
sign_in dossier . user
end
2015-09-28 18:55:16 +02:00
describe 'GET #show' do
2016-01-25 15:54:21 +01:00
describe 'before_action authorized_routes?' do
context 'when dossier’ s procedure have api carto actived' do
context 'when dossier does not have a valid state' do
before do
dossier . state = 'validated'
dossier . save
get :show , dossier_id : dossier . id
end
it { is_expected . to redirect_to root_path }
end
end
context 'when dossier’ s procedure does not have api carto actived' do
2016-02-04 14:08:35 +01:00
let ( :dossier ) { create ( :dossier ) }
2016-01-25 15:54:21 +01:00
before do
get :show , dossier_id : dossier . id
end
it { is_expected . to redirect_to ( root_path ) }
end
end
2015-10-09 14:43:19 +02:00
context 'user is not connected' do
before do
sign_out dossier . user
end
2015-11-02 16:36:52 +01:00
it 'redirects to users/sign_in' do
2015-11-25 17:05:14 +01:00
get :show , dossier_id : dossier . id
2015-10-09 14:43:19 +02:00
expect ( response ) . to redirect_to ( '/users/sign_in' )
end
end
2015-12-08 11:18:49 +01:00
it 'returns http success if carto is activated' do
2015-11-25 17:05:14 +01:00
get :show , dossier_id : dossier . id
2015-09-28 18:55:16 +02:00
expect ( response ) . to have_http_status ( :success )
end
2015-12-08 11:18:49 +01:00
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
2015-09-28 18:55:16 +02:00
end
2015-10-09 17:33:33 +02:00
it_behaves_like " not owner of dossier " , :show
2015-09-28 18:55:16 +02:00
end
2015-11-25 17:05:14 +01:00
describe 'POST #save' do
2015-09-28 18:55:16 +02:00
context 'Aucune localisation n\'a jamais été enregistrée' do
it do
2015-11-25 17:05:14 +01:00
post :save , dossier_id : dossier . id , json_latlngs : ''
expect ( response ) . to redirect_to ( " /users/dossiers/ #{ dossier . id } /description " )
2015-09-28 18:55:16 +02:00
end
end
context 'En train de modifier la localisation' do
2016-02-04 14:08:35 +01:00
let ( :dossier ) { create ( :dossier , state : 'initiated' ) }
2015-09-28 18:55:16 +02:00
before do
2015-11-25 17:05:14 +01:00
post :save , dossier_id : dossier . id , json_latlngs : ''
2015-09-28 18:55:16 +02:00
end
it 'Redirection vers la page récapitulatif' do
2015-11-25 17:05:14 +01:00
expect ( response ) . to redirect_to ( " /users/dossiers/ #{ dossier . id } /recapitulatif " )
end
end
describe 'Save quartier prioritaire' do
2016-01-18 12:03:18 +01:00
let ( :module_api_carto ) { create ( :module_api_carto , :with_quartiers_prioritaires ) }
2015-11-25 17:05:14 +01:00
before do
2015-12-08 16:02:35 +01:00
allow_any_instance_of ( CARTO :: SGMAP :: QuartiersPrioritaires :: Adapter ) .
2015-11-25 17:05:14 +01:00
to receive ( :to_params ) .
and_return ( { " QPCODE1234 " = > { :code = > " QPCODE1234 " , :nom = > " QP de test " , :commune = > " Paris " , :geometry = > { :type = > " MultiPolygon " , :coordinates = > [ [ [ [ 2 . 38715792094576 , 48 . 8723062632126 ] , [ 2 . 38724851642619 , 48 . 8721392348061 ] ] ] ] } } } )
post :save , dossier_id : dossier . id , json_latlngs : json_latlngs
end
context 'when json_latlngs params is empty' do
context 'when dossier have quartier prioritaire in database' do
2016-02-04 14:08:35 +01:00
let! ( :dossier ) { create ( :dossier , :with_two_quartier_prioritaires ) }
2015-11-25 17:05:14 +01:00
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
2015-09-28 18:55:16 +02:00
end
end
2016-01-18 12:03:18 +01:00
describe 'Save cadastre' do
let ( :module_api_carto ) { create ( :module_api_carto , :with_cadastre ) }
before do
allow_any_instance_of ( CARTO :: SGMAP :: Cadastre :: Adapter ) .
to receive ( :to_params ) .
2016-01-25 15:54:21 +01:00
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 ] ] ] ] } } ] )
2016-01-18 12:03:18 +01:00
post :save , dossier_id : dossier . id , json_latlngs : json_latlngs
end
context 'when json_latlngs params is empty' do
context 'when dossier have cadastres in database' do
2016-02-04 14:08:35 +01:00
let! ( :dossier ) { create ( :dossier , :with_two_cadastres ) }
2016-01-18 12:03:18 +01:00
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' ) }
2016-01-25 15:54:21 +01:00
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 ] ] ] ] } ) }
2016-01-18 12:03:18 +01:00
end
end
end
2015-09-28 18:55:16 +02:00
end
describe '#get_position' do
2016-01-27 15:48:27 +01:00
context 'Geocodeur renvoie les positions par defaut' do
2016-01-07 11:47:52 +01:00
let ( :etablissement ) { create ( :etablissement , adresse : bad_adresse , numero_voie : 'dzj' , type_voie : 'fzjfk' , nom_voie : 'hdidjkz' , complement_adresse : 'fjef' , code_postal : 'fjeiefk' , localite : 'zjfkfz' ) }
2016-02-04 14:08:35 +01:00
let ( :dossier ) { create ( :dossier , etablissement : etablissement ) }
2016-01-27 15:48:27 +01:00
2015-09-28 18:55:16 +02:00
before do
2016-01-07 11:47:52 +01:00
stub_request ( :get , / http: \/ \/ api-adresse[.]data[.]gouv[.]fr \/ search[?]limit=1&q= / )
2015-11-25 10:26:55 +01:00
. to_return ( status : 200 , body : '{"query": "babouba", "version": "draft", "licence": "ODbL 1.0", "features": [], "type": "FeatureCollection", "attribution": "BAN"}' , headers : { } )
2015-09-28 18:55:16 +02:00
get :get_position , dossier_id : dossier . id
end
2015-11-02 18:56:41 +01:00
subject { JSON . parse ( response . body ) }
2015-09-28 18:55:16 +02:00
2016-01-27 15:48:27 +01:00
it 'on enregistre des coordonnées lat et lon avec les valeurs par defaut' do
expect ( subject [ 'lat' ] ) . to eq ( '46.538192' )
expect ( subject [ 'lon' ] ) . to eq ( '2.428462' )
2015-09-28 18:55:16 +02:00
end
end
context 'retour d\'un fichier JSON avec 3 attributs' do
before do
stub_request ( :get , " http://api-adresse.data.gouv.fr/search?limit=1&q= #{ adresse } " )
2015-11-25 10:26:55 +01:00
. to_return ( status : 200 , body : '{"query": "50 avenue des champs u00e9lysu00e9es Paris 75008", "version": "draft", "licence": "ODbL 1.0", "features": [{"geometry": {"coordinates": [2.306888, 48.870374], "type": "Point"}, "type": "Feature", "properties": {"city": "Paris", "label": "50 Avenue des Champs u00c9lysu00e9es 75008 Paris", "housenumber": "50", "id": "ADRNIVX_0000000270748251", "postcode": "75008", "name": "50 Avenue des Champs u00c9lysu00e9es", "citycode": "75108", "context": "75, u00cele-de-France", "score": 0.9054545454545454, "type": "housenumber"}}], "type": "FeatureCollection", "attribution": "BAN"}' , headers : { } )
2015-09-28 18:55:16 +02:00
2015-11-25 17:05:14 +01:00
get :get_position , dossier_id : dossier . id
2015-09-28 18:55:16 +02:00
end
subject { JSON . parse ( response . body ) }
it 'format JSON valide' do
expect ( response . content_type ) . to eq ( 'application/json' )
end
it 'latitude' do
expect ( subject [ 'lat' ] ) . to eq ( '48.870374' )
end
it 'longitude' do
expect ( subject [ 'lon' ] ) . to eq ( '2.306888' )
end
it 'dossier_id' do
expect ( subject [ 'dossier_id' ] ) . to eq ( dossier . id . to_s )
end
end
end
2015-11-25 10:26:55 +01:00
describe 'POST #get_qp' do
before do
2015-12-08 16:02:35 +01:00
allow_any_instance_of ( CARTO :: SGMAP :: QuartiersPrioritaires :: Adapter ) .
2015-11-25 10:26:55 +01:00
to receive ( :to_params ) .
2015-11-25 17:05:14 +01:00
and_return ( { " QPCODE1234 " = > { :code = > " QPCODE1234 " , :geometry = > { :type = > " MultiPolygon " , :coordinates = > [ [ [ [ 2 . 38715792094576 , 48 . 8723062632126 ] , [ 2 . 38724851642619 , 48 . 8721392348061 ] ] ] ] } } } )
2015-11-25 10:26:55 +01:00
2015-11-25 17:05:14 +01:00
post :get_qp , dossier_id : dossier . id , coordinates : coordinates
2015-11-25 10:26:55 +01:00
end
context 'when coordinates are empty' do
let ( :coordinates ) { '[]' }
subject { JSON . parse ( response . body ) }
2015-11-30 17:03:36 +01:00
it 'Quartier Prioritaire Adapter does not call' do
expect ( subject [ 'quartier_prioritaires' ] ) . to eq ( { } )
end
2015-11-25 10:26:55 +01:00
end
context 'when coordinates are informed' do
let ( :coordinates ) { '[[{"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}]]' }
2015-11-30 17:03:36 +01:00
subject { JSON . parse ( response . body ) [ 'quartier_prioritaires' ] }
it { expect ( subject ) . not_to be_nil }
it { expect ( subject [ 'QPCODE1234' ] [ 'code' ] ) . to eq ( 'QPCODE1234' ) }
it { expect ( subject [ 'QPCODE1234' ] [ 'geometry' ] [ 'type' ] ) . to eq ( 'MultiPolygon' ) }
it { expect ( subject [ 'QPCODE1234' ] [ 'geometry' ] [ 'coordinates' ] ) . to eq ( [ [ [ [ 2 . 38715792094576 , 48 . 8723062632126 ] , [ 2 . 38724851642619 , 48 . 8721392348061 ] ] ] ] ) }
2015-11-25 10:26:55 +01:00
end
end
2015-09-28 18:55:16 +02:00
end