2019-05-12 23:04:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-22 05:09:40 -04:00
|
|
|
require 'rails_helper'
|
FEATURE: Publish WebHook event when solving/unsolving (#85)
* FEATURE: Publish WebHook event when solving/unsolving
This feature will publish a post edit webhook event whenever a solution
is accepted or unaccepted.
I went ahead and used the existing post-edit webhook because all the
post custom fields for the solved plugin are already included in the
post-edit serializer.
* Create Solved Event Webhook
This commit adds a solved event webhook that will only trigger when an
answer has been marked as accepted or unaccepted.
It uses 100 as the webhook ID. This way any new webhooks in core can
keep using lower numbers like 11, 12, 13, but plugins can use 101, 102,
etc.
* Removed functionality that was added to core
This [PR][1] to discourse core adds what what removed in this commit.
It is better to have this logic in core so that it is discoverable and
future webhooks won't end up accidentally using the same ID.
[1]: https://github.com/discourse/discourse/pull/9110
* UX: Add "solved" status filter in advanced search page.
And rename `in:solved` to `status:solved`.
* FEATURE: Publish WebHook event when solving/unsolving
This feature will publish a post edit webhook event whenever a solution
is accepted or unaccepted.
I went ahead and used the existing post-edit webhook because all the
post custom fields for the solved plugin are already included in the
post-edit serializer.
* Create Solved Event Webhook
This commit adds a solved event webhook that will only trigger when an
answer has been marked as accepted or unaccepted.
It uses 100 as the webhook ID. This way any new webhooks in core can
keep using lower numbers like 11, 12, 13, but plugins can use 101, 102,
etc.
* Removed functionality that was added to core
This [PR][1] to discourse core adds what what removed in this commit.
It is better to have this logic in core so that it is discoverable and
future webhooks won't end up accidentally using the same ID.
[1]: https://github.com/discourse/discourse/pull/9110
Co-authored-by: Vinoth Kannan <vinothkannan@vinkas.com>
2020-03-06 13:28:29 -05:00
|
|
|
require_relative '../fabricators/solved_hook_fabricator.rb'
|
2017-05-22 05:09:40 -04:00
|
|
|
|
|
|
|
RSpec.describe "Managing Posts solved status" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:user) { Fabricate(:trust_level_4) }
|
|
|
|
let(:p1) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.allow_solved_on_all_topics = true
|
|
|
|
end
|
|
|
|
|
2018-07-17 20:56:56 -04:00
|
|
|
describe 'auto bump' do
|
|
|
|
it 'does not automatically bump solved topics' do
|
2019-08-06 06:39:09 -04:00
|
|
|
category = Fabricate(:category_with_definition)
|
2018-07-17 20:56:56 -04:00
|
|
|
|
|
|
|
post = create_post(category: category)
|
|
|
|
post2 = create_post(category: category)
|
|
|
|
|
|
|
|
DiscourseSolved.accept_answer!(post, Discourse.system_user)
|
|
|
|
|
|
|
|
category.num_auto_bump_daily = 2
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
freeze_time 1.month.from_now
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(true)
|
|
|
|
|
|
|
|
freeze_time 13.hours.from_now
|
|
|
|
|
|
|
|
expect(category.auto_bump_topic!).to eq(false)
|
|
|
|
|
|
|
|
expect(post.topic.reload.posts_count).to eq(1)
|
|
|
|
expect(post2.topic.reload.posts_count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-22 05:09:40 -04:00
|
|
|
describe 'accepting a post as the answer' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
SiteSetting.solved_topics_auto_close_hours = 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can mark a post as the accepted answer correctly' do
|
2018-08-31 00:03:42 -04:00
|
|
|
freeze_time
|
|
|
|
|
2017-08-31 22:42:31 -04:00
|
|
|
post "/solution/accept.json", params: { id: p1.id }
|
2017-05-22 05:09:40 -04:00
|
|
|
|
2018-08-31 00:03:42 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-05-22 05:09:40 -04:00
|
|
|
expect(p1.reload.custom_fields["is_accepted_answer"]).to eq("true")
|
|
|
|
|
2018-08-31 00:03:42 -04:00
|
|
|
expect(topic.public_topic_timer.status_type)
|
2020-12-02 23:01:42 -05:00
|
|
|
.to eq(TopicTimer.types[:silent_close])
|
2018-08-31 00:03:42 -04:00
|
|
|
|
|
|
|
expect(topic.custom_fields[
|
|
|
|
DiscourseSolved::AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD
|
|
|
|
].to_i).to eq(topic.public_topic_timer.id)
|
2017-05-22 05:09:40 -04:00
|
|
|
|
|
|
|
expect(topic.public_topic_timer.execute_at)
|
2019-03-28 02:41:27 -04:00
|
|
|
.to eq_time(Time.zone.now + 2.hours)
|
2017-05-22 05:09:40 -04:00
|
|
|
|
|
|
|
expect(topic.public_topic_timer.based_on_last_post).to eq(true)
|
|
|
|
end
|
2018-08-31 00:03:42 -04:00
|
|
|
|
2020-10-30 12:31:27 -04:00
|
|
|
it 'sends notifications to correct users' do
|
|
|
|
SiteSetting.notify_on_staff_accept_solved = true
|
|
|
|
user = Fabricate(:user)
|
|
|
|
topic = Fabricate(:topic, user: user)
|
|
|
|
post = Fabricate(:post, post_number: 2, topic: topic)
|
|
|
|
|
|
|
|
op = topic.user
|
|
|
|
user = post.user
|
|
|
|
|
|
|
|
expect {
|
|
|
|
DiscourseSolved.accept_answer!(post, Discourse.system_user)
|
|
|
|
}.to \
|
|
|
|
change { user.notifications.count }.by(1) &
|
|
|
|
change { op.notifications.count }.by(1)
|
|
|
|
|
|
|
|
notification = user.notifications.last
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:custom])
|
|
|
|
expect(notification.topic_id).to eq(post.topic_id)
|
|
|
|
expect(notification.post_number).to eq(post.post_number)
|
|
|
|
|
|
|
|
notification = op.notifications.last
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:custom])
|
|
|
|
expect(notification.topic_id).to eq(post.topic_id)
|
|
|
|
expect(notification.post_number).to eq(post.post_number)
|
|
|
|
end
|
|
|
|
|
2017-06-12 16:29:53 -04:00
|
|
|
it 'does not set a timer when the topic is closed' do
|
2017-07-18 15:24:09 -04:00
|
|
|
topic.update!(closed: true)
|
2017-08-31 22:42:31 -04:00
|
|
|
post "/solution/accept.json", params: { id: p1.id }
|
2018-08-31 00:03:42 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2017-07-18 15:24:09 -04:00
|
|
|
p1.reload
|
|
|
|
topic.reload
|
2017-06-12 16:29:53 -04:00
|
|
|
|
2017-07-18 15:24:09 -04:00
|
|
|
expect(p1.custom_fields["is_accepted_answer"]).to eq("true")
|
|
|
|
expect(topic.public_topic_timer).to eq(nil)
|
|
|
|
expect(topic.closed).to eq(true)
|
2017-06-12 16:29:53 -04:00
|
|
|
end
|
2019-03-18 11:27:29 -04:00
|
|
|
|
|
|
|
it 'works with staff and trashed topics' do
|
|
|
|
topic.trash!(Discourse.system_user)
|
|
|
|
|
|
|
|
post "/solution/accept.json", params: { id: p1.id }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
post "/solution/accept.json", params: { id: p1.id }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
p1.reload
|
|
|
|
expect(p1.custom_fields["is_accepted_answer"]).to eq("true")
|
|
|
|
end
|
2020-02-23 03:16:31 -05:00
|
|
|
|
|
|
|
it 'does not allow you to accept a whisper' do
|
|
|
|
whisper = Fabricate(:post, topic: topic, post_type: Post.types[:whisper])
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
|
|
|
|
post "/solution/accept.json", params: { id: whisper.id }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
FEATURE: Publish WebHook event when solving/unsolving (#85)
* FEATURE: Publish WebHook event when solving/unsolving
This feature will publish a post edit webhook event whenever a solution
is accepted or unaccepted.
I went ahead and used the existing post-edit webhook because all the
post custom fields for the solved plugin are already included in the
post-edit serializer.
* Create Solved Event Webhook
This commit adds a solved event webhook that will only trigger when an
answer has been marked as accepted or unaccepted.
It uses 100 as the webhook ID. This way any new webhooks in core can
keep using lower numbers like 11, 12, 13, but plugins can use 101, 102,
etc.
* Removed functionality that was added to core
This [PR][1] to discourse core adds what what removed in this commit.
It is better to have this logic in core so that it is discoverable and
future webhooks won't end up accidentally using the same ID.
[1]: https://github.com/discourse/discourse/pull/9110
* UX: Add "solved" status filter in advanced search page.
And rename `in:solved` to `status:solved`.
* FEATURE: Publish WebHook event when solving/unsolving
This feature will publish a post edit webhook event whenever a solution
is accepted or unaccepted.
I went ahead and used the existing post-edit webhook because all the
post custom fields for the solved plugin are already included in the
post-edit serializer.
* Create Solved Event Webhook
This commit adds a solved event webhook that will only trigger when an
answer has been marked as accepted or unaccepted.
It uses 100 as the webhook ID. This way any new webhooks in core can
keep using lower numbers like 11, 12, 13, but plugins can use 101, 102,
etc.
* Removed functionality that was added to core
This [PR][1] to discourse core adds what what removed in this commit.
It is better to have this logic in core so that it is discoverable and
future webhooks won't end up accidentally using the same ID.
[1]: https://github.com/discourse/discourse/pull/9110
Co-authored-by: Vinoth Kannan <vinothkannan@vinkas.com>
2020-03-06 13:28:29 -05:00
|
|
|
|
|
|
|
it 'triggers a webhook' do
|
|
|
|
Fabricate(:solved_web_hook)
|
|
|
|
post "/solution/accept.json", params: { id: p1.id }
|
|
|
|
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first
|
|
|
|
|
|
|
|
expect(job_args["event_name"]).to eq("accepted_solution")
|
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(p1.id)
|
|
|
|
end
|
2017-05-22 05:09:40 -04:00
|
|
|
end
|
2018-08-31 00:03:42 -04:00
|
|
|
|
|
|
|
describe '#unaccept' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when solved_topics_auto_close_hours is enabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.solved_topics_auto_close_hours = 2
|
|
|
|
DiscourseSolved.accept_answer!(p1, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should unmark the post as solved' do
|
|
|
|
expect do
|
|
|
|
post "/solution/unaccept.json", params: { id: p1.id }
|
|
|
|
end.to change { topic.reload.public_topic_timer }.to(nil)
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
p1.reload
|
|
|
|
|
|
|
|
expect(p1.custom_fields["is_accepted_answer"]).to eq(nil)
|
|
|
|
expect(p1.topic.custom_fields["accepted_answer_post_id"]).to eq(nil)
|
|
|
|
end
|
FEATURE: Publish WebHook event when solving/unsolving (#85)
* FEATURE: Publish WebHook event when solving/unsolving
This feature will publish a post edit webhook event whenever a solution
is accepted or unaccepted.
I went ahead and used the existing post-edit webhook because all the
post custom fields for the solved plugin are already included in the
post-edit serializer.
* Create Solved Event Webhook
This commit adds a solved event webhook that will only trigger when an
answer has been marked as accepted or unaccepted.
It uses 100 as the webhook ID. This way any new webhooks in core can
keep using lower numbers like 11, 12, 13, but plugins can use 101, 102,
etc.
* Removed functionality that was added to core
This [PR][1] to discourse core adds what what removed in this commit.
It is better to have this logic in core so that it is discoverable and
future webhooks won't end up accidentally using the same ID.
[1]: https://github.com/discourse/discourse/pull/9110
* UX: Add "solved" status filter in advanced search page.
And rename `in:solved` to `status:solved`.
* FEATURE: Publish WebHook event when solving/unsolving
This feature will publish a post edit webhook event whenever a solution
is accepted or unaccepted.
I went ahead and used the existing post-edit webhook because all the
post custom fields for the solved plugin are already included in the
post-edit serializer.
* Create Solved Event Webhook
This commit adds a solved event webhook that will only trigger when an
answer has been marked as accepted or unaccepted.
It uses 100 as the webhook ID. This way any new webhooks in core can
keep using lower numbers like 11, 12, 13, but plugins can use 101, 102,
etc.
* Removed functionality that was added to core
This [PR][1] to discourse core adds what what removed in this commit.
It is better to have this logic in core so that it is discoverable and
future webhooks won't end up accidentally using the same ID.
[1]: https://github.com/discourse/discourse/pull/9110
Co-authored-by: Vinoth Kannan <vinothkannan@vinkas.com>
2020-03-06 13:28:29 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'triggers a webhook' do
|
|
|
|
Fabricate(:solved_web_hook)
|
|
|
|
post "/solution/unaccept.json", params: { id: p1.id }
|
|
|
|
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first
|
|
|
|
|
|
|
|
expect(job_args["event_name"]).to eq("unaccepted_solution")
|
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(p1.id)
|
2018-08-31 00:03:42 -04:00
|
|
|
end
|
|
|
|
end
|
2020-07-27 17:42:42 -04:00
|
|
|
|
|
|
|
context 'group moderators' do
|
|
|
|
fab!(:group_user) { Fabricate(:group_user) }
|
|
|
|
let(:user_gm) { group_user.user }
|
|
|
|
let(:group) { group_user.group }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
p1.topic.category.update!(reviewable_by_group_id: group.id)
|
|
|
|
sign_in(user_gm)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can accept a solution' do
|
|
|
|
post "/solution/accept.json", params: { id: p1.id }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
2017-05-22 05:09:40 -04:00
|
|
|
end
|