Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ensure credentials set in Twitter.configure are of a valid type.

This commit adds basic credential validation after the credentials
are set using the Twitter.configure block. If the credentials are
not a string or symbol, a Twitter::Error::ConfigurationError is
raised with a helpful message.
  • Loading branch information...
commit fc152dbe56b99639896bcaaf7fe158659e8c50b9 1 parent 65972c5
@DanKnox DanKnox authored committed
View
21 lib/twitter/configurable.rb
@@ -1,3 +1,5 @@
+require 'twitter/error/configuration_error'
+
module Twitter
module Configurable
attr_writer :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret
@@ -21,8 +23,12 @@ def keys
end
# Convenience method to allow configuration options to be set in a block
+ #
+ # @raise [Twitter::Error::ConfigurationError] Error is raised when supplied
+ # twitter credentials are not a String or Symbol.
def configure
yield self
+ validate_credential_type!
self
end
@@ -61,5 +67,20 @@ def options
Hash[Twitter::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
end
+ # Ensures that all credentials set during configuration are of a
+ # valid type. Valid types are String and Symbol.
+ #
+ # @raise [Twitter::Error::ConfigurationError] Error is raised when
+ # supplied twitter credentials are not a String or Symbol.
+ def validate_credential_type!
+ credentials.each do |credential, value|
+ next if value.nil?
+
+ unless value.is_a?(String) || value.is_a?(Symbol)
+ raise(Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string.")
+ end
+ end
+ end
+
end
end
View
8 lib/twitter/error/configuration_error.rb
@@ -0,0 +1,8 @@
+require 'twitter/error'
+
+module Twitter
+ class Error
+ class ConfigurationError < ::ArgumentError
+ end
+ end
+end
View
24 spec/twitter_spec.rb
@@ -64,6 +64,30 @@
expect(Twitter.instance_variable_get(:"@#{key}")).to eq key
end
end
+
+ context "when invalid credentials are provided" do
+ it "raises a ConfigurationError exception" do
+ expect {
+ Twitter.configure do |config|
+ config.consumer_key = [12345, 54321]
+ config.consumer_secret = 'valid_data'
+ end
+ }.to raise_exception(Twitter::Error::ConfigurationError)
+ end
+ end
+
+ context "when no credentials are provided" do
+ it "does not raise an exception" do
+ expect {
+ Twitter.configure do |config|
+ config.consumer_key = nil
+ config.consumer_secret = nil
+ config.oauth_token = nil
+ config.oauth_token_secret = nil
+ end
+ }.to_not raise_exception(Twitter::Error::ConfigurationError)
+ end
+ end
end
describe ".credentials?" do
Please sign in to comment.
Something went wrong with that request. Please try again.