twurl 0.9.1 → 0.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,44 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class Twurl::ConfigurationController::DispatchTest < Minitest::Test
4
- def test_error_message_is_displayed_if_setting_is_unrecognized
5
- options = Twurl::Options.test_exemplar
6
- client = Twurl::OAuthClient.test_exemplar
7
-
8
- options.subcommands = ['unrecognized', 'value']
9
-
10
- mock(Twurl::CLI).puts(Twurl::ConfigurationController::UNRECOGNIZED_SETTING_MESSAGE % 'unrecognized').times(1)
11
- mock(Twurl::OAuthClient.rcfile).save.times(0)
12
-
13
- controller = Twurl::ConfigurationController.new(client, options)
14
- controller.dispatch
15
- end
16
- end
17
-
18
- class Twurl::ConfigurationController::DispatchDefaultSettingTest < Minitest::Test
19
- def test_setting_default_profile_just_by_username
20
- options = Twurl::Options.test_exemplar
21
- client = Twurl::OAuthClient.test_exemplar
22
-
23
- options.subcommands = ['default', client.username]
24
- mock(Twurl::OAuthClient).load_client_for_username(client.username).times(1) { client }
25
- mock(Twurl::OAuthClient.rcfile).default_profile = client
26
- mock(Twurl::OAuthClient.rcfile).save.times(1)
27
-
28
- controller = Twurl::ConfigurationController.new(client, options)
29
- controller.dispatch
30
- end
31
-
32
- def test_setting_default_profile_by_username_and_consumer_key
33
- options = Twurl::Options.test_exemplar
34
- client = Twurl::OAuthClient.test_exemplar
35
-
36
- options.subcommands = ['default', client.username, client.consumer_key]
37
- mock(Twurl::OAuthClient).load_client_for_username_and_consumer_key(client.username, client.consumer_key).times(1) { client }
38
- mock(Twurl::OAuthClient.rcfile).default_profile = client
39
- mock(Twurl::OAuthClient.rcfile).save.times(1)
40
-
41
- controller = Twurl::ConfigurationController.new(client, options)
42
- controller.dispatch
43
- end
44
- end
@@ -1,165 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class Twurl::OAuthClient::AbstractOAuthClientTest < Minitest::Test
4
- attr_reader :client, :options
5
- def setup
6
- Twurl::OAuthClient.instance_variable_set(:@rcfile, nil)
7
-
8
- @options = Twurl::Options.test_exemplar
9
- @client = Twurl::OAuthClient.test_exemplar
10
- options.base_url = 'api.twitter.com'
11
- options.request_method = 'get'
12
- options.path = '/path/does/not/matter.json'
13
- options.data = {}
14
- options.headers = {}
15
-
16
- Twurl.options = options
17
- end
18
-
19
- def teardown
20
- super
21
- Twurl.options = Twurl::Options.new
22
- # Make sure we don't do any disk IO in these tests
23
- assert !File.exists?(Twurl::RCFile.file_path)
24
- end
25
-
26
- def test_nothing
27
- # Appeasing test/unit
28
- end
29
- end
30
-
31
- class Twurl::OAuthClient::BasicRCFileLoadingTest < Twurl::OAuthClient::AbstractOAuthClientTest
32
- def test_rcfile_is_memoized
33
- mock.proxy(Twurl::RCFile).new.times(1)
34
-
35
- Twurl::OAuthClient.rcfile
36
- Twurl::OAuthClient.rcfile
37
- end
38
-
39
- def test_forced_reloading
40
- mock.proxy(Twurl::RCFile).new.times(2)
41
-
42
- Twurl::OAuthClient.rcfile
43
- Twurl::OAuthClient.rcfile(:reload)
44
- Twurl::OAuthClient.rcfile
45
- end
46
- end
47
-
48
- class Twurl::OAuthClient::ClientLoadingFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
49
- def test_if_username_is_supplied_and_no_profile_exists_for_username_then_new_client_is_created
50
- mock(Twurl::OAuthClient).load_client_for_username(options.username).never
51
- mock(Twurl::OAuthClient).load_new_client_from_options(options).times(1)
52
- mock(Twurl::OAuthClient).load_default_client.never
53
-
54
- Twurl::OAuthClient.load_from_options(options)
55
- end
56
-
57
- def test_if_username_is_supplied_and_profile_exists_for_username_then_client_is_loaded
58
- mock(Twurl::OAuthClient.rcfile).save.times(1)
59
- Twurl::OAuthClient.rcfile << client
60
-
61
- mock(Twurl::OAuthClient).load_client_for_username_and_consumer_key(options.username, options.consumer_key).times(1)
62
- mock(Twurl::OAuthClient).load_new_client_from_options(options).never
63
- mock(Twurl::OAuthClient).load_default_client.never
64
-
65
- Twurl::OAuthClient.load_from_options(options)
66
- end
67
-
68
- def test_if_username_is_not_provided_then_the_default_client_is_loaded
69
- options.username = nil
70
-
71
- mock(Twurl::OAuthClient).load_client_for_username(options.username).never
72
- mock(Twurl::OAuthClient).load_new_client_from_options(options).never
73
- mock(Twurl::OAuthClient).load_default_client.times(1)
74
-
75
- Twurl::OAuthClient.load_from_options(options)
76
- end
77
- end
78
-
79
- class Twurl::OAuthClient::ClientLoadingForUsernameTest < Twurl::OAuthClient::AbstractOAuthClientTest
80
- def test_attempting_to_load_a_username_that_is_not_in_the_file_fails
81
- assert_nil Twurl::OAuthClient.rcfile[client.username]
82
-
83
- assert_raises Twurl::Exception do
84
- Twurl::OAuthClient.load_client_for_username_and_consumer_key(client.username, client.consumer_key)
85
- end
86
- end
87
-
88
- def test_loading_a_username_that_exists
89
- mock(Twurl::OAuthClient.rcfile).save.times(1)
90
-
91
- Twurl::OAuthClient.rcfile << client
92
-
93
- client_from_file = Twurl::OAuthClient.load_client_for_username_and_consumer_key(client.username, client.consumer_key)
94
- assert_equal client.to_hash, client_from_file.to_hash
95
- end
96
- end
97
-
98
- class Twurl::OAuthClient::DefaultClientLoadingTest < Twurl::OAuthClient::AbstractOAuthClientTest
99
- def test_loading_default_client_when_there_is_none_fails
100
- assert_nil Twurl::OAuthClient.rcfile.default_profile
101
-
102
- assert_raises Twurl::Exception do
103
- Twurl::OAuthClient.load_default_client
104
- end
105
- end
106
-
107
- def test_loading_default_client_from_file
108
- mock(Twurl::OAuthClient.rcfile).save.times(1)
109
-
110
- Twurl::OAuthClient.rcfile << client
111
- assert_equal [client.username, client.consumer_key], Twurl::OAuthClient.rcfile.default_profile
112
-
113
- client_from_file = Twurl::OAuthClient.load_default_client
114
-
115
- assert_equal client.to_hash, client_from_file.to_hash
116
- end
117
- end
118
-
119
- class Twurl::OAuthClient::NewClientLoadingFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
120
- attr_reader :new_client
121
- def setup
122
- super
123
- @new_client = Twurl::OAuthClient.load_new_client_from_options(options)
124
- end
125
-
126
- def test_password_is_included
127
- assert_equal options.password, new_client.password
128
- end
129
-
130
- def test_oauth_options_are_passed_through
131
- assert_equal client.to_hash, new_client.to_hash
132
- end
133
- end
134
-
135
- class Twurl::OAuthClient::PerformingRequestsFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
136
- def test_request_is_made_using_request_method_and_path_and_data_in_options
137
- client = Twurl::OAuthClient.test_exemplar
138
- mock(client.consumer.http).request(satisfy { |req|
139
- req.is_a?(Net::HTTP::Get) && (req.path == options.path)
140
- })
141
-
142
- client.perform_request_from_options(options)
143
- end
144
- end
145
-
146
- class Twurl::OAuthClient::CredentialsForAccessTokenExchangeTest < Twurl::OAuthClient::AbstractOAuthClientTest
147
- def test_successful_exchange_parses_token_and_secret_from_response_body
148
- parsed_response = {:oauth_token => "123456789",
149
- :oauth_token_secret => "abcdefghi",
150
- :user_id => "3191321",
151
- :screen_name => "noradio",
152
- :x_auth_expires => "0"}
153
-
154
- mock(client.consumer).
155
- token_request(:post,
156
- client.consumer.access_token_path,
157
- nil,
158
- {},
159
- client.client_auth_parameters) { parsed_response }
160
-
161
- assert client.needs_to_authorize?
162
- client.exchange_credentials_for_access_token
163
- assert !client.needs_to_authorize?
164
- end
165
- end
@@ -1,147 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class Twurl::RCFile::PathConstructionTest < Minitest::Test
4
- def test_file_path_appends_file_to_directory
5
- assert_equal File.join(Twurl::RCFile.directory, Twurl::RCFile::FILE), Twurl::RCFile.file_path
6
- end
7
- end
8
-
9
- class Twurl::RCFile::LoadingTest < Minitest::Test
10
- def test_load_parses_and_loads_file_if_it_exists
11
- mock(YAML).load_file(Twurl::RCFile.file_path).times(1)
12
- mock(Twurl::RCFile).default_rcfile_structure.never
13
-
14
- Twurl::RCFile.load
15
- end
16
-
17
- def test_load_returns_default_file_structure_if_file_does_not_exist
18
- mock(YAML).load_file(Twurl::RCFile.file_path) { raise Errno::ENOENT }.times(1)
19
- mock(Twurl::RCFile).default_rcfile_structure.times(1)
20
-
21
- Twurl::RCFile.load
22
- end
23
- end
24
-
25
- class Twurl::RCFile::InitializationTest < Minitest::Test
26
- def test_initializing_when_the_file_does_not_exist_loads_default_rcfile_structure
27
- mock(YAML).load_file(Twurl::RCFile.file_path) { raise Errno::ENOENT }.times(1)
28
-
29
- rcfile = Twurl::RCFile.new
30
- assert_equal Twurl::RCFile.default_rcfile_structure, rcfile.data
31
- end
32
-
33
- def test_initializing_when_the_file_does_exists_loads_content_of_file
34
- mock_content_of_rcfile = {'this data' => 'does not matter'}
35
- mock(YAML).load_file(Twurl::RCFile.file_path) { mock_content_of_rcfile }.times(1)
36
- mock(Twurl::RCFile).default_rcfile_structure.never
37
-
38
- rcfile = Twurl::RCFile.new
39
- assert_equal mock_content_of_rcfile, rcfile.data
40
- end
41
- end
42
-
43
- class Twurl::RCFile::DefaultProfileFromDefaultRCFileTest < Minitest::Test
44
- attr_reader :rcfile
45
- def setup
46
- mock(YAML).load_file(Twurl::RCFile.file_path) { raise Errno::ENOENT }.times(1)
47
- mock.proxy(Twurl::RCFile).default_rcfile_structure.times(any_times)
48
-
49
- @rcfile = Twurl::RCFile.new
50
- end
51
-
52
- def test_default_rcfile_structure_has_no_default_profile
53
- assert_nil rcfile.default_profile
54
- end
55
-
56
- def test_rcfile_is_considered_empty_at_first
57
- assert rcfile.empty?
58
- end
59
-
60
- def test_setting_default_profile
61
- options = Twurl::Options.test_exemplar
62
-
63
- client = Twurl::OAuthClient.load_new_client_from_options(options)
64
- rcfile.default_profile = client
65
- assert_equal [options.username, options.consumer_key], rcfile.default_profile
66
- end
67
- end
68
-
69
- class Twurl::RCFile::UpdatingTest < Minitest::Test
70
- attr_reader :rcfile
71
- def setup
72
- mock(YAML).load_file(Twurl::RCFile.file_path) { raise Errno::ENOENT }.times(1)
73
-
74
- @rcfile = Twurl::RCFile.new
75
- assert rcfile.profiles.empty?
76
- assert_nil rcfile.default_profile
77
- mock(rcfile).save.times(any_times)
78
- end
79
-
80
- def test_adding_the_first_client_sets_it_as_default_profile
81
- client = Twurl::OAuthClient.test_exemplar
82
-
83
- rcfile << client
84
- assert_equal [client.username, client.consumer_key], rcfile.default_profile
85
- assert rcfile.has_oauth_profile_for_username_with_consumer_key?(client.username, client.consumer_key)
86
- assert_equal({client.username => {client.consumer_key => client.to_hash}}, rcfile.profiles)
87
- end
88
-
89
- def test_adding_additional_clients_does_not_change_default_profile
90
- first_client = Twurl::OAuthClient.test_exemplar
91
-
92
- rcfile << first_client
93
- assert_equal [first_client.username, first_client.consumer_key], rcfile.default_profile
94
- assert rcfile.has_oauth_profile_for_username_with_consumer_key?(first_client.username, first_client.consumer_key)
95
-
96
- additional_client = Twurl::OAuthClient.test_exemplar(:username => 'additional_exemplar_username')
97
-
98
- rcfile << additional_client
99
- assert_equal [first_client.username, first_client.consumer_key], rcfile.default_profile
100
- assert rcfile.has_oauth_profile_for_username_with_consumer_key?(additional_client.username, additional_client.consumer_key)
101
-
102
- expected_profiles = {
103
- first_client.username => {first_client.consumer_key => first_client.to_hash},
104
- additional_client.username => {additional_client.consumer_key => additional_client.to_hash}
105
- }
106
-
107
- assert_equal expected_profiles, rcfile.profiles
108
- end
109
- end
110
-
111
- class Twurl::RCFile::SavingTest < Minitest::Test
112
- attr_reader :rcfile
113
- def setup
114
- delete_rcfile
115
- assert !rcfile_exists?
116
- @rcfile = Twurl::RCFile.new
117
- assert !rcfile_exists?
118
- end
119
-
120
- def teardown
121
- super
122
- delete_rcfile
123
- end
124
-
125
- def test_save_writes_profiles_to_disk
126
- client = Twurl::OAuthClient.test_exemplar
127
- rcfile << client
128
- assert rcfile_exists?
129
- end
130
-
131
- def test_file_is_not_world_readable
132
- client = Twurl::OAuthClient.test_exemplar
133
- rcfile << client
134
- assert_equal 33152, File.stat(Twurl::RCFile.file_path).mode
135
- end
136
-
137
- private
138
- def rcfile_exists?
139
- File.exists?(Twurl::RCFile.file_path)
140
- end
141
-
142
- def delete_rcfile
143
- File.unlink(Twurl::RCFile.file_path)
144
- rescue Errno::ENOENT
145
- # Do nothing
146
- end
147
- end
@@ -1,58 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class Twurl::RequestController::AbstractTestCase < Minitest::Test
4
- attr_reader :options, :client, :controller
5
- def setup
6
- Twurl::CLI.output = StringIO.new
7
- @options = Twurl::Options.test_exemplar
8
- @client = Twurl::OAuthClient.test_exemplar
9
- @controller = Twurl::RequestController.new(client, options)
10
- end
11
-
12
- def teardown
13
- super
14
- Twurl::CLI.output = STDOUT
15
- end
16
-
17
- def test_nothing
18
- # Appeasing test/unit
19
- end
20
- end
21
-
22
- class Twurl::RequestController::DispatchTest < Twurl::RequestController::AbstractTestCase
23
- def test_request_will_be_made_if_client_is_authorized
24
- mock(client).needs_to_authorize? { false }.times(1)
25
- mock(controller).perform_request.times(1)
26
-
27
- controller.dispatch
28
- end
29
-
30
- def test_request_will_not_be_made_if_client_is_not_authorized
31
- mock(client).needs_to_authorize? { true }.times(1)
32
- mock(controller).perform_request.never
33
-
34
- assert_raises Twurl::Exception do
35
- controller.dispatch
36
- end
37
- end
38
- end
39
-
40
- class Twurl::RequestController::RequestTest < Twurl::RequestController::AbstractTestCase
41
- def test_request_response_is_written_to_output
42
- expected_body = 'this is a fake response body'
43
- response = Object.new
44
- mock(response).read_body { |block| block.call expected_body }
45
- mock(client).perform_request_from_options(options).times(1) { |options, block| block.call(response) }
46
-
47
- controller.perform_request
48
-
49
- assert_equal expected_body, Twurl::CLI.output.string
50
- end
51
-
52
- def test_invalid_or_unspecified_urls_report_error
53
- mock(Twurl::CLI).puts(Twurl::RequestController::NO_URI_MESSAGE).times(1)
54
- mock(client).perform_request_from_options(options).times(1) { raise URI::InvalidURIError }
55
-
56
- controller.perform_request
57
- end
58
- end
@@ -1,44 +0,0 @@
1
- require 'simplecov'
2
- require 'coveralls'
3
-
4
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
- SimpleCov::Formatter::HTMLFormatter,
6
- Coveralls::SimpleCov::Formatter
7
- ]
8
- SimpleCov.start
9
-
10
- require 'twurl'
11
- require 'minitest/autorun'
12
- require 'rr'
13
-
14
- Twurl::RCFile.directory = ENV['TMPDIR']
15
-
16
- module Twurl
17
- class Options
18
- class << self
19
- def test_exemplar
20
- options = new
21
- options.username = 'exemplar_user_name'
22
- options.password = 'secret'
23
- options.consumer_key = '123456789'
24
- options.consumer_secret = '987654321'
25
- options.subcommands = []
26
- options
27
- end
28
- end
29
- end
30
-
31
- class OAuthClient
32
- class << self
33
- def test_exemplar(overrides = {})
34
- options = Twurl::Options.test_exemplar
35
-
36
- overrides.each do |attribute, value|
37
- options.send("#{attribute}=", value)
38
- end
39
-
40
- load_new_client_from_options(options)
41
- end
42
- end
43
- end
44
- end