Add scheme to websites before Nonprofit validation

This commit is contained in:
Clarissa Borges 2021-04-01 13:16:25 -03:00 committed by Eric Schultz
parent 58777ed7bb
commit 13df866c3d
2 changed files with 71 additions and 4 deletions

View file

@ -101,7 +101,7 @@ class Nonprofit < ApplicationRecord
validates_format_of :email, with: Email::Regex, allow_nil: true validates_format_of :email, with: Email::Regex, allow_nil: true
validates_presence_of :slug validates_presence_of :slug
validates_uniqueness_of :slug, scope: %i[city_slug state_code_slug] validates_uniqueness_of :slug, scope: %i[city_slug state_code_slug]
validates :website, url: { schemes: ['http', 'https'], public_suffix: true }, allow_nil: true validates :website, url: { schemes: ['http', 'https'], public_suffix: true, no_local: true }, allow_nil: true
validates_presence_of :user_id, on: :create, unless: -> {register_np_only} validates_presence_of :user_id, on: :create, unless: -> {register_np_only}
validate :user_is_valid, on: :create, unless: -> {register_np_only} validate :user_is_valid, on: :create, unless: -> {register_np_only}
@ -144,6 +144,7 @@ class Nonprofit < ApplicationRecord
set_slugs set_slugs
set_user set_user
add_billing_subscription add_billing_subscription
add_scheme_to_website
self self
end end
@ -276,6 +277,22 @@ private
b_sub = build_billing_subscription(billing_plan: billing_plan, status: 'active') b_sub = build_billing_subscription(billing_plan: billing_plan, status: 'active')
end end
def add_scheme_to_website
website = self.website
begin
uri = URI.parse(website)
host = uri && uri.host
scheme = uri && uri.scheme
valid_scheme = host && scheme
valid_website = website && website.include?('.')
self.website = 'http://' + website if scheme.blank? && valid_website
rescue URI::InvalidURIError => e
e
end
end
def user_registerable_as_admin def user_registerable_as_admin
if user && user.roles.nonprofit_admins.any? if user && user.roles.nonprofit_admins.any?
errors.add(:user_id, "cannot already be an admin for a nonprofit.") errors.add(:user_id, "cannot already be an admin for a nonprofit.")

View file

@ -143,9 +143,59 @@ RSpec.describe Nonprofit, type: :model do
expect(nonprofit_with_bad_email_and_website.errors['email'].first).to match /.*invalid.*/ expect(nonprofit_with_bad_email_and_website.errors['email'].first).to match /.*invalid.*/
end end
it 'marks website as having errors if they do' do describe 'website validation' do
expect(nonprofit_with_bad_email_and_website.errors['website'].first) it 'marks as having errors if it does not have a public suffix' do
.to match('is not a valid URL') expect(nonprofit_with_bad_email_and_website.errors['website'].first)
.to match('is not a valid URL')
end
it 'does not mark website as having errors if it does not have a scheme and adds scheme' do
nonprofit_with_bad_email_and_website.update(website: 'a_website.com')
expect(nonprofit_with_bad_email_and_website.errors['website'].first)
.to be_nil
expect(nonprofit_with_bad_email_and_website.website)
.to eq('http://a_website.com')
end
it 'marks as having errors if a non-accpted scheme is provided' do
nonprofit_with_bad_email_and_website.update(website: 'ftp://invalid.com')
expect(nonprofit_with_bad_email_and_website.errors['website'].first)
.to match('is not a valid URL')
expect(nonprofit_with_bad_email_and_website.website)
.to eq('ftp://invalid.com')
end
it 'marks as having errors if an array is provided' do
nonprofit_with_bad_email_and_website.update(website: [])
expect(nonprofit_with_bad_email_and_website.errors['website'].first)
.to match('is not a valid URL')
expect(nonprofit_with_bad_email_and_website.website)
.to eq("[]")
end
it 'marks as having errors if there is a space in the website string' do
nonprofit_with_bad_email_and_website.update(website: 'invalid .com')
expect(nonprofit_with_bad_email_and_website.errors['website'].first)
.to match('is not a valid URL')
expect(nonprofit_with_bad_email_and_website.website)
.to eq('invalid .com')
end
it 'marks as having errors if a number is provided' do
nonprofit_with_bad_email_and_website.update(website: 1234)
expect(nonprofit_with_bad_email_and_website.errors['website'].first)
.to match('is not a valid URL')
expect(nonprofit_with_bad_email_and_website.website)
.to eq("1234")
end
it 'marks as having errors if a hash is provided' do
nonprofit_with_bad_email_and_website.update(website: {})
expect(nonprofit_with_bad_email_and_website.errors['website'].first)
.to match('is not a valid URL')
expect(nonprofit_with_bad_email_and_website.website)
.to eq("{}")
end
end end
it 'marks an nonprofit as invalid when no slug could be created ' do it 'marks an nonprofit as invalid when no slug could be created ' do