1
0

dropbox_watch_agent_spec.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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) do
  44. Dropbox::API::Object.convert(
  45. [
  46. { 'path_display' => '1.json', 'rev' => '1', 'server_modified' => '01-01-01' },
  47. { 'path_display' => 'sub_dir_1', '.tag' => 'folder' }
  48. ],
  49. nil
  50. )
  51. end
  52. before(:each) do
  53. allow(Dropbox::API::Client).to receive(:new) do
  54. instance_double(Dropbox::API::Client).tap { |api|
  55. allow(api).to receive(:ls).with('/my/dropbox/dir') { first_result }
  56. }
  57. end
  58. end
  59. it 'saves the directory listing in its memory' do
  60. @agent.check
  61. expect(@agent.memory).to eq({"contents"=>[{"path"=>"1.json", "rev"=>"1", "modified"=>"01-01-01"}]})
  62. end
  63. context 'first time' do
  64. before(:each) { @agent.memory = {} }
  65. it 'does not send any events' do
  66. expect { @agent.check }.to_not change(Event, :count)
  67. end
  68. end
  69. context 'subsequent calls' do
  70. let(:second_result) do
  71. Dropbox::API::Object.convert(
  72. [
  73. { 'path_display' => '2.json', 'rev' => '1', 'server_modified' => '02-02-02' },
  74. { 'path_display' => 'sub_dir_2', '.tag' => 'folder' }
  75. ],
  76. nil
  77. )
  78. end
  79. before(:each) do
  80. @agent.memory = { 'contents' => 'not_empty' }
  81. allow(Dropbox::API::Client).to receive(:new) do
  82. instance_double(Dropbox::API::Client).tap { |api|
  83. allow(api).to receive(:ls).with('/my/dropbox/dir') { second_result }
  84. }
  85. end
  86. end
  87. it 'sends an event upon a different directory listing' do
  88. payload = { 'diff' => 'object as hash' }
  89. allow(Agents::DropboxWatchAgent::DropboxDirDiff).to receive(:new).with(@agent.memory['contents'], [{"path"=>"2.json", "rev"=>"1", "modified"=>"02-02-02"}]) do
  90. instance_double(Agents::DropboxWatchAgent::DropboxDirDiff).tap { |diff|
  91. allow(diff).to receive(:empty?) { false }
  92. allow(diff).to receive(:to_hash) { payload }
  93. }
  94. end
  95. expect { @agent.check }.to change(Event, :count).by(1)
  96. expect(Event.last.payload).to eq(payload)
  97. end
  98. it 'does not sent any events when there is no difference on the directory listing' do
  99. allow(Agents::DropboxWatchAgent::DropboxDirDiff).to receive(:new).with(@agent.memory['contents'], [{"path"=>"2.json", "rev"=>"1", "modified"=>"02-02-02"}]) do
  100. instance_double(Agents::DropboxWatchAgent::DropboxDirDiff).tap { |diff|
  101. allow(diff).to receive(:empty?) { true }
  102. }
  103. end
  104. expect { @agent.check }.to_not change(Event, :count)
  105. end
  106. end
  107. end
  108. describe Agents::DropboxWatchAgent::DropboxDirDiff do
  109. let(:previous) { [
  110. { 'path' => '1.json', 'rev' => '1' },
  111. { 'path' => '2.json', 'rev' => '1' },
  112. { 'path' => '3.json', 'rev' => '1' }
  113. ] }
  114. let(:current) { [
  115. { 'path' => '1.json', 'rev' => '2' },
  116. { 'path' => '3.json', 'rev' => '1' },
  117. { 'path' => '4.json', 'rev' => '1' }
  118. ] }
  119. describe '#empty?' do
  120. it 'is true when no differences are detected' do
  121. diff = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, previous)
  122. expect(diff.empty?).to eq true
  123. end
  124. it 'is false when differences were detected' do
  125. diff = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current)
  126. expect(diff.empty?).to eq false
  127. end
  128. end
  129. describe '#to_hash' do
  130. subject(:diff_hash) { Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current).to_hash }
  131. it 'detects additions' do
  132. expect(diff_hash[:added]).to eq [{ 'path' => '4.json', 'rev' => '1' }]
  133. end
  134. it 'detects removals' do
  135. expect(diff_hash[:removed]).to eq [ { 'path' => '2.json', 'rev' => '1' } ]
  136. end
  137. it 'detects updates' do
  138. expect(diff_hash[:updated]).to eq [ { 'path' => '1.json', 'rev' => '2' } ]
  139. end
  140. context 'when the previous value is not defined' do
  141. it 'considers all additions' do
  142. diff_hash = Agents::DropboxWatchAgent::DropboxDirDiff.new(nil, current).to_hash
  143. expect(diff_hash[:added]).to eq current
  144. expect(diff_hash[:removed]).to eq []
  145. expect(diff_hash[:updated]).to eq []
  146. end
  147. end
  148. context 'when the current value is not defined' do
  149. it 'considers all removals' do
  150. diff_hash = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, nil).to_hash
  151. expect(diff_hash[:added]).to eq []
  152. expect(diff_hash[:removed]).to eq previous
  153. expect(diff_hash[:updated]).to eq []
  154. end
  155. end
  156. end
  157. end
  158. end