google_calendar_publish_agent_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. require 'rails_helper'
  2. describe Agents::GoogleCalendarPublishAgent, :vcr do
  3. let(:valid_params) do
  4. {
  5. 'expected_update_period_in_days' => "10",
  6. 'calendar_id' => calendar_id,
  7. 'google' => {
  8. 'key_file' => File.dirname(__FILE__) + '/../../data_fixtures/private.key',
  9. 'key_secret' => 'notasecret',
  10. 'service_account_email' => '1029936966326-ncjd7776pcspc98hsg82gsb56t3217ef@developer.gserviceaccount.com'
  11. }
  12. }
  13. end
  14. let(:agent) do
  15. _agent = Agents::GoogleCalendarPublishAgent.new(name: "somename", options: valid_params)
  16. _agent.user = users(:jane)
  17. _agent.save!
  18. _agent
  19. end
  20. describe '#receive' do
  21. let(:event) do
  22. _event = Event.new
  23. _event.agent = agents(:bob_manual_event_agent)
  24. _event.payload = { 'message' => message }
  25. _event.save!
  26. _event
  27. end
  28. let(:calendar_id) { 'sqv39gj35tc837gdns1g4d81cg@group.calendar.google.com' }
  29. let(:message) do
  30. {
  31. 'visibility' => 'default',
  32. 'summary' => "Awesome event",
  33. 'description' => "An example event with text. Pro tip: DateTimes are in RFC3339",
  34. 'end' => {
  35. 'dateTime' => '2014-10-02T11:00:00-05:00'
  36. },
  37. 'start' => {
  38. 'dateTime' => '2014-10-02T10:00:00-05:00'
  39. }
  40. }
  41. end
  42. let(:response_body) do
  43. {"kind"=>"calendar#event",
  44. "etag"=>"\"2908684044040000\"",
  45. "id"=>"baz",
  46. "status"=>"confirmed",
  47. "htmlLink"=>
  48. "https://calendar.google.com/calendar/event?eid=foobar",
  49. "created"=>"2016-02-01T15:53:41.000Z",
  50. "updated"=>"2016-02-01T15:53:42.020Z",
  51. "summary"=>"Awesome event",
  52. "description"=>
  53. "An example event with text. Pro tip: DateTimes are in RFC3339",
  54. "creator"=>
  55. {"email"=>
  56. "blah-foobar@developer.gserviceaccount.com"},
  57. "organizer"=>
  58. {"email"=>calendar_id,
  59. "displayName"=>"Huginn Location Log",
  60. "self"=>true},
  61. "start"=>{"dateTime"=>"2014-10-03T00:30:00+09:30"},
  62. "end"=>{"dateTime"=>"2014-10-03T01:30:00+09:30"},
  63. "iCalUID"=>"blah@google.com",
  64. "sequence"=>0,
  65. "reminders"=>{"useDefault"=>true}
  66. }.to_json
  67. end
  68. def setup_mock!
  69. fake_interface = Object.new
  70. mock(GoogleCalendar).new(agent.interpolate_options(agent.options), Rails.logger) { fake_interface }
  71. mock(fake_interface).publish_as(calendar_id, message) { stub!.response.stub!.body { response_body } }
  72. end
  73. describe 'when the calendar_id is in the options' do
  74. it 'should publish any payload it receives' do
  75. setup_mock!
  76. expect {
  77. agent.receive([event])
  78. }.to change { agent.events.count }.by(1)
  79. expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => JSON.parse(response_body), "agent_id" => event.agent_id, "event_id" => event.id })
  80. end
  81. end
  82. describe 'with Liquid templating' do
  83. it 'should allow Liquid in the calendar_id' do
  84. setup_mock!
  85. agent.options['calendar_id'] = '{{ cal_id }}'
  86. agent.save!
  87. event.payload['cal_id'] = calendar_id
  88. event.save!
  89. agent.receive([event])
  90. expect(agent.events.count).to eq(1)
  91. expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => JSON.parse(response_body), "agent_id" => event.agent_id, "event_id" => event.id })
  92. end
  93. it 'should allow Liquid in the key' do
  94. agent.options['google'].delete('key_file')
  95. agent.options['google']['key'] = '{% credential google_key %}'
  96. agent.save!
  97. users(:jane).user_credentials.create! credential_name: 'google_key', credential_value: 'something'
  98. agent.reload
  99. setup_mock!
  100. agent.receive([event])
  101. expect(agent.events.count).to eq(1)
  102. end
  103. end
  104. end
  105. end