Aucune description http://j1x-huginn.herokuapp.com

webhooks_controller_spec.rb 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. require 'spec_helper'
  2. describe WebhooksController do
  3. class Agents::WebhookReceiverAgent < Agent
  4. cannot_receive_events!
  5. cannot_be_scheduled!
  6. def receive_webhook(params)
  7. if params.delete(:secret) == options[:secret]
  8. memory[:webhook_values] = params
  9. ["success", 200]
  10. else
  11. ["failure", 404]
  12. end
  13. end
  14. end
  15. before do
  16. stub(Agents::WebhookReceiverAgent).valid_type?("Agents::WebhookReceiverAgent") { true }
  17. @agent = Agents::WebhookReceiverAgent.new(:name => "something", :options => { :secret => "my_secret" })
  18. @agent.user = users(:bob)
  19. @agent.save!
  20. end
  21. it "should not require login to trigger a webhook" do
  22. @agent.last_webhook_at.should be_nil
  23. post :create, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
  24. @agent.reload.last_webhook_at.should be_within(2).of(Time.now)
  25. response.body.should == "success"
  26. response.should be_success
  27. end
  28. it "should call receive_webhook" do
  29. post :create, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
  30. @agent.reload.memory[:webhook_values].should == { 'key' => "value", 'another_key' => "5" }
  31. response.body.should == "success"
  32. response.should be_success
  33. post :create, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "not_my_secret", :no => "go"
  34. @agent.reload.memory[:webhook_values].should_not == { 'no' => "go" }
  35. response.body.should == "failure"
  36. response.should be_missing
  37. end
  38. it "should fail on incorrect users" do
  39. post :create, :user_id => users(:jane).to_param, :agent_id => @agent.id, :secret => "my_secret", :no => "go"
  40. response.should be_missing
  41. end
  42. it "should fail on incorrect agents" do
  43. post :create, :user_id => users(:bob).to_param, :agent_id => 454545, :secret => "my_secret", :no => "go"
  44. response.should be_missing
  45. end
  46. end