google_calendar_publish_agent_spec.rb 6.9 KB

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