demarches-normaliennes/spec/controllers/champs/carte_controller_spec.rb

86 lines
3.2 KiB
Ruby
Raw Normal View History

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: {
'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
}).champ.create(dossier: dossier)
2018-10-22 15:00:52 +02:00
end
describe 'POST #show' do
render_views
context 'when the API is available' do
render_views
2018-10-22 15:00:52 +02: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)
.to receive(:results)
.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
post :show, params: params, format: 'js'
end
2018-10-22 15:00:52 +02:00
context 'when coordinates are empty' do
let(:value) { '[]' }
it {
expect(assigns(:error)).to eq(nil)
expect(champ.reload.value).to eq(nil)
expect(champ.reload.geo_areas).to eq([])
expect(response.body).to include("DS.fire('carte:update'")
}
end
context 'when coordinates are informed' do
let(:value) { [[{ "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 }]].to_json }
it { expect(response.body).not_to be_nil }
it { expect(response.body).to include('MultiPolygon') }
it { expect(response.body).to include('[2.38715792094576,48.8723062632126]') }
end
2018-10-22 15:00:52 +02: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
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)
.to receive(:results)
2020-03-05 13:50:38 +01:00
.and_raise(ApiCarto::API::ResourceNotFound)
post :show, params: params, format: 'js'
end
let(:value) { [[{ "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 }]].to_json }
2018-11-21 12:42:13 +01:00
it { expect(response.status).to eq 503 }
it { 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