From 13df866c3d94924a20cf2aafcd95ab755ce14788 Mon Sep 17 00:00:00 2001 From: Clarissa Borges Date: Thu, 1 Apr 2021 13:16:25 -0300 Subject: [PATCH] Add scheme to websites before Nonprofit validation --- app/models/nonprofit.rb | 19 +++++++++++- spec/models/nonprofit_spec.rb | 56 +++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/app/models/nonprofit.rb b/app/models/nonprofit.rb index 06c188b1..ce3bd0d5 100755 --- a/app/models/nonprofit.rb +++ b/app/models/nonprofit.rb @@ -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.") diff --git a/spec/models/nonprofit_spec.rb b/spec/models/nonprofit_spec.rb index 43d02709..20070545 100644 --- a/spec/models/nonprofit_spec.rb +++ b/spec/models/nonprofit_spec.rb @@ -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