dropbox_watch_agent_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. require 'rails_helper'
  2. describe Agents::DropboxWatchAgent do
  3. before(:each) do
  4. @agent = Agents::DropboxWatchAgent.new(
  5. name: 'save to dropbox',
  6. options: {
  7. access_token: '70k3n',
  8. dir_to_watch: '/my/dropbox/dir',
  9. expected_update_period_in_days: 2
  10. }
  11. )
  12. @agent.user = users(:bob)
  13. @agent.service = services(:generic)
  14. @agent.save!
  15. end
  16. it 'cannot receive events' do
  17. expect(@agent.cannot_receive_events?).to eq true
  18. end
  19. it 'has agent description' do
  20. expect(@agent.description).to_not be_nil
  21. end
  22. it 'has event description' do
  23. expect(@agent.event_description).to_not be_nil
  24. end
  25. describe '#valid?' do
  26. before(:each) { expect(@agent.valid?).to eq true }
  27. it 'requires a "dir_to_watch"' do
  28. @agent.options[:dir_to_watch] = nil
  29. expect(@agent.valid?).to eq false
  30. end
  31. describe 'expected_update_period_in_days' do
  32. it 'needs to be present' do
  33. @agent.options[:expected_update_period_in_days] = nil
  34. expect(@agent.valid?).to eq false
  35. end
  36. it 'needs to be a positive integer' do
  37. @agent.options[:expected_update_period_in_days] = -1
  38. expect(@agent.valid?).to eq false
  39. end
  40. end
  41. end
  42. describe '#check' do
  43. let(:first_result) { Dropbox::API::Object.convert([{ 'path_display' => '1.json', 'rev' => '1', 'server_modified' => '01-01-01' }], nil) }
  44. before(:each) do
  45. stub.proxy(Dropbox::API::Client).new do |api|
  46. stub(api).ls('/my/dropbox/dir') { first_result }
  47. end
  48. end
  49. it 'saves the directory listing in its memory' do
  50. @agent.check
  51. expect(@agent.memory).to eq({"contents"=>[{"path"=>"1.json", "rev"=>"1", "modified"=>"01-01-01"}]})
  52. end
  53. context 'first time' do
  54. before(:each) { @agent.memory = {} }
  55. it 'does not send any events' do
  56. expect { @agent.check }.to_not change(Event, :count)
  57. end
  58. end
  59. context 'subsequent calls' do
  60. let(:second_result) { Dropbox::API::Object.convert([{ 'path_display' => '2.json', 'rev' => '1', 'server_modified' => '02-02-02' }], nil) }
  61. before(:each) do
  62. @agent.memory = { 'contents' => 'not_empty' }
  63. stub.proxy(Dropbox::API::Client).new do |api|
  64. stub(api).ls('/my/dropbox/dir') { second_result }
  65. end
  66. end
  67. it 'sends an event upon a different directory listing' do
  68. payload = { 'diff' => 'object as hash' }
  69. stub.proxy(Agents::DropboxWatchAgent::DropboxDirDiff).new(@agent.memory['contents'], [{"path"=>"2.json", "rev"=>"1", "modified"=>"02-02-02"}]) do |diff|
  70. stub(diff).empty? { false }
  71. stub(diff).to_hash { payload }
  72. end
  73. expect { @agent.check }.to change(Event, :count).by(1)
  74. expect(Event.last.payload).to eq(payload)
  75. end
  76. it 'does not sent any events when there is no difference on the directory listing' do
  77. stub.proxy(Agents::DropboxWatchAgent::DropboxDirDiff).new(@agent.memory['contents'], [{"path"=>"2.json", "rev"=>"1", "modified"=>"02-02-02"}]) do |diff|
  78. stub(diff).empty? { true }
  79. end
  80. expect { @agent.check }.to_not change(Event, :count)
  81. end
  82. end
  83. end
  84. describe Agents::DropboxWatchAgent::DropboxDirDiff do
  85. let(:previous) { [
  86. { 'path' => '1.json', 'rev' => '1' },
  87. { 'path' => '2.json', 'rev' => '1' },
  88. { 'path' => '3.json', 'rev' => '1' }
  89. ] }
  90. let(:current) { [
  91. { 'path' => '1.json', 'rev' => '2' },
  92. { 'path' => '3.json', 'rev' => '1' },
  93. { 'path' => '4.json', 'rev' => '1' }
  94. ] }
  95. describe '#empty?' do
  96. it 'is true when no differences are detected' do
  97. diff = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, previous)
  98. expect(diff.empty?).to eq true
  99. end
  100. it 'is false when differences were detected' do
  101. diff = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current)
  102. expect(diff.empty?).to eq false
  103. end
  104. end
  105. describe '#to_hash' do
  106. subject(:diff_hash) { Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current).to_hash }
  107. it 'detects additions' do
  108. expect(diff_hash[:added]).to eq [{ 'path' => '4.json', 'rev' => '1' }]
  109. end
  110. it 'detects removals' do
  111. expect(diff_hash[:removed]).to eq [ { 'path' => '2.json', 'rev' => '1' } ]
  112. end
  113. it 'detects updates' do
  114. expect(diff_hash[:updated]).to eq [ { 'path' => '1.json', 'rev' => '2' } ]
  115. end
  116. context 'when the previous value is not defined' do
  117. it 'considers all additions' do
  118. diff_hash = Agents::DropboxWatchAgent::DropboxDirDiff.new(nil, current).to_hash
  119. expect(diff_hash[:added]).to eq current
  120. expect(diff_hash[:removed]).to eq []
  121. expect(diff_hash[:updated]).to eq []
  122. end
  123. end
  124. context 'when the current value is not defined' do
  125. it 'considers all removals' do
  126. diff_hash = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, nil).to_hash
  127. expect(diff_hash[:added]).to eq []
  128. expect(diff_hash[:removed]).to eq previous
  129. expect(diff_hash[:updated]).to eq []
  130. end
  131. end
  132. end
  133. end
  134. end