user_location_agent_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require 'rails_helper'
  2. describe Agents::UserLocationAgent do
  3. before do
  4. @agent = Agent.build_for_type('Agents::UserLocationAgent', users(:bob),
  5. :name => 'something',
  6. :options => { :secret => 'my_secret',
  7. :max_accuracy => '50',
  8. :min_distance => '50',
  9. :api_key => 'api_key' })
  10. @agent.save!
  11. end
  12. it 'receives an event' do
  13. event = Event.new
  14. event.agent = agents(:bob_weather_agent)
  15. event.created_at = Time.now
  16. event.payload = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
  17. expect {
  18. @agent.receive([event])
  19. }.to change { @agent.events.count }.by(1)
  20. expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'something' => 'else' })
  21. expect(@agent.events.last.lat).to eq(45)
  22. expect(@agent.events.last.lng).to eq(123)
  23. end
  24. it 'does not accept a web request that is not POST' do
  25. %w[get put delete patch].each { |method|
  26. content, status, content_type = @agent.receive_web_request({ 'secret' => 'my_secret' }, method, 'application/json')
  27. expect(status).to eq(404)
  28. }
  29. end
  30. it 'requires a valid secret for a web request' do
  31. content, status, content_type = @agent.receive_web_request({ 'secret' => 'fake' }, 'post', 'application/json')
  32. expect(status).to eq(401)
  33. content, status, content_type = @agent.receive_web_request({ 'secret' => 'my_secret' }, 'post', 'application/json')
  34. expect(status).to eq(200)
  35. end
  36. it 'creates an event on a web request' do
  37. expect {
  38. @agent.receive_web_request({ 'secret' => 'my_secret', 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }, 'post', 'application/json')
  39. }.to change { @agent.events.count }.by(1)
  40. expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'something' => 'else' })
  41. expect(@agent.events.last.lat).to eq(45)
  42. expect(@agent.events.last.lng).to eq(123)
  43. end
  44. it 'does not create event when too inaccurate' do
  45. event = Event.new
  46. event.agent = agents(:bob_weather_agent)
  47. event.created_at = Time.now
  48. event.payload = { 'longitude' => 123, 'latitude' => 45, 'accuracy' => '100', 'something' => 'else' }
  49. expect {
  50. @agent.receive([event])
  51. }.to change { @agent.events.count }.by(0)
  52. end
  53. it 'does create event when accurate enough' do
  54. event = Event.new
  55. event.agent = agents(:bob_weather_agent)
  56. event.created_at = Time.now
  57. event.payload = { 'longitude' => 123, 'latitude' => 45, 'accuracy' => '20', 'something' => 'else' }
  58. expect {
  59. @agent.receive([event])
  60. }.to change { @agent.events.count }.by(1)
  61. expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'accuracy' => '20', 'something' => 'else' })
  62. expect(@agent.events.last.lat).to eq(45)
  63. expect(@agent.events.last.lng).to eq(123)
  64. end
  65. it 'allows a custom accuracy field' do
  66. event = Event.new
  67. event.agent = agents(:bob_weather_agent)
  68. event.created_at = Time.now
  69. @agent.options['accuracy_field'] = 'estimated_to'
  70. event.payload = { 'longitude' => 123, 'latitude' => 45, 'estimated_to' => '20', 'something' => 'else' }
  71. expect {
  72. @agent.receive([event])
  73. }.to change { @agent.events.count }.by(1)
  74. expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'estimated_to' => '20', 'something' => 'else' })
  75. expect(@agent.events.last.lat).to eq(45)
  76. expect(@agent.events.last.lng).to eq(123)
  77. end
  78. it 'does create an event when far enough' do
  79. @agent.memory["last_location"] = { 'longitude' => 12, 'latitude' => 34, 'something' => 'else' }
  80. event = Event.new
  81. event.agent = agents(:bob_weather_agent)
  82. event.created_at = Time.now
  83. event.payload = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
  84. expect {
  85. @agent.receive([event])
  86. }.to change { @agent.events.count }.by(1)
  87. expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'something' => 'else' })
  88. expect(@agent.events.last.lat).to eq(45)
  89. expect(@agent.events.last.lng).to eq(123)
  90. end
  91. it 'does not create an event when too close' do
  92. @agent.memory["last_location"] = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
  93. event = Event.new
  94. event.agent = agents(:bob_weather_agent)
  95. event.created_at = Time.now
  96. event.payload = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
  97. expect {
  98. @agent.receive([event])
  99. }.to change { @agent.events.count }.by(0)
  100. end
  101. end