twimock 0.0.1

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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +125 -0
  8. data/Rakefile +6 -0
  9. data/db/.gitkeep +0 -0
  10. data/lib/twimock/access_token.rb +31 -0
  11. data/lib/twimock/api/account/verify_credentials.rb +40 -0
  12. data/lib/twimock/api/application.rb +29 -0
  13. data/lib/twimock/api/intent/sessions.rb +60 -0
  14. data/lib/twimock/api/oauth/access_token.rb +65 -0
  15. data/lib/twimock/api/oauth/authenticate.rb +51 -0
  16. data/lib/twimock/api/oauth/request_token.rb +49 -0
  17. data/lib/twimock/api/oauth.rb +83 -0
  18. data/lib/twimock/api.rb +35 -0
  19. data/lib/twimock/application.rb +21 -0
  20. data/lib/twimock/auth_hash.rb +8 -0
  21. data/lib/twimock/config.rb +90 -0
  22. data/lib/twimock/database/table.rb +359 -0
  23. data/lib/twimock/database.rb +133 -0
  24. data/lib/twimock/errors.rb +13 -0
  25. data/lib/twimock/omniauth/strategies/twitter.rb +28 -0
  26. data/lib/twimock/omniauth_twitter.rb +36 -0
  27. data/lib/twimock/request_token.rb +23 -0
  28. data/lib/twimock/user.rb +58 -0
  29. data/lib/twimock/version.rb +3 -0
  30. data/lib/twimock.rb +39 -0
  31. data/spec/spec_helper.rb +18 -0
  32. data/spec/support/api_spec_helper.rb +30 -0
  33. data/spec/support/omniauth_twitter_helper.rb +26 -0
  34. data/spec/support/tables_helper.rb +54 -0
  35. data/spec/support/test_application_helper.rb +9 -0
  36. data/spec/twimock/access_token_spec.rb +128 -0
  37. data/spec/twimock/api/account/verify_credentials_spec.rb +125 -0
  38. data/spec/twimock/api/application_spec.rb +27 -0
  39. data/spec/twimock/api/intent/sessions_spec.rb +184 -0
  40. data/spec/twimock/api/oauth/access_token_spec.rb +185 -0
  41. data/spec/twimock/api/oauth/authenticate_spec.rb +96 -0
  42. data/spec/twimock/api/oauth/request_token_spec.rb +123 -0
  43. data/spec/twimock/api_spec.rb +81 -0
  44. data/spec/twimock/application_spec.rb +120 -0
  45. data/spec/twimock/auth_hash_spec.rb +7 -0
  46. data/spec/twimock/config_spec.rb +192 -0
  47. data/spec/twimock/database/table_spec.rb +769 -0
  48. data/spec/twimock/database_spec.rb +261 -0
  49. data/spec/twimock/omniauth_twitter_spec.rb +129 -0
  50. data/spec/twimock/request_token_spec.rb +140 -0
  51. data/spec/twimock/user_spec.rb +271 -0
  52. data/spec/twimock_spec.rb +76 -0
  53. data/twimock.gemspec +38 -0
  54. data/view/authenticate.html.erb +23 -0
  55. metadata +343 -0
