dropbox_file_url_agent_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. require 'rails_helper'
  2. describe Agents::DropboxFileUrlAgent do
  3. before(:each) do
  4. @agent = Agents::DropboxFileUrlAgent.new(
  5. name: 'dropbox file to url',
  6. options: {}
  7. )
  8. @agent.user = users(:bob)
  9. @agent.service = services(:generic)
  10. @agent.save!
  11. end
  12. it 'cannot be scheduled' do
  13. expect(@agent.cannot_be_scheduled?).to eq true
  14. end
  15. it 'has agent description' do
  16. expect(@agent.description).to_not be_nil
  17. end
  18. it 'has event description' do
  19. expect(@agent.event_description).to_not be_nil
  20. end
  21. it 'renders the event description when link_type=permanent' do
  22. @agent.options['link_type'] = 'permanent'
  23. expect(@agent.event_description).to_not be_nil
  24. end
  25. describe "#receive" do
  26. def event(payload)
  27. event = Event.new(payload: payload)
  28. event.agent = agents(:bob_manual_event_agent)
  29. event
  30. end
  31. context 'with temporary urls' do
  32. let(:first_dropbox_url_payload) { Dropbox::API::Object.new({ 'link' => 'http://dropbox.com/first/path/url' }, nil) }
  33. let(:second_dropbox_url_payload) { Dropbox::API::Object.new({ 'link' => 'http://dropbox.com/second/path/url' }, nil) }
  34. let(:third_dropbox_url_payload) { Dropbox::API::Object.new({ 'link' => 'http://dropbox.com/third/path/url' }, nil) }
  35. before(:each) do
  36. allow(Dropbox::API::Client).to receive(:new) do
  37. instance_double(Dropbox::API::Client).tap { |api|
  38. allow(api).to receive(:find).with('/first/path') { Dropbox::API::File.new({}, nil).tap { |file| allow(file).to receive(:direct_url) { first_dropbox_url_payload } } }
  39. allow(api).to receive(:find).with('/second/path') { Dropbox::API::File.new({}, nil).tap { |file| allow(file).to receive(:direct_url) { second_dropbox_url_payload } } }
  40. allow(api).to receive(:find).with('/third/path') { Dropbox::API::File.new({}, nil).tap { |file| allow(file).to receive(:direct_url) { third_dropbox_url_payload } } }
  41. }
  42. end
  43. end
  44. context 'with a single path' do
  45. before(:each) { @event = event(paths: '/first/path') }
  46. it 'creates one event with the temporary dropbox link' do
  47. expect { @agent.receive([@event]) }.to change(Event, :count).by(1)
  48. expect(Event.last.payload).to eq({ 'url' => 'http://dropbox.com/first/path/url' })
  49. end
  50. end
  51. context 'with multiple comma-separated paths' do
  52. before(:each) { @event = event(paths: '/first/path, /second/path, /third/path') }
  53. it 'creates one event with the temporary dropbox link for each path' do
  54. expect { @agent.receive([@event]) }.to change(Event, :count).by(3)
  55. last_events = Event.last(3)
  56. expect(last_events[0].payload['url']).to eq('http://dropbox.com/first/path/url')
  57. expect(last_events[1].payload['url']).to eq('http://dropbox.com/second/path/url')
  58. expect(last_events[2].payload['url']).to eq('http://dropbox.com/third/path/url')
  59. end
  60. end
  61. end
  62. context 'with permanent urls' do
  63. def response_for(url)
  64. Dropbox::API::Object.new({'url' => "https://www.dropbox.com/s/#{url}?dl=0"}, nil)
  65. end
  66. let(:first_dropbox_url_payload) { response_for('/first/path') }
  67. let(:second_dropbox_url_payload) { response_for('/second/path') }
  68. let(:third_dropbox_url_payload) { response_for('/third/path') }
  69. before(:each) do
  70. allow(Dropbox::API::Client).to receive(:new) do
  71. instance_double(Dropbox::API::Client).tap { |api|
  72. allow(api).to receive(:find).with('/first/path') { Dropbox::API::File.new({}, nil).tap { |file| allow(file).to receive(:share_url) { first_dropbox_url_payload } } }
  73. allow(api).to receive(:find).with('/second/path') { Dropbox::API::File.new({}, nil).tap { |file| allow(file).to receive(:share_url) { second_dropbox_url_payload } } }
  74. allow(api).to receive(:find).with('/third/path') { Dropbox::API::File.new({}, nil).tap { |file| allow(file).to receive(:share_url) { third_dropbox_url_payload } } }
  75. }
  76. end
  77. @agent.options['link_type'] = 'permanent'
  78. end
  79. it 'creates one event with a single path' do
  80. expect { @agent.receive([event(paths: '/first/path')]) }.to change(Event, :count).by(1)
  81. expect(Event.last.payload).to eq(first_dropbox_url_payload.response)
  82. end
  83. it 'creates one event with the permanent dropbox link for each path' do
  84. event = event(paths: '/first/path, /second/path, /third/path')
  85. expect { @agent.receive([event]) }.to change(Event, :count).by(3)
  86. last_events = Event.last(3)
  87. expect(last_events[0].payload).to eq(first_dropbox_url_payload.response)
  88. expect(last_events[1].payload).to eq(second_dropbox_url_payload.response)
  89. expect(last_events[2].payload).to eq(third_dropbox_url_payload.response)
  90. end
  91. end
  92. end
  93. end