facemock 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Facemock
2
+ VERSION = "0.0.2"
3
+ end
data/lib/facemock.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "facemock/version"
2
+ require "facemock/fb_graph"
3
+ require "facemock/config"
4
+
5
+ module Facemock
6
+ extend self
7
+
8
+ def on(options={})
9
+ Facemock::FbGraph.on(options)
10
+ end
11
+
12
+ def off
13
+ Facemock::FbGraph.off
14
+ end
15
+ end
@@ -0,0 +1,220 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::Config::Database do
4
+ let(:default_db_name) { "facemock" }
5
+ let(:adapter) { "sqlite3" }
6
+ let(:table_names) { [:applications, :users, :user_rights] }
7
+ let(:db_directory) { File.expand_path("../../../../db", __FILE__) }
8
+ let(:db_filepath) { File.join(db_directory, "#{db_name}.#{adapter}") }
9
+
10
+ let(:db_name) { ".test" }
11
+
12
+ describe '::ADAPTER' do
13
+ subject { Facemock::Config::Database::ADAPTER }
14
+ it { is_expected.to eq adapter }
15
+ end
16
+
17
+ describe '::DB_DIRECTORY' do
18
+ subject { Facemock::Config::Database::DB_DIRECTORY }
19
+ it { is_expected.to eq db_directory }
20
+ end
21
+
22
+ describe '::TABLE_NAMES' do
23
+ subject { Facemock::Config::Database::TABLE_NAMES }
24
+ it { is_expected.to eq table_names }
25
+ end
26
+
27
+ describe '::DEFAULT_DB_NAMES' do
28
+ subject { Facemock::Config::Database::DEFAULT_DB_NAME }
29
+ it { is_expected.to eq default_db_name }
30
+ end
31
+
32
+ describe '#initialize' do
33
+ before do
34
+ allow_any_instance_of(Facemock::Config::Database).to receive(:connect) { true }
35
+ allow_any_instance_of(Facemock::Config::Database).to receive(:create_tables) { true }
36
+ end
37
+
38
+ context 'with database name' do
39
+ subject { lambda { Facemock::Config::Database.new(db_name) } }
40
+ it { is_expected.not_to raise_error }
41
+
42
+ describe '.name' do
43
+ subject { Facemock::Config::Database.new(db_name).name }
44
+ it { is_expected.to eq db_name }
45
+ end
46
+ end
47
+
48
+ context 'without argument' do
49
+ subject { lambda { Facemock::Config::Database.new } }
50
+ it { is_expected.not_to raise_error }
51
+
52
+ describe '.name' do
53
+ subject { Facemock::Config::Database.new.name }
54
+ it { is_expected.to eq default_db_name }
55
+ end
56
+ end
57
+
58
+ context 'with nil or "" or default database name' do
59
+ [nil, "", "facemock"].each do |argument|
60
+ subject { lambda { Facemock::Config::Database.new(argument) } }
61
+ it { is_expected.not_to raise_error }
62
+
63
+ describe '.name' do
64
+ subject { Facemock::Config::Database.new(argument).name }
65
+ it { is_expected.to eq default_db_name }
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ describe '#connect' do
72
+ before do
73
+ allow_any_instance_of(Facemock::Config::Database).to receive(:create_tables) { true }
74
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
75
+ @database = Facemock::Config.database
76
+ end
77
+
78
+ subject { lambda { @database.connect } }
79
+ it { is_expected.not_to raise_error }
80
+ it { expect(ActiveRecord::Base.connected?).to eq true }
81
+ it { expect(File.exist?(@database.filepath)).to eq true }
82
+
83
+ after { Facemock::Config.database.drop }
84
+ end
85
+
86
+ describe '#disconnect' do
87
+ before do
88
+ allow_any_instance_of(Facemock::Config::Database).to receive(:create_tables) { true }
89
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
90
+ @database = Facemock::Config.database
91
+ end
92
+
93
+ subject { lambda { @database.disconnect! } }
94
+ it { is_expected.not_to raise_error }
95
+
96
+ context 'when success' do
97
+ describe 'datbase file is not removed' do
98
+ before { @database.disconnect! }
99
+ it { expect(File.exist?(@database.filepath)).to eq true }
100
+ end
101
+ end
102
+
103
+ after { Facemock::Config.database.drop }
104
+ end
105
+
106
+ describe '#drop' do
107
+ before do
108
+ allow_any_instance_of(Facemock::Config::Database).to receive(:create_tables) { true }
109
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
110
+ @database = Facemock::Config.database
111
+ end
112
+
113
+ subject { lambda { @database.drop } }
114
+ it { is_expected.not_to raise_error }
115
+
116
+ context 'when success' do
117
+ describe 'database file does not exist' do
118
+ before { @database.drop }
119
+ it { expect(File.exist?(@database.filepath)).to eq false }
120
+ end
121
+
122
+ describe 're-drop is success' do
123
+ before { @database.drop }
124
+ subject { lambda { @database.drop } }
125
+ it { is_expected.not_to raise_error }
126
+ end
127
+ end
128
+
129
+ after { Facemock::Config.database.drop }
130
+ end
131
+
132
+ describe '#clear' do
133
+ before do
134
+ allow_any_instance_of(Facemock::Config::Database).to receive(:create_tables) { true }
135
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
136
+ @database = Facemock::Config.database
137
+ expect(@database).to receive(:drop_tables)
138
+ expect(@database).to receive(:create_tables)
139
+ end
140
+
141
+ subject { @database.clear }
142
+ it { is_expected.to be_truthy }
143
+
144
+ after { Facemock::Config.database.drop }
145
+ end
146
+
147
+ describe '#create_tables' do
148
+ before do
149
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
150
+ @database = Facemock::Config.database
151
+ @database.drop_tables
152
+ end
153
+
154
+ subject { lambda { @database.create_tables } }
155
+ it { is_expected.not_to raise_error }
156
+
157
+ after { Facemock::Config.database.drop }
158
+ end
159
+
160
+ describe '#drop_tables' do
161
+ before do
162
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
163
+ @database = Facemock::Config.database
164
+ end
165
+
166
+ subject { lambda { @database.drop_tables } }
167
+ it { is_expected.not_to raise_error }
168
+
169
+ after { Facemock::Config.database.drop }
170
+ end
171
+
172
+ describe '#filepath' do
173
+ before do
174
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
175
+ @database = Facemock::Config.database
176
+ end
177
+
178
+ subject { @database.filepath }
179
+ it { is_expected.to eq db_filepath }
180
+
181
+ context 'then database file is exist' do
182
+ subject { File.exist? @database.filepath }
183
+ it { is_expected.to eq true }
184
+ end
185
+
186
+ after { Facemock::Config.database.drop }
187
+ end
188
+
189
+ describe '#connected?' do
190
+ before do
191
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
192
+ @database = Facemock::Config.database
193
+ end
194
+ after { Facemock::Config.database.drop }
195
+
196
+ context 'after new' do
197
+ subject { @database.connected? }
198
+ it { is_expected.to eq true }
199
+ end
200
+
201
+ context 'after disconnect!' do
202
+ before do
203
+ @database.disconnect!
204
+ end
205
+
206
+ subject { @database.connected? }
207
+ it { is_expected.to eq false }
208
+ end
209
+
210
+ context 'after connect' do
211
+ before do
212
+ @database.disconnect!
213
+ @database.connect
214
+ end
215
+
216
+ subject { @database.connected? }
217
+ it { is_expected.to eq true }
218
+ end
219
+ end
220
+ end
@@ -0,0 +1,195 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ describe Facemock::Config do
5
+ let(:db_name) { ".test" }
6
+ let(:ymlfile) { "testdata.yml" }
7
+
8
+ it 'should have a database module' do
9
+ expect(Facemock::Config::Database).to be_truthy
10
+ end
11
+
12
+ describe '#default_database' do
13
+ before do
14
+ allow_any_instance_of(Facemock::Config::Database).to receive(:connect) { true }
15
+ allow_any_instance_of(Facemock::Config::Database).to receive(:create_tables) { true }
16
+ allow_any_instance_of(Facemock::Config::Database).to receive(:disconnect!) { true }
17
+ end
18
+
19
+ subject { Facemock::Config.default_database }
20
+ it { is_expected.to be_truthy }
21
+
22
+ describe '.name' do
23
+ subject { Facemock::Config.default_database.name }
24
+ it { is_expected.not_to be_nil }
25
+ it { is_expected.not_to be_empty }
26
+ end
27
+ end
28
+
29
+ describe '#database' do
30
+ before do
31
+ allow_any_instance_of(Facemock::Config::Database).to receive(:connect) { true }
32
+ allow_any_instance_of(Facemock::Config::Database).to receive(:create_tables) { true }
33
+ allow_any_instance_of(Facemock::Config::Database).to receive(:disconnect!) { true }
34
+ end
35
+
36
+ context 'without argument' do
37
+ subject { Facemock::Config.database }
38
+ it { is_expected.to be_truthy }
39
+
40
+ describe '.name' do
41
+ subject { Facemock::Config.database.name }
42
+ it { is_expected.not_to be_nil }
43
+ it { is_expected.not_to be_empty }
44
+ end
45
+ end
46
+
47
+ context 'with name options' do
48
+ subject { Facemock::Config.database(db_name) }
49
+ it { is_expected.to be_truthy }
50
+
51
+ describe '.name' do
52
+ subject { Facemock::Config.database(db_name).name }
53
+ it { is_expected.to eq db_name }
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#reset_database' do
59
+ context 'when does not set database' do
60
+ subject { Facemock::Config.reset_database }
61
+ it { is_expected.to eq nil }
62
+ end
63
+
64
+ context 'when already set database' do
65
+ before do
66
+ stub_const("Facemock::Config::Database::DEFAULT_DATABASE_NAME", db_name)
67
+ Facemock::Config.database
68
+ end
69
+
70
+ subject { Facemock::Config.reset_database }
71
+ it { is_expected.to eq nil }
72
+
73
+ after { Facemock::Config.database.drop }
74
+ end
75
+ end
76
+
77
+ describe '#load_users' do
78
+ let(:user1) { { identifier: "100000000000001",
79
+ name: "test user one",
80
+ email: "test_user_one@example.com",
81
+ password: "testpass" } }
82
+ let(:user2) { { identifier: "100000000000002",
83
+ name: "test user two",
84
+ email: "test_user_two@example.com",
85
+ password: "testpass" } }
86
+ let(:user3) { { identifier: 100000000000003,
87
+ name: "test user three",
88
+ email: "test_user_three@example.com",
89
+ password: "testpass" } }
90
+
91
+ let(:app1_id) { "000000000000001" }
92
+ let(:app1_secret) { "test_secret_one" }
93
+ let(:app1_users) { [user1, user2] }
94
+ let(:app1) { { app_id: app1_id, app_secret: app1_secret, users: app1_users } }
95
+
96
+ let(:app2_id) { 000000000000002 }
97
+ let(:app2_secret) { "test_secret_two" }
98
+ let(:app2_users) { [user3] }
99
+ let(:app2) { { app_id: app2_id, app_secret: app2_secret, users: app2_users } }
100
+
101
+ let(:yaml_load_data) { [ app1, app2 ] }
102
+
103
+ context 'without argument' do
104
+ subject { lambda { Facemock::Config.load_users } }
105
+ it { is_expected.to raise_error ArgumentError }
106
+ end
107
+
108
+ context 'with yaml file path' do
109
+ before do
110
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
111
+ Facemock::Config.database
112
+ end
113
+ after { Facemock::Config.database.drop }
114
+
115
+ context 'but file does not exist' do
116
+ subject { lambda { Facemock::Config.load_users("testdata.yml") } }
117
+ it { is_expected.to raise_error Errno::ENOENT }
118
+ end
119
+
120
+ def create_temporary_yaml_file(data)
121
+ path = Tempfile.open(ymlfile) do |tempfile|
122
+ tempfile.puts YAML.dump(data)
123
+ tempfile.path
124
+ end
125
+ end
126
+
127
+ shared_context 'app and user should not be created', assert: :incorrect_data_format do
128
+ subject { lambda { Facemock::Config.load_users(@path) } }
129
+ it { is_expected.to raise_error Facemock::Errors::IncorrectDataFormat }
130
+
131
+ it 'app and user should not be created' do
132
+ begin
133
+ Facemock::Config.load_users(@path)
134
+ rescue => error
135
+ expect(Facemock::FbGraph::Application.all).to be_empty
136
+ expect(Facemock::FbGraph::Application::User.all).to be_empty
137
+ end
138
+ end
139
+ end
140
+
141
+ context 'but incorrect format' do
142
+ context 'when load data is not array', assert: :incorrect_data_format do
143
+ before do
144
+ users_data = ""
145
+ @path = create_temporary_yaml_file(users_data)
146
+ end
147
+ end
148
+
149
+ context 'when app id does not exist', assert: :incorrect_data_format do
150
+ before do
151
+ users_data = [ { app_secret: app1_secret, users: app1_users } ]
152
+ @path = create_temporary_yaml_file(users_data)
153
+ end
154
+ end
155
+
156
+ context 'when app secret does not exist', assert: :incorrect_data_format do
157
+ before do
158
+ users_data = [ { app_id: app1_id, users: app1_users } ]
159
+ @path = create_temporary_yaml_file(users_data)
160
+ end
161
+ end
162
+
163
+ context 'when users does not exist', assert: :incorrect_data_format do
164
+ before do
165
+ users_data = [ { app_id: app1_id, app_secret: app1_secret } ]
166
+ @path = create_temporary_yaml_file(users_data)
167
+ end
168
+ end
169
+
170
+ context 'when users have only identifier', assert: :incorrect_data_format do
171
+ before do
172
+ users_data = [ { app_id: app1_id, app_secret: app1_secret,
173
+ users: [ { identifier: "100000000000001" } ] } ]
174
+ @path = create_temporary_yaml_file(users_data)
175
+ end
176
+ end
177
+ end
178
+
179
+ context 'yaml is correct format' do
180
+ before { @path = create_temporary_yaml_file(yaml_load_data) }
181
+
182
+ subject { lambda { Facemock::Config.load_users(@path) } }
183
+ it { is_expected.not_to raise_error }
184
+
185
+ it 'app and user should be created' do
186
+ app_count = yaml_load_data.size
187
+ user_count = yaml_load_data.inject(0){|count, data| count += data[:users].size }
188
+ Facemock::Config.load_users(@path)
189
+ expect(Facemock::FbGraph::Application.all.count).to eq app_count
190
+ expect(Facemock::FbGraph::Application::User.all.count).to eq user_count
191
+ end
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::FbGraph::Application::TestUsers do
4
+ let(:default_limit) { 50 }
5
+ let(:default_after) { 0 }
6
+
7
+ let(:db_name) { ".test" }
8
+ let(:adapter) { "sqlite3" }
9
+ let(:db_directory) { File.expand_path("../../../../db", __FILE__) }
10
+ let(:db_filepath) { File.join(db_directory, "#{db_name}.#{adapter}") }
11
+ let(:facebook_app_id) { "100000000000000" }
12
+
13
+ before do
14
+ stub_const("Facemock::Config::Database::DEFAULT_DB_NAME", db_name)
15
+ Facemock::Config.database
16
+ end
17
+ after { Facemock::Config.database.drop }
18
+
19
+ describe '::DEFAULT_LIMIT' do
20
+ subject { Facemock::FbGraph::Application::TestUsers::DEFAULT_LIMIT }
21
+ it { is_expected.to eq default_limit }
22
+ end
23
+
24
+ describe '::DEFAULT_AFTER' do
25
+ subject { Facemock::FbGraph::Application::TestUsers::DEFAULT_AFTER }
26
+ it { is_expected.to eq default_after }
27
+ end
28
+
29
+ describe '#new' do
30
+ context 'without argument' do
31
+ subject { lambda { Facemock::FbGraph::Application::TestUsers.new } }
32
+ it { is_expected.to raise_error ArgumentError }
33
+ end
34
+
35
+ context 'with application id' do
36
+ subject { Facemock::FbGraph::Application::TestUsers.new(facebook_app_id) }
37
+ it { is_expected.to be_kind_of Array }
38
+
39
+ context 'when user does not exist' do
40
+ subject { Facemock::FbGraph::Application::TestUsers.new(facebook_app_id) }
41
+ it { is_expected.to be_empty }
42
+ end
43
+
44
+ context 'when user exist only one' do
45
+ before do
46
+ @user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
47
+ @user.save!
48
+ end
49
+
50
+ it 'should have user' do
51
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id)
52
+ expect(test_users).not_to be_empty
53
+ expect(test_users).to include @user
54
+ expect(test_users.count).to eq 1
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'with application id and options' do
60
+ context 'that limit is 1' do
61
+ context 'when user is exist only two' do
62
+ before do
63
+ user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
64
+ user.save!
65
+ @last_created_user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
66
+ @last_created_user.save!
67
+ @limit = 1
68
+ end
69
+
70
+ it 'should have only one user' do
71
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, limit: @limit)
72
+ expect(test_users).not_to be_empty
73
+ expect(test_users.count).to eq 1
74
+ expect(test_users).to include @last_created_user
75
+ end
76
+ end
77
+ end
78
+
79
+ context 'that after is 1' do
80
+ context 'when user is exist only two' do
81
+ before do
82
+ @first_created_user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
83
+ @first_created_user.save!
84
+ user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
85
+ user.save!
86
+ @after = 1
87
+ end
88
+
89
+ it 'should have only one user' do
90
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, after: @after)
91
+ expect(test_users).not_to be_empty
92
+ expect(test_users.count).to eq 1
93
+ expect(test_users).to include @first_created_user
94
+ end
95
+ end
96
+ end
97
+
98
+ context 'that limit and after are both 1' do
99
+ context 'when user is exist only three' do
100
+ before do
101
+ user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
102
+ user.save!
103
+ @second_created_user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
104
+ @second_created_user.save!
105
+ user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
106
+ user.save!
107
+ @options = { limit: 1, after: 1 }
108
+ end
109
+
110
+ it 'should have only one user' do
111
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, @options)
112
+ expect(test_users).not_to be_empty
113
+ expect(test_users.count).to eq 1
114
+ expect(test_users).to include @second_created_user
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#collection' do
122
+ before do
123
+ user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
124
+ user.save!
125
+ @test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id)
126
+ end
127
+
128
+ it 'should equal self' do
129
+ collection = @test_users.collection
130
+ expect(collection.first).to eq @test_users.first
131
+ expect(collection.count).to eq @test_users.count
132
+ end
133
+ end
134
+
135
+ describe '#next' do
136
+ before do
137
+ @first_created_user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
138
+ @first_created_user.save!
139
+ @second_created_user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
140
+ @second_created_user.save!
141
+ @last_created_user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
142
+ @last_created_user.save!
143
+ end
144
+
145
+ it 'should get next users array' do
146
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, limit: 1)
147
+ expect(test_users.first).to eq @last_created_user
148
+ expect(test_users.next.first).to eq @second_created_user
149
+ expect(test_users.next.next.first).to eq @first_created_user
150
+ end
151
+ end
152
+
153
+ describe '#select' do
154
+ before { @expected = { limit: default_limit, after: default_after } }
155
+
156
+ subject { Facemock::FbGraph::Application::TestUsers.new(facebook_app_id).select }
157
+ it { is_expected.to eq @expected }
158
+ end
159
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::FbGraph::Application::User::Right do
4
+ let(:table_name) { "user_rights" }
5
+
6
+ describe '.table_name' do
7
+ subject { Facemock::FbGraph::Application::User::Right.table_name }
8
+ it { is_expected.to eq table_name }
9
+ end
10
+ end