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,271 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twimock::User do
4
+ include TableHelper
5
+
6
+ let(:db_name) { ".test" }
7
+ let(:table_name) { :users }
8
+ let(:column_names) { [ :id,
9
+ :name,
10
+ :twitter_id,
11
+ :email,
12
+ :password,
13
+ :created_at ] }
14
+ let(:children) { [ Twimock::AccessToken, Twimock::RequestToken ] }
15
+ let(:info_keys) { [ :id, :name, :created_at ] }
16
+
17
+ let(:id) { 1 }
18
+ let(:name) { "test user" }
19
+ let(:twitter_id) { "test_user" }
20
+ let(:email) { "test@example.com" }
21
+ let(:password) { "testpass" }
22
+ let(:created_at) { Time.now }
23
+ let(:options) { { id: id,
24
+ name: name,
25
+ twitter_id: twitter_id,
26
+ email: email,
27
+ password: password,
28
+ created_at: created_at } }
29
+ let(:access_token_string_size) { 50 }
30
+ let(:access_token_secret_size) { 45 }
31
+
32
+ after { remove_dynamically_defined_all_method }
33
+
34
+ describe '::TABLE_NAME' do
35
+ subject { Twimock::User::TABLE_NAME }
36
+ it { is_expected.to eq table_name }
37
+ end
38
+
39
+ describe '::COLUMN_NAMES' do
40
+ subject { Twimock::User::COLUMN_NAMES }
41
+ it { is_expected.to eq column_names }
42
+ end
43
+
44
+ describe '::CHILDREN' do
45
+ subject { Twimock::User::CHILDREN }
46
+ it { is_expected.to eq children }
47
+ end
48
+
49
+ describe '::INFO_KEYS' do
50
+ subject { Twimock::User::INFO_KEYS }
51
+ it { is_expected.to eq info_keys }
52
+ end
53
+
54
+ describe '#initialize' do
55
+ context 'without option' do
56
+ subject { Twimock::User.new }
57
+ it { is_expected.to be_kind_of Twimock::User }
58
+
59
+ describe '.id' do
60
+ subject { Twimock::User.new.id }
61
+ it { is_expected.to be > 0 }
62
+ it { is_expected.to be < 10000000000 }
63
+ end
64
+
65
+ describe '.name' do
66
+ subject { Twimock::User.new.name }
67
+ it { is_expected.to be_kind_of String }
68
+
69
+ describe '.size' do
70
+ subject { Twimock::User.new.name.size }
71
+ it { is_expected.to be > 0 }
72
+ end
73
+ end
74
+
75
+ describe '.twitter_id' do
76
+ before { @user = Twimock::User.new }
77
+ subject { @user.twitter_id }
78
+ it { is_expected.to be_kind_of String }
79
+
80
+ describe '.size' do
81
+ subject { @user.twitter_id.size }
82
+ it { is_expected.to eq @user.name.size }
83
+ end
84
+ end
85
+
86
+ describe '.email' do
87
+ before { @user = Twimock::User.new }
88
+ subject { @user.email }
89
+ it { is_expected.to be_kind_of String }
90
+
91
+ describe '.size' do
92
+ subject { @user.email.size }
93
+ it { is_expected.to be > 0 }
94
+ end
95
+ end
96
+
97
+ describe '.password' do
98
+ subject { Twimock::User.new.password }
99
+ it { is_expected.to be_kind_of String }
100
+
101
+ describe '.size' do
102
+ subject { Twimock::User.new.password.size }
103
+ it { is_expected.to be_between(8, 16) }
104
+ end
105
+ end
106
+
107
+ describe '.created_at' do
108
+ subject { Twimock::User.new.created_at }
109
+ it { is_expected.to be_nil }
110
+ end
111
+ end
112
+
113
+ context 'with id option but it is not integer' do
114
+ before { @opts = { id: "test_id" } }
115
+ subject { Twimock::User.new(@opts) }
116
+ it { is_expected.to be_kind_of Twimock::User }
117
+
118
+ describe '.id' do
119
+ subject { Twimock::User.new(@opts).id }
120
+ it { is_expected.to be > 0 }
121
+ it { is_expected.to be < 10000000000 }
122
+ end
123
+ end
124
+
125
+ context 'with identifier option' do
126
+ before { @opts = { identifier: 1000000000 } }
127
+ subject { Twimock::User.new(@opts) }
128
+ it { is_expected.to be_kind_of Twimock::User }
129
+
130
+ describe '.id' do
131
+ subject { Twimock::User.new(@opts).id }
132
+ it { is_expected.to eq @opts[:identifier] }
133
+ end
134
+
135
+ describe '.identifier' do
136
+ subject { Twimock::User.new(@opts).identifier }
137
+ it { is_expected.to eq @opts[:identifier] }
138
+ end
139
+ end
140
+
141
+ context 'with all options' do
142
+ subject { Twimock::User.new(options) }
143
+ it { is_expected.to be_kind_of Twimock::User }
144
+
145
+ context 'then attributes' do
146
+ it 'should set specified value by option' do
147
+ column_names.each do |column_name|
148
+ value = Twimock::User.new(options).send(column_name)
149
+ expect(value).to eq options[column_name]
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ describe '#info' do
157
+ let(:user) { Twimock::User.new }
158
+ let(:info) { user.info }
159
+ let(:info_keys) { [:id, :id_str, :name, :created_at] }
160
+
161
+ it 'should return user information' do
162
+ expect(info).to be_kind_of Hashie::Mash
163
+ Twimock::User::INFO_KEYS.each { |key| expect(info.send(key)).to eq user.send(key) }
164
+ expect(info.id_str).to eq user.id.to_s
165
+ end
166
+ end
167
+
168
+ describe '#generate_access_token' do
169
+ before do
170
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
171
+ @database = Twimock::Database.new
172
+ end
173
+ after { @database.drop }
174
+
175
+ context 'without application_id' do
176
+ context 'when user does not save yet' do
177
+ before do
178
+ @user = Twimock::User.new
179
+ @access_token = @user.generate_access_token
180
+ end
181
+
182
+ it 'should return not saved Twimock::AccessToken instance' do
183
+ expect(@access_token).to be_instance_of Twimock::AccessToken
184
+ expect(@access_token.persisted?).to eq false
185
+ expect(@access_token.id).to be_nil
186
+ expect(@access_token.user_id).to be_nil
187
+ expect(@access_token.application_id).to be_nil
188
+ expect(@access_token.string).not_to be_nil
189
+ expect(@access_token.string.size).to eq access_token_string_size
190
+ expect(@access_token.secret).not_to be_nil
191
+ expect(@access_token.secret.size).to eq access_token_secret_size
192
+ end
193
+ end
194
+
195
+ context 'when user has saved' do
196
+ before do
197
+ @user = Twimock::User.new
198
+ @user.save!
199
+ @access_token = @user.generate_access_token
200
+ end
201
+
202
+ it 'should return saved Twimock::AccessToken instance' do
203
+ expect(@access_token).to be_instance_of Twimock::AccessToken
204
+ expect(@access_token.persisted?).to eq true
205
+ expect(@access_token.id).not_to be_nil
206
+ expect(@access_token.user_id).to eq @user.id
207
+ expect(@access_token.application_id).to be_nil
208
+ expect(@access_token.string).not_to be_nil
209
+ expect(@access_token.string.size).to eq access_token_string_size
210
+ expect(@access_token.secret).not_to be_nil
211
+ expect(@access_token.secret.size).to eq access_token_secret_size
212
+ end
213
+ end
214
+ end
215
+
216
+ context 'with application_id' do
217
+ context 'when specified application does not exist' do
218
+ let(:user) { Twimock::User.new }
219
+ let(:application_id) { 100000 }
220
+ subject { lambda { user.generate_access_token(application_id) } }
221
+ it { is_expected.to raise_error Twimock::Errors::ApplicationNotFound }
222
+ end
223
+
224
+ context 'when specified application exist' do
225
+ before do
226
+ @application = Twimock::Application.new
227
+ @application.save!
228
+ end
229
+
230
+ context 'and user does not saved' do
231
+ before do
232
+ @user = Twimock::User.new
233
+ @access_token = @user.generate_access_token(@application.id)
234
+ end
235
+
236
+ it 'should return not saved Twimock::AccessToken instance' do
237
+ expect(@access_token).to be_instance_of Twimock::AccessToken
238
+ expect(@access_token.persisted?).to eq false
239
+ expect(@access_token.id).to be_nil
240
+ expect(@access_token.user_id).to be_nil
241
+ expect(@access_token.application_id).to eq @application.id
242
+ expect(@access_token.string).not_to be_nil
243
+ expect(@access_token.string.size).to eq access_token_string_size
244
+ expect(@access_token.secret).not_to be_nil
245
+ expect(@access_token.secret.size).to eq access_token_secret_size
246
+ end
247
+ end
248
+
249
+ context 'and user has saved' do
250
+ before do
251
+ @user = Twimock::User.new
252
+ @user.save!
253
+ @access_token = @user.generate_access_token(@application.id)
254
+ end
255
+
256
+ it 'should return saved Twimock::AccessToken instance' do
257
+ expect(@access_token).to be_instance_of Twimock::AccessToken
258
+ expect(@access_token.persisted?).to eq true
259
+ expect(@access_token.id).not_to be_nil
260
+ expect(@access_token.user_id).to eq @user.id
261
+ expect(@access_token.application_id).to eq @application.id
262
+ expect(@access_token.string).not_to be_nil
263
+ expect(@access_token.string.size).to eq access_token_string_size
264
+ expect(@access_token.secret).not_to be_nil
265
+ expect(@access_token.secret.size).to eq access_token_secret_size
266
+ end
267
+ end
268
+ end
269
+ end
270
+ end
271
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twimock do
4
+ let(:version) { '0.0.1' }
5
+ let(:db_name) { '.test' }
6
+ let(:provider) { 'twitter' }
7
+
8
+ it 'should have a version number' do
9
+ expect(Twimock::VERSION).to eq version
10
+ end
11
+
12
+ describe '.auth_hash' do
13
+ context 'withou argument' do
14
+ subject { Twimock.auth_hash }
15
+ it { is_expected.to be_kind_of Twimock::AuthHash }
16
+ it { is_expected.to be_empty }
17
+ end
18
+
19
+ context 'with incorrect argument' do
20
+ it 'should return empty hash' do
21
+ [nil, false, true, 1, ""].each do |argument|
22
+ value = Twimock.auth_hash(argument)
23
+ expect(value).to be_kind_of Twimock::AuthHash
24
+ expect(value).to be_empty
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'with incorrect access_token' do
30
+ before do
31
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
32
+ @database = Twimock::Database.new
33
+ @user = Twimock::User.new
34
+ @access_token = @user.generate_access_token
35
+ end
36
+
37
+ context 'that is incorrect' do
38
+ it 'should return empty AuthHash' do
39
+ auth_hash = Twimock.auth_hash(@access_token.string)
40
+ expect(auth_hash).to be_kind_of Twimock::AuthHash
41
+ expect(auth_hash).to be_empty
42
+ end
43
+ end
44
+ end
45
+
46
+ context 'with access_token' do
47
+ before do
48
+ stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
49
+ @database = Twimock::Database.new
50
+ application = Twimock::Application.create!
51
+ @user = Twimock::User.create!
52
+ @access_token = @user.generate_access_token
53
+ end
54
+ after { @database.drop }
55
+
56
+ context 'that is correct' do
57
+ it 'should return AuthHash with some keys and value' do
58
+ auth_hash = Twimock.auth_hash(@access_token.string)
59
+ expect(auth_hash).to be_kind_of Twimock::AuthHash
60
+ expect(auth_hash).not_to be_empty
61
+ expect(auth_hash.provider).to eq provider
62
+ expect(auth_hash.uid).to eq @user.id
63
+ [ auth_hash.info, auth_hash.credentials,
64
+ auth_hash.extra, auth_hash.extra.raw_info ].each do |value|
65
+ expect(value).to be_kind_of Hash
66
+ end
67
+ expect(auth_hash.info.name).to eq @user.name
68
+ expect(auth_hash.credentials.token).to eq @access_token.string
69
+ expect(auth_hash.credentials.expires_at).to be > Time.now
70
+ expect(auth_hash.extra.raw_info.id).to eq @user.id
71
+ expect(auth_hash.extra.raw_info.name).to eq @user.name
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
data/twimock.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twimock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "twimock"
8
+ spec.version = Twimock::VERSION
9
+ spec.authors = ["ogawatti"]
10
+ spec.email = ["ogawattim@gmail.com"]
11
+ spec.description = %q{This gem is used to mock the communication part of the twitter api.}
12
+ spec.summary = %q{This is twitter mock application.}
13
+ spec.homepage = "https://github.com/ogawatti/twimock"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sqlite3"
22
+ spec.add_dependency "omniauth"
23
+ spec.add_dependency "activesupport"
24
+ spec.add_dependency "hashie"
25
+ spec.add_dependency "faker"
26
+ spec.add_dependency "sham_rack"
27
+ spec.add_dependency "excon"
28
+ spec.add_dependency "omniauth-twitter"
29
+ spec.add_dependency "addressable"
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.3"
32
+ spec.add_development_dependency "rake"
33
+ spec.add_development_dependency "rspec"
34
+ spec.add_development_dependency "rack-test"
35
+ spec.add_development_dependency "simplecov"
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency "coveralls"
38
+ end
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html>
2
+ <html lang="ja" dir="ltr" class="">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Authorize Twimock Application</title>
6
+ </head>
7
+
8
+ <body class="oauth authenticate write tfw ja logged-out noloki">
9
+ <div id="bd" role="main">
10
+ <div class="auth">
11
+ <h1>Login Twimock</h1>
12
+ <!-- POST https://twitter.com/intent/sessions -->
13
+ <form id="oauth_form" action="<%= @action_url %>" method="post">
14
+ <label>Username or email<input type="text" id="username_or_email" name="session[username_or_email]"></label><br>
15
+ <label>Password<input type="password" id="password" name="session[password]"></label><br>
16
+ <input type="hidden" name="remember_me" value="1">
17
+ <input type="hidden" name="oauth_token" value="<%= @oauth_token %>">
18
+ <input type="submit" value="login" class="submit button selected" id="allow">
19
+ </form>
20
+ </div>
21
+ </div>
22
+ </body>
23
+ </html>