From 7df5a33fe52d8ac4419d2e8ce2d0314d95192946 Mon Sep 17 00:00:00 2001 From: Richard Pattinson Date: Wed, 6 Sep 2023 16:38:04 +0100 Subject: [PATCH] Remove leading and trailing whitespace from filters --- app/lib/filters/base.rb | 2 +- spec/lib/filters/country_spec.rb | 32 ++++++++++++++++++++++++++++++ spec/lib/filters/email_spec.rb | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/app/lib/filters/base.rb b/app/lib/filters/base.rb index ea9c6c54b5..30456179df 100644 --- a/app/lib/filters/base.rb +++ b/app/lib/filters/base.rb @@ -6,7 +6,7 @@ class Base def initialize(scope:, params:) @scope = scope - @params = params + @params = params.transform_values(&:strip) end def apply diff --git a/spec/lib/filters/country_spec.rb b/spec/lib/filters/country_spec.rb index 4316ed35f8..77079c6595 100644 --- a/spec/lib/filters/country_spec.rb +++ b/spec/lib/filters/country_spec.rb @@ -35,4 +35,36 @@ expect(subject).to eq(scope) end end + + context "with trailing whitespace" do + let(:params) { { location: "country:US " } } + let(:scope) { ApplicationForm.all } + + let!(:included) do + create( + :application_form, + region: create(:region, country: create(:country, code: "US")), + ) + end + + it "returns a filtered scope" do + expect(subject).to contain_exactly(included) + end + end + + context "with leading whitespace" do + let(:params) { { location: " country:US" } } + let(:scope) { ApplicationForm.all } + + let!(:included) do + create( + :application_form, + region: create(:region, country: create(:country, code: "US")), + ) + end + + it "returns a filtered scope" do + expect(subject).to contain_exactly(included) + end + end end diff --git a/spec/lib/filters/email_spec.rb b/spec/lib/filters/email_spec.rb index b55deaa503..0e655f0c91 100644 --- a/spec/lib/filters/email_spec.rb +++ b/spec/lib/filters/email_spec.rb @@ -31,6 +31,40 @@ it { is_expected.to contain_exactly(included) } end + + context "with trailing whitespace" do + let(:params) { { email: "Jane.Smith@example.com " } } + let(:scope) { ApplicationForm.all } + let!(:included) { create(:application_form, teacher:) } + + before do + create( + :application_form, + teacher: create(:teacher, email: "jane.jones@example.com"), + ) + end + + it "returns a filtered scope" do + expect(subject).to contain_exactly(included) + end + end + + context "with leading whitespace" do + let(:params) { { email: " Jane.Smith@example.com" } } + let(:scope) { ApplicationForm.all } + let!(:included) { create(:application_form, teacher:) } + + before do + create( + :application_form, + teacher: create(:teacher, email: "jane.jones@example.com"), + ) + end + + it "returns a filtered scope" do + expect(subject).to contain_exactly(included) + end + end end context "the params don't include email" do