FIX: Apply watched words to user fields

Currently we don’t apply watched words to custom user fields nor user
profile fields.
This led to users being able to use blocked words in their bio, location
or some custom user fields.

This patch addresses this issue by adding some validations so it’s not
possible anymore to save the User model or the UserProfile model if they
contain blocked words.
This commit is contained in:
Loïc Guitaut
2022-04-28 11:00:02 +02:00
committed by Loïc Guitaut
parent 26c5002144
commit ba148e082d
7 changed files with 78 additions and 14 deletions

View File

@ -1,6 +1,45 @@
# frozen_string_literal: true
describe UserProfile do
RSpec.describe UserProfile do
describe "Validations" do
subject(:profile) { user.user_profile }
fab!(:user) { Fabricate(:user) }
fab!(:watched_word) { Fabricate(:watched_word, word: "bad") }
describe "#location" do
context "when it contains watched words" do
before { profile.location = "bad location" }
it "is not valid" do
profile.valid?
expect(profile.errors[:base].size).to eq(1)
expect(profile.errors.messages[:base]).to include(/you can't post the word/)
end
end
context "when it does not contain watched words" do
it { is_expected.to be_valid }
end
end
describe "#bio_raw" do
context "when it contains watched words" do
before { profile.bio_raw = "bad bio" }
it "is not valid" do
profile.valid?
expect(profile.errors[:base].size).to eq(1)
expect(profile.errors.messages[:base]).to include(/you can't post the word/)
end
end
context "when it does not contain watched words" do
it { is_expected.to be_valid }
end
end
end
it 'is created automatically when a user is created' do
user = Fabricate(:evil_trout)
expect(user.user_profile).to be_present
@ -224,7 +263,5 @@ describe UserProfile do
expect(user.card_background_upload).to eq(nil)
end
end
end
end