From d2e41bf8d73750e189517cb85b9c6607d6ca4e3c Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 5 Aug 2022 16:16:28 +0100 Subject: [PATCH] Strip whitespaces from names before saving organisation, scheme or a user (#816) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Stip whitespaces from names before saving organisation, scheme or a user * Abstract strip whitespaces method and use it for locations too * refactor * lint * Use a gem 🙃 * sort gems alphabetically --- Gemfile | 1 + Gemfile.lock | 3 +++ app/models/location.rb | 2 ++ app/models/organisation.rb | 2 ++ app/models/scheme.rb | 2 ++ app/models/user.rb | 2 ++ spec/requests/locations_controller_spec.rb | 2 +- spec/requests/organisations_controller_spec.rb | 4 ++-- spec/requests/schemes_controller_spec.rb | 2 +- spec/requests/users_controller_spec.rb | 12 ++++++++++-- 10 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 168ace7af..6586ed800 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby "3.1.2" +gem "auto_strip_attributes" # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem "rails", "~> 7.0.2" # Use postgresql as the database for Active Record diff --git a/Gemfile.lock b/Gemfile.lock index 4bcfa6266..2239fcdf5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,6 +81,8 @@ GEM addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) ast (2.4.2) + auto_strip_attributes (2.6.0) + activerecord (>= 4.0) aws-eventstream (1.2.0) aws-partitions (1.609.0) aws-sdk-core (3.131.3) @@ -428,6 +430,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + auto_strip_attributes aws-sdk-s3 bootsnap (>= 1.4.4) bundler-audit diff --git a/app/models/location.rb b/app/models/location.rb index fd2dde9fe..c89ff0236 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -7,6 +7,8 @@ class Location < ApplicationRecord before_save :lookup_postcode!, if: :postcode_changed? + auto_strip_attributes :name + attr_accessor :add_another_location scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 609aa3cbf..625c0bd05 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -12,6 +12,8 @@ class Organisation < ApplicationRecord has_paper_trail + auto_strip_attributes :name + PROVIDER_TYPE = { LA: 1, PRP: 2, diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 727388fae..45f09d4a5 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -19,6 +19,8 @@ class Scheme < ApplicationRecord validate :validate_confirmed + auto_strip_attributes :service_name + SENSITIVE = { No: 0, Yes: 1, diff --git a/app/models/user.rb b/app/models/user.rb index ee1a2fb5f..f9ed4a0e9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,6 +27,8 @@ class User < ApplicationRecord has_one_time_password(encrypted: true) + auto_strip_attributes :name + ROLES = { data_provider: 1, data_coordinator: 2, diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index ed2132809..080260128 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -269,7 +269,7 @@ RSpec.describe LocationsController, type: :request do context "when signed in as a support user" do let(:user) { FactoryBot.create(:user, :support) } let!(:scheme) { FactoryBot.create(:scheme) } - let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", add_another_location: "No", postcode: "ZZ1 1ZZ", mobility_type: "N" } } } + let(:params) { { location: { name: " Test", units: "5", type_of_unit: "Bungalow", add_another_location: "No", postcode: "ZZ1 1ZZ", mobility_type: "N" } } } before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 0498209f9..c467cd585 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -962,7 +962,7 @@ RSpec.describe OrganisationsController, type: :request do end describe "#create" do - let(:name) { "Unique new org name" } + let(:name) { " Unique new org name" } let(:address_line1) { "12 Random Street" } let(:address_line2) { "Manchester" } let(:postcode) { "MD1 5TR" } @@ -993,7 +993,7 @@ RSpec.describe OrganisationsController, type: :request do it "sets the organisation attributes correctly" do request organisation = Organisation.find_by(housing_registration_no:) - expect(organisation.name).to eq(name) + expect(organisation.name).to eq("Unique new org name") expect(organisation.address_line1).to eq(address_line1) expect(organisation.address_line2).to eq(address_line2) expect(organisation.postcode).to eq(postcode) diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index fe133253f..a5c0b6e4f 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -346,7 +346,7 @@ RSpec.describe SchemesController, type: :request do context "when signed in as a data coordinator" do let(:user) { FactoryBot.create(:user, :data_coordinator) } let(:params) do - { scheme: { service_name: "testy", + { scheme: { service_name: " testy ", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index a99d1e6c3..c5303776a 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -841,7 +841,7 @@ RSpec.describe UsersController, type: :request do let(:params) do { "user": { - name: "new user", + name: "new user ", email: "new_user@example.com", role: "data_coordinator", }, @@ -850,7 +850,7 @@ RSpec.describe UsersController, type: :request do let(:personalisation) do { - name: params[:user][:name], + name: "new user", email: params[:user][:email], organisation: user.organisation.name, link: include("/account/confirmation?confirmation_token="), @@ -871,6 +871,14 @@ RSpec.describe UsersController, type: :request do request end + it "creates a new scheme for user organisation with valid params" do + request + + expect(User.last.name).to eq("new user") + expect(User.last.email).to eq("new_user@example.com") + expect(User.last.role).to eq("data_coordinator") + end + it "redirects back to organisation users page" do request expect(response).to redirect_to("/organisations/#{user.organisation.id}/users")