Fix linkedin initialization

closes CNVS-21340

this initializer was producing the right keys, but not
 with indifferent access, so string access wasn't working and the
linkedin registered service would just die.

Also fixed a problem in the twitter connector which has the same
issue with running a find at initialization time.

TEST PLAN:
 1) linked in as a registered service should not break horribly
 2) twitter as a registered service should not break horribly

Change-Id: Iaa3a5c7f3f0ac5561079a334493d215399d45d76
Reviewed-on: https://gerrit.instructure.com/57186
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Ethan Vizitei <evizitei@instructure.com>
Tested-by: Jenkins
This commit is contained in:
Ethan Vizitei 2015-06-25 08:56:41 -06:00
parent aa349e812a
commit c6887533fe
6 changed files with 137 additions and 91 deletions

View File

@ -1,11 +1,17 @@
LinkedIn::Connection.config = Proc.new do
settings = Canvas::Plugin.find(:linked_in).try(:settings)
if settings
{
api_key: settings[:client_id],
secret_key: settings[:client_secret_dec]
}
else
ConfigFile.load('linked_in')
class CanvasLinkedInConfig
def self.call
settings = Canvas::Plugin.find(:linked_in).try(:settings)
if settings
{
api_key: settings[:client_id],
secret_key: settings[:client_secret_dec]
}.with_indifferent_access
else
ConfigFile.load('linked_in')
end
end
end
LinkedIn::Connection.config = CanvasLinkedInConfig

View File

@ -1,14 +1,11 @@
class CanvasTwitterConfig
def initialize(plugin=Canvas::Plugin.find(:twitter))
@plugin = plugin
end
def call
settings = @plugin.try(:settings)
def self.call
settings = Canvas::Plugin.find(:twitter).try(:settings)
if settings
{
api_key: settings[:consumer_key],
secret_key: settings[:consumer_secret_dec]
api_key: settings[:consumer_key],
secret_key: settings[:consumer_secret_dec]
}.with_indifferent_access
else
ConfigFile.load('twitter')
@ -18,4 +15,4 @@ class CanvasTwitterConfig
end
Twitter::Connection.config = CanvasTwitterConfig.new
Twitter::Connection.config = CanvasTwitterConfig

View File

@ -61,8 +61,8 @@ module LinkedIn
end
def self.config=(config)
if !config.is_a?(Proc)
raise "Config must be a Proc"
unless config.respond_to?(:call)
raise "Config must respond to #call"
end
@config = config
end

View File

