2017-05-24 13:50:20 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe User do
|
|
|
|
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
|
2019-03-12 04:58:35 +08:00
|
|
|
run_jobs_synchronously!
|
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
|
|
|
|
context 'disabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.disable_discourse_narrative_bot_welcome_post = true
|
|
|
|
end
|
|
|
|
|
|
|
|
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(i18n_t(
|
2018-03-28 00:00:29 +08:00
|
|
|
'discourse_narrative_bot.new_user_narrative.hello.title'
|
|
|
|
).gsub(/:robot:/, '').strip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-01 16:20:16 +08:00
|
|
|
context 'enabled' do
|
2017-05-24 13:50:20 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.disable_discourse_narrative_bot_welcome_post = false
|
|
|
|
end
|
|
|
|
|
|
|
|
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(i18n_t(
|
2017-05-24 13:50:20 +08:00
|
|
|
'discourse_narrative_bot.new_user_narrative.hello.title'
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when send welcome message is selected" do
|
|
|
|
before do
|
|
|
|
SiteSetting.discourse_narrative_bot_welcome_post_type = 'welcome_message'
|
|
|
|
end
|
|
|
|
|
|
|
|
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(i18n_t(
|
2017-05-24 13:50:20 +08:00
|
|
|
"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 do
|
|
|
|
SiteSetting.discourse_narrative_bot_welcome_post_delay = 100
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context 'when user is anonymous?' do
|
|
|
|
let(:anonymous_user) { Fabricate(:anonymous) }
|
|
|
|
|
|
|
|
it 'should not initiate the bot' do
|
|
|
|
SiteSetting.allow_anonymous_posting = true
|
|
|
|
|
|
|
|
expect { anonymous_user }.to_not change { Post.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user's username should be ignored" do
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.discourse_narrative_bot_ignored_usernames = 'discourse|test'
|
|
|
|
end
|
|
|
|
|
|
|
|
['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
|
|
|
|
end
|