Merge pull request #1399 from betagouv/fix-478

[Fix #478] /recherche can deal with very big int
This commit is contained in:
gregoirenovel 2018-02-07 13:04:01 +01:00 committed by GitHub
commit b1c273e254
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View file

@ -4,10 +4,9 @@ module NewGestionnaire
@search_terms = params[:q]
# exact id match?
if @search_terms.to_i != 0
@dossiers = current_gestionnaire.dossiers.where(id: @search_terms.to_i) +
current_gestionnaire.dossiers_from_avis.where(id: @search_terms.to_i)
@dossiers.uniq!
id = @search_terms.to_i
if id != 0 && id_compatible?(id) # Sometimes gestionnaire is searching dossiers with a big number (ex: SIRET), ActiveRecord can't deal with them and throws ActiveModel::RangeError. id_compatible? prevents this.
@dossiers = dossiers_by_id(id)
end
if @dossiers.nil?
@ -23,5 +22,22 @@ module NewGestionnaire
).results
end
end
private
def dossiers_by_id(id)
dossiers = current_gestionnaire.dossiers.where(id: id) +
current_gestionnaire.dossiers_from_avis.where(id: id)
dossiers.uniq
end
def id_compatible?(number)
begin
ActiveRecord::Type::Integer.new.serialize(number)
true
rescue ActiveModel::RangeError
false
end
end
end
end

View file

@ -36,6 +36,17 @@ describe NewGestionnaire::RechercheController, type: :controller do
expect(assigns(:dossiers).count).to eq(0)
end
end
context 'with an id out of range' do
let(:query) { 123456789876543234567 }
it { is_expected.to have_http_status(200) }
it 'does not return the dossier' do
subject
expect(assigns(:dossiers).count).to eq(0)
end
end
end
end
end