Skip to content

Commit

Permalink
Test coverage for the OAuthClient methods that make requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Molina committed Oct 23, 2009
1 parent 655ced6 commit 44f0619
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
3 changes: 1 addition & 2 deletions lib/twurl/cli.rb
Expand Up @@ -10,8 +10,7 @@ class CLI
@output ||= STDOUT

class << self
attr_reader :options
attr_accessor :output
attr_accessor :options, :output

def run(args)
options = parse_options(args)
Expand Down
39 changes: 21 additions & 18 deletions lib/twurl/oauth_client.rb
Expand Up @@ -54,6 +54,10 @@ def #{request_method}(url, options = {})
EVAL
end

def perform_request_from_options(options)
send(options.request_method, options.path, options.data)
end

def exchange_credentials_for_access_token
response = consumer.token_request(:post, consumer.access_token_path, nil, {}, client_auth_parameters)
@token = response[:oauth_token]
Expand Down Expand Up @@ -86,26 +90,25 @@ def to_hash
end
end

private
def consumer
@consumer ||=
begin
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
:site => CLI.options.base_url
)
consumer.http.set_debug_output(STDERR) if CLI.options.trace
if CLI.options.ssl?
consumer.http.use_ssl = true
consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
consumer
def consumer
@consumer ||=
begin
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
:site => CLI.options.base_url
)
consumer.http.set_debug_output(STDERR) if CLI.options.trace
if CLI.options.ssl?
consumer.http.use_ssl = true
consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
consumer
end
end

def access_token
@access_token ||= OAuth::AccessToken.new(consumer, token, secret)
end
def access_token
@access_token ||= OAuth::AccessToken.new(consumer, token, secret)
end
end
end
2 changes: 1 addition & 1 deletion lib/twurl/request_controller.rb
Expand Up @@ -8,7 +8,7 @@ def dispatch
end

def perform_request_from_options(options)
response = client.send(options.request_method, options.path, options.data)
response = client.perform_request_from_options(options)
CLI.puts response.body
end
end
Expand Down
49 changes: 43 additions & 6 deletions test/oauth_client_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/test_helper'

class Twurl::OAuthClient::AbstractRCFileLoadingTest < Test::Unit::TestCase
class Twurl::OAuthClient::AbstractOAuthClientTest < Test::Unit::TestCase
attr_reader :client, :options
def setup
Twurl::OAuthClient.instance_variable_set(:@rcfile, nil)
Expand All @@ -11,10 +11,17 @@ def setup
options.password = client.password
options.consumer_key = client.consumer_key
options.consumer_secret = client.consumer_secret
options.base_url = 'api.twitter.com'
options.request_method = 'get'
options.path = '/path/does/not/matter.xml'
options.data = {}

Twurl::CLI.options = options
end

def teardown
super
Twurl::CLI.options = nil
# Make sure we don't do any disk IO in these tests
assert !File.exists?(Twurl::RCFile.file_path)
end
Expand All @@ -24,7 +31,7 @@ def test_nothing
end
end

class Twurl::OAuthClient::BasicRCFileLoadingTest < Twurl::OAuthClient::AbstractRCFileLoadingTest
class Twurl::OAuthClient::BasicRCFileLoadingTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_rcfile_is_memoized
mock.proxy(Twurl::RCFile).new.times(1)

Expand All @@ -41,7 +48,7 @@ def test_forced_reloading
end
end

class Twurl::OAuthClient::ClientLoadingFromOptionsTest < Twurl::OAuthClient::AbstractRCFileLoadingTest
class Twurl::OAuthClient::ClientLoadingFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_if_username_is_supplied_and_no_profile_exists_for_username_then_new_client_is_created
mock(Twurl::OAuthClient).load_client_for_username(options.username).never
mock(Twurl::OAuthClient).load_new_client_from_options(options).times(1)
Expand Down Expand Up @@ -72,7 +79,7 @@ def test_if_username_is_not_provided_then_the_default_client_is_loaded
end
end

class Twurl::OAuthClient::ClientLoadingForUsernameTest < Twurl::OAuthClient::AbstractRCFileLoadingTest
class Twurl::OAuthClient::ClientLoadingForUsernameTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_attempting_to_load_a_username_that_is_not_in_the_file_fails
assert_nil Twurl::OAuthClient.rcfile[client.username]

Expand All @@ -91,7 +98,7 @@ def test_loading_a_username_that_exists
end
end

class Twurl::OAuthClient::DefaultClientLoadingTest < Twurl::OAuthClient::AbstractRCFileLoadingTest
class Twurl::OAuthClient::DefaultClientLoadingTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_loading_default_client_when_there_is_none_fails
assert_nil Twurl::OAuthClient.rcfile.default_profile

Expand All @@ -112,7 +119,7 @@ def test_loading_default_client_from_file
end
end

class Twurl::OAuthClient::NewClientLoadingFromOptionsTest < Twurl::OAuthClient::AbstractRCFileLoadingTest
class Twurl::OAuthClient::NewClientLoadingFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
attr_reader :new_client
def setup
super
Expand All @@ -127,3 +134,33 @@ def test_oauth_options_are_passed_through
assert_equal client.to_hash, new_client.to_hash
end
end

class Twurl::OAuthClient::PerformingRequestsFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_request_is_made_using_request_method_and_path_and_data_in_options
client = Twurl::OAuthClient.test_exemplar
mock(client).get(options.path, options.data)

client.perform_request_from_options(options)
end
end

class Twurl::OAuthClient::CredentialsForAccessTokenExchangeTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_successful_exchange_parses_token_and_secret_from_response_body
parsed_response = {:oauth_token => "123456789",
:oauth_token_secret => "abcdefghi",
:user_id => "3191321",
:screen_name => "noradio",
:x_auth_expires => "0"}

mock(client.consumer).
token_request(:post,
client.consumer.access_token_path,
nil,
{},
client.client_auth_parameters) { parsed_response }

assert client.needs_to_authorize?
client.exchange_credentials_for_access_token
assert !client.needs_to_authorize?
end
end

0 comments on commit 44f0619

Please sign in to comment.