2017-03-21 23:12:02 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe "Managing a topic's status update", type: :request do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'when a user is not logged in' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
expect do
|
2017-05-11 18:23:18 -04:00
|
|
|
post "/t/#{topic.id}/timer.json",
|
2017-03-21 23:12:02 -04:00
|
|
|
time: '24',
|
2017-05-11 18:23:18 -04:00
|
|
|
status_type: TopicTimer.types[1]
|
2017-03-21 23:12:02 -04:00
|
|
|
end.to raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when does not have permission' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
post "/t/#{topic.id}/timer.json",
|
2017-03-21 23:12:02 -04:00
|
|
|
time: '24',
|
2017-05-11 18:23:18 -04:00
|
|
|
status_type: TopicTimer.types[1]
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
expect(JSON.parse(response.body)["error_type"]).to eq('invalid_access')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in as an admin' do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to create a topic status update' do
|
|
|
|
time = 24
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
post "/t/#{topic.id}/timer.json",
|
2017-03-21 23:12:02 -04:00
|
|
|
time: 24,
|
2017-05-11 18:23:18 -04:00
|
|
|
status_type: TopicTimer.types[1]
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
topic_status_update = TopicTimer.last
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(topic)
|
|
|
|
|
|
|
|
expect(topic_status_update.execute_at)
|
|
|
|
.to be_within(1.second).of(24.hours.from_now)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(DateTime.parse(json['execute_at']))
|
|
|
|
.to be_within(1.seconds).of(DateTime.parse(topic_status_update.execute_at.to_s))
|
|
|
|
|
|
|
|
expect(json['duration']).to eq(topic_status_update.duration)
|
2017-03-31 03:02:36 -04:00
|
|
|
expect(json['closed']).to eq(topic.reload.closed)
|
2017-03-21 23:12:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to delete a topic status update' do
|
2017-07-24 02:56:08 -04:00
|
|
|
Fabricate(:topic_timer, topic: topic)
|
2017-03-21 23:12:02 -04:00
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
post "/t/#{topic.id}/timer.json",
|
2017-03-21 23:12:02 -04:00
|
|
|
time: nil,
|
2017-05-11 18:23:18 -04:00
|
|
|
status_type: TopicTimer.types[1]
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
expect(response).to be_success
|
2017-05-16 14:49:42 -04:00
|
|
|
expect(topic.reload.public_topic_timer).to eq(nil)
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json['execute_at']).to eq(nil)
|
|
|
|
expect(json['duration']).to eq(nil)
|
2017-03-31 03:02:36 -04:00
|
|
|
expect(json['closed']).to eq(topic.closed)
|
2017-03-21 23:12:02 -04:00
|
|
|
end
|
|
|
|
|
2017-04-03 05:28:41 -04:00
|
|
|
describe 'publishing topic to category in the future' do
|
|
|
|
it 'should be able to create the topic status update' do
|
2017-04-10 22:43:34 -04:00
|
|
|
SiteSetting.queue_jobs = true
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
post "/t/#{topic.id}/timer.json",
|
2017-04-03 05:28:41 -04:00
|
|
|
time: 24,
|
2017-05-11 18:23:18 -04:00
|
|
|
status_type: TopicTimer.types[3],
|
2017-04-03 05:28:41 -04:00
|
|
|
category_id: topic.category_id
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
topic_status_update = TopicTimer.last
|
2017-04-03 05:28:41 -04:00
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(topic)
|
|
|
|
|
|
|
|
expect(topic_status_update.execute_at)
|
|
|
|
.to be_within(1.second).of(24.hours.from_now)
|
|
|
|
|
|
|
|
expect(topic_status_update.status_type)
|
2017-05-11 18:23:18 -04:00
|
|
|
.to eq(TopicTimer.types[:publish_to_category])
|
2017-04-03 05:28:41 -04:00
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json['category_id']).to eq(topic.category_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
describe 'invalid status type' do
|
|
|
|
it 'should raise the right error' do
|
|
|
|
expect do
|
2017-05-11 18:23:18 -04:00
|
|
|
post "/t/#{topic.id}/timer.json",
|
2017-03-21 23:12:02 -04:00
|
|
|
time: 10,
|
|
|
|
status_type: 'something'
|
|
|
|
end.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|