web_requests_controller_spec.rb 4.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. require 'spec_helper'
  2. describe WebRequestsController do
  3. class Agents::WebRequestReceiverAgent < Agent
  4. cannot_receive_events!
  5. cannot_be_scheduled!
  6. def receive_web_request(params, method, format)
  7. if params.delete(:secret) == options[:secret]
  8. memory[:web_request_values] = params
  9. memory[:web_request_format] = format
  10. memory[:web_request_method] = method
  11. ["success", 200, memory['content_type']]
  12. else
  13. ["failure", 404]
  14. end
  15. end
  16. end
  17. before do
  18. stub(Agents::WebRequestReceiverAgent).valid_type?("Agents::WebRequestReceiverAgent") { true }
  19. @agent = Agents::WebRequestReceiverAgent.new(:name => "something", :options => { :secret => "my_secret" })
  20. @agent.user = users(:bob)
  21. @agent.save!
  22. end
  23. it "should not require login to receive a web request" do
  24. @agent.last_web_request_at.should be_nil
  25. post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
  26. @agent.reload.last_web_request_at.should be_within(2).of(Time.now)
  27. response.body.should == "success"
  28. response.should be_success
  29. end
  30. it "should call receive_web_request" do
  31. post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
  32. @agent.reload
  33. @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
  34. @agent.memory[:web_request_format].should == "text/html"
  35. @agent.memory[:web_request_method].should == "post"
  36. response.body.should == "success"
  37. response.headers['Content-Type'].should == 'text/plain; charset=utf-8'
  38. response.should be_success
  39. post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "not_my_secret", :no => "go"
  40. @agent.reload.memory[:web_request_values].should_not == { 'no' => "go" }
  41. response.body.should == "failure"
  42. response.should be_missing
  43. end
  44. it "should accept gets" do
  45. get :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
  46. @agent.reload
  47. @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
  48. @agent.memory[:web_request_format].should == "text/html"
  49. @agent.memory[:web_request_method].should == "get"
  50. response.body.should == "success"
  51. response.should be_success
  52. end
  53. it "should pass through the received format" do
  54. get :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5", :format => :json
  55. @agent.reload
  56. @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
  57. @agent.memory[:web_request_format].should == "application/json"
  58. @agent.memory[:web_request_method].should == "get"
  59. post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5", :format => :xml
  60. @agent.reload
  61. @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
  62. @agent.memory[:web_request_format].should == "application/xml"
  63. @agent.memory[:web_request_method].should == "post"
  64. put :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5", :format => :atom
  65. @agent.reload
  66. @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
  67. @agent.memory[:web_request_format].should == "application/atom+xml"
  68. @agent.memory[:web_request_method].should == "put"
  69. end
  70. it "can accept a content-type to return" do
  71. @agent.memory['content_type'] = 'application/json'
  72. @agent.save!
  73. get :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
  74. response.headers['Content-Type'].should == 'application/json; charset=utf-8'
  75. end
  76. it "should fail on incorrect users" do
  77. post :handle_request, :user_id => users(:jane).to_param, :agent_id => @agent.id, :secret => "my_secret", :no => "go"
  78. response.should be_missing
  79. end
  80. it "should fail on incorrect agents" do
  81. post :handle_request, :user_id => users(:bob).to_param, :agent_id => 454545, :secret => "my_secret", :no => "go"
  82. response.should be_missing
  83. end
  84. end