twimock 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +125 -0
- data/Rakefile +6 -0
- data/db/.gitkeep +0 -0
- data/lib/twimock/access_token.rb +31 -0
- data/lib/twimock/api/account/verify_credentials.rb +40 -0
- data/lib/twimock/api/application.rb +29 -0
- data/lib/twimock/api/intent/sessions.rb +60 -0
- data/lib/twimock/api/oauth/access_token.rb +65 -0
- data/lib/twimock/api/oauth/authenticate.rb +51 -0
- data/lib/twimock/api/oauth/request_token.rb +49 -0
- data/lib/twimock/api/oauth.rb +83 -0
- data/lib/twimock/api.rb +35 -0
- data/lib/twimock/application.rb +21 -0
- data/lib/twimock/auth_hash.rb +8 -0
- data/lib/twimock/config.rb +90 -0
- data/lib/twimock/database/table.rb +359 -0
- data/lib/twimock/database.rb +133 -0
- data/lib/twimock/errors.rb +13 -0
- data/lib/twimock/omniauth/strategies/twitter.rb +28 -0
- data/lib/twimock/omniauth_twitter.rb +36 -0
- data/lib/twimock/request_token.rb +23 -0
- data/lib/twimock/user.rb +58 -0
- data/lib/twimock/version.rb +3 -0
- data/lib/twimock.rb +39 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/api_spec_helper.rb +30 -0
- data/spec/support/omniauth_twitter_helper.rb +26 -0
- data/spec/support/tables_helper.rb +54 -0
- data/spec/support/test_application_helper.rb +9 -0
- data/spec/twimock/access_token_spec.rb +128 -0
- data/spec/twimock/api/account/verify_credentials_spec.rb +125 -0
- data/spec/twimock/api/application_spec.rb +27 -0
- data/spec/twimock/api/intent/sessions_spec.rb +184 -0
- data/spec/twimock/api/oauth/access_token_spec.rb +185 -0
- data/spec/twimock/api/oauth/authenticate_spec.rb +96 -0
- data/spec/twimock/api/oauth/request_token_spec.rb +123 -0
- data/spec/twimock/api_spec.rb +81 -0
- data/spec/twimock/application_spec.rb +120 -0
- data/spec/twimock/auth_hash_spec.rb +7 -0
- data/spec/twimock/config_spec.rb +192 -0
- data/spec/twimock/database/table_spec.rb +769 -0
- data/spec/twimock/database_spec.rb +261 -0
- data/spec/twimock/omniauth_twitter_spec.rb +129 -0
- data/spec/twimock/request_token_spec.rb +140 -0
- data/spec/twimock/user_spec.rb +271 -0
- data/spec/twimock_spec.rb +76 -0
- data/twimock.gemspec +38 -0
- data/view/authenticate.html.erb +23 -0
- metadata +343 -0
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Twimock::Application do
|
4
|
+
include TableHelper
|
5
|
+
|
6
|
+
let(:db_name) { ".test" }
|
7
|
+
|
8
|
+
let(:table_name) { :applications }
|
9
|
+
let(:column_names) { [ :id, :api_key, :api_secret, :created_at ] }
|
10
|
+
let(:children) { [ Twimock::AccessToken, Twimock::RequestToken ] }
|
11
|
+
|
12
|
+
let(:id) { 1 }
|
13
|
+
let(:api_key) { "test_api_key" }
|
14
|
+
let(:api_secret) { "test_api_secret" }
|
15
|
+
let(:created_at) { Time.now }
|
16
|
+
let(:options) { { id: id, api_key: api_key, api_secret: api_secret, created_at: created_at } }
|
17
|
+
|
18
|
+
after { remove_dynamically_defined_all_method }
|
19
|
+
|
20
|
+
describe '::TABLE_NAME' do
|
21
|
+
subject { Twimock::Application::TABLE_NAME }
|
22
|
+
it { is_expected.to eq table_name }
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '::COLUMN_NAMES' do
|
26
|
+
subject { Twimock::Application::COLUMN_NAMES }
|
27
|
+
it { is_expected.to eq column_names }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '::CHILDREN' do
|
31
|
+
subject { Twimock::Application::CHILDREN }
|
32
|
+
it { is_expected.to eq children }
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#initialize' do
|
36
|
+
context 'without option' do
|
37
|
+
subject { Twimock::Application.new }
|
38
|
+
it { is_expected.to be_kind_of Twimock::Application }
|
39
|
+
|
40
|
+
describe '.id' do
|
41
|
+
subject { Twimock::Application.new.id }
|
42
|
+
it { is_expected.to be > 0 }
|
43
|
+
it { is_expected.to be < 10000000000 }
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '.api_key' do
|
47
|
+
subject { Twimock::Application.new.api_key }
|
48
|
+
it { is_expected.to be_kind_of String }
|
49
|
+
|
50
|
+
describe '.size' do
|
51
|
+
subject { Twimock::Application.new.api_key.size }
|
52
|
+
it { is_expected.to eq 25 }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '.api_secret' do
|
57
|
+
subject { Twimock::Application.new.api_secret }
|
58
|
+
it { is_expected.to be_kind_of String }
|
59
|
+
|
60
|
+
describe '.size' do
|
61
|
+
subject { Twimock::Application.new.api_secret.size }
|
62
|
+
it { is_expected.to eq 50 }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '.created_at' do
|
67
|
+
subject { Twimock::Application.new.created_at }
|
68
|
+
it { is_expected.to be_nil }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'with id option but it is not integer' do
|
73
|
+
before { @opts = { id: "test_id" } }
|
74
|
+
subject { Twimock::Application.new(@opts) }
|
75
|
+
it { is_expected.to be_kind_of Twimock::Application }
|
76
|
+
|
77
|
+
describe '.id' do
|
78
|
+
subject { Twimock::Application.new(@opts).id }
|
79
|
+
it { is_expected.to be > 0 }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'with all options' do
|
84
|
+
subject { Twimock::Application.new(options) }
|
85
|
+
it { is_expected.to be_kind_of Twimock::Application }
|
86
|
+
|
87
|
+
context 'then attributes' do
|
88
|
+
it 'should set specified values by option' do
|
89
|
+
column_names.each do |column_name|
|
90
|
+
value = Twimock::Application.new(options).send(column_name)
|
91
|
+
expect(value).to eq options[column_name]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'destroy' do
|
99
|
+
before do
|
100
|
+
stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name)
|
101
|
+
@database = Twimock::Database.new
|
102
|
+
end
|
103
|
+
after { @database.drop }
|
104
|
+
|
105
|
+
context 'when has user' do
|
106
|
+
before do
|
107
|
+
@application = Twimock::Application.create!
|
108
|
+
Twimock::User.create!
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should delete permissions' do
|
112
|
+
@application.destroy
|
113
|
+
access_tokens = Twimock::AccessToken.find_all_by_application_id(@application.id)
|
114
|
+
expect(access_tokens).to be_empty
|
115
|
+
request_tokens = Twimock::RequestToken.find_all_by_application_id(@application.id)
|
116
|
+
expect(request_tokens).to be_empty
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'faker'
|
4
|
+
|
5
|
+
describe Twimock::Config do
|
6
|
+
let(:db_name) { ".test" }
|
7
|
+
let(:database) { Twimock::Database.new }
|
8
|
+
|
9
|
+
let(:host) { "api.twimock.com" }
|
10
|
+
let(:port) { 443 }
|
11
|
+
let(:callback_url) { "http://localhost/auth/twiter/callback" }
|
12
|
+
|
13
|
+
before { stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name) }
|
14
|
+
after { database.drop }
|
15
|
+
|
16
|
+
describe '.host' do
|
17
|
+
after { Twimock::Config.host = host }
|
18
|
+
|
19
|
+
subject { Twimock::Config.host }
|
20
|
+
it { is_expected.to eq host }
|
21
|
+
|
22
|
+
context 'when set this' do
|
23
|
+
before { Twimock::Config.host = host }
|
24
|
+
let(:host) { "test.twimock.com" }
|
25
|
+
it { is_expected.to eq host }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.port' do
|
30
|
+
after { Twimock::Config.port = port }
|
31
|
+
|
32
|
+
subject { Twimock::Config.port }
|
33
|
+
it { is_expected.to eq port }
|
34
|
+
|
35
|
+
context 'when set this' do
|
36
|
+
before { Twimock::Config.port = port }
|
37
|
+
let(:port) { 80 }
|
38
|
+
it { is_expected.to eq port }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '.callback_url' do
|
43
|
+
after { Twimock::Config.callback_url = callback_url }
|
44
|
+
|
45
|
+
subject { Twimock::Config.callback_url }
|
46
|
+
it { is_expected.to eq callback_url }
|
47
|
+
|
48
|
+
context 'when set this' do
|
49
|
+
before { Twimock::Config.callback_url = callback_url }
|
50
|
+
let(:callback_url) { "http://localhost:3000/users/auth/twitter/callback" }
|
51
|
+
it { is_expected.to eq callback_url }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
['default_database', 'database']. each do |db|
|
56
|
+
describe "##{db}" do
|
57
|
+
before do
|
58
|
+
[:connect, :create_tables, :disconnect!].each do |method|
|
59
|
+
allow_any_instance_of(Twimock::Database).to receive(method) { true }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
subject { Twimock::Config.send(db) }
|
64
|
+
it { is_expected.to be_truthy }
|
65
|
+
|
66
|
+
describe '.name' do
|
67
|
+
subject { Twimock::Config.send(db).name }
|
68
|
+
it { is_expected.not_to be_nil }
|
69
|
+
it { is_expected.not_to be_empty }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#reset_database' do
|
75
|
+
subject { Twimock::Config.reset_database }
|
76
|
+
it { is_expected.to be_nil }
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#load_users' do
|
80
|
+
let(:app) { app_data }
|
81
|
+
let(:yaml_load_data) { [app] }
|
82
|
+
let(:path) { create_temporary_yaml_file(yaml_load_data) }
|
83
|
+
|
84
|
+
context 'without argument' do
|
85
|
+
subject { lambda { Twimock::Config.load_users } }
|
86
|
+
it { is_expected.to raise_error ArgumentError }
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'with yaml file path' do
|
90
|
+
subject { lambda { Twimock::Config.load_users(path) } }
|
91
|
+
|
92
|
+
context 'but file does not exist' do
|
93
|
+
let(:path) { 'testdata.yml' }
|
94
|
+
it { is_expected.to raise_error Errno::ENOENT }
|
95
|
+
end
|
96
|
+
|
97
|
+
shared_examples 'IncorrectDataFormat' do
|
98
|
+
it 'app and user should not be created' do
|
99
|
+
is_expected.to raise_error Twimock::Errors::IncorrectDataFormat
|
100
|
+
expect(Twimock::Application.all).to be_empty
|
101
|
+
expect(Twimock::User.all).to be_empty
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'but incorrect format' do
|
106
|
+
context 'when load data is not array' do
|
107
|
+
let(:yaml_load_data) { "" }
|
108
|
+
it_behaves_like 'IncorrectDataFormat'
|
109
|
+
end
|
110
|
+
|
111
|
+
[:id, :api_key, :api_secret, :users].each do |key|
|
112
|
+
context "when #{key} is not exist" do
|
113
|
+
before { app.delete(key) }
|
114
|
+
it_behaves_like 'IncorrectDataFormat'
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
[:id, :name, :password].each do |key|
|
119
|
+
context "when users #{key} is not exist" do
|
120
|
+
before { app[:users].first.delete(key) }
|
121
|
+
it_behaves_like 'IncorrectDataFormat'
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'yaml is correct format' do
|
127
|
+
let(:yaml_load_data) { [app_data, app_data(2)] }
|
128
|
+
let(:app_count) { yaml_load_data.size }
|
129
|
+
let(:user_count) { yaml_load_data.inject(0){ |count, data| count += data[:users].size } }
|
130
|
+
let(:app) { yaml_load_data.first }
|
131
|
+
let(:user) { yaml_load_data.first[:users].first }
|
132
|
+
|
133
|
+
it 'app and user should be created' do
|
134
|
+
expect{ Twimock::Config.load_users(path) }.not_to raise_error
|
135
|
+
expect(Twimock::Application.all.count).to eq app_count
|
136
|
+
expect(Twimock::User.all.count).to eq user_count
|
137
|
+
expect(Twimock::AccessToken.all.count).to eq user_count
|
138
|
+
|
139
|
+
# 要改善 : yamlの全部が登録されたことがチェックされていない
|
140
|
+
[:api_key, :api_secret].each do |key|
|
141
|
+
expect(Twimock::Application.find_by_id(app[:id]).send(key).to_s).to eq app[key].to_s
|
142
|
+
end
|
143
|
+
[:name, :password].each do |key|
|
144
|
+
expect(Twimock::User.find_by_id(user[:id]).send(key).to_s).to eq user[key].to_s
|
145
|
+
end
|
146
|
+
access_tokens = Twimock::AccessToken.where(user_id: user[:id])
|
147
|
+
expect(access_tokens.count).to eq 1
|
148
|
+
access_token = access_tokens.first
|
149
|
+
expect(access_token.string).to eq user[:access_token]
|
150
|
+
expect(access_token.secret).to eq user[:access_token_secret]
|
151
|
+
expect(access_token.application_id).to eq app[:id]
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
context 'same user should not be created' do
|
157
|
+
let(:new_path) { create_temporary_yaml_file(yaml_load_data) }
|
158
|
+
before { Twimock::Config.load_users(path) }
|
159
|
+
it { expect{ Twimock::Config.load_users(new_path) }.not_to change{ Twimock::User.all.count } }
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def app_data(user_count = 1)
|
166
|
+
users = []
|
167
|
+
user_count.times do
|
168
|
+
user = { id: Faker::Number.number(10),
|
169
|
+
name: create_user_name,
|
170
|
+
password: Faker::Internet.password,
|
171
|
+
access_token: Faker::Lorem.characters(50),
|
172
|
+
access_token_secret: Faker::Lorem.characters(45) }
|
173
|
+
users.push user
|
174
|
+
end
|
175
|
+
app = { id: Faker::Number.number(10).to_i,
|
176
|
+
api_key: Faker::Lorem.characters(25),
|
177
|
+
api_secret: Faker::Lorem.characters(50),
|
178
|
+
users: users }
|
179
|
+
end
|
180
|
+
|
181
|
+
def create_user_name
|
182
|
+
n = Faker::Name.name
|
183
|
+
(n.include?("'") || n.include?(".")) ? create_user_name : n
|
184
|
+
end
|
185
|
+
|
186
|
+
def create_temporary_yaml_file(data)
|
187
|
+
ymlfile = 'testdata.yml'
|
188
|
+
path = Tempfile.open(ymlfile) do |tempfile|
|
189
|
+
tempfile.puts YAML.dump(data)
|
190
|
+
tempfile.path
|
191
|
+
end
|
192
|
+
end
|