Fix RuboCop offenses

This commit is contained in:
Erik Michaels-Ober 2014-03-24 11:00:08 +01:00
parent 595ab454c1
commit c19aab01f8
4 changed files with 116 additions and 120 deletions

View file

@ -28,19 +28,18 @@ module SimpleOAuth
def escape(value) def escape(value)
uri_parser.escape(value.to_s, /[^a-z0-9\-\.\_\~]/i) uri_parser.escape(value.to_s, /[^a-z0-9\-\.\_\~]/i)
end end
alias encode escape alias_method :encode, :escape
def unescape(value) def unescape(value)
uri_parser.unescape(value.to_s) uri_parser.unescape(value.to_s)
end end
alias decode unescape alias_method :decode, :unescape
private private
def uri_parser def uri_parser
@uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
end end
end end
def initialize(method, url, params, oauth = {}) def initialize(method, url, params, oauth = {})
@ -78,11 +77,11 @@ module SimpleOAuth
private private
def normalized_attributes def normalized_attributes
signed_attributes.sort_by{|k,v| k.to_s }.map{|k,v| %(#{k}="#{self.class.escape(v)}") }.join(', ') signed_attributes.sort_by { |k, v| k.to_s }.collect { |k, v| %(#{k}="#{self.class.escape(v)}") }.join(', ')
end end
def attributes def attributes
ATTRIBUTE_KEYS.inject({}){|a,k| options[k] ? a.merge(:"oauth_#{k}" => options[k]) : a } ATTRIBUTE_KEYS.inject({}) { |a, e| options[e] ? a.merge(:"oauth_#{e}" => options[e]) : a }
end end
def signature def signature
@ -94,16 +93,16 @@ module SimpleOAuth
end end
def secret def secret
options.values_at(:consumer_secret, :token_secret).map{|v| self.class.escape(v) }.join('&') options.values_at(:consumer_secret, :token_secret).collect { |v| self.class.escape(v) }.join('&')
end end
alias_method :plaintext_signature, :secret alias_method :plaintext_signature, :secret
def signature_base def signature_base
[method, url, normalized_params].map{|v| self.class.escape(v) }.join('&') [method, url, normalized_params].collect { |v| self.class.escape(v) }.join('&')
end end
def normalized_params def normalized_params
signature_params.map{|p| p.map{|v| self.class.escape(v) } }.sort.map{|p| p.join('=') }.join('&') signature_params.collect { |p| p.collect { |v| self.class.escape(v) } }.sort.collect { |p| p.join('=') }.join('&')
end end
def signature_params def signature_params
@ -111,7 +110,7 @@ module SimpleOAuth
end end
def url_params def url_params
CGI.parse(@uri.query || '').inject([]){|p,(k,vs)| p + vs.sort.map{|v| [k, v] } } CGI.parse(@uri.query || '').inject([]) { |p, (k, vs)| p + vs.sort.collect { |v| [k, v] } }
end end
def rsa_sha1_signature def rsa_sha1_signature
@ -121,6 +120,5 @@ module SimpleOAuth
def private_key def private_key
OpenSSL::PKey::RSA.new(options[:consumer_secret]) OpenSSL::PKey::RSA.new(options[:consumer_secret])
end end
end end
end end

View file

@ -1,18 +1,16 @@
# encoding: utf-8
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.0' spec.add_development_dependency 'bundler', '~> 1.0'
spec.name = 'simple_oauth' spec.name = 'simple_oauth'
spec.version = '0.2.0' spec.version = '0.2.0'
spec.authors = ["Steve Richert", "Erik Michaels-Ober"] spec.authors = ['Steve Richert', 'Erik Michaels-Ober']
spec.email = ['steve.richert@gmail.com', 'sferik@gmail.com'] spec.email = %w[steve.richert@gmail.com sferik@gmail.com]
spec.description = 'Simply builds and verifies OAuth headers' spec.description = 'Simply builds and verifies OAuth headers'
spec.summary = spec.description spec.summary = spec.description
spec.homepage = 'https://github.com/laserlemon/simple_oauth' spec.homepage = 'https://github.com/laserlemon/simple_oauth'
spec.licenses = ['MIT'] spec.licenses = %w[MIT]
spec.files = `git ls-files`.split($\) spec.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
spec.test_files = spec.files.grep(/^test\//) spec.test_files = spec.files.grep(/^test\//)
spec.require_paths = ["lib"] spec.require_paths = %w[lib]
end end

View file

@ -3,27 +3,27 @@
require 'helper' require 'helper'
describe SimpleOAuth::Header do describe SimpleOAuth::Header do
describe ".default_options" do describe '.default_options' do
let(:default_options) { SimpleOAuth::Header.default_options } let(:default_options) { SimpleOAuth::Header.default_options }
it "is different every time" do it 'is different every time' do
expect(SimpleOAuth::Header.default_options).not_to eq default_options expect(SimpleOAuth::Header.default_options).not_to eq default_options
end end
it "is used for new headers" do it 'is used for new headers' do
allow(SimpleOAuth::Header).to receive(:default_options).and_return(default_options) allow(SimpleOAuth::Header).to receive(:default_options).and_return(default_options)
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {})
expect(header.options).to eq default_options expect(header.options).to eq default_options
end end
it "includes a signature method and an OAuth version" do it 'includes a signature method and an OAuth version' do
expect(default_options[:signature_method]).not_to be_nil expect(default_options[:signature_method]).not_to be_nil
expect(default_options[:version]).not_to be_nil expect(default_options[:version]).not_to be_nil
end end
end end
describe ".escape" do describe '.escape' do
it "escapes (most) non-word characters" do it 'escapes (most) non-word characters' do
[' ', '!', '@', '#', '$', '%', '^', '&'].each do |character| [' ', '!', '@', '#', '$', '%', '^', '&'].each do |character|
escaped = SimpleOAuth::Header.escape(character) escaped = SimpleOAuth::Header.escape(character)
expect(escaped).not_to eq character expect(escaped).not_to eq character
@ -31,7 +31,7 @@ describe SimpleOAuth::Header do
end end
end end
it "does not escape - . or ~" do it 'does not escape - . or ~' do
['-', '.', '~'].each do |character| ['-', '.', '~'].each do |character|
escaped = SimpleOAuth::Header.escape(character) escaped = SimpleOAuth::Header.escape(character)
expect(escaped).to eq character expect(escaped).to eq character
@ -39,11 +39,11 @@ describe SimpleOAuth::Header do
end end
def self.test_special_characters def self.test_special_characters
it "escapes non-ASCII characters" do it 'escapes non-ASCII characters' do
expect(SimpleOAuth::Header.escape('é')).to eq '%C3%A9' expect(SimpleOAuth::Header.escape('é')).to eq '%C3%A9'
end end
it "escapes multibyte characters" do it 'escapes multibyte characters' do
expect(SimpleOAuth::Header.escape('あ')).to eq '%E3%81%82' expect(SimpleOAuth::Header.escape('あ')).to eq '%E3%81%82'
end end
end end
@ -51,37 +51,37 @@ describe SimpleOAuth::Header do
if RUBY_VERSION >= '1.9' if RUBY_VERSION >= '1.9'
test_special_characters test_special_characters
else else
%w(n N e E s S u U).each do |kcode| %w[n N e E s S u U].each do |kcode|
describe %(when $KCODE = "#{kcode}") do describe %(when $KCODE = "#{kcode}") do
original_kcode = $KCODE original_kcode = $KCODE # rubocop:disable GlobalVars
begin begin
$KCODE = kcode $KCODE = kcode # rubocop:disable GlobalVars
test_special_characters test_special_characters
ensure ensure
$KCODE = original_kcode $KCODE = original_kcode # rubocop:disable GlobalVars
end end
end end
end end
end end
end end
describe ".unescape" do describe '.unescape' do
pending pending
end end
describe ".parse" do describe '.parse' do
let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}) } let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}) }
let(:parsed_options) { SimpleOAuth::Header.parse(header) } let(:parsed_options) { SimpleOAuth::Header.parse(header) }
it "returns a hash" do it 'returns a hash' do
expect(parsed_options).to be_a(Hash) expect(parsed_options).to be_a(Hash)
end end
it "includes the options used to build the header" do it 'includes the options used to build the header' do
expect(parsed_options.reject { |k, _| k == :signature }).to eq header.options expect(parsed_options.reject { |k, _| k == :signature }).to eq header.options
end end
it "includes a signature" do it 'includes a signature' do
expect(header.options).not_to have_key(:signature) expect(header.options).not_to have_key(:signature)
expect(parsed_options).to have_key(:signature) expect(parsed_options).to have_key(:signature)
expect(parsed_options[:signature]).not_to be_nil expect(parsed_options[:signature]).not_to be_nil
@ -106,25 +106,25 @@ describe SimpleOAuth::Header do
end end
end end
describe "#initialize" do describe '#initialize' do
let(:header) { SimpleOAuth::Header.new(:get, 'HTTPS://api.TWITTER.com:443/1/statuses/friendships.json?foo=bar#anchor', {}) } let(:header) { SimpleOAuth::Header.new(:get, 'HTTPS://api.TWITTER.com:443/1/statuses/friendships.json?foo=bar#anchor', {}) }
it "stringifies and uppercases the request method" do it 'stringifies and uppercases the request method' do
expect(header.method).to eq 'GET' expect(header.method).to eq 'GET'
end end
it "downcases the scheme and authority" do it 'downcases the scheme and authority' do
expect(header.url).to match %r(^https://api\.twitter\.com/) expect(header.url).to match %r{^https://api\.twitter\.com/}
end end
it "ignores the query and fragment" do it 'ignores the query and fragment' do
expect(header.url).to match %r(/1/statuses/friendships\.json$) expect(header.url).to match %r{/1/statuses/friendships\.json$}
end end
end end
describe "#valid?" do describe '#valid?' do
context "using the HMAC-SHA1 signature method" do context 'using the HMAC-SHA1 signature method' do
it "requires consumer and token secrets" do it 'requires consumer and token secrets' do
secrets = {:consumer_secret => 'CONSUMER_SECRET', :token_secret => 'TOKEN_SECRET'} secrets = {:consumer_secret => 'CONSUMER_SECRET', :token_secret => 'TOKEN_SECRET'}
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, secrets) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, secrets)
parsed_header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, header) parsed_header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, header)
@ -133,8 +133,8 @@ describe SimpleOAuth::Header do
end end
end end
context "using the RSA-SHA1 signature method" do context 'using the RSA-SHA1 signature method' do
it "requires an identical private key" do it 'requires an identical private key' do
secrets = {:consumer_secret => rsa_private_key} secrets = {:consumer_secret => rsa_private_key}
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, secrets.merge(:signature_method => 'RSA-SHA1')) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, secrets.merge(:signature_method => 'RSA-SHA1'))
parsed_header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, header) parsed_header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, header)
@ -143,8 +143,8 @@ describe SimpleOAuth::Header do
end end
end end
context "using the RSA-SHA1 signature method" do context 'using the RSA-SHA1 signature method' do
it "requires consumer and token secrets" do it 'requires consumer and token secrets' do
secrets = {:consumer_secret => 'CONSUMER_SECRET', :token_secret => 'TOKEN_SECRET'} secrets = {:consumer_secret => 'CONSUMER_SECRET', :token_secret => 'TOKEN_SECRET'}
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, secrets.merge(:signature_method => 'PLAINTEXT')) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, secrets.merge(:signature_method => 'PLAINTEXT'))
parsed_header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, header) parsed_header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, header)
@ -154,29 +154,29 @@ describe SimpleOAuth::Header do
end end
end end
describe "#normalized_attributes" do describe '#normalized_attributes' do
let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}) } let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}) }
let(:normalized_attributes) { header.send(:normalized_attributes) } let(:normalized_attributes) { header.send(:normalized_attributes) }
it "returns a sorted-key, quoted-value and comma-separated list" do it 'returns a sorted-key, quoted-value and comma-separated list' do
allow(header).to receive(:signed_attributes).and_return({:d => 1, :c => 2, :b => 3, :a => 4}) allow(header).to receive(:signed_attributes).and_return(:d => 1, :c => 2, :b => 3, :a => 4)
expect(normalized_attributes).to eq 'a="4", b="3", c="2", d="1"' expect(normalized_attributes).to eq 'a="4", b="3", c="2", d="1"'
end end
it "URI encodes its values" do it 'URI encodes its values' do
allow(header).to receive(:signed_attributes).and_return({1 => '!', 2 => '@', 3 => '#', 4 => '$'}) allow(header).to receive(:signed_attributes).and_return(1 => '!', 2 => '@', 3 => '#', 4 => '$')
expect(normalized_attributes).to eq '1="%21", 2="%40", 3="%23", 4="%24"' expect(normalized_attributes).to eq '1="%21", 2="%40", 3="%23", 4="%24"'
end end
end end
describe "#signed_attributes" do describe '#signed_attributes' do
it "includes the OAuth signature" do it 'includes the OAuth signature' do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {})
expect(header.send(:signed_attributes)).to have_key(:oauth_signature) expect(header.send(:signed_attributes)).to have_key(:oauth_signature)
end end
end end
describe "#attributes" do describe '#attributes' do
let(:header) do let(:header) do
options = {} options = {}
SimpleOAuth::Header::ATTRIBUTE_KEYS.each { |k| options[k] = k.to_s.upcase } SimpleOAuth::Header::ATTRIBUTE_KEYS.each { |k| options[k] = k.to_s.upcase }
@ -189,32 +189,32 @@ describe SimpleOAuth::Header do
expect(attributes.keys).to be_all { |k| k.to_s =~ /^oauth_/ } expect(attributes.keys).to be_all { |k| k.to_s =~ /^oauth_/ }
end end
it "excludes keys not included in the list of valid attributes" do it 'excludes keys not included in the list of valid attributes' do
expect(attributes.keys).to be_all { |k| k.is_a?(Symbol) } expect(attributes.keys).to be_all { |k| k.is_a?(Symbol) }
expect(attributes).not_to have_key(:oauth_other) expect(attributes).not_to have_key(:oauth_other)
end end
it "preserves values for valid keys" do it 'preserves values for valid keys' do
expect(attributes.size).to eq SimpleOAuth::Header::ATTRIBUTE_KEYS.size expect(attributes.size).to eq SimpleOAuth::Header::ATTRIBUTE_KEYS.size
expect(attributes).to be_all { |k, v| k.to_s == "oauth_#{v.downcase}" } expect(attributes).to be_all { |k, v| k.to_s == "oauth_#{v.downcase}" }
end end
end end
describe "#signature" do describe '#signature' do
context "calls the appropriate signature method" do context 'calls the appropriate signature method' do
specify "when using HMAC-SHA1" do specify 'when using HMAC-SHA1' do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, :signature_method => 'HMAC-SHA1') header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, :signature_method => 'HMAC-SHA1')
expect(header).to receive(:hmac_sha1_signature).once.and_return('HMAC_SHA1_SIGNATURE') expect(header).to receive(:hmac_sha1_signature).once.and_return('HMAC_SHA1_SIGNATURE')
expect(header.send(:signature)).to eq 'HMAC_SHA1_SIGNATURE' expect(header.send(:signature)).to eq 'HMAC_SHA1_SIGNATURE'
end end
specify "when using RSA-SHA1" do specify 'when using RSA-SHA1' do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, :signature_method => 'RSA-SHA1') header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, :signature_method => 'RSA-SHA1')
expect(header).to receive(:rsa_sha1_signature).once.and_return('RSA_SHA1_SIGNATURE') expect(header).to receive(:rsa_sha1_signature).once.and_return('RSA_SHA1_SIGNATURE')
expect(header.send(:signature)).to eq 'RSA_SHA1_SIGNATURE' expect(header.send(:signature)).to eq 'RSA_SHA1_SIGNATURE'
end end
specify "when using PLAINTEXT" do specify 'when using PLAINTEXT' do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, :signature_method => 'PLAINTEXT') header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friends.json', {}, :signature_method => 'PLAINTEXT')
expect(header).to receive(:plaintext_signature).once.and_return('PLAINTEXT_SIGNATURE') expect(header).to receive(:plaintext_signature).once.and_return('PLAINTEXT_SIGNATURE')
expect(header.send(:signature)).to eq 'PLAINTEXT_SIGNATURE' expect(header.send(:signature)).to eq 'PLAINTEXT_SIGNATURE'
@ -222,8 +222,8 @@ describe SimpleOAuth::Header do
end end
end end
describe "#hmac_sha1_signature" do describe '#hmac_sha1_signature' do
it "reproduces a successful Twitter GET" do it 'reproduces a successful Twitter GET' do
options = { options = {
:consumer_key => '8karQBlMg6gFOwcf8kcoYw', :consumer_key => '8karQBlMg6gFOwcf8kcoYw',
:consumer_secret => '3d0vcHyUiiqADpWxolW8nlDIpSWMlyK7YNgc5Qna2M', :consumer_secret => '3d0vcHyUiiqADpWxolW8nlDIpSWMlyK7YNgc5Qna2M',
@ -237,7 +237,7 @@ describe SimpleOAuth::Header do
expect(header.to_s).to eq 'OAuth oauth_consumer_key="8karQBlMg6gFOwcf8kcoYw", oauth_nonce="547fed103e122eecf84c080843eedfe6", oauth_signature="i9CT6ahDRAlfGX3hKYf78QzXsaw%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1286830180", oauth_token="201425800-Sv4sTcgoffmHGkTCue0JnURT8vrm4DiFAkeFNDkh", oauth_version="1.0"' expect(header.to_s).to eq 'OAuth oauth_consumer_key="8karQBlMg6gFOwcf8kcoYw", oauth_nonce="547fed103e122eecf84c080843eedfe6", oauth_signature="i9CT6ahDRAlfGX3hKYf78QzXsaw%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1286830180", oauth_token="201425800-Sv4sTcgoffmHGkTCue0JnURT8vrm4DiFAkeFNDkh", oauth_version="1.0"'
end end
it "reproduces a successful Twitter POST" do it 'reproduces a successful Twitter POST' do
options = { options = {
:consumer_key => '8karQBlMg6gFOwcf8kcoYw', :consumer_key => '8karQBlMg6gFOwcf8kcoYw',
:consumer_secret => '3d0vcHyUiiqADpWxolW8nlDIpSWMlyK7YNgc5Qna2M', :consumer_secret => '3d0vcHyUiiqADpWxolW8nlDIpSWMlyK7YNgc5Qna2M',
@ -252,33 +252,33 @@ describe SimpleOAuth::Header do
end end
end end
describe "#secret" do describe '#secret' do
let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) } let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) }
let(:secret) { header.send(:secret) } let(:secret) { header.send(:secret) }
it "combines the consumer and token secrets with an ampersand" do it 'combines the consumer and token secrets with an ampersand' do
allow(header).to receive(:options).and_return({:consumer_secret => 'CONSUMER_SECRET', :token_secret => 'TOKEN_SECRET'}) allow(header).to receive(:options).and_return(:consumer_secret => 'CONSUMER_SECRET', :token_secret => 'TOKEN_SECRET')
expect(secret).to eq 'CONSUMER_SECRET&TOKEN_SECRET' expect(secret).to eq 'CONSUMER_SECRET&TOKEN_SECRET'
end end
it "URI encodes each secret value before combination" do it 'URI encodes each secret value before combination' do
allow(header).to receive(:options).and_return({:consumer_secret => 'CONSUM#R_SECRET', :token_secret => 'TOKEN_S#CRET'}) allow(header).to receive(:options).and_return(:consumer_secret => 'CONSUM#R_SECRET', :token_secret => 'TOKEN_S#CRET')
expect(secret).to eq 'CONSUM%23R_SECRET&TOKEN_S%23CRET' expect(secret).to eq 'CONSUM%23R_SECRET&TOKEN_S%23CRET'
end end
end end
describe "#signature_base" do describe '#signature_base' do
let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) } let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) }
let(:signature_base) { header.send(:signature_base) } let(:signature_base) { header.send(:signature_base) }
it "combines the request method, URL and normalized parameters using ampersands" do it 'combines the request method, URL and normalized parameters using ampersands' do
allow(header).to receive(:method).and_return('METHOD') allow(header).to receive(:method).and_return('METHOD')
allow(header).to receive(:url).and_return('URL') allow(header).to receive(:url).and_return('URL')
allow(header).to receive(:normalized_params).and_return('NORMALIZED_PARAMS') allow(header).to receive(:normalized_params).and_return('NORMALIZED_PARAMS')
expect(signature_base).to eq 'METHOD&URL&NORMALIZED_PARAMS' expect(signature_base).to eq 'METHOD&URL&NORMALIZED_PARAMS'
end end
it "URI encodes each value before combination" do it 'URI encodes each value before combination' do
allow(header).to receive(:method).and_return('ME#HOD') allow(header).to receive(:method).and_return('ME#HOD')
allow(header).to receive(:url).and_return('U#L') allow(header).to receive(:url).and_return('U#L')
allow(header).to receive(:normalized_params).and_return('NORMAL#ZED_PARAMS') allow(header).to receive(:normalized_params).and_return('NORMAL#ZED_PARAMS')
@ -286,60 +286,60 @@ describe SimpleOAuth::Header do
end end
end end
describe "#normalized_params" do describe '#normalized_params' do
let(:header) do let(:header) do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {})
allow(header).to receive(:signature_params).and_return([['A', '4'], ['B', '3'], ['B', '2'], ['C', '1'], ['D[]', '0 ']]) allow(header).to receive(:signature_params).and_return([%w[A 4], %w[B 3], %w[B 2], %w[C 1], ['D[]', '0 ']])
header header
end end
let(:signature_params) { header.send(:signature_params) } let(:signature_params) { header.send(:signature_params) }
let(:normalized_params) { header.send(:normalized_params) } let(:normalized_params) { header.send(:normalized_params) }
it "joins key/value pairs with equal signs and ampersands" do it 'joins key/value pairs with equal signs and ampersands' do
expect(normalized_params).to be_a(String) expect(normalized_params).to be_a(String)
parts = normalized_params.split('&') parts = normalized_params.split('&')
expect(parts.size).to eq signature_params.size expect(parts.size).to eq signature_params.size
pairs = parts.map{|p| p.split('=') } pairs = parts.collect { |p| p.split('=') }
expect(pairs).to be_all { |p| p.size == 2 } expect(pairs).to be_all { |p| p.size == 2 }
end end
end end
describe "#signature_params" do describe '#signature_params' do
let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) } let(:header) { SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) }
let(:signature_params) { header.send(:signature_params) } let(:signature_params) { header.send(:signature_params) }
it "combines OAuth header attributes, body parameters and URL parameters into an flattened array of key/value pairs" do it 'combines OAuth header attributes, body parameters and URL parameters into an flattened array of key/value pairs' do
allow(header).to receive(:attributes).and_return({:attribute => 'ATTRIBUTE'}) allow(header).to receive(:attributes).and_return(:attribute => 'ATTRIBUTE')
allow(header).to receive(:params).and_return({'param' => 'PARAM'}) allow(header).to receive(:params).and_return('param' => 'PARAM')
allow(header).to receive(:url_params).and_return([['url_param', '1'], ['url_param', '2']]) allow(header).to receive(:url_params).and_return([%w[url_param 1], %w[url_param 2]])
expect(signature_params).to eq [ expect(signature_params).to eq [
[:attribute, 'ATTRIBUTE'], [:attribute, 'ATTRIBUTE'],
['param', 'PARAM'], %w[param PARAM],
['url_param', '1'], %w[url_param 1],
['url_param', '2'] %w[url_param 2]
] ]
end end
end end
describe "#url_params" do describe '#url_params' do
it "returns an empty array when the URL has no query parameters" do it 'returns an empty array when the URL has no query parameters' do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {}) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json', {})
expect(header.send(:url_params)).to eq [] expect(header.send(:url_params)).to eq []
end end
it "returns an array of key/value pairs for each query parameter" do it 'returns an array of key/value pairs for each query parameter' do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json?test=TEST', {}) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json?test=TEST', {})
expect(header.send(:url_params)).to eq [['test', 'TEST']] expect(header.send(:url_params)).to eq [%w[test TEST]]
end end
it "sorts values for repeated keys" do it 'sorts values for repeated keys' do
header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json?test=3&test=1&test=2', {}) header = SimpleOAuth::Header.new(:get, 'https://api.twitter.com/1/statuses/friendships.json?test=3&test=1&test=2', {})
expect(header.send(:url_params)).to eq [['test', '1'], ['test', '2'], ['test', '3']] expect(header.send(:url_params)).to eq [%w[test 1], %w[test 2], %w[test 3]]
end end
end end
describe "#rsa_sha1_signature" do describe '#rsa_sha1_signature' do
it "reproduces a successful OAuth example GET" do it 'reproduces a successful OAuth example GET' do
options = { options = {
:consumer_key => 'dpf43f3p2l4k3l03', :consumer_key => 'dpf43f3p2l4k3l03',
:consumer_secret => rsa_private_key, :consumer_secret => rsa_private_key,
@ -352,12 +352,12 @@ describe SimpleOAuth::Header do
end end
end end
describe "#private_key" do describe '#private_key' do
pending pending
end end
describe "#plaintext_signature" do describe '#plaintext_signature' do
it "reproduces a successful OAuth example GET" do it 'reproduces a successful OAuth example GET' do
options = { options = {
:consumer_key => 'abcd', :consumer_key => 'abcd',
:consumer_secret => 'efgh', :consumer_secret => 'efgh',