* Validate website_url profile field is a url related to issue #14300 May need a data update script to (fixup? remove?) invalid profiles since not being able to save existing profiles will cause problems. * Check that URI is a valid url Require the scheme to be one of https or http, not something like mailto:// or telnet:// (nobody would do that, but don't try and link to it if they did). * Rubocop fixup for validation rule URI::regexp is obsolete and should not be used. Instead, use URI::DEFAULT_PARSER.make_regexp Prefer %w[] literals for arrays of words Prefer the new style validations `validates :column, format: value` to validates_format_of * Permit empty website_url fields The previous validation was rejecting nil, which was the default. This caused a lot of user factory calls to fail (since users didn't need website urls in the profiles during testing unless the test was about the website url link). * Use :url validation as suggested * Update validation error message The validate_url gem gives a more complete error message to the user. Update the spec to expect this. * Use url_field rather than text_field in profile form https://apidock.com/rails/v6.1.3.1/ActionView/Helpers/FormHelper/url_field * Add data update to either fixup or clear invalid website urls Checking blazer there are about 2600 profiles with "invalid" website urls, typically a hostname, sometimes a hostname + path component, which should be fixed up. Naively add https:// to the front of the url, check that a valid scheme and host are present on the resulting url, and save. If an invalid url is generated (specifically, if host or scheme are nil), just set the website url (with the invalid link) to an empty string. It's possible we'd want to notify users affected by this, that was not included in this pass. * Skip validations for intentionally invalid test cases
103 lines
3.2 KiB
Ruby
103 lines
3.2 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe Profile, type: :model do
|
|
let(:user) { create(:user) }
|
|
let(:profile) { user.profile }
|
|
|
|
describe "validations" do
|
|
subject { profile }
|
|
|
|
it { is_expected.to validate_uniqueness_of(:user_id) }
|
|
|
|
describe "conditionally validating summary" do
|
|
let(:invalid_summary) { "x" * ProfileValidator::MAX_SUMMARY_LENGTH.next }
|
|
|
|
it "is valid if users previously had long summaries and are grandfathered" do
|
|
profile.summary = invalid_summary
|
|
profile.save(validate: false)
|
|
profile.summary = "x" * 999
|
|
expect(profile).to be_valid
|
|
end
|
|
|
|
it "is not valid if the summary is too long and the user is not grandfathered" do
|
|
profile.summary = invalid_summary
|
|
expect(profile).not_to be_valid
|
|
expect(profile.errors_as_sentence).to eq "Summary is too long"
|
|
end
|
|
|
|
it "is valid if the summary is less than the limit" do
|
|
profile.summary = "Hello 👋"
|
|
expect(profile).to be_valid
|
|
end
|
|
end
|
|
|
|
describe "validating text areas" do
|
|
it "is valid if the text is short enough" do
|
|
profile.skills_languages = "Ruby"
|
|
expect(profile).to be_valid
|
|
end
|
|
|
|
it "is invalid if the text is too long" do
|
|
profile.skills_languages = "x" * ProfileValidator::MAX_TEXT_AREA_LENGTH.next
|
|
expect(profile).not_to be_valid
|
|
expect(profile.errors_as_sentence)
|
|
.to eq "Skills languages is too long (maximum is 200 characters)"
|
|
end
|
|
end
|
|
|
|
describe "validating text fields" do
|
|
it "is valid if the text is short enough" do
|
|
profile.location = "Somewhere"
|
|
expect(profile).to be_valid
|
|
end
|
|
|
|
it "is invalid if the text is too long" do
|
|
profile.location = "x" * ProfileValidator::MAX_TEXT_FIELD_LENGTH.next
|
|
expect(profile).not_to be_valid
|
|
expect(profile.errors_as_sentence).to eq "Location is too long (maximum is 100 characters)"
|
|
end
|
|
end
|
|
|
|
describe "validating website_url" do
|
|
it "is valid if blank" do
|
|
profile.website_url = nil
|
|
expect(profile).to be_valid
|
|
end
|
|
it "is valid with a complete url" do
|
|
profile.website_url = "https://dev.to"
|
|
expect(profile).to be_valid
|
|
end
|
|
|
|
it "is invalid with an incomplete url" do
|
|
profile.website_url = "dev.to"
|
|
expect(profile).not_to be_valid
|
|
expect(profile.errors_as_sentence).to eq "Website url is not a valid URL"
|
|
end
|
|
end
|
|
end
|
|
|
|
context "when accessing profile fields" do
|
|
before do
|
|
create(:profile_field, label: "Test 1")
|
|
create(:profile_field, label: "Test 2", input_type: :check_box)
|
|
described_class.refresh_attributes!
|
|
end
|
|
|
|
let(:profile) { described_class.new }
|
|
|
|
it "defines accessors for active profile fields", :aggregate_failures do
|
|
expect(profile).to respond_to(:test1)
|
|
expect(profile).to respond_to(:test2)
|
|
end
|
|
|
|
it "performs ActiveRecord typecasting for profile fields", :aggregate_failures do
|
|
expect do
|
|
profile.test2 = "true"
|
|
end.to change(profile, :test2).from(nil).to(true)
|
|
|
|
expect do
|
|
profile.test2 = "f"
|
|
end.to change(profile, :test2).to(false)
|
|
end
|
|
end
|
|
end
|