tumblr_publish_agent_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require 'rails_helper'
  2. describe Agents::TumblrPublishAgent do
  3. describe "Should create post" do
  4. before do
  5. @opts = {
  6. :blog_name => "huginnbot.tumblr.com",
  7. :post_type => "text",
  8. :expected_update_period_in_days => "2",
  9. :options => {
  10. :title => "{{title}}",
  11. :body => "{{body}}",
  12. },
  13. }
  14. @checker = Agents::TumblrPublishAgent.new(:name => "HuginnBot", :options => @opts)
  15. @checker.service = services(:generic)
  16. @checker.user = users(:bob)
  17. @checker.save!
  18. @event = Event.new
  19. @event.agent = agents(:bob_weather_agent)
  20. @event.payload = { :title => "Gonna rain...", :body => 'San Francisco is gonna get wet' }
  21. @event.save!
  22. @post_body = {
  23. "id" => 5,
  24. "title" => "Gonna rain...",
  25. "link" => "http://huginnbot.tumblr.com/gonna-rain..."
  26. }
  27. allow_any_instance_of(Agents::TumblrPublishAgent).to receive(:tumblr) {
  28. double.tap { |obj|
  29. allow(obj).to receive(:text).with(anything, anything) { { "id" => "5" } }
  30. allow(obj).to receive(:posts).with("huginnbot.tumblr.com", { id: "5" }) { {"posts" => [@post_body]} }
  31. }
  32. }
  33. end
  34. describe '#receive' do
  35. it 'should publish any payload it receives' do
  36. Agents::TumblrPublishAgent.async_receive(@checker.id, [@event.id])
  37. expect(@checker.events.count).to eq(1)
  38. expect(@checker.events.first.payload['post_id']).to eq('5')
  39. expect(@checker.events.first.payload['published_post']).to eq('[huginnbot.tumblr.com] text')
  40. expect(@checker.events.first.payload["post"]).to eq @post_body
  41. end
  42. end
  43. end
  44. describe "Should handle tumblr error" do
  45. before do
  46. @opts = {
  47. :blog_name => "huginnbot.tumblr.com",
  48. :post_type => "text",
  49. :expected_update_period_in_days => "2",
  50. :options => {
  51. :title => "{{title}}",
  52. :body => "{{body}}",
  53. },
  54. }
  55. @checker = Agents::TumblrPublishAgent.new(:name => "HuginnBot", :options => @opts)
  56. @checker.service = services(:generic)
  57. @checker.user = users(:bob)
  58. @checker.save!
  59. @event = Event.new
  60. @event.agent = agents(:bob_weather_agent)
  61. @event.payload = { :title => "Gonna rain...", :body => 'San Francisco is gonna get wet' }
  62. @event.save!
  63. allow_any_instance_of(Agents::TumblrPublishAgent).to receive(:tumblr) {
  64. double.tap { |obj|
  65. allow(obj).to receive(:text).with(anything, anything) { {"status" => 401,"msg" => "Not Authorized"} }
  66. }
  67. }
  68. end
  69. describe '#receive' do
  70. it 'should publish any payload it receives and handle error' do
  71. Agents::TumblrPublishAgent.async_receive(@checker.id, [@event.id])
  72. expect(@checker.events.count).to eq(0)
  73. end
  74. end
  75. end
  76. end