@ -19,83 +19,110 @@
require 'spec_helper'
describe LinkedIn::Connection do
before do
config = {
'api_key' => 'key',
'secret_key' => 'secret'
}
LinkedIn::Connection.config = Proc.new do
config
let(:connection) { LinkedIn::Connection.new }
describe ".config" do
it "accepts any object with a call interface" do
conf_class= Class.new do
def call
{'some' => 'config'}
end
end
described_class.config = conf_class.new
expect(described_class.config['some']).to eq('config')
end
it "rejects uncallable configs" do
expect { described_class.config = Object.new }.to(
raise_error(RuntimeError) do |e|
expect(e.message).to match(/must respond to/)
end
)
end
end
let (:connection) {LinkedIn::Connection.new}
context "with valid configuration" do
describe "#get_service_user_info" do
it "returns service user info" do
token_response_body = "<html><id>#1</id><first-name>john</first-name><last-name>doe</last-name><public-profile-url>http://example.com/linkedin</public-profile-url></html>"
mock_access_token = stub()
mock_access_token.expects(:get)
.with('/v1/people/~:(id,first-name,last-name,public-profile-url,picture-url)')
.returns(stub(body: token_response_body))
service_user_id, service_user_name, service_user_url = connection.get_service_user_info(mock_access_token)
service_user_id.should == "#1"
service_user_name.should == "john doe"
service_user_url.should == "http://example.com/linkedin"
end
end
describe "#get_access_token" do
it "builds access token based on the supplied parameters" do
token = mock
secret = mock
oauth_verifier = mock
consumer = mock
request_token = mock
OAuth::Consumer.expects(:new).returns(consumer)
OAuth::RequestToken.expects(:new).with(consumer, token, secret).returns(request_token)
request_token.expects(:get_access_token).with(:oauth_verifier => oauth_verifier)
connection.get_access_token(token, secret, oauth_verifier)
end
end
describe "#request_token" do
it "builds access token based on the supplied parameters" do
consumer = mock
oauth_callback = mock
OAuth::Consumer.expects(:new).returns(consumer)
consumer.expects(:get_request_token).with(:oauth_callback => oauth_callback)
connection.request_token(oauth_callback)
end
end
describe ".consumer" do
it "uses the config values" do
OAuth::Consumer.expects(:new).with('key', 'secret', {
:site => "https://api.linkedin.com",
:request_token_path => "/uas/oauth/requestToken",
:access_token_path => "/uas/oauth/accessToken",
:authorize_path => "/uas/oauth/authorize",
:signature_method => "HMAC-SHA1"
})
LinkedIn::Connection.consumer
before do
config = {
'api_key' => 'key',
'secret_key' => 'secret'
}
LinkedIn::Connection.config = proc{ config }
end
it "user the supplied parameters" do
OAuth::Consumer.expects(:new).with('my_key', 'my_secret', {
:site => "https://api.linkedin.com",
:request_token_path => "/uas/oauth/requestToken",
:access_token_path => "/uas/oauth/accessToken",
:authorize_path => "/uas/oauth/authorize",
:signature_method => "HMAC-SHA1"
})
describe "#get_service_user_info" do
it "returns service user info" do
token_response_body = "<html><id>#1</id>"\
"<first-name>john</first-name>"\
"<last-name>doe</last-name>"\
"<public-profile-url>http://example.com/linkedin</public-profile-url>"\
"</html>"
mock_access_token = stub()
mock_access_token.expects(:get)
.with('/v1/people/~:(id,first-name,last-name,public-profile-url,picture-url)')
.returns(stub(body: token_response_body))
LinkedIn::Connection.consumer('my_key', 'my_secret')
service_user_id, service_user_name, service_user_url = connection.get_service_user_info(mock_access_token)
expect(service_user_id).to eq("#1")
expect(service_user_name).to eq("john doe")
expect(service_user_url).to eq("http://example.com/linkedin")
end
end
describe "#get_access_token" do
it "builds access token based on the supplied parameters" do
token = mock
secret = mock
oauth_verifier = mock
consumer = mock
request_token = mock
OAuth::Consumer.expects(:new).returns(consumer)
OAuth::RequestToken.expects(:new).with(consumer, token, secret).returns(request_token)
request_token.expects(:get_access_token).with(:oauth_verifier => oauth_verifier)
connection.get_access_token(token, secret, oauth_verifier)
end
end
describe "#request_token" do
it "builds access token based on the supplied parameters" do
consumer = mock
oauth_callback = mock
OAuth::Consumer.expects(:new).returns(consumer)
consumer.expects(:get_request_token).with(:oauth_callback => oauth_callback)
connection.request_token(oauth_callback)
end
end
describe ".consumer" do
it "uses the config values" do
OAuth::Consumer.expects(:new).with('key', 'secret', {
:site => "https://api.linkedin.com",
:request_token_path => "/uas/oauth/requestToken",
:access_token_path => "/uas/oauth/accessToken",
:authorize_path => "/uas/oauth/authorize",
:signature_method => "HMAC-SHA1"
})
LinkedIn::Connection.consumer
end
it "user the supplied parameters" do
OAuth::Consumer.expects(:new).with('my_key', 'my_secret', {
:site => "https://api.linkedin.com",
:request_token_path => "/uas/oauth/requestToken",
:access_token_path => "/uas/oauth/accessToken",
:authorize_path => "/uas/oauth/authorize",
:signature_method => "HMAC-SHA1"
})
LinkedIn::Connection.consumer('my_key', 'my_secret')
end
end
end
end

View File

@ -0,0 +1,16 @@
require_relative '../spec_helper'
require_relative '../../config/initializers/linked_in'
describe CanvasLinkedInConfig do
describe ".call" do
it "returns a config with indifferent access" do
plugin = stub(settings: {client_id: "abcdefg", client_secret_dec: "12345"})
Canvas::Plugin.stubs(:find).with(:linked_in).returns(plugin)
output = described_class.call
expect(output['api_key']).to eq("abcdefg")
expect(output[:api_key]).to eq("abcdefg")
end
end
end

View File

@ -6,8 +6,8 @@ describe CanvasTwitterConfig do
describe "#call" do
it "returns a config with indifference access" do
plugin = stub(settings: {consumer_key: "abcdefg", consumer_secret_dec: "12345"})
config = described_class.new(plugin)
output = config.call
Canvas::Plugin.stubs(:find).with(:twitter).returns(plugin)
output = described_class.call
expect(output['api_key']).to eq("abcdefg")
expect(output[:api_key]).to eq("abcdefg")
end