2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-26 17:23:37 -04:00
|
|
|
require "rails_helper"
|
|
|
|
|
|
|
|
describe Admin::WebHooksController do
|
|
|
|
|
|
|
|
it 'is a subclass of AdminController' do
|
|
|
|
expect(Admin::WebHooksController < Admin::AdminController).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in as an admin' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:web_hook) { Fabricate(:web_hook) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-06-11 00:47:29 -04:00
|
|
|
|
2017-04-26 17:23:37 -04:00
|
|
|
before do
|
2018-06-11 00:47:29 -04:00
|
|
|
sign_in(admin)
|
2017-04-26 17:23:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#create' do
|
|
|
|
it 'creates a webhook' do
|
2018-06-11 00:47:29 -04:00
|
|
|
post "/admin/api/web_hooks.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
web_hook: {
|
|
|
|
payload_url: 'https://meta.discourse.org/',
|
|
|
|
content_type: 1,
|
|
|
|
secret: "a_secret_for_webhooks",
|
|
|
|
wildcard_web_hook: false,
|
|
|
|
active: true,
|
|
|
|
verify_certificate: true,
|
|
|
|
web_hook_event_type_ids: [1],
|
|
|
|
group_ids: [],
|
|
|
|
category_ids: []
|
|
|
|
}
|
2018-06-11 00:47:29 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-26 17:23:37 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-06-11 00:47:29 -04:00
|
|
|
expect(json["web_hook"]["payload_url"]).to eq("https://meta.discourse.org/")
|
2019-03-21 11:13:09 -04:00
|
|
|
expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:web_hook_create]).count).to eq(1)
|
2017-04-26 17:23:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error when field is not filled correctly' do
|
2018-06-11 00:47:29 -04:00
|
|
|
post "/admin/api/web_hooks.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
web_hook: {
|
|
|
|
content_type: 1,
|
|
|
|
secret: "a_secret_for_webhooks",
|
|
|
|
wildcard_web_hook: false,
|
|
|
|
active: true,
|
|
|
|
verify_certificate: true,
|
|
|
|
web_hook_event_type_ids: [1],
|
|
|
|
group_ids: [],
|
|
|
|
category_ids: []
|
|
|
|
}
|
2018-06-11 00:47:29 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-11 00:47:29 -04:00
|
|
|
expect(response.status).to eq(422)
|
2020-05-07 11:04:12 -04:00
|
|
|
response_body = response.parsed_body
|
2017-04-26 17:23:37 -04:00
|
|
|
|
|
|
|
expect(response_body["errors"]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-21 11:13:09 -04:00
|
|
|
describe '#update' do
|
|
|
|
it "logs webhook update" do
|
|
|
|
put "/admin/api/web_hooks/#{web_hook.id}.json", params: {
|
|
|
|
web_hook: { active: false, payload_url: "https://test.com" }
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(UserHistory.where(acting_user_id: admin.id,
|
|
|
|
action: UserHistory.actions[:web_hook_update],
|
2021-04-21 05:36:32 -04:00
|
|
|
new_value: "active: false, payload_url: https://test.com").exists?).to eq(true)
|
2019-03-21 11:13:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy' do
|
|
|
|
it "logs webhook destroy" do
|
|
|
|
delete "/admin/api/web_hooks/#{web_hook.id}.json", params: {
|
|
|
|
web_hook: { active: false, payload_url: "https://test.com" }
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:web_hook_destroy]).exists?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-26 17:23:37 -04:00
|
|
|
describe '#ping' do
|
|
|
|
it 'enqueues the ping event' do
|
2018-06-11 00:47:29 -04:00
|
|
|
expect do
|
|
|
|
post "/admin/api/web_hooks/#{web_hook.id}/ping.json"
|
|
|
|
end.to change { Jobs::EmitWebHookEvent.jobs.size }.by(1)
|
2017-04-26 17:23:37 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-11 00:47:29 -04:00
|
|
|
job_args = Jobs::EmitWebHookEvent.jobs.first["args"].first
|
|
|
|
expect(job_args["web_hook_id"]).to eq(web_hook.id)
|
|
|
|
expect(job_args["event_type"]).to eq("ping")
|
2017-04-26 17:23:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|