2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe User do
|
2017-05-24 13:50:20 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:profile_page_url) { "#{Discourse.base_url}/users/#{user.username}" }
|
|
|
|
|
2018-09-22 00:37:35 +08:00
|
|
|
def i18n_post_args(extra = {})
|
|
|
|
{ base_uri: "" }.merge(extra)
|
|
|
|
end
|
|
|
|
|
|
|
|
def i18n_t(key, params = {})
|
|
|
|
I18n.t(key, i18n_post_args.merge(params))
|
|
|
|
end
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
before do
|
2021-10-19 19:42:29 +08:00
|
|
|
stub_image_size
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2017-05-24 13:50:20 +08:00
|
|
|
SiteSetting.discourse_narrative_bot_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when a user is created" do
|
|
|
|
it "should initiate the bot" do
|
2017-06-12 15:41:39 +08:00
|
|
|
NotificationEmailer.expects(:process_notification).never
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
user
|
|
|
|
|
2018-09-22 00:37:35 +08:00
|
|
|
expected_raw =
|
|
|
|
i18n_t(
|
|
|
|
"discourse_narrative_bot.new_user_narrative.hello.message",
|
2017-05-24 13:50:20 +08:00
|
|
|
username: user.username,
|
|
|
|
title: SiteSetting.title,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(Post.last.raw).to include(expected_raw.chomp)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "welcome post" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when disabled" do
|
2017-05-24 13:50:20 +08:00
|
|
|
before { SiteSetting.disable_discourse_narrative_bot_welcome_post = true }
|
|
|
|
|
|
|
|
it "should not initiate the bot" do
|
|
|
|
expect { user }.to_not change { Post.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-28 00:00:29 +08:00
|
|
|
context "with title emoji disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.disable_discourse_narrative_bot_welcome_post = false
|
|
|
|
SiteSetting.max_emojis_in_title = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "initiates the bot" do
|
|
|
|
expect { user }.to change { Topic.count }.by(1)
|
|
|
|
|
2018-09-22 00:37:35 +08:00
|
|
|
expect(Topic.last.title).to eq(
|
2018-03-28 00:00:29 +08:00
|
|
|
i18n_t("discourse_narrative_bot.new_user_narrative.hello.title").gsub(
|
|
|
|
/:robot:/,
|
2023-01-07 04:42:16 +08:00
|
|
|
"",
|
2018-03-28 00:00:29 +08:00
|
|
|
).strip,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when enabled" do
|
2017-05-24 13:50:20 +08:00
|
|
|
before { SiteSetting.disable_discourse_narrative_bot_welcome_post = false }
|
|
|
|
|
|
|
|
it "initiate the bot" do
|
|
|
|
expect { user }.to change { Topic.count }.by(1)
|
|
|
|
|
2018-09-22 00:37:35 +08:00
|
|
|
expect(Topic.last.title).to eq(
|
2017-05-24 13:50:20 +08:00
|
|
|
i18n_t("discourse_narrative_bot.new_user_narrative.hello.title"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when send welcome message is selected" do
|
|
|
|
before { SiteSetting.discourse_narrative_bot_welcome_post_type = "welcome_message" }
|
|
|
|
|
|
|
|
it "should send the right welcome message" do
|
|
|
|
expect { user }.to change { Topic.count }.by(1)
|
|
|
|
|
2018-09-22 00:37:35 +08:00
|
|
|
expect(Topic.last.title).to eq(
|
2017-05-24 13:50:20 +08:00
|
|
|
i18n_t("system_messages.welcome_user.subject_template", site_name: SiteSetting.title),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-01 16:20:16 +08:00
|
|
|
describe "when welcome message is configured to be delayed" do
|
2017-05-24 13:50:20 +08:00
|
|
|
before { SiteSetting.discourse_narrative_bot_welcome_post_delay = 100 }
|
|
|
|
|
2017-06-01 16:20:16 +08:00
|
|
|
it "should delay the welcome post until user logs in" do
|
|
|
|
user
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2017-06-01 16:20:16 +08:00
|
|
|
expect(Jobs::NarrativeInit.jobs.count).to eq(0)
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is staged" do
|
|
|
|
let(:user) { Fabricate(:user, staged: true) }
|
|
|
|
|
|
|
|
it "should not initiate the bot" do
|
|
|
|
expect { user }.to_not change { Post.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-18 16:13:40 +08:00
|
|
|
context "when user skipped the new user tips" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "should not initiate the bot" do
|
|
|
|
SiteSetting.default_other_skip_new_user_tips = true
|
|
|
|
expect { user }.to_not change { Post.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should delete the existing PM" do
|
|
|
|
user.user_option.skip_new_user_tips = true
|
|
|
|
|
2020-09-23 14:55:39 +08:00
|
|
|
expect { user.user_option.save! }.to change { Topic.count }.by(-1).and not_change {
|
2022-07-19 22:03:03 +08:00
|
|
|
UserHistory.count
|
2020-10-02 08:32:45 +08:00
|
|
|
}.and change { user.unread_high_priority_notifications }.by(-1).and change {
|
|
|
|
user.notifications.count
|
|
|
|
}.by(-1)
|
2020-08-18 16:13:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
context "when user is anonymous?" do
|
|
|
|
before { SiteSetting.allow_anonymous_posting = true }
|
2019-05-29 13:05:37 +08:00
|
|
|
|
|
|
|
it "should initiate bot for real user only" do
|
|
|
|
user = Fabricate(:user, trust_level: 1)
|
2023-10-25 09:45:10 +08:00
|
|
|
Group.refresh_automatic_groups!
|
2019-05-29 13:05:37 +08:00
|
|
|
shadow = AnonymousShadowCreator.get(user)
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2019-05-29 13:05:37 +08:00
|
|
|
expect(TopicAllowedUser.where(user_id: shadow.id).count).to eq(0)
|
|
|
|
expect(TopicAllowedUser.where(user_id: user.id).count).to eq(1)
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user's username should be ignored" do
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
|
|
|
|
|
|
|
before { SiteSetting.discourse_narrative_bot_ignored_usernames = "discourse|test" }
|
|
|
|
|
|
|
|
%w[discourse test].each do |username|
|
|
|
|
it "should not initiate the bot" do
|
|
|
|
expect { user.update!(username: username) }.to_not change { Post.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when a user has been destroyed" do
|
|
|
|
it "should clean up plugin's store" do
|
|
|
|
DiscourseNarrativeBot::Store.set(user.id, "test")
|
|
|
|
|
|
|
|
user.destroy!
|
|
|
|
|
|
|
|
expect(DiscourseNarrativeBot::Store.get(user.id)).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2021-03-30 01:21:30 +08:00
|
|
|
|
|
|
|
describe "#manually_disabled_discobot?" do
|
|
|
|
it "returns true if the user manually disabled new user tips" do
|
|
|
|
user.user_option.skip_new_user_tips = true
|
|
|
|
|
|
|
|
expect(user.manually_disabled_discobot?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|