Skip to content

Commit

Permalink
Allow a key with credential instead of a key_file
Browse files Browse the repository at this point in the history
  • Loading branch information
cantino committed Feb 2, 2016
1 parent d6a0e3d commit 67fa705
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/models/agents/google_calendar_publish_agent.rb
Expand Up @@ -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'
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 8 additions & 2 deletions 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
Expand Down Expand Up @@ -60,4 +66,4 @@ def events_as(who, date)
@logger.debug ret.to_yaml
ret
end
end
end
24 changes: 22 additions & 2 deletions spec/models/agents/google_calendar_publish_agent_spec.rb
Expand Up @@ -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)
Expand All @@ -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!

Expand All @@ -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

0 comments on commit 67fa705

Please sign in to comment.