@@ -0,0 +1,261 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twimock::Database do
4
+ let(:db_name) { ".test" }
5
+ let(:default_db_name) { "twimock" }
6
+ let(:adapter) { "sqlite3" }
7
+ let(:table_names) { [:applications, :users, :access_tokens, :request_tokens] }
8
+ let(:db_directory) { File.expand_path("../../../db", __FILE__) }
9
+ let(:db_filepath) { File.join(db_directory, "#{db_name}.#{adapter}") }
10
+
11
+ describe '::ADAPTER' do
12
+ subject { Twimock::Database::ADAPTER }
13
+ it { is_expected.to eq adapter }
14
+ end
15
+
16
+ describe '::DB_DIRECTORY' do
17
+ subject { Twimock::Database::DB_DIRECTORY }
18
+ it { is_expected.to eq db_directory }
19
+ end
20
+
21
+ describe '::TABLE_NAMES' do
22
+ subject { Twimock::Database::TABLE_NAMES }
23
+ it { is_expected.to eq table_names }
24
+ end
25
+
26
+ describe '::DEFAULT_DB_NAMES' do
27
+ subject { Twimock::Database::DEFAULT_DB_NAME }
28
+ it { is_expected.to eq default_db_name }
29
+ end
30
+
31
+ describe '#initialize' do
32
+ before do
33
+ allow_any_instance_of(Twimock::Database).to receive(:connect) { true }
34
+ allow_any_instance_of(Twimock::Database).to receive(:create_tables) { true }
35
+ end
36
+
37
+ subject { lambda { Twimock::Database.new } }
38
+ it { is_expected.not_to raise_error }
39
+
40
+ describe '.name' do
41
+ subject { Twimock::Database.new.name }
42
+ it { is_expected.to eq default_db_name }
43
+ end
44
+ end
45
+
46
+ describe '#connect' do
47
+ before do
48
+ allow_any_instance_of(Twimock::Database).to receive(:create_tables) { true }
49
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
50
+ @database = Twimock::Database.new
51
+ end
52
+ after { @database.drop }
53
+
54
+ subject { lambda { @database.connect } }
55
+ it { is_expected.not_to raise_error }
56
+ it { expect(File.exist?(@database.filepath)).to eq true }
57
+ end
58
+
59
+ describe '#disconnect' do
60
+ before do
61
+ allow_any_instance_of(Twimock::Database).to receive(:create_tables) { true }
62
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
63
+ @database = Twimock::Database.new
64
+ end
65
+ after { @database.drop }
66
+
67
+ subject { lambda { @database.disconnect! } }
68
+ it { is_expected.not_to raise_error }
69
+
70
+ context 'when success' do
71
+ describe 'datbase file is not removed' do
72
+ before { @database.disconnect! }
73
+ it { expect(File.exist?(@database.filepath)).to eq true }
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '#connected?' do
79
+ before do
80
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
81
+ @database = Twimock::Database.new
82
+ end
83
+ after { @database.drop }
84
+
85
+ context 'after new' do
86
+ subject { @database.connected? }
87
+ it { is_expected.to eq true }
88
+ end
89
+
90
+ context 'after disconnect!' do
91
+ before do
92
+ @database.disconnect!
93
+ end
94
+
95
+ subject { @database.connected? }
96
+ it { is_expected.to eq false }
97
+ end
98
+
99
+ context 'after connect' do
100
+ before do
101
+ @database.disconnect!
102
+ @database.connect
103
+ end
104
+
105
+ subject { @database.connected? }
106
+ it { is_expected.to eq true }
107
+ end
108
+ end
109
+
110
+ describe '#drop' do
111
+ before do
112
+ allow_any_instance_of(Twimock::Database).to receive(:create_tables) { true }
113
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
114
+ @database = Twimock::Database.new
115
+ end
116
+ after { @database.drop }
117
+
118
+ subject { lambda { @database.drop } }
119
+ it { is_expected.not_to raise_error }
120
+
121
+ context 'when success' do
122
+ describe 'database file does not exist' do
123
+ before { @database.drop }
124
+ it { expect(File.exist?(@database.filepath)).to eq false }
125
+ end
126
+
127
+ describe 're-drop is success' do
128
+ before { @database.drop }
129
+ subject { lambda { @database.drop } }
130
+ it { is_expected.not_to raise_error }
131
+ end
132
+ end
133
+ end
134
+
135
+ describe '#clear' do
136
+ before do
137
+ allow_any_instance_of(Twimock::Database).to receive(:create_tables) { true }
138
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
139
+ @database = Twimock::Database.new
140
+ expect(@database).to receive(:drop_tables)
141
+ expect(@database).to receive(:create_tables)
142
+ end
143
+ after { @database.drop }
144
+
145
+ subject { @database.clear }
146
+ it { is_expected.to be_truthy }
147
+ end
148
+
149
+ describe '#create_tables' do
150
+ before do
151
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
152
+ @database = Twimock::Database.new
153
+ @database.drop_tables
154
+ end
155
+ after { @database.drop }
156
+
157
+ subject { lambda { @database.create_tables } }
158
+ it { is_expected.not_to raise_error }
159
+ end
160
+
161
+ describe '#drop_table' do
162
+ before do
163
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
164
+ @database = Twimock::Database.new
165
+ end
166
+ after { @database.drop }
167
+
168
+ context 'when table exist' do
169
+ it 'should return true' do
170
+ table_names.each do |table_name|
171
+ expect(@database.drop_table(table_name)).to eq true
172
+ end
173
+ end
174
+ end
175
+
176
+ context 'when table does not exist' do
177
+ it 'should return true' do
178
+ @database.drop_tables
179
+ table_names.each do |table_name|
180
+ expect(@database.drop_table(table_name)).to eq false
181
+ end
182
+ end
183
+ end
184
+
185
+ context 'when database does not exist' do
186
+ it 'should return false' do
187
+ @database.drop
188
+ table_names.each do |table_name|
189
+ expect(@database.drop_table(table_name)).to eq false
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ describe '#drop_tables' do
196
+ before do
197
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
198
+ @database = Twimock::Database.new
199
+ end
200
+ after { @database.drop }
201
+
202
+ context 'when table exist' do
203
+ subject { @database.drop_tables }
204
+ it { is_expected.to eq true }
205
+ end
206
+
207
+ context 'when table does not exist' do
208
+ before { table_names.each{|table_name| @database.drop_table(table_name)} }
209
+ subject { @database.drop_tables }
210
+ it { is_expected.to eq true }
211
+ end
212
+
213
+ context 'when database does not exist' do
214
+ before { @database.drop }
215
+ subject { @database.drop_tables }
216
+ it { is_expected.to eq false }
217
+ end
218
+ end
219
+
220
+ describe '#filepath' do
221
+ before do
222
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
223
+ @database = Twimock::Database.new
224
+ end
225
+ after { @database.drop }
226
+
227
+ subject { @database.filepath }
228
+ it { is_expected.to eq db_filepath }
229
+
230
+ context 'then database file is exist' do
231
+ subject { File.exist? @database.filepath }
232
+ it { is_expected.to eq true }
233
+ end
234
+ end
235
+
236
+ describe '#table_exists?' do
237
+ before do
238
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
239
+ @database = Twimock::Database.new
240
+ end
241
+ after { @database.drop }
242
+
243
+ context 'when new' do
244
+ it 'should exist all tables' do
245
+ table_names.each do |table_name|
246
+ expect(@database.table_exists?(table_name)).to eq true
247
+ end
248
+ end
249
+ end
250
+
251
+ context 'when drop tables' do
252
+ before { @database.drop_tables }
253
+
254
+ it 'should not exist any tables' do
255
+ table_names.each do |table_name|
256
+ expect(@database.table_exists?(table_name)).to eq false
257
+ end
258
+ end
259
+ end
260
+ end
261
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twimock::OmniAuthTwitter do
4
+ include OmniAuthTwitterHelper
5
+
6
+ describe '.on' do
7
+ before { OmniAuth.config.logger.level = 2 }
8
+ after { Twimock::OmniAuthTwitter.off }
9
+
10
+ subject { Twimock::OmniAuthTwitter.on }
11
+ it { is_expected.to eq true }
12
+
13
+ context 'when OmniAuth Twitter mock is off' do
14
+ before do
15
+ Twimock::OmniAuthTwitter.off
16
+ Twimock::OmniAuthTwitter.on
17
+ end
18
+
19
+ it '@@enable is true' do
20
+ expect(Twimock::OmniAuthTwitter.class_variable_get(:@@enable)).to eq true
21
+ end
22
+
23
+ context 'then call ::OmniAuth::Strategies::Twitter#request_phase' do
24
+ before do
25
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
26
+ application = Twimock::Application.new
27
+ application.save!
28
+ @env = create_request_env(application.api_key, application.api_secret)
29
+ OmniAuth.config.path_prefix = "/users/auth"
30
+ Twimock::API.on
31
+
32
+ rackapp = lambda {|env| [ 200, {}, [ "Test App" ] ] }
33
+ @twitter = ::OmniAuth::Strategies::Twitter.new(rackapp)
34
+ @twitter.options["consumer_key"] = application.api_key
35
+ @twitter.options["consumer_secret"] = application.api_secret
36
+ end
37
+ after do
38
+ database.drop
39
+ Twimock::Config.port = 80
40
+ end
41
+
42
+ let(:db_name) { ".test" }
43
+ let(:database) { Twimock::Database.new }
44
+
45
+ shared_examples "Redirect to twimock" do
46
+ it 'should return mock 302 response' do
47
+ status, header, body = @twitter.call(@env)
48
+
49
+ url = case Twimock::Config.port
50
+ when 443 then "https://#{Twimock::Config.host}"
51
+ when 80 then "http://#{Twimock::Config.host}"
52
+ else "http://#{Twimock::Config.host}:#{Twimock::Config.port}"
53
+ end
54
+ url = File.join(url, @twitter.options.client_options.authorize_path)
55
+ oauth_token = Twimock::RequestToken.last
56
+ location = url + "?oauth_token=#{oauth_token.string}"
57
+
58
+ expect(status).to eq 302
59
+ expect(header["Location"]).to eq location
60
+ end
61
+ end
62
+
63
+ it_behaves_like "Redirect to twimock"
64
+
65
+ context 'and Twimock::Config.port set 80' do
66
+ before { Twimock::Config.port = 80 }
67
+ it_behaves_like "Redirect to twimock"
68
+ end
69
+
70
+ context 'and Twimock::Config.port set 443' do
71
+ before { Twimock::Config.port = 443 }
72
+ it_behaves_like "Redirect to twimock"
73
+ end
74
+
75
+ context 'and Twimock::Config.port set 3000' do
76
+ before { Twimock::Config.port = 3000 }
77
+ it_behaves_like "Redirect to twimock"
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ describe '.off' do
84
+ subject { Twimock::OmniAuthTwitter.off }
85
+ it { is_expected.to eq true }
86
+
87
+ context 'when OmniAuth Twitter mock is off' do
88
+ before do
89
+ Twimock::OmniAuthTwitter.on
90
+ Twimock::OmniAuthTwitter.off
91
+ end
92
+
93
+ it '@@enable is false' do
94
+ expect(Twimock::OmniAuthTwitter.class_variable_get(:@@enable)).to eq false
95
+ end
96
+
97
+ context 'then call ::OmniAuth::Strategies::Twitter#request_phase' do
98
+ before do
99
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
100
+ application = Twimock::Application.new
101
+ application.save!
102
+ @env = create_request_env(application.api_key, application.api_secret)
103
+ OmniAuth.config.path_prefix = "/users/auth"
104
+ Twimock::API.on
105
+
106
+ rackapp = lambda {|env| [ 200, {}, [ "Test App" ] ] }
107
+ @twitter = ::OmniAuth::Strategies::Twitter.new(rackapp)
108
+ @twitter.options["consumer_key"] = application.api_key
109
+ @twitter.options["consumer_secret"] = application.api_secret
110
+ end
111
+ after { database.drop }
112
+
113
+ let(:db_name) { ".test" }
114
+ let(:database) { Twimock::Database.new }
115
+
116
+ it 'should return mock 302 response' do
117
+ status, header, body = @twitter.call(@env)
118
+
119
+ oauth_token = Twimock::RequestToken.last
120
+ url = File.join("https://api.twitter.com", @twitter.options.client_options.authorize_path)
121
+ location = url + "?oauth_token=#{oauth_token.string}"
122
+
123
+ expect(status).to eq 302
124
+ expect(header["Location"]).to eq location
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twimock::RequestToken do
4
+ include TableHelper
5
+
6
+ let(:db_name) { ".test" }
7
+ let(:table_name) { :request_tokens }
8
+ let(:column_names) { [ :id, :string, :secret, :verifier, :application_id, :user_id, :created_at ] }
9
+
10
+ let(:id) { 1 }
11
+ let(:string) { "test_token" }
12
+ let(:secret) { "test_token_secret" }
13
+ let(:verifier) { "test_token_verifier" }
14
+ let(:application_id) { 1 }
15
+ let(:user_id) { 1 }
16
+ let(:created_at) { Time.now }
17
+ let(:options) { { id: id,
18
+ string: string,
19
+ secret: secret,
20
+ verifier: verifier,
21
+ application_id: application_id,
22
+ user_id: user_id,
23
+ created_at: created_at } }
24
+
25
+ after { remove_dynamically_defined_all_method }
26
+
27
+ describe '::TABLE_NAME' do
28
+ subject { Twimock::RequestToken::TABLE_NAME }
29
+ it { is_expected.to eq table_name }
30
+ end
31
+
32
+ describe '::COLUMN_NAMES' do
33
+ subject { Twimock::RequestToken::COLUMN_NAMES }
34
+ it { is_expected.to eq column_names }
35
+ end
36
+
37
+ describe '#initialize' do
38
+ context 'without option' do
39
+ subject { Twimock::RequestToken.new }
40
+ it { is_expected.to be_kind_of Twimock::RequestToken }
41
+
42
+ describe '.id' do
43
+ subject { Twimock::RequestToken.new.id }
44
+ it { is_expected.to be_nil }
45
+ end
46
+
47
+ describe '.string' do
48
+ subject { Twimock::RequestToken.new.string }
49
+ it { is_expected.to be_kind_of String }
50
+
51
+ describe '.size' do
52
+ subject { Twimock::RequestToken.new.string.size }
53
+ it { is_expected.to eq 32 }
54
+ end
55
+ end
56
+
57
+ describe '.secret' do
58
+ subject { Twimock::RequestToken.new.secret }
59
+ it { is_expected.to be_kind_of String }
60
+
61
+ describe '.size' do
62
+ subject { Twimock::RequestToken.new.secret.size }
63
+ it { is_expected.to eq 32 }
64
+ end
65
+ end
66
+
67
+ describe '.verifier' do
68
+ subject { Twimock::RequestToken.new.verifier }
69
+ it { is_expected.to be_kind_of String }
70
+
71
+ describe '.size' do
72
+ subject { Twimock::RequestToken.new.verifier.size }
73
+ it { is_expected.to eq 32 }
74
+ end
75
+ end
76
+
77
+ describe '.user_id' do
78
+ subject { Twimock::RequestToken.new.user_id }
79
+ it { is_expected.to be_nil }
80
+ end
81
+
82
+ describe '.application_id' do
83
+ subject { Twimock::RequestToken.new.application_id }
84
+ it { is_expected.to be_nil }
85
+ end
86
+
87
+ describe '.created_at' do
88
+ subject { Twimock::RequestToken.new.created_at }
89
+ it { is_expected.to be_nil }
90
+ end
91
+ end
92
+
93
+ context 'with id option but it is not integer' do
94
+ before { @opts = { id: "test_id" } }
95
+ subject { Twimock::RequestToken.new(@opts) }
96
+ it { is_expected.to be_kind_of Twimock::RequestToken }
97
+
98
+ describe '.id' do
99
+ subject { Twimock::RequestToken.new(@opts).id }
100
+ it { is_expected.to be_nil }
101
+ end
102
+ end
103
+
104
+ context 'with user_id option but it is not integer' do
105
+ before { @opts = { user_id: "test_id" } }
106
+ subject { Twimock::RequestToken.new(@opts) }
107
+ it { is_expected.to be_kind_of Twimock::RequestToken }
108
+
109
+ describe '.user_id' do
110
+ subject { Twimock::RequestToken.new(@opts).user_id }
111
+ it { is_expected.to be_nil }
112
+ end
113
+ end
114
+
115
+ context 'with application_id option but it is not integer' do
116
+ before { @opts = { application_id: "test_id" } }
117
+ subject { Twimock::RequestToken.new(@opts) }
118
+ it { is_expected.to be_kind_of Twimock::RequestToken }
119
+
120
+ describe '.application_id' do
121
+ subject { Twimock::RequestToken.new(@opts).application_id }
122
+ it { is_expected.to be_nil }
123
+ end
124
+ end
125
+
126
+ context 'with all options' do
127
+ subject { Twimock::RequestToken.new(options) }
128
+ it { is_expected.to be_kind_of Twimock::RequestToken }
129
+
130
+ context 'then attributes' do
131
+ it 'should set specified value by option' do
132
+ column_names.each do |column_name|
133
+ value = Twimock::RequestToken.new(options).send(column_name)
134
+ expect(value).to eq options[column_name]
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end