2016-06-15 12:49:57 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe WebHook do
|
|
|
|
it { is_expected.to validate_presence_of :payload_url }
|
|
|
|
it { is_expected.to validate_presence_of :content_type }
|
|
|
|
it { is_expected.to validate_presence_of :last_delivery_status }
|
|
|
|
it { is_expected.to validate_presence_of :web_hook_event_types }
|
|
|
|
|
|
|
|
describe '#content_types' do
|
|
|
|
subject { WebHook.content_types }
|
|
|
|
|
|
|
|
it "'json' (application/json) should be at 1st position" do
|
|
|
|
expect(subject['application/json']).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'url_encoded' (application/x-www-form-urlencoded) should be at 2st position" do
|
|
|
|
expect(subject['application/x-www-form-urlencoded']).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#last_delivery_statuses' do
|
|
|
|
subject { WebHook.last_delivery_statuses }
|
|
|
|
|
|
|
|
it "inactive should be at 1st position" do
|
|
|
|
expect(subject[:inactive]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "failed should be at 2st position" do
|
|
|
|
expect(subject[:failed]).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "successful should be at 3st position" do
|
|
|
|
expect(subject[:successful]).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'web hooks' do
|
2017-12-11 02:15:50 -06:00
|
|
|
let!(:post_hook) { Fabricate(:web_hook, payload_url: " https://example.com ") }
|
2016-06-15 12:49:57 -05:00
|
|
|
let!(:topic_hook) { Fabricate(:topic_web_hook) }
|
|
|
|
|
2017-12-11 02:15:50 -06:00
|
|
|
it "removes whitspace from payload_url before saving" do
|
|
|
|
expect(post_hook.payload_url).to eq("https://example.com")
|
|
|
|
end
|
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
describe '#active_web_hooks' do
|
2018-01-03 03:00:44 -06:00
|
|
|
it "returns unique hooks" do
|
|
|
|
post_hook.web_hook_event_types << WebHookEventType.find_by(name: 'topic')
|
|
|
|
post_hook.update!(wildcard_web_hook: true)
|
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
expect(WebHook.active_web_hooks(:post)).to eq([post_hook])
|
2018-01-03 03:00:44 -06:00
|
|
|
end
|
|
|
|
|
2016-06-15 12:49:57 -05:00
|
|
|
it 'find relevant hooks' do
|
2018-05-21 03:23:09 -05:00
|
|
|
expect(WebHook.active_web_hooks(:post)).to eq([post_hook])
|
|
|
|
expect(WebHook.active_web_hooks(:topic)).to eq([topic_hook])
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes inactive hooks' do
|
2018-05-21 03:23:09 -05:00
|
|
|
post_hook.update!(active: false)
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
expect(WebHook.active_web_hooks(:post)).to eq([])
|
|
|
|
expect(WebHook.active_web_hooks(:topic)).to eq([topic_hook])
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
describe 'wildcard web hooks' do
|
|
|
|
let!(:wildcard_hook) { Fabricate(:wildcard_web_hook) }
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
it 'should include wildcard hooks' do
|
|
|
|
expect(WebHook.active_web_hooks(:wildcard)).to eq([wildcard_hook])
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
expect(WebHook.active_web_hooks(:post)).to contain_exactly(
|
|
|
|
post_hook, wildcard_hook
|
|
|
|
)
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
expect(WebHook.active_web_hooks(:topic)).to contain_exactly(
|
|
|
|
topic_hook, wildcard_hook
|
|
|
|
)
|
|
|
|
end
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
describe '#enqueue_hooks' do
|
|
|
|
before do
|
|
|
|
SiteSetting.queue_jobs = true
|
|
|
|
end
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
it 'accepts additional parameters' do
|
|
|
|
payload = { test: 'some payload' }.to_json
|
|
|
|
WebHook.enqueue_hooks(:post, payload: payload)
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.first["args"].first
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
expect(job_args["web_hook_id"]).to eq(post_hook.id)
|
|
|
|
expect(job_args["event_type"]).to eq('post')
|
|
|
|
expect(job_args["payload"]).to eq(payload)
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
context 'includes wildcard hooks' do
|
|
|
|
let!(:wildcard_hook) { Fabricate(:wildcard_web_hook) }
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
describe '#enqueue_hooks' do
|
|
|
|
it 'enqueues hooks with ids' do
|
|
|
|
WebHook.enqueue_hooks(:post)
|
|
|
|
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.first["args"].first
|
|
|
|
|
|
|
|
expect(job_args["web_hook_id"]).to eq(post_hook.id)
|
|
|
|
expect(job_args["event_type"]).to eq('post')
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
expect(job_args["web_hook_id"]).to eq(wildcard_hook.id)
|
|
|
|
expect(job_args["event_type"]).to eq('post')
|
|
|
|
end
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'enqueues hooks' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
2016-09-13 03:03:17 -05:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2016-06-15 12:49:57 -05:00
|
|
|
let(:topic) { Fabricate(:topic, user: user) }
|
2016-09-13 03:03:17 -05:00
|
|
|
let(:post) { Fabricate(:post, topic: topic, user: user) }
|
2018-05-21 03:23:09 -05:00
|
|
|
let(:topic_web_hook) { Fabricate(:topic_web_hook) }
|
2017-03-16 01:44:09 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.queue_jobs = true
|
2018-05-21 03:23:09 -05:00
|
|
|
topic_web_hook
|
2017-03-16 01:44:09 -05:00
|
|
|
end
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
describe 'when there are no active hooks' do
|
|
|
|
it 'should not enqueue anything' do
|
|
|
|
topic_web_hook.destroy!
|
|
|
|
post = PostCreator.create(user, raw: 'post', title: 'topic', skip_validations: true)
|
|
|
|
expect(Jobs::EmitWebHookEvent.jobs.length).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
it 'should enqueue the right hooks for topic events' do
|
2018-02-26 21:07:37 -06:00
|
|
|
post = PostCreator.create(user, raw: 'post', title: 'topic', skip_validations: true)
|
2018-05-21 03:23:09 -05:00
|
|
|
topic_id = post.topic.id
|
2018-02-26 21:07:37 -06:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("topic_created")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(topic_id)
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
PostDestroyer.new(user, post).destroy
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("topic_destroyed")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(topic_id)
|
2018-02-26 21:07:37 -06:00
|
|
|
|
|
|
|
PostDestroyer.new(user, post).recover
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("topic_recovered")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(topic_id)
|
2018-02-26 21:07:37 -06:00
|
|
|
|
|
|
|
%w{archived closed visible}.each do |status|
|
|
|
|
post.topic.update_status(status, true, topic.user)
|
2017-03-16 01:44:09 -05:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("topic_#{status}_status_updated")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(topic_id)
|
2017-03-16 01:44:09 -05:00
|
|
|
end
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
|
|
|
|
2018-01-22 13:58:01 -06:00
|
|
|
describe 'when topic has been deleted' do
|
|
|
|
it 'should not enqueue a post/topic edited hooks' do
|
|
|
|
topic.trash!
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
PostRevisor.new(post, topic).revise!(
|
|
|
|
post.user,
|
|
|
|
{
|
|
|
|
category_id: Category.last.id,
|
|
|
|
raw: "#{post.raw} new"
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(Jobs::EmitWebHookEvent.jobs.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-15 12:49:57 -05:00
|
|
|
it 'should enqueue the right hooks for post events' do
|
2017-03-16 01:44:09 -05:00
|
|
|
Fabricate(:web_hook)
|
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
post = PostCreator.create!(user,
|
2018-02-26 21:07:37 -06:00
|
|
|
raw: 'post',
|
|
|
|
topic_id: topic.id,
|
|
|
|
reply_to_post_number: 1,
|
|
|
|
skip_validations: true
|
|
|
|
)
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("post_created")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(post.id)
|
|
|
|
|
|
|
|
Jobs::EmitWebHookEvent.jobs.clear
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
# post destroy or recover triggers a moderator post
|
|
|
|
expect { PostDestroyer.new(user, post).destroy }
|
2018-05-21 03:23:09 -05:00
|
|
|
.to change { Jobs::EmitWebHookEvent.jobs.count }.by(3)
|
2016-06-15 12:49:57 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("post_edited")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(post.id)
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[1]["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("post_destroyed")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(post.id)
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-05-21 03:23:09 -05:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[2]["args"].first
|
|
|
|
|
|
|
|
expect(job_args["event_name"]).to eq("topic_destroyed")
|
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(post.topic.id)
|
|
|
|
|
|
|
|
Jobs::EmitWebHookEvent.jobs.clear
|
|
|
|
|
|
|
|
expect { PostDestroyer.new(user, post).recover }
|
|
|
|
.to change { Jobs::EmitWebHookEvent.jobs.count }.by(3)
|
|
|
|
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first
|
|
|
|
|
|
|
|
expect(job_args["event_name"]).to eq("post_edited")
|
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(post.id)
|
|
|
|
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[1]["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("post_recovered")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(post.id)
|
|
|
|
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs[2]["args"].first
|
|
|
|
|
|
|
|
expect(job_args["event_name"]).to eq("topic_recovered")
|
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(post.topic.id)
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
2016-09-13 03:03:17 -05:00
|
|
|
|
2016-12-12 21:26:26 -06:00
|
|
|
it 'should enqueue the right hooks for user events' do
|
2018-01-22 13:58:01 -06:00
|
|
|
Fabricate(:user_web_hook, active: true)
|
2016-09-13 03:03:17 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
user
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2016-09-13 03:03:17 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("user_created")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(user.id)
|
2016-12-12 21:26:26 -06:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
admin
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("user_created")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(admin.id)
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
user.approve(admin)
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("user_approved")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(user.id)
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
UserUpdater.new(admin, user).update(username: 'testing123')
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2017-03-16 01:44:09 -05:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("user_updated")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(user.id)
|
2017-12-20 03:47:30 -06:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
user.logged_out
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2017-12-20 03:47:30 -06:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("user_logged_out")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(user.id)
|
2017-12-20 03:47:30 -06:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
user.logged_in
|
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
|
2017-12-20 03:47:30 -06:00
|
|
|
|
2018-02-26 21:07:37 -06:00
|
|
|
expect(job_args["event_name"]).to eq("user_logged_in")
|
2018-05-21 03:23:09 -05:00
|
|
|
payload = JSON.parse(job_args["payload"])
|
|
|
|
expect(payload["id"]).to eq(user.id)
|
2016-09-13 03:03:17 -05:00
|
|
|
end
|
2016-06-15 12:49:57 -05:00
|
|
|
end
|
|
|
|
end
|