2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-28 05:00:02 -04:00
|
|
|
RSpec.describe UserProfile do
|
|
|
|
describe "Validations" do
|
|
|
|
subject(:profile) { user.user_profile }
|
|
|
|
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2022-04-28 05:00:02 -04:00
|
|
|
fab!(:watched_word) { Fabricate(:watched_word, word: "bad") }
|
|
|
|
|
|
|
|
describe "#location" do
|
|
|
|
context "when it contains watched words" do
|
2022-06-14 12:27:01 -04:00
|
|
|
before { profile.location = location }
|
2022-04-28 05:00:02 -04:00
|
|
|
|
2022-06-14 12:27:01 -04:00
|
|
|
context "when watched words are of type 'Block'" do
|
|
|
|
let(: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 watched words are of type 'Censor'" do
|
|
|
|
let!(:censored_word) do
|
|
|
|
Fabricate(:watched_word, word: "censored", action: WatchedWord.actions[:censor])
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2022-06-14 12:27:01 -04:00
|
|
|
let(:location) { "censored location" }
|
|
|
|
|
|
|
|
it "censors the words upon saving" do
|
|
|
|
expect { profile.save! }.to change { profile.location }.to eq "■■■■■■■■ location"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when watched words are of type 'Replace'" do
|
|
|
|
let(:location) { "word to replace" }
|
|
|
|
let!(:replace_word) do
|
|
|
|
Fabricate(
|
|
|
|
:watched_word,
|
|
|
|
word: "to replace",
|
|
|
|
replacement: "replaced",
|
|
|
|
action: WatchedWord.actions[:replace],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "replaces the words upon saving" do
|
|
|
|
expect { profile.save! }.to change { profile.location }.to eq "word replaced"
|
|
|
|
end
|
2022-04-28 05:00:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-20 22:07:06 -04:00
|
|
|
context "when it is > 3000 characters" do
|
|
|
|
before { profile.location = "a" * 3500 }
|
|
|
|
|
|
|
|
it "is not valid" do
|
|
|
|
expect(profile.valid?).to eq(false)
|
|
|
|
expect(profile.errors.full_messages).to include(
|
|
|
|
/Location is too long \(maximum is 3000 characters\)/,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-28 05:00:02 -04:00
|
|
|
context "when it does not contain watched words" do
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
2022-07-26 11:15:42 -04:00
|
|
|
|
|
|
|
it "is not cooked" do
|
|
|
|
profile.location = "https://discourse.org"
|
|
|
|
expect { profile.save! }.not_to change { profile.location }
|
|
|
|
end
|
2022-04-28 05:00:02 -04:00
|
|
|
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
|
|
|
|
|
2022-09-20 22:07:06 -04:00
|
|
|
context "when it is > 3000 characters" do
|
|
|
|
before { profile.bio_raw = "a" * 3500 }
|
|
|
|
|
|
|
|
it "is not valid" do
|
|
|
|
expect(profile.valid?).to eq(false)
|
|
|
|
expect(profile.errors.full_messages).to include(
|
|
|
|
/About Me is too long \(maximum is 3000 characters\)/,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-28 05:00:02 -04:00
|
|
|
context "when it does not contain watched words" do
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-10 01:19:08 -04:00
|
|
|
it "is created automatically when a user is created" do
|
2014-05-27 13:54:04 -04:00
|
|
|
user = Fabricate(:evil_trout)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_profile).to be_present
|
2014-05-27 13:54:04 -04:00
|
|
|
end
|
2014-06-10 01:19:08 -04:00
|
|
|
|
2014-08-05 02:37:56 -04:00
|
|
|
describe "rebaking" do
|
|
|
|
it "correctly rebakes bio" do
|
|
|
|
user_profile = Fabricate(:evil_trout).user_profile
|
|
|
|
user_profile.update_columns(bio_raw: "test", bio_cooked: "broken", bio_cooked_version: nil)
|
|
|
|
|
|
|
|
problems = UserProfile.rebake_old(10)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(problems.length).to eq(0)
|
2014-08-05 02:37:56 -04:00
|
|
|
|
|
|
|
user_profile.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user_profile.bio_cooked).to eq("<p>test</p>")
|
|
|
|
expect(user_profile.bio_cooked_version).to eq(UserProfile::BAKED_VERSION)
|
2014-08-05 02:37:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-10 01:19:08 -04:00
|
|
|
describe "new" do
|
2019-03-10 23:12:11 -04:00
|
|
|
let(:user_profile) { UserProfile.new(bio_raw: "test") }
|
2014-06-10 01:19:08 -04:00
|
|
|
|
|
|
|
it "is not valid without user" do
|
2014-10-31 13:44:26 -04:00
|
|
|
expect(user_profile.valid?).to be false
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is is valid with user" do
|
|
|
|
user_profile.user = Fabricate.build(:user)
|
2014-10-31 13:44:26 -04:00
|
|
|
expect(user_profile.valid?).to be true
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
|
2014-09-08 15:17:31 -04:00
|
|
|
it "doesn't support really long bios" do
|
|
|
|
user_profile = Fabricate.build(:user_profile_long)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user_profile).not_to be_valid
|
2014-09-08 15:17:31 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with website validation" do
|
2019-05-24 00:58:36 -04:00
|
|
|
let(:user_profile) { Fabricate.build(:user_profile, user: Fabricate(:user)) }
|
2016-02-05 15:49:48 -05:00
|
|
|
|
2017-12-20 23:27:17 -05:00
|
|
|
it "should not allow invalid URLs" do
|
|
|
|
user_profile.website = "http://https://google.com"
|
|
|
|
expect(user_profile).to_not be_valid
|
2016-12-26 10:24:54 -05:00
|
|
|
end
|
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
it "validates website domain if allowed_user_website_domains setting is present" do
|
|
|
|
SiteSetting.allowed_user_website_domains = "discourse.org"
|
2017-12-20 23:27:17 -05:00
|
|
|
|
|
|
|
user_profile.website = "https://google.com"
|
|
|
|
expect(user_profile).not_to be_valid
|
|
|
|
|
|
|
|
user_profile.website = "http://discourse.org"
|
|
|
|
expect(user_profile).to be_valid
|
2016-12-26 10:24:54 -05:00
|
|
|
end
|
2018-05-17 03:51:24 -04:00
|
|
|
|
|
|
|
it "doesn't blow up with an invalid URI" do
|
2020-07-26 20:23:54 -04:00
|
|
|
SiteSetting.allowed_user_website_domains = "discourse.org"
|
2018-05-17 03:51:24 -04:00
|
|
|
|
|
|
|
user_profile.website = "user - https://forum.example.com/user"
|
|
|
|
expect { user_profile.save! }.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
2022-09-20 22:07:06 -04:00
|
|
|
|
|
|
|
it "does not allow > 3000 characters" do
|
|
|
|
user_profile.website = "a" * 3500
|
|
|
|
expect(user_profile).to_not be_valid
|
|
|
|
end
|
2016-02-05 15:49:48 -05:00
|
|
|
end
|
|
|
|
|
2014-06-10 01:19:08 -04:00
|
|
|
describe "after save" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2014-06-10 01:19:08 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
user.user_profile.bio_raw = "my bio"
|
|
|
|
user.user_profile.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has cooked bio" do
|
|
|
|
expect(user.user_profile.bio_cooked).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has bio summary" do
|
|
|
|
expect(user.user_profile.bio_summary).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "changing bio" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2014-06-10 01:19:08 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
user.user_profile.bio_raw = "**turtle power!**"
|
|
|
|
user.user_profile.save
|
|
|
|
user.user_profile.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should markdown the raw_bio and put it in cooked_bio" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_profile.bio_cooked).to eq("<p><strong>turtle power!</strong></p>")
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-15 04:05:09 -04:00
|
|
|
describe "bio excerpt emojis" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:upload)
|
2017-08-15 04:05:09 -04:00
|
|
|
|
|
|
|
before do
|
2018-05-31 23:56:56 -04:00
|
|
|
CustomEmoji.create!(name: "test", upload: upload)
|
2017-08-15 04:05:09 -04:00
|
|
|
Emoji.clear_cache
|
|
|
|
|
2018-05-31 23:56:56 -04:00
|
|
|
user.user_profile.update!(bio_raw: "hello :test: :woman_scientist:t5: 🤔")
|
2017-08-15 04:05:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports emoji images" do
|
2022-02-09 06:18:59 -05:00
|
|
|
expect(user.user_profile.bio_excerpt(500, keep_emoji_images: true)).to eq(
|
|
|
|
"hello <img src=\"#{upload.url}?v=#{Emoji::EMOJI_VERSION}\" title=\":test:\" class=\"emoji emoji-custom\" alt=\":test:\" loading=\"lazy\" width=\"20\" height=\"20\"> <img src=\"/images/emoji/twitter/woman_scientist/5.png?v=#{Emoji::EMOJI_VERSION}\" title=\":woman_scientist:t5:\" class=\"emoji\" alt=\":woman_scientist:t5:\" loading=\"lazy\" width=\"20\" height=\"20\"> <img src=\"/images/emoji/twitter/thinking.png?v=#{Emoji::EMOJI_VERSION}\" title=\":thinking:\" class=\"emoji\" alt=\":thinking:\" loading=\"lazy\" width=\"20\" height=\"20\">",
|
|
|
|
)
|
2017-08-15 04:05:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-10 01:19:08 -04:00
|
|
|
describe "bio link stripping" do
|
|
|
|
it "returns an empty string with no bio" do
|
|
|
|
expect(Fabricate.build(:user_profile).bio_excerpt).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a user that has a link in their bio" do
|
2014-07-24 22:15:43 -04:00
|
|
|
let(:user_profile) { Fabricate.build(:user_profile, bio_raw: "I love http://discourse.org") }
|
2014-06-10 01:19:08 -04:00
|
|
|
let(:user) do
|
|
|
|
user = Fabricate.build(:user, user_profile: user_profile)
|
|
|
|
user_profile.user = user
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:created_user) do
|
2014-06-10 01:19:08 -04:00
|
|
|
user = Fabricate(:user)
|
2014-07-24 22:15:43 -04:00
|
|
|
user.user_profile.bio_raw = "I love http://discourse.org"
|
2014-06-10 01:19:08 -04:00
|
|
|
user.user_profile.save!
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the link as nofollow if the user is not new" do
|
|
|
|
user.user_profile.send(:cook)
|
2020-09-10 11:59:51 -04:00
|
|
|
expect(user_profile.bio_excerpt).to match_html(
|
|
|
|
"I love <a href='http://discourse.org' rel='noopener nofollow ugc'>http://discourse.org</a>",
|
|
|
|
)
|
|
|
|
expect(user_profile.bio_processed).to match_html(
|
|
|
|
"<p>I love <a href=\"http://discourse.org\" rel=\"noopener nofollow ugc\">http://discourse.org</a></p>",
|
|
|
|
)
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the link if the user is new" do
|
2014-09-05 01:20:39 -04:00
|
|
|
user.trust_level = TrustLevel[0]
|
2014-06-10 01:19:08 -04:00
|
|
|
user_profile.send(:cook)
|
2014-07-24 22:15:43 -04:00
|
|
|
expect(user_profile.bio_excerpt).to match_html("I love http://discourse.org")
|
|
|
|
expect(user_profile.bio_processed).to eq("<p>I love http://discourse.org</p>")
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
|
2014-11-25 16:14:21 -05:00
|
|
|
it "removes the link if the user is suspended" do
|
|
|
|
user.suspended_till = 1.month.from_now
|
|
|
|
user_profile.send(:cook)
|
|
|
|
expect(user_profile.bio_excerpt).to match_html("I love http://discourse.org")
|
|
|
|
expect(user_profile.bio_processed).to eq("<p>I love http://discourse.org</p>")
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when tl3_links_no_follow is false" do
|
2017-07-07 02:09:14 -04:00
|
|
|
before { SiteSetting.tl3_links_no_follow = false }
|
2014-07-14 13:34:21 -04:00
|
|
|
|
|
|
|
it "includes the link without nofollow if the user is trust level 3 or higher" do
|
2014-09-05 01:20:39 -04:00
|
|
|
user.trust_level = TrustLevel[3]
|
2014-07-14 13:34:21 -04:00
|
|
|
user_profile.send(:cook)
|
2014-07-24 22:15:43 -04:00
|
|
|
expect(user_profile.bio_excerpt).to match_html(
|
|
|
|
"I love <a href='http://discourse.org'>http://discourse.org</a>",
|
|
|
|
)
|
|
|
|
expect(user_profile.bio_processed).to match_html(
|
|
|
|
"<p>I love <a href=\"http://discourse.org\">http://discourse.org</a></p>",
|
|
|
|
)
|
2014-07-14 13:34:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes nofollow from links in bio when trust level is increased" do
|
2014-09-05 01:20:39 -04:00
|
|
|
created_user.change_trust_level!(TrustLevel[3])
|
2014-07-24 22:15:43 -04:00
|
|
|
expect(created_user.user_profile.bio_excerpt).to match_html(
|
|
|
|
"I love <a href='http://discourse.org'>http://discourse.org</a>",
|
|
|
|
)
|
|
|
|
expect(created_user.user_profile.bio_processed).to match_html(
|
|
|
|
"<p>I love <a href=\"http://discourse.org\">http://discourse.org</a></p>",
|
|
|
|
)
|
2014-07-14 13:34:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds nofollow to links in bio when trust level is decreased" do
|
2014-09-05 01:20:39 -04:00
|
|
|
created_user.trust_level = TrustLevel[3]
|
2014-07-14 13:34:21 -04:00
|
|
|
created_user.save
|
2014-09-27 19:38:00 -04:00
|
|
|
created_user.reload
|
2014-09-05 01:20:39 -04:00
|
|
|
created_user.change_trust_level!(TrustLevel[2])
|
2020-09-10 11:59:51 -04:00
|
|
|
expect(created_user.user_profile.bio_excerpt).to match_html(
|
|
|
|
"I love <a href='http://discourse.org' rel='noopener nofollow ugc'>http://discourse.org</a>",
|
|
|
|
)
|
|
|
|
expect(created_user.user_profile.bio_processed).to match_html(
|
|
|
|
"<p>I love <a href=\"http://discourse.org\" rel=\"noopener nofollow ugc\">http://discourse.org</a></p>",
|
|
|
|
)
|
2014-07-14 13:34:21 -04:00
|
|
|
end
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when tl3_links_no_follow is true" do
|
2017-07-07 02:09:14 -04:00
|
|
|
before { SiteSetting.tl3_links_no_follow = true }
|
2014-06-10 01:19:08 -04:00
|
|
|
|
2014-07-14 13:34:21 -04:00
|
|
|
it "includes the link with nofollow if the user is trust level 3 or higher" do
|
2014-09-05 01:20:39 -04:00
|
|
|
user.trust_level = TrustLevel[3]
|
2014-07-14 13:34:21 -04:00
|
|
|
user_profile.send(:cook)
|
2020-09-10 11:59:51 -04:00
|
|
|
expect(user_profile.bio_excerpt).to match_html(
|
|
|
|
"I love <a href='http://discourse.org' rel='noopener nofollow ugc'>http://discourse.org</a>",
|
|
|
|
)
|
|
|
|
expect(user_profile.bio_processed).to match_html(
|
|
|
|
"<p>I love <a href=\"http://discourse.org\" rel=\"noopener nofollow ugc\">http://discourse.org</a></p>",
|
|
|
|
)
|
2014-07-14 13:34:21 -04:00
|
|
|
end
|
2014-06-10 01:19:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-07 04:03:26 -04:00
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe ".import_url_for_user" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2018-05-07 04:03:26 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:any, "thisfakesomething.something.com").to_return(
|
|
|
|
body: "abc",
|
|
|
|
status: 404,
|
|
|
|
headers: {
|
|
|
|
"Content-Length" => 3,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when profile_background_url returns an invalid status code" do
|
|
|
|
it "should not do anything" do
|
|
|
|
url = "http://thisfakesomething.something.com/"
|
|
|
|
|
|
|
|
UserProfile.import_url_for_user(url, user, is_card_background: false)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
2019-04-28 23:58:52 -04:00
|
|
|
expect(user.profile_background_upload).to eq(nil)
|
2018-05-07 04:03:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when card_background_url returns an invalid status code" do
|
|
|
|
it "should not do anything" do
|
|
|
|
url = "http://thisfakesomething.something.com/"
|
|
|
|
|
|
|
|
UserProfile.import_url_for_user(url, user, is_card_background: true)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
2019-04-28 23:58:52 -04:00
|
|
|
expect(user.card_background_upload).to eq(nil)
|
2018-05-07 04:03:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-27 13:54:04 -04:00
|
|
|
end
|