dropbox_file_url_agent_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. stub.proxy(Dropbox::API::Client).new do |api|
  37. stub(api).find('/first/path') { stub(Dropbox::API::File.new({}, nil)).direct_url { first_dropbox_url_payload } }
  38. stub(api).find('/second/path') { stub(Dropbox::API::File.new({}, nil)).direct_url { second_dropbox_url_payload } }
  39. stub(api).find('/third/path') { stub(Dropbox::API::File.new({}, nil)).direct_url { third_dropbox_url_payload } }
  40. end
  41. end
  42. context 'with a single path' do
  43. before(:each) { @event = event(paths: '/first/path') }
  44. it 'creates one event with the temporary dropbox link' do
  45. expect { @agent.receive([@event]) }.to change(Event, :count).by(1)
  46. expect(Event.last.payload).to eq({ 'url' => 'http://dropbox.com/first/path/url' })
  47. end
  48. end
  49. context 'with multiple comma-separated paths' do
  50. before(:each) { @event = event(paths: '/first/path, /second/path, /third/path') }
  51. it 'creates one event with the temporary dropbox link for each path' do
  52. expect { @agent.receive([@event]) }.to change(Event, :count).by(3)
  53. last_events = Event.last(3)
  54. expect(last_events[0].payload['url']).to eq('http://dropbox.com/first/path/url')
  55. expect(last_events[1].payload['url']).to eq('http://dropbox.com/second/path/url')
  56. expect(last_events[2].payload['url']).to eq('http://dropbox.com/third/path/url')
  57. end
  58. end
  59. end
  60. context 'with permanent urls' do
  61. def response_for(url)
  62. Dropbox::API::Object.new({'url' => "https://www.dropbox.com/s/#{url}?dl=0"}, nil)
  63. end
  64. let(:first_dropbox_url_payload) { response_for('/first/path') }
  65. let(:second_dropbox_url_payload) { response_for('/second/path') }
  66. let(:third_dropbox_url_payload) { response_for('/third/path') }
  67. before(:each) do
  68. stub.proxy(Dropbox::API::Client).new do |api|
  69. stub(api).find('/first/path') { stub(Dropbox::API::File.new({}, nil)).share_url { first_dropbox_url_payload } }
  70. stub(api).find('/second/path') { stub(Dropbox::API::File.new({}, nil)).share_url { second_dropbox_url_payload } }
  71. stub(api).find('/third/path') { stub(Dropbox::API::File.new({}, nil)).share_url { third_dropbox_url_payload } }
  72. end
  73. @agent.options['link_type'] = 'permanent'
  74. end
  75. it 'creates one event with a single path' do
  76. expect { @agent.receive([event(paths: '/first/path')]) }.to change(Event, :count).by(1)
  77. expect(Event.last.payload).to eq(first_dropbox_url_payload.response)
  78. end
  79. it 'creates one event with the permanent dropbox link for each path' do
  80. event = event(paths: '/first/path, /second/path, /third/path')
  81. expect { @agent.receive([event]) }.to change(Event, :count).by(3)
  82. last_events = Event.last(3)
  83. expect(last_events[0].payload).to eq(first_dropbox_url_payload.response)
  84. expect(last_events[1].payload).to eq(second_dropbox_url_payload.response)
  85. expect(last_events[2].payload).to eq(third_dropbox_url_payload.response)
  86. end
  87. end
  88. end
  89. end