web_requests_controller_spec.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. describe "legacy update_location endpoint" do
  85. before do
  86. @agent = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), name: "something", options: { secret: "my_secret" })
  87. @agent.save!
  88. end
  89. it "should create events without requiring login" do
  90. post :update_location, user_id: users(:bob).to_param, secret: "my_secret", longitude: 123, latitude: 45, something: "else"
  91. @agent.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" }
  92. @agent.events.last.lat.should == 45
  93. @agent.events.last.lng.should == 123
  94. end
  95. it "should only consider Agents::UserLocationAgents for the given user" do
  96. @jane_agent = Agent.build_for_type("Agents::UserLocationAgent", users(:jane), name: "something", options: { secret: "my_secret" })
  97. @jane_agent.save!
  98. post :update_location, user_id: users(:bob).to_param, secret: "my_secret", longitude: 123, latitude: 45, something: "else"
  99. @agent.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" }
  100. @jane_agent.events.should be_empty
  101. end
  102. it "should raise a 404 error when given an invalid user id" do
  103. post :update_location, user_id: "123", secret: "not_my_secret", longitude: 123, latitude: 45, something: "else"
  104. response.should be_missing
  105. end
  106. it "should only look at agents with the given secret" do
  107. @agent2 = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), name: "something", options: { secret: "my_secret2" })
  108. @agent2.save!
  109. lambda {
  110. post :update_location, user_id: users(:bob).to_param, secret: "my_secret2", longitude: 123, latitude: 45, something: "else"
  111. @agent2.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" }
  112. }.should_not change { @agent.events.count }
  113. end
  114. end
  115. end