2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
RSpec.describe TopicStatusUpdate, type: :model do
|
|
|
|
let(:topic_status_update) { Fabricate(:topic_status_update) }
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2015-12-08 20:43:23 +08:00
|
|
|
|
2017-04-03 09:06:15 +08:00
|
|
|
before do
|
|
|
|
Jobs::ToggleTopicClosed.jobs.clear
|
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
context "validations" do
|
|
|
|
describe '#status_type' do
|
|
|
|
it 'should ensure that only one active topic status update exists' do
|
|
|
|
topic_status_update.update!(topic: topic)
|
|
|
|
Fabricate(:topic_status_update, deleted_at: Time.zone.now, topic: topic)
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
expect { Fabricate(:topic_status_update, topic: topic) }
|
|
|
|
.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
describe '#execute_at' do
|
|
|
|
describe 'when #execute_at is greater than #created_at' do
|
|
|
|
it 'should be valid' do
|
|
|
|
topic_status_update = Fabricate.build(:topic_status_update,
|
|
|
|
execute_at: Time.zone.now + 1.hour,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
expect(topic_status_update).to be_valid
|
|
|
|
end
|
|
|
|
end
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
describe 'when #execute_at is smaller than #created_at' do
|
|
|
|
it 'should not be valid' do
|
|
|
|
topic_status_update = Fabricate.build(:topic_status_update,
|
|
|
|
execute_at: Time.zone.now - 1.hour,
|
|
|
|
created_at: Time.zone.now,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
expect(topic_status_update).to_not be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-04 09:47:12 +08:00
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
context 'callbacks' do
|
|
|
|
describe 'when #execute_at and #user_id are not changed' do
|
|
|
|
it 'should not schedule another to update topic' do
|
|
|
|
Jobs.expects(:enqueue_at).with(
|
|
|
|
topic_status_update.execute_at,
|
|
|
|
:toggle_topic_closed,
|
|
|
|
topic_status_update_id: topic_status_update.id,
|
|
|
|
state: true
|
|
|
|
).once
|
|
|
|
|
|
|
|
topic_status_update
|
|
|
|
|
|
|
|
Jobs.expects(:cancel_scheduled_job).never
|
|
|
|
|
|
|
|
topic_status_update.update!(topic: Fabricate(:topic))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when #execute_at value is changed' do
|
|
|
|
it 'reschedules the job' do
|
|
|
|
Timecop.freeze do
|
|
|
|
topic_status_update
|
|
|
|
|
|
|
|
Jobs.expects(:cancel_scheduled_job).with(
|
|
|
|
:toggle_topic_closed, topic_status_update_id: topic_status_update.id
|
|
|
|
)
|
|
|
|
|
|
|
|
Jobs.expects(:enqueue_at).with(
|
|
|
|
3.days.from_now, :toggle_topic_closed,
|
|
|
|
topic_status_update_id: topic_status_update.id,
|
|
|
|
state: true
|
|
|
|
)
|
|
|
|
|
|
|
|
topic_status_update.update!(execute_at: 3.days.from_now, created_at: Time.zone.now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when execute_at is smaller than the current time' do
|
|
|
|
it 'should enqueue the job immediately' do
|
|
|
|
Timecop.freeze do
|
|
|
|
topic_status_update
|
|
|
|
|
|
|
|
Jobs.expects(:enqueue_at).with(
|
|
|
|
Time.zone.now, :toggle_topic_closed,
|
|
|
|
topic_status_update_id: topic_status_update.id,
|
|
|
|
state: true
|
|
|
|
)
|
|
|
|
|
|
|
|
topic_status_update.update!(
|
|
|
|
execute_at: Time.zone.now - 1.hour,
|
|
|
|
created_at: Time.zone.now - 2.hour
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user is changed' do
|
|
|
|
it 'should update the job' do
|
|
|
|
Timecop.freeze do
|
|
|
|
topic_status_update
|
|
|
|
|
|
|
|
Jobs.expects(:cancel_scheduled_job).with(
|
|
|
|
:toggle_topic_closed, topic_status_update_id: topic_status_update.id
|
|
|
|
)
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
Jobs.expects(:enqueue_at).with(
|
|
|
|
topic_status_update.execute_at,
|
|
|
|
:toggle_topic_closed,
|
|
|
|
topic_status_update_id: topic_status_update.id,
|
|
|
|
state: true
|
|
|
|
)
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
topic_status_update.update!(user: admin)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-03 09:06:15 +08:00
|
|
|
|
|
|
|
describe 'when a open topic status update is created for an open topic' do
|
|
|
|
let(:topic) { Fabricate(:topic, closed: false) }
|
|
|
|
|
|
|
|
let(:topic_status_update) do
|
|
|
|
Fabricate(:topic_status_update,
|
|
|
|
status_type: described_class.types[:open],
|
|
|
|
topic: topic
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should close the topic' do
|
|
|
|
topic_status_update
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when topic has been deleted' do
|
|
|
|
it 'should not queue the job' do
|
|
|
|
topic.trash!
|
|
|
|
topic_status_update
|
|
|
|
|
|
|
|
expect(Jobs::ToggleTopicClosed.jobs).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when a close topic status update is created for a closed topic' do
|
|
|
|
let(:topic) { Fabricate(:topic, closed: true) }
|
|
|
|
|
|
|
|
let(:topic_status_update) do
|
|
|
|
Fabricate(:topic_status_update,
|
|
|
|
status_type: described_class.types[:close],
|
|
|
|
topic: topic
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should open the topic' do
|
|
|
|
topic_status_update
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when topic has been deleted' do
|
|
|
|
it 'should not queue the job' do
|
|
|
|
topic.trash!
|
|
|
|
topic_status_update
|
|
|
|
|
|
|
|
expect(Jobs::ToggleTopicClosed.jobs).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
describe '.ensure_consistency!' do
|
|
|
|
before do
|
|
|
|
SiteSetting.queue_jobs = true
|
|
|
|
Jobs::ToggleTopicClosed.jobs.clear
|
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
it 'should enqueue jobs that have been missed' do
|
|
|
|
close_topic_status_update = Fabricate(:topic_status_update,
|
|
|
|
execute_at: Time.zone.now - 1.hour,
|
|
|
|
created_at: Time.zone.now - 2.hour
|
|
|
|
)
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
open_topic_status_update = Fabricate(:topic_status_update,
|
|
|
|
status_type: described_class.types[:open],
|
|
|
|
execute_at: Time.zone.now - 1.hour,
|
|
|
|
created_at: Time.zone.now - 2.hour
|
|
|
|
)
|
|
|
|
|
|
|
|
Fabricate(:topic_status_update)
|
|
|
|
|
|
|
|
expect { described_class.ensure_consistency! }
|
|
|
|
.to change { Jobs::ToggleTopicClosed.jobs.count }.by(2)
|
|
|
|
|
|
|
|
job_args = Jobs::ToggleTopicClosed.jobs.first["args"].first
|
|
|
|
|
|
|
|
expect(job_args["topic_status_update_id"]).to eq(close_topic_status_update.id)
|
|
|
|
expect(job_args["state"]).to eq(true)
|
|
|
|
|
|
|
|
job_args = Jobs::ToggleTopicClosed.jobs.last["args"].first
|
|
|
|
|
|
|
|
expect(job_args["topic_status_update_id"]).to eq(open_topic_status_update.id)
|
|
|
|
expect(job_args["state"]).to eq(false)
|
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
end
|
2013-07-04 09:47:12 +08:00
|
|
|
end
|