user_location_agent_spec.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. expect {
  13. @agent.receive([event])
  14. }.to change { @agent.events.count }.by(1)
  15. expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'something' => 'else' })
  16. expect(@agent.events.last.lat).to eq(45)
  17. expect(@agent.events.last.lng).to eq(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. expect(status).to eq(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. expect(status).to eq(401)
  28. content, status, content_type = @agent.receive_web_request({ 'secret' => 'my_secret' }, 'post', 'application/json')
  29. expect(status).to eq(200)
  30. end
  31. it 'creates an event on a web request' do
  32. expect {
  33. @agent.receive_web_request({ 'secret' => 'my_secret', 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }, 'post', 'application/json')
  34. }.to change { @agent.events.count }.by(1)
  35. expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'something' => 'else' })
  36. expect(@agent.events.last.lat).to eq(45)
  37. expect(@agent.events.last.lng).to eq(123)
  38. end
  39. end