Browse Source

Strip whitespaces from names before saving organisation, scheme or a user (#816)

* 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
pull/819/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
d2e41bf8d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Gemfile
  2. 3
      Gemfile.lock
  3. 2
      app/models/location.rb
  4. 2
      app/models/organisation.rb
  5. 2
      app/models/scheme.rb
  6. 2
      app/models/user.rb
  7. 2
      spec/requests/locations_controller_spec.rb
  8. 4
      spec/requests/organisations_controller_spec.rb
  9. 2
      spec/requests/schemes_controller_spec.rb
  10. 12
      spec/requests/users_controller_spec.rb

1
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

3
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

2
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(' ')}%") }

2
app/models/organisation.rb

@ -12,6 +12,8 @@ class Organisation < ApplicationRecord
has_paper_trail
auto_strip_attributes :name
PROVIDER_TYPE = {
LA: 1,
PRP: 2,

2
app/models/scheme.rb

@ -19,6 +19,8 @@ class Scheme < ApplicationRecord
validate :validate_confirmed
auto_strip_attributes :service_name
SENSITIVE = {
No: 0,
Yes: 1,

2
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,

2
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)

4
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)

2
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",

12
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")

Loading…
Cancel
Save