Add scheme to websites before Nonprofit validation
This commit is contained in:
parent
58777ed7bb
commit
13df866c3d
2 changed files with 71 additions and 4 deletions
|
@ -101,7 +101,7 @@ class Nonprofit < ApplicationRecord
|
|||
validates_format_of :email, with: Email::Regex, allow_nil: true
|
||||
validates_presence_of :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}
|
||||
validate :user_is_valid, on: :create, unless: -> {register_np_only}
|
||||
|
@ -144,6 +144,7 @@ class Nonprofit < ApplicationRecord
|
|||
set_slugs
|
||||
set_user
|
||||
add_billing_subscription
|
||||
add_scheme_to_website
|
||||
self
|
||||
end
|
||||
|
||||
|
@ -276,6 +277,22 @@ private
|
|||
b_sub = build_billing_subscription(billing_plan: billing_plan, status: 'active')
|
||||
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
|
||||
if user && user.roles.nonprofit_admins.any?
|
||||
errors.add(:user_id, "cannot already be an admin for a nonprofit.")
|
||||
|
|
|
@ -143,9 +143,59 @@ RSpec.describe Nonprofit, type: :model do
|
|||
expect(nonprofit_with_bad_email_and_website.errors['email'].first).to match /.*invalid.*/
|
||||
end
|
||||
|
||||
it 'marks website as having errors if they do' do
|
||||
expect(nonprofit_with_bad_email_and_website.errors['website'].first)
|
||||
.to match('is not a valid URL')
|
||||
describe 'website validation' do
|
||||
it 'marks as having errors if it does not have a public suffix' do
|
||||
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
|
||||
|
||||
it 'marks an nonprofit as invalid when no slug could be created ' do
|
||||
|
|
Loading…
Reference in a new issue