diff --git a/app/models/agents/google_calendar_publish_agent.rb b/app/models/agents/google_calendar_publish_agent.rb index 663add1427..0bc81d3133 100644 --- a/app/models/agents/google_calendar_publish_agent.rb +++ b/app/models/agents/google_calendar_publish_agent.rb @@ -31,7 +31,7 @@ class GoogleCalendarPublishAgent < Agent `google` `service_account_email` - The authorised service account. - `google` `key_file` - The path to the key file. + `google` `key_file` OR `google` `key` - The path to the key file or the key itself. Liquid formatting is supported if you want to use a Credential. (E.g., `{% credential google_key %}`) `google` `key_secret` - The secret for the key, typically 'notasecret' @@ -91,7 +91,7 @@ def default_options def receive(incoming_events) incoming_events.each do |event| - calendar = GoogleCalendar.new(options, Rails.logger) + calendar = GoogleCalendar.new(interpolate_options(options, event), Rails.logger) calendar_event = JSON.parse(calendar.publish_as(interpolated(event)['calendar_id'], event.payload["message"]).response.body) diff --git a/lib/google_calendar.rb b/lib/google_calendar.rb index e840e4f7cf..81a5f2e09d 100644 --- a/lib/google_calendar.rb +++ b/lib/google_calendar.rb @@ -1,7 +1,13 @@ class GoogleCalendar def initialize(config, logger) @config = config - @key = Google::APIClient::PKCS12.load_key(@config['google']['key_file'], @config['google']['key_secret']) + + if @config['google']['key'].present? + @key = OpenSSL::PKCS12.new(@config['google']['key'], @config['google']['key_secret']).key + else + @key = Google::APIClient::PKCS12.load_key(@config['google']['key_file'], @config['google']['key_secret']) + end + @client = Google::APIClient.new(application_name: "Huginn", application_version: "0.0.1") @client.retries = 2 @logger ||= logger @@ -60,4 +66,4 @@ def events_as(who, date) @logger.debug ret.to_yaml ret end -end \ No newline at end of file +end diff --git a/spec/models/agents/google_calendar_publish_agent_spec.rb b/spec/models/agents/google_calendar_publish_agent_spec.rb index a791c4a770..7ca4f02c44 100644 --- a/spec/models/agents/google_calendar_publish_agent_spec.rb +++ b/spec/models/agents/google_calendar_publish_agent_spec.rb @@ -70,14 +70,16 @@ }.to_json end - before do + def setup_mock! fake_interface = Object.new - mock(GoogleCalendar).new(agent.options, Rails.logger) { fake_interface } + mock(GoogleCalendar).new(agent.interpolate_options(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 + setup_mock! + expect { agent.receive([event]) }.to change { agent.events.count }.by(1) @@ -88,6 +90,8 @@ describe 'with Liquid templating' do it 'should allow Liquid in the calendar_id' do + setup_mock! + agent.options['calendar_id'] = '{{ cal_id }}' agent.save! @@ -99,6 +103,22 @@ 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 + + it 'should allow Liquid in the key' do + agent.options['google'].delete('key_file') + agent.options['google']['key'] = '{% credential google_key %}' + agent.save! + + users(:jane).user_credentials.create! credential_name: 'google_key', credential_value: 'something' + + agent.reload + + setup_mock! + + agent.receive([event]) + + expect(agent.events.count).to eq(1) + end end end end