user_location_agent_spec.rb 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. require 'spec_helper'
  2. describe Agents::UserLocationAgent do
  3. before do
  4. @agent = Agent.build_for_type('Agents::UserLocationAgent', users(:bob), :name => 'something', :options => { :secret => 'my_secret' })
  5. @agent.save!
  6. end
  7. it 'receives an event' do
  8. event = Event.new
  9. event.agent = agents(:bob_weather_agent)
  10. event.created_at = Time.now
  11. event.payload = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
  12. lambda {
  13. @agent.receive([event])
  14. }.should change { @agent.events.count }.by(1)
  15. @agent.events.last.payload.should == { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
  16. @agent.events.last.lat.should == 45
  17. @agent.events.last.lng.should == 123
  18. end
  19. it 'does not accept a web request that is not POST' do
  20. %w[get put delete patch].each { |method|
  21. content, status, content_type = @agent.receive_web_request({ 'secret' => 'my_secret' }, method, 'application/json')
  22. status.should == 404
  23. }
  24. end
  25. it 'requires a valid secret for a web request' do
  26. content, status, content_type = @agent.receive_web_request({ 'secret' => 'fake' }, 'post', 'application/json')
  27. status.should == 401
  28. content, status, content_type = @agent.receive_web_request({ 'secret' => 'my_secret' }, 'post', 'application/json')
  29. status.should == 200
  30. end
  31. it 'creates an event on a web request' do
  32. lambda {
  33. @agent.receive_web_request({ 'secret' => 'my_secret', 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }, 'post', 'application/json')
  34. }.should change { @agent.events.count }.by(1)
  35. @agent.events.last.payload.should == { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
  36. @agent.events.last.lat.should == 45
  37. @agent.events.last.lng.should == 123
  38. end
  39. end