tumblr_publish_agent_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. stub.any_instance_of(Agents::TumblrPublishAgent).tumblr {
  28. obj = Object.new
  29. stub(obj).text(anything, anything) { { "id" => "5" } }
  30. stub(obj).posts("huginnbot.tumblr.com", {:id => "5"}) {
  31. {"posts" => [@post_body]}
  32. }
  33. }
  34. end
  35. describe '#receive' do
  36. it 'should publish any payload it receives' do
  37. Agents::TumblrPublishAgent.async_receive(@checker.id, [@event.id])
  38. expect(@checker.events.count).to eq(1)
  39. expect(@checker.events.first.payload['post_id']).to eq('5')
  40. expect(@checker.events.first.payload['published_post']).to eq('[huginnbot.tumblr.com] text')
  41. expect(@checker.events.first.payload["post"]).to eq @post_body
  42. end
  43. end
  44. end
  45. describe "Should handle tumblr error" do
  46. before do
  47. @opts = {
  48. :blog_name => "huginnbot.tumblr.com",
  49. :post_type => "text",
  50. :expected_update_period_in_days => "2",
  51. :options => {
  52. :title => "{{title}}",
  53. :body => "{{body}}",
  54. },
  55. }
  56. @checker = Agents::TumblrPublishAgent.new(:name => "HuginnBot", :options => @opts)
  57. @checker.service = services(:generic)
  58. @checker.user = users(:bob)
  59. @checker.save!
  60. @event = Event.new
  61. @event.agent = agents(:bob_weather_agent)
  62. @event.payload = { :title => "Gonna rain...", :body => 'San Francisco is gonna get wet' }
  63. @event.save!
  64. stub.any_instance_of(Agents::TumblrPublishAgent).tumblr {
  65. stub!.text(anything, anything) { {"status" => 401,"msg" => "Not Authorized"} }
  66. }
  67. end
  68. describe '#receive' do
  69. it 'should publish any payload it receives and handle error' do
  70. Agents::TumblrPublishAgent.async_receive(@checker.id, [@event.id])
  71. expect(@checker.events.count).to eq(0)
  72. end
  73. end
  74. end
  75. end