FIX: Discobot welcome post delay should start counting when a user logs in.

This commit is contained in:
Guo Xiang Tan 2017-06-01 17:20:16 +09:00
parent 2ee144c27f
commit edbb876d1b
3 changed files with 84 additions and 19 deletions

View File

@ -114,6 +114,18 @@ after_initialize do
end end
self.add_model_callback(User, :after_commit, on: :create) do self.add_model_callback(User, :after_commit, on: :create) do
if SiteSetting.discourse_narrative_bot_welcome_post_delay == 0
self.enqueue_bot_welcome_post
end
end
self.on(:user_first_logged_in) do |user|
if SiteSetting.discourse_narrative_bot_welcome_post_delay > 0
user.enqueue_bot_welcome_post
end
end
self.add_to_class(:user, :enqueue_bot_welcome_post) do
return if SiteSetting.disable_discourse_narrative_bot_welcome_post return if SiteSetting.disable_discourse_narrative_bot_welcome_post
delay = SiteSetting.discourse_narrative_bot_welcome_post_delay delay = SiteSetting.discourse_narrative_bot_welcome_post_delay

View File

@ -0,0 +1,67 @@
require 'rails_helper'
describe "Discobot welcome post" do
let(:user) { Fabricate(:user) }
before do
SiteSetting.queue_jobs = true
SiteSetting.discourse_narrative_bot_enabled = true
end
after do
Jobs::NarrativeInit.jobs.clear
end
context 'when discourse_narrative_bot_welcome_post_delay is 0' do
it 'should not delay the welcome post' do
user
expect { sign_in(user) }.to_not change { Jobs::NarrativeInit.jobs.count }
end
end
context 'When discourse_narrative_bot_welcome_post_delay is greater than 0' do
before do
SiteSetting.discourse_narrative_bot_welcome_post_delay = 5
end
context 'when user logs in normally' do
it 'should delay the welcome post until user logs in' do
expect { sign_in(user) }.to change { Jobs::NarrativeInit.jobs.count }.by(1)
expect(Jobs::NarrativeInit.jobs.first["args"].first["user_id"]).to eq(user.id)
end
end
context 'when user redeems an invite' do
let(:invite) { Fabricate(:invite, invited_by: Fabricate(:admin), email: 'testing@gmail.com') }
it 'should delay the welcome post until the user logs in' do
invite
expect do
xhr :put, "/invites/show/#{invite.invite_key}",
username: 'somename',
name: 'testing',
password: 'asodaasdaosdhq'
end.to change { User.count }.by(1)
expect(Jobs::NarrativeInit.jobs.first["args"].first["user_id"]).to eq(User.last.id)
end
end
context 'when user redeems a disposable invite' do
it 'should delay the welcome post until the user logs in' do
token = Invite.generate_disposable_tokens(user).first
expect do
xhr :get, "/invites/redeem/#{token}",
email: 'testing@gmail.com',
username: 'somename',
name: 'testing',
password: 'asodaasdaosdhq'
end.to change { User.count }.by(1)
expect(Jobs::NarrativeInit.jobs.first["args"].first["user_id"]).to eq(User.last.id)
end
end
end
end

View File

@ -30,7 +30,7 @@ describe User do
end end
end end
describe 'enabled' do context 'enabled' do
before do before do
SiteSetting.disable_discourse_narrative_bot_welcome_post = false SiteSetting.disable_discourse_narrative_bot_welcome_post = false
end end
@ -58,30 +58,16 @@ describe User do
end end
end end
describe 'when welcome message is delayed' do describe 'when welcome message is configured to be delayed' do
before do before do
SiteSetting.discourse_narrative_bot_welcome_post_delay = 100 SiteSetting.discourse_narrative_bot_welcome_post_delay = 100
SiteSetting.queue_jobs = true SiteSetting.queue_jobs = true
end end
it 'should delay the initialization of the new user track' do it 'should delay the welcome post until user logs in' do
Timecop.freeze do user
user
expect(Jobs::NarrativeInit.jobs.first['at']) expect(Jobs::NarrativeInit.jobs.count).to eq(0)
.to be_within(1.second).of(Time.zone.now.to_f + 100)
end
end
it 'should delay sending the welcome message' do
SiteSetting.discourse_narrative_bot_welcome_post_type = 'welcome_message'
Timecop.freeze do
user
expect(Jobs::SendDefaultWelcomeMessage.jobs.first['at'])
.to be_within(1.second).of(Time.zone.now.to_f + 100)
end
end end
end end
end end