Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The GoogleCalendarPublishAgent now accepts Liquid in the calendar_id …
…option
- Loading branch information
Showing
2 changed files
with
93 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 90 additions & 29 deletions
119
spec/models/agents/google_calendar_publish_agent_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,104 @@ | ||
require 'rails_helper' | ||
|
||
describe Agents::GoogleCalendarPublishAgent, :vcr do | ||
before do | ||
@valid_params = { | ||
'expected_update_period_in_days' => "10", | ||
'calendar_id' => 'sqv39gj35tc837gdns1g4d81cg@group.calendar.google.com', | ||
'google' => { | ||
'key_file' => File.dirname(__FILE__) + '/../../data_fixtures/private.key', | ||
'key_secret' => 'notasecret', | ||
'service_account_email' => '1029936966326-ncjd7776pcspc98hsg82gsb56t3217ef@developer.gserviceaccount.com' | ||
} | ||
let(:valid_params) do | ||
{ | ||
'expected_update_period_in_days' => "10", | ||
'calendar_id' => calendar_id, | ||
'google' => { | ||
'key_file' => File.dirname(__FILE__) + '/../../data_fixtures/private.key', | ||
'key_secret' => 'notasecret', | ||
'service_account_email' => '1029936966326-ncjd7776pcspc98hsg82gsb56t3217ef@developer.gserviceaccount.com' | ||
} | ||
@checker = Agents::GoogleCalendarPublishAgent.new(:name => "somename", :options => @valid_params) | ||
@checker.user = users(:jane) | ||
@checker.save! | ||
} | ||
end | ||
|
||
let(:agent) do | ||
_agent = Agents::GoogleCalendarPublishAgent.new(name: "somename", options: valid_params) | ||
_agent.user = users(:jane) | ||
_agent.save! | ||
_agent | ||
end | ||
|
||
describe '#receive' do | ||
it 'should publish any payload it receives' do | ||
event1 = Event.new | ||
event1.agent = agents(:bob_manual_event_agent) | ||
event1.payload = { | ||
'message' => { | ||
'visibility' => 'default', | ||
'summary' => "Awesome event", | ||
'description' => "An example event with text. Pro tip: DateTimes are in RFC3339", | ||
'end' => { | ||
'dateTime' => '2014-10-02T11:00:00-05:00' | ||
}, | ||
'start' => { | ||
'dateTime' => '2014-10-02T10:00:00-05:00' | ||
} | ||
let(:event) do | ||
_event = Event.new | ||
_event.agent = agents(:bob_manual_event_agent) | ||
_event.payload = { 'message' => message } | ||
_event.save! | ||
_event | ||
end | ||
|
||
let(:calendar_id) { 'sqv39gj35tc837gdns1g4d81cg@group.calendar.google.com' } | ||
let(:message) do | ||
{ | ||
'visibility' => 'default', | ||
'summary' => "Awesome event", | ||
'description' => "An example event with text. Pro tip: DateTimes are in RFC3339", | ||
'end' => { | ||
'dateTime' => '2014-10-02T11:00:00-05:00' | ||
}, | ||
'start' => { | ||
'dateTime' => '2014-10-02T10:00:00-05:00' | ||
} | ||
} | ||
event1.save! | ||
end | ||
let(:response_body) do | ||
{"kind"=>"calendar#event", | ||
"etag"=>"\"2908684044040000\"", | ||
"id"=>"baz", | ||
"status"=>"confirmed", | ||
"htmlLink"=> | ||
"https://calendar.google.com/calendar/event?eid=foobar", | ||
"created"=>"2016-02-01T15:53:41.000Z", | ||
"updated"=>"2016-02-01T15:53:42.020Z", | ||
"summary"=>"Awesome event", | ||
"description"=> | ||
"An example event with text. Pro tip: DateTimes are in RFC3339", | ||
"creator"=> | ||
{"email"=> | ||
"blah-foobar@developer.gserviceaccount.com"}, | ||
"organizer"=> | ||
{"email"=>calendar_id, | ||
"displayName"=>"Huginn Location Log", | ||
"self"=>true}, | ||
"start"=>{"dateTime"=>"2014-10-03T00:30:00+09:30"}, | ||
"end"=>{"dateTime"=>"2014-10-03T01:30:00+09:30"}, | ||
"iCalUID"=>"blah@google.com", | ||
"sequence"=>0, | ||
"reminders"=>{"useDefault"=>true} | ||
}.to_json | ||
end | ||
|
||
before do | ||
fake_interface = Object.new | ||
mock(GoogleCalendar).new(agent.options, Rails.logger) { fake_interface } | ||
mock(fake_interface).publish_as(calendar_id, message) { stub!.response.stub!.body { response_body } } | ||
end | ||
|
||
describe 'when the calendar_id is in the options' do | ||
it 'should publish any payload it receives' do | ||
expect { | ||
agent.receive([event]) | ||
}.to change { agent.events.count }.by(1) | ||
|
||
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 }) | ||
end | ||
end | ||
|
||
describe 'with Liquid templating' do | ||
it 'should allow Liquid in the calendar_id' do | ||
agent.options['calendar_id'] = '{{ cal_id }}' | ||
agent.save! | ||
|
||
event.payload['cal_id'] = calendar_id | ||
event.save! | ||
|
||
@checker.receive([event1]) | ||
agent.receive([event]) | ||
|
||
expect(@checker.events.count).to eq(1) | ||
expect(agent.events.count).to eq(1) | ||
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 }) | ||
end | ||
end | ||
end | ||
end |