2018-10-22 15:00:52 +02:00
|
|
|
describe Champs::CarteController, type: :controller do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:procedure) { create(:procedure, :published) }
|
|
|
|
let(:dossier) { create(:dossier, user: user, procedure: procedure) }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
dossier: {
|
|
|
|
champs_attributes: {
|
2018-11-27 12:15:36 +01:00
|
|
|
'1' => { value: value }
|
2018-10-22 15:00:52 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
position: '1',
|
|
|
|
champ_id: champ.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
let(:champ) do
|
|
|
|
create(:type_de_champ_carte, options: {
|
2020-04-09 19:08:18 +02:00
|
|
|
cadastres: true
|
2020-04-09 17:32:20 +02:00
|
|
|
}).champ.create(dossier: dossier)
|
2018-10-22 15:00:52 +02:00
|
|
|
end
|
2020-05-05 15:09:29 +02:00
|
|
|
describe 'features' do
|
|
|
|
let(:feature) { attributes_for(:geo_area, :polygon) }
|
|
|
|
let(:geo_area) { create(:geo_area, :selection_utilisateur, :polygon, champ: champ) }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
champ_id: champ.id,
|
|
|
|
feature: feature
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in user
|
|
|
|
request.accept = "application/json"
|
|
|
|
request.content_type = "application/json"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #create' do
|
|
|
|
before do
|
|
|
|
post :create, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(response.status).to eq 201 }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH #update' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
champ_id: champ.id,
|
|
|
|
id: geo_area.id,
|
|
|
|
feature: feature
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
patch :update, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(response.status).to eq 204 }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE #destroy' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
champ_id: champ.id,
|
|
|
|
id: geo_area.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
delete :destroy, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(response.status).to eq 204 }
|
|
|
|
end
|
|
|
|
|
2020-05-07 12:06:42 +02:00
|
|
|
describe 'POST #import' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
champ_id: champ.id,
|
|
|
|
feature_collection: {
|
|
|
|
features: [feature]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
post :import, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
expect(response.status).to eq 201
|
|
|
|
expect(response.body).to include("bbox")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
describe 'GET #index' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
before do
|
|
|
|
request.accept = "application/javascript"
|
|
|
|
request.content_type = "application/javascript"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with cadastres update' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
champ_id: champ.id,
|
|
|
|
cadastres: 'update'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
get :index, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
expect(response.body).to include("DS.fire('cadastres:update'")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without cadastres update' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
champ_id: champ.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
get :index, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
expect(response.body).not_to include("DS.fire('cadastres:update'")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-22 15:00:52 +02:00
|
|
|
|
|
|
|
describe 'POST #show' do
|
|
|
|
render_views
|
|
|
|
|
2019-01-24 18:30:14 +01:00
|
|
|
context 'when the API is available' do
|
|
|
|
render_views
|
2018-10-22 15:00:52 +02:00
|
|
|
|
2019-01-24 18:30:14 +01:00
|
|
|
before do
|
|
|
|
sign_in user
|
2018-10-22 15:00:52 +02:00
|
|
|
|
2020-04-09 19:08:18 +02:00
|
|
|
allow_any_instance_of(ApiCarto::CadastreAdapter)
|
2019-01-24 18:30:14 +01:00
|
|
|
.to receive(:results)
|
2020-04-09 17:32:20 +02:00
|
|
|
.and_return([{ code: "QPCODE1234", surface_parcelle: 4, geometry: { type: "MultiPolygon", coordinates: [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061], [2.38724851642620, 48.8721392348064], [2.38715792094576, 48.8723062632126]]]] } }])
|
2018-10-22 15:00:52 +02:00
|
|
|
|
2019-01-24 18:30:14 +01:00
|
|
|
post :show, params: params, format: 'js'
|
|
|
|
end
|
2018-10-22 15:00:52 +02:00
|
|
|
|
2019-01-24 18:30:14 +01:00
|
|
|
context 'when coordinates are empty' do
|
2020-04-14 10:24:30 +02:00
|
|
|
let(:value) do
|
|
|
|
{
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: []
|
|
|
|
}.to_json
|
|
|
|
end
|
2019-01-24 18:30:14 +01:00
|
|
|
|
|
|
|
it {
|
|
|
|
expect(assigns(:error)).to eq(nil)
|
|
|
|
expect(champ.reload.value).to eq(nil)
|
|
|
|
expect(champ.reload.geo_areas).to eq([])
|
2020-04-09 17:32:20 +02:00
|
|
|
expect(response.body).to include("DS.fire('carte:update'")
|
2019-01-24 18:30:14 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when coordinates are informed' do
|
2020-04-14 10:24:30 +02:00
|
|
|
let(:value) do
|
|
|
|
{
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: [
|
|
|
|
{
|
|
|
|
type: 'Feature',
|
|
|
|
properties: {
|
|
|
|
source: 'selection_utilisateur'
|
|
|
|
},
|
|
|
|
geometry: { type: 'Polygon', coordinates: [[[2.3859214782714844, 48.87442541960633], [2.3850631713867183, 48.87273183590832], [2.3809432983398438, 48.87081237174292], [2.377510070800781, 48.8712640169951], [2.3859214782714844, 48.87442541960633]]] }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}.to_json
|
|
|
|
end
|
2019-01-24 18:30:14 +01:00
|
|
|
|
2020-04-14 10:24:30 +02:00
|
|
|
it {
|
|
|
|
expect(response.body).not_to be_nil
|
|
|
|
expect(response.body).to include('MultiPolygon')
|
|
|
|
expect(response.body).to include('[2.38715792094576,48.8723062632126]')
|
|
|
|
}
|
2019-01-24 18:30:14 +01:00
|
|
|
end
|
2018-10-22 15:00:52 +02:00
|
|
|
|
2019-01-24 18:30:14 +01:00
|
|
|
context 'when error' do
|
|
|
|
let(:value) { '' }
|
|
|
|
|
|
|
|
it {
|
|
|
|
expect(assigns(:error)).to eq(true)
|
|
|
|
expect(champ.reload.value).to eq(nil)
|
|
|
|
expect(champ.reload.geo_areas).to eq([])
|
|
|
|
}
|
|
|
|
end
|
2018-10-22 15:00:52 +02:00
|
|
|
end
|
2018-11-21 12:42:13 +01:00
|
|
|
|
2019-01-24 18:30:14 +01:00
|
|
|
context 'when the API is unavailable' do
|
|
|
|
before do
|
|
|
|
sign_in user
|
|
|
|
|
2020-04-09 19:08:18 +02:00
|
|
|
allow_any_instance_of(ApiCarto::CadastreAdapter)
|
2019-01-24 18:30:14 +01:00
|
|
|
.to receive(:results)
|
2020-03-05 13:50:38 +01:00
|
|
|
.and_raise(ApiCarto::API::ResourceNotFound)
|
2019-01-24 18:30:14 +01:00
|
|
|
|
|
|
|
post :show, params: params, format: 'js'
|
|
|
|
end
|
|
|
|
|
2020-04-14 10:24:30 +02:00
|
|
|
let(:value) do
|
|
|
|
{
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: [
|
|
|
|
{
|
|
|
|
type: 'Feature',
|
|
|
|
properties: {
|
|
|
|
source: 'selection_utilisateur'
|
|
|
|
},
|
|
|
|
geometry: { type: 'Polygon', coordinates: [[[2.3859214782714844, 48.87442541960633], [2.3850631713867183, 48.87273183590832], [2.3809432983398438, 48.87081237174292], [2.377510070800781, 48.8712640169951], [2.3859214782714844, 48.87442541960633]]] }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}.to_json
|
|
|
|
end
|
2018-11-21 12:42:13 +01:00
|
|
|
|
2020-04-14 10:24:30 +02:00
|
|
|
it {
|
|
|
|
expect(response.status).to eq 503
|
|
|
|
expect(response.body).to include('Les données cartographiques sont temporairement indisponibles')
|
|
|
|
}
|
2018-11-21 12:42:13 +01:00
|
|
|
end
|
2018-10-22 15:00:52 +02:00
|
|
|
end
|
|
|
|
end
|