google_calendar_publish_agent_spec.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. require 'rails_helper'
  2. describe Agents::GoogleCalendarPublishAgent 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(:message) do
  22. {
  23. 'visibility' => 'default',
  24. 'summary' => "Awesome event",
  25. 'description' => "An example event with text. Pro tip: DateTimes are in RFC3339",
  26. 'end' => {
  27. 'date_time' => '2014-10-02T11:00:00-05:00'
  28. },
  29. 'start' => {
  30. 'date_time' => '2014-10-02T10:00:00-05:00'
  31. }
  32. }
  33. end
  34. let(:event) do
  35. _event = Event.new
  36. _event.agent = agents(:bob_manual_event_agent)
  37. _event.payload = { 'message' => message }
  38. _event.save!
  39. _event
  40. end
  41. let(:calendar_id) { 'sqv39gj35tc837gdns1g4d81cg@group.calendar.google.com' }
  42. let(:response_hash) do
  43. {"kind"=>"calendar#event",
  44. "etag"=>"\"2908684044040000\"",
  45. "id"=>"baz",
  46. "status"=>"confirmed",
  47. "html_link"=>
  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. "display_name"=>"Huginn Location Log",
  60. "self"=>true},
  61. "start"=>{"date_time"=>"2014-10-03T00:30:00+09:30"},
  62. "end"=>{"date_time"=>"2014-10-03T01:30:00+09:30"},
  63. "i_cal_uid"=>"blah@google.com",
  64. "sequence"=>0,
  65. "reminders"=>{"use_default"=>true}
  66. }
  67. end
  68. def setup_mock!
  69. fake_interface = Object.new
  70. expect(GoogleCalendar).to receive(:new).with(agent.interpolate_options(agent.options), Rails.logger) { fake_interface }
  71. expect(fake_interface).to receive(:publish_as).with(calendar_id, message) { response_hash }
  72. expect(fake_interface).to receive(:cleanup!)
  73. end
  74. describe 'when the calendar_id is in the options' do
  75. it 'should publish any payload it receives' do
  76. setup_mock!
  77. expect {
  78. agent.receive([event])
  79. }.to change { agent.events.count }.by(1)
  80. expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => response_hash, "agent_id" => event.agent_id, "event_id" => event.id })
  81. end
  82. end
  83. describe 'with Liquid templating' do
  84. it 'should allow Liquid in the calendar_id' do
  85. setup_mock!
  86. agent.options['calendar_id'] = '{{ cal_id }}'
  87. agent.save!
  88. event.payload['cal_id'] = calendar_id
  89. event.save!
  90. agent.receive([event])
  91. expect(agent.events.count).to eq(1)
  92. expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => response_hash, "agent_id" => event.agent_id, "event_id" => event.id })
  93. end
  94. it 'should allow Liquid in the key' do
  95. agent.options['google'].delete('key_file')
  96. agent.options['google']['key'] = '{% credential google_key %}'
  97. agent.save!
  98. users(:jane).user_credentials.create! credential_name: 'google_key', credential_value: 'something'
  99. agent.reload
  100. setup_mock!
  101. agent.receive([event])
  102. expect(agent.events.count).to eq(1)
  103. end
  104. end
  105. end
  106. describe '#receive old style event' do
  107. let(:event) do
  108. _event = Event.new
  109. _event.agent = agents(:bob_manual_event_agent)
  110. _event.payload = { 'message' => {
  111. 'visibility' => 'default',
  112. 'summary' => "Awesome event",
  113. 'description' => "An example event with text. Pro tip: DateTimes are in RFC3339",
  114. 'end' => {
  115. 'dateTime' => '2014-10-02T11:00:00-05:00'
  116. },
  117. 'start' => {
  118. 'dateTime' => '2014-10-02T10:00:00-05:00'
  119. }
  120. } }
  121. _event.save!
  122. _event
  123. end
  124. let(:calendar_id) { 'sqv39gj35tc837gdns1g4d81cg@group.calendar.google.com' }
  125. let(:message) do
  126. {
  127. 'visibility' => 'default',
  128. 'summary' => "Awesome event",
  129. 'description' => "An example event with text. Pro tip: DateTimes are in RFC3339",
  130. 'end' => {
  131. 'date_time' => '2014-10-02T11:00:00-05:00'
  132. },
  133. 'start' => {
  134. 'date_time' => '2014-10-02T10:00:00-05:00'
  135. }
  136. }
  137. end
  138. let(:response_hash) do
  139. {"kind"=>"calendar#event",
  140. "etag"=>"\"2908684044040000\"",
  141. "id"=>"baz",
  142. "status"=>"confirmed",
  143. "html_link"=>
  144. "https://calendar.google.com/calendar/event?eid=foobar",
  145. "created"=>"2016-02-01T15:53:41.000Z",
  146. "updated"=>"2016-02-01T15:53:42.020Z",
  147. "summary"=>"Awesome event",
  148. "description"=>
  149. "An example event with text. Pro tip: DateTimes are in RFC3339",
  150. "creator"=>
  151. {"email"=>
  152. "blah-foobar@developer.gserviceaccount.com"},
  153. "organizer"=>
  154. {"email"=>calendar_id,
  155. "display_name"=>"Huginn Location Log",
  156. "self"=>true},
  157. "start"=>{"date_time"=>"2014-10-03T00:30:00+09:30"},
  158. "end"=>{"date_time"=>"2014-10-03T01:30:00+09:30"},
  159. "i_cal_uid"=>"blah@google.com",
  160. "sequence"=>0,
  161. "reminders"=>{"use_default"=>true}
  162. }
  163. end
  164. def setup_mock!
  165. fake_interface = Object.new
  166. expect(GoogleCalendar).to receive(:new).with(agent.interpolate_options(agent.options), Rails.logger) { fake_interface }
  167. expect(fake_interface).to receive(:publish_as).with(calendar_id, message) { response_hash }
  168. expect(fake_interface).to receive(:cleanup!)
  169. end
  170. describe 'when the calendar_id is in the options' do
  171. it 'should publish old style payload it receives' do
  172. setup_mock!
  173. expect {
  174. agent.receive([event])
  175. }.to change { agent.events.count }.by(1)
  176. expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => response_hash, "agent_id" => event.agent_id, "event_id" => event.id })
  177. end
  178. end
  179. end
  180. end