@@ -0,0 +1,35 @@ |
||
1 |
+module Agents |
|
2 |
+ class WebhookAgent < Agent |
|
3 |
+ cannot_be_scheduled! |
|
4 |
+ |
|
5 |
+ description <<-MD |
|
6 |
+ Use this Agent to create events by receiving webhooks from any source. |
|
7 |
+ |
|
8 |
+ Options: |
|
9 |
+ |
|
10 |
+ * `secret` - A token that the host will provide for authentication. |
|
11 |
+ MD |
|
12 |
+ |
|
13 |
+ def default_options |
|
14 |
+ { "secret" => "supersecretstring", } |
|
15 |
+ end |
|
16 |
+ |
|
17 |
+ def receive_webhook(params) |
|
18 |
+ return ["Not Authorized", 401] unless params[:secret] == options[:secret] |
|
19 |
+ |
|
20 |
+ create_event(:payload => params[:payload]) |
|
21 |
+ |
|
22 |
+ ['Event Created', 201] |
|
23 |
+ end |
|
24 |
+ |
|
25 |
+ def working? |
|
26 |
+ true |
|
27 |
+ end |
|
28 |
+ |
|
29 |
+ def validate_options |
|
30 |
+ unless options[:secret].present? |
|
31 |
+ errors.add(:base, "Must specify a :secret for 'Authenticating' requests") |
|
32 |
+ end |
|
33 |
+ end |
|
34 |
+ end |
|
35 |
+end |
@@ -0,0 +1,31 @@ |
||
1 |
+require 'spec_helper' |
|
2 |
+ |
|
3 |
+describe Agents::WebhookAgent do |
|
4 |
+ let(:agent) do |
|
5 |
+ _agent = Agents::WebhookAgent.new(:name => 'webhook', |
|
6 |
+ :options => {:secret => :foobar}) |
|
7 |
+ _agent.user = users(:bob) |
|
8 |
+ _agent.save! |
|
9 |
+ _agent |
|
10 |
+ end |
|
11 |
+ |
|
12 |
+ after { agent.destroy } |
|
13 |
+ |
|
14 |
+ describe 'receive_webhook' do |
|
15 |
+ it 'should create event if secret matches' do |
|
16 |
+ out = nil |
|
17 |
+ lambda { |
|
18 |
+ out = agent.receive_webhook({:secret => :foobar, :payload => {:some => :info}}) |
|
19 |
+ }.should change { Event.count }.by(1) |
|
20 |
+ out.should eq(['Event Created', 201]) |
|
21 |
+ end |
|
22 |
+ |
|
23 |
+ it 'should not create event if secrets dont match' do |
|
24 |
+ out = nil |
|
25 |
+ lambda { |
|
26 |
+ out = agent.receive_webhook({:secret => :bazbat, :payload => {:some => :info}}) |
|
27 |
+ }.should change { Event.count }.by(0) |
|
28 |
+ out.should eq(['Not Authorized', 401]) |
|
29 |
+ end |
|
30 |
+ end |
|
31 |
+end |