1
0

web_requests_controller_spec.rb 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. require 'rails_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", (options[:status] || 200).to_i, memory['content_type'], memory['response_headers']]
  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. expect(@agent.last_web_request_at).to be_nil
  25. post :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}
  26. expect(@agent.reload.last_web_request_at).to be_within(2).of(Time.now)
  27. expect(response.body).to eq("success")
  28. expect(response).to be_successful
  29. end
  30. it "should call receive_web_request" do
  31. post :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}
  32. @agent.reload
  33. expect(@agent.memory[:web_request_values]).to eq({ 'key' => "value", 'another_key' => "5" })
  34. expect(@agent.memory[:web_request_format]).to eq("text/html")
  35. expect(@agent.memory[:web_request_method]).to eq("post")
  36. expect(response.body).to eq("success")
  37. expect(response.headers['Content-Type']).to eq('text/plain; charset=utf-8')
  38. expect(response).to be_successful
  39. post :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "not_my_secret", :no => "go"}
  40. expect(@agent.reload.memory[:web_request_values]).not_to eq({ 'no' => "go" })
  41. expect(response.body).to eq("failure")
  42. expect(response).to be_not_found
  43. end
  44. it "should accept gets" do
  45. get :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}
  46. @agent.reload
  47. expect(@agent.memory[:web_request_values]).to eq({ 'key' => "value", 'another_key' => "5" })
  48. expect(@agent.memory[:web_request_format]).to eq("text/html")
  49. expect(@agent.memory[:web_request_method]).to eq("get")
  50. expect(response.body).to eq("success")
  51. expect(response).to be_successful
  52. end
  53. it "should pass through the received format" do
  54. get :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}, :format => :json
  55. @agent.reload
  56. expect(@agent.memory[:web_request_values]).to eq({ 'key' => "value", 'another_key' => "5" })
  57. expect(@agent.memory[:web_request_format]).to eq("application/json")
  58. expect(@agent.memory[:web_request_method]).to eq("get")
  59. post :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}, :format => :xml
  60. @agent.reload
  61. expect(@agent.memory[:web_request_values]).to eq({ 'key' => "value", 'another_key' => "5" })
  62. expect(@agent.memory[:web_request_format]).to eq("application/xml")
  63. expect(@agent.memory[:web_request_method]).to eq("post")
  64. put :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}, :format => :atom
  65. @agent.reload
  66. expect(@agent.memory[:web_request_values]).to eq({ 'key' => "value", 'another_key' => "5" })
  67. expect(@agent.memory[:web_request_format]).to eq("application/atom+xml")
  68. expect(@agent.memory[:web_request_method]).to eq("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, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}
  74. expect(response.headers['Content-Type']).to eq('application/json; charset=utf-8')
  75. end
  76. it "can accept custom response headers to return" do
  77. @agent.memory['response_headers'] = {"Access-Control-Allow-Origin" => "*"}
  78. @agent.save!
  79. get :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}
  80. expect(response.headers['Access-Control-Allow-Origin']).to eq('*')
  81. end
  82. it "can accept multiple custom response headers to return" do
  83. @agent.memory['response_headers'] = {"Access-Control-Allow-Origin" => "*", "X-My-Custom-Header" => "hello"}
  84. @agent.save!
  85. get :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"}
  86. expect(response.headers['Access-Control-Allow-Origin']).to eq('*')
  87. expect(response.headers['X-My-Custom-Header']).to eq('hello')
  88. end
  89. it 'should redirect correctly' do
  90. @agent.options['status'] = 302
  91. @agent.save
  92. post :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret"}, format: :json
  93. expect(response).to redirect_to('success')
  94. end
  95. it "should fail on incorrect users" do
  96. post :handle_request, params: {:user_id => users(:jane).to_param, :agent_id => @agent.id, :secret => "my_secret", :no => "go"}
  97. expect(response).to be_not_found
  98. end
  99. it "should fail on incorrect agents" do
  100. post :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => 454545, :secret => "my_secret", :no => "go"}
  101. expect(response).to be_not_found
  102. end
  103. describe "legacy update_location endpoint" do
  104. before do
  105. @agent = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), name: "something", options: { secret: "my_secret" })
  106. @agent.save!
  107. end
  108. it "should create events without requiring login" do
  109. post :update_location, params: {user_id: users(:bob).to_param, secret: "my_secret", longitude: 123, latitude: 45, something: "else"}
  110. expect(@agent.events.last.payload).to eq({ 'longitude' => "123", 'latitude' => "45", 'something' => "else" })
  111. expect(@agent.events.last.lat).to eq(45)
  112. expect(@agent.events.last.lng).to eq(123)
  113. end
  114. it "should only consider Agents::UserLocationAgents for the given user" do
  115. @jane_agent = Agent.build_for_type("Agents::UserLocationAgent", users(:jane), name: "something", options: { secret: "my_secret" })
  116. @jane_agent.save!
  117. post :update_location, params: {user_id: users(:bob).to_param, secret: "my_secret", longitude: 123, latitude: 45, something: "else"}
  118. expect(@agent.events.last.payload).to eq({ 'longitude' => "123", 'latitude' => "45", 'something' => "else" })
  119. expect(@jane_agent.events).to be_empty
  120. end
  121. it "should raise a 404 error when given an invalid user id" do
  122. post :update_location, params: {user_id: "123", secret: "not_my_secret", longitude: 123, latitude: 45, something: "else"}
  123. expect(response).to be_not_found
  124. end
  125. it "should only look at agents with the given secret" do
  126. @agent2 = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), name: "something", options: { secret: "my_secret2" })
  127. @agent2.save!
  128. expect {
  129. post :update_location, params: {user_id: users(:bob).to_param, secret: "my_secret2", longitude: 123, latitude: 45, something: "else"}
  130. expect(@agent2.events.last.payload).to eq({ 'longitude' => "123", 'latitude' => "45", 'something' => "else" })
  131. }.not_to change { @agent.events.count }
  132. end
  133. end
  134. end