facemock 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.coveralls.yml +1 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +154 -0
  8. data/Rakefile +6 -0
  9. data/db/.gitkeep +0 -0
  10. data/facemock.gemspec +31 -0
  11. data/lib/facemock/config.rb +88 -0
  12. data/lib/facemock/database/application.rb +20 -0
  13. data/lib/facemock/database/permission.rb +21 -0
  14. data/lib/facemock/database/table.rb +340 -0
  15. data/lib/facemock/database/user.rb +26 -0
  16. data/lib/facemock/database.rb +121 -0
  17. data/lib/facemock/errors.rb +7 -0
  18. data/lib/facemock/fb_graph/application/test_users.rb +36 -0
  19. data/lib/facemock/fb_graph/application/user/permission.rb +10 -0
  20. data/lib/facemock/fb_graph/application/user.rb +69 -0
  21. data/lib/facemock/fb_graph/application.rb +48 -0
  22. data/lib/facemock/fb_graph/user.rb +13 -0
  23. data/lib/facemock/fb_graph.rb +30 -0
  24. data/lib/facemock/version.rb +3 -0
  25. data/lib/facemock.rb +19 -0
  26. data/spec/facemock/config_spec.rb +185 -0
  27. data/spec/facemock/database/application_spec.rb +73 -0
  28. data/spec/facemock/database/permission_spec.rb +52 -0
  29. data/spec/facemock/database/tables_spec.rb +728 -0
  30. data/spec/facemock/database/user_spec.rb +169 -0
  31. data/spec/facemock/database_spec.rb +270 -0
  32. data/spec/facemock/errors_spec.rb +9 -0
  33. data/spec/facemock/fb_graph/application/test_users_spec.rb +155 -0
  34. data/spec/facemock/fb_graph/application/user_spec.rb +208 -0
  35. data/spec/facemock/fb_graph/application_spec.rb +132 -0
  36. data/spec/facemock/fb_graph/user_spec.rb +36 -0
  37. data/spec/facemock/fb_graph_spec.rb +47 -0
  38. data/spec/facemock_spec.rb +74 -0
  39. data/spec/spec_helper.rb +18 -0
  40. data/spec/support/tables_helper.rb +46 -0
  41. metadata +64 -3
@@ -0,0 +1,3 @@
1
+ module Facemock
2
+ VERSION = '0.0.5'
3
+ end
data/lib/facemock.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "facemock/version"
2
+ require "facemock/fb_graph"
3
+ require "facemock/config"
4
+
5
+ module Facemock
6
+ extend self
7
+
8
+ def on
9
+ Facemock::FbGraph.on
10
+ end
11
+
12
+ def off
13
+ Facemock::FbGraph.off
14
+ end
15
+
16
+ def on?
17
+ FbGraph == Facemock::FbGraph
18
+ end
19
+ end
@@ -0,0 +1,185 @@
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::Database).to be_truthy
10
+ end
11
+
12
+ before { stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name) }
13
+
14
+ describe '#default_database' do
15
+ before do
16
+ allow_any_instance_of(Facemock::Database).to receive(:connect) { true }
17
+ allow_any_instance_of(Facemock::Database).to receive(:create_tables) { true }
18
+ allow_any_instance_of(Facemock::Database).to receive(:disconnect!) { true }
19
+ end
20
+
21
+ subject { Facemock::Config.default_database }
22
+ it { is_expected.to be_truthy }
23
+
24
+ describe '.name' do
25
+ subject { Facemock::Config.default_database.name }
26
+ it { is_expected.not_to be_nil }
27
+ it { is_expected.not_to be_empty }
28
+ end
29
+ end
30
+
31
+ describe '#database' do
32
+ before do
33
+ allow_any_instance_of(Facemock::Database).to receive(:connect) { true }
34
+ allow_any_instance_of(Facemock::Database).to receive(:create_tables) { true }
35
+ allow_any_instance_of(Facemock::Database).to receive(:disconnect!) { true }
36
+ end
37
+
38
+ subject { Facemock::Config.database }
39
+ it { is_expected.to be_truthy }
40
+
41
+ describe '.name' do
42
+ subject { Facemock::Config.database.name }
43
+ it { is_expected.not_to be_nil }
44
+ it { is_expected.not_to be_empty }
45
+ end
46
+ end
47
+
48
+ describe '#reset_database' do
49
+ context 'when does not set database' do
50
+ subject { Facemock::Config.reset_database }
51
+ it { is_expected.to eq nil }
52
+ end
53
+
54
+ context 'when already set database' do
55
+ before do
56
+ stub_const("Facemock::Database::DEFAULT_DATABASE_NAME", db_name)
57
+ @database = Facemock::Database.new
58
+ end
59
+
60
+ subject { Facemock::Config.reset_database }
61
+ it { is_expected.to eq nil }
62
+
63
+ after { @database.drop }
64
+ end
65
+ end
66
+
67
+ describe '#load_users' do
68
+ let(:user1) { { identifier: "100000000000001",
69
+ name: "test user one",
70
+ email: "test_user_one@example.com",
71
+ password: "testpass" } }
72
+ let(:user2) { { identifier: "100000000000002",
73
+ name: "test user two",
74
+ email: "test_user_two@example.com",
75
+ password: "testpass" } }
76
+ let(:user3) { { identifier: 100000000000003,
77
+ name: "test user three",
78
+ email: "test_user_three@example.com",
79
+ password: "testpass" } }
80
+
81
+ let(:app1_id) { "000000000000001" }
82
+ let(:app1_secret) { "test_secret_one" }
83
+ let(:app1_users) { [user1, user2] }
84
+ let(:app1) { { app_id: app1_id, app_secret: app1_secret, users: app1_users } }
85
+
86
+ let(:app2_id) { 000000000000002 }
87
+ let(:app2_secret) { "test_secret_two" }
88
+ let(:app2_users) { [user3] }
89
+ let(:app2) { { app_id: app2_id, app_secret: app2_secret, users: app2_users } }
90
+
91
+ let(:yaml_load_data) { [ app1, app2 ] }
92
+
93
+ context 'without argument' do
94
+ subject { lambda { Facemock::Config.load_users } }
95
+ it { is_expected.to raise_error ArgumentError }
96
+ end
97
+
98
+ context 'with yaml file path' do
99
+ before do
100
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
101
+ @database = Facemock::Database.new
102
+ end
103
+ after { @database.drop }
104
+
105
+ context 'but file does not exist' do
106
+ subject { lambda { Facemock::Config.load_users("testdata.yml") } }
107
+ it { is_expected.to raise_error Errno::ENOENT }
108
+ end
109
+
110
+ def create_temporary_yaml_file(data)
111
+ path = Tempfile.open(ymlfile) do |tempfile|
112
+ tempfile.puts YAML.dump(data)
113
+ tempfile.path
114
+ end
115
+ end
116
+
117
+ shared_context 'app and user should not be created', assert: :incorrect_data_format do
118
+ subject { lambda { Facemock::Config.load_users(@path) } }
119
+ it { is_expected.to raise_error Facemock::Errors::IncorrectDataFormat }
120
+
121
+ it 'app and user should not be created' do
122
+ begin
123
+ Facemock::Config.load_users(@path)
124
+ rescue => error
125
+ expect(Facemock::Database::Application.all).to be_empty
126
+ expect(Facemock::Database::User.all).to be_empty
127
+ end
128
+ end
129
+ end
130
+
131
+ context 'but incorrect format' do
132
+ context 'when load data is not array', assert: :incorrect_data_format do
133
+ before do
134
+ users_data = ""
135
+ @path = create_temporary_yaml_file(users_data)
136
+ end
137
+ end
138
+
139
+ context 'when app id does not exist', assert: :incorrect_data_format do
140
+ before do
141
+ users_data = [ { app_secret: app1_secret, users: app1_users } ]
142
+ @path = create_temporary_yaml_file(users_data)
143
+ end
144
+ end
145
+
146
+ context 'when app secret does not exist', assert: :incorrect_data_format do
147
+ before do
148
+ users_data = [ { app_id: app1_id, users: app1_users } ]
149
+ @path = create_temporary_yaml_file(users_data)
150
+ end
151
+ end
152
+
153
+ context 'when users does not exist', assert: :incorrect_data_format do
154
+ before do
155
+ users_data = [ { app_id: app1_id, app_secret: app1_secret } ]
156
+ @path = create_temporary_yaml_file(users_data)
157
+ end
158
+ end
159
+
160
+ context 'when users have only identifier', assert: :incorrect_data_format do
161
+ before do
162
+ users_data = [ { app_id: app1_id, app_secret: app1_secret,
163
+ users: [ { identifier: "100000000000001" } ] } ]
164
+ @path = create_temporary_yaml_file(users_data)
165
+ end
166
+ end
167
+ end
168
+
169
+ context 'yaml is correct format' do
170
+ before { @path = create_temporary_yaml_file(yaml_load_data) }
171
+
172
+ subject { lambda { Facemock::Config.load_users(@path) } }
173
+ it { is_expected.not_to raise_error }
174
+
175
+ it 'app and user should be created' do
176
+ app_count = yaml_load_data.size
177
+ user_count = yaml_load_data.inject(0){|count, data| count += data[:users].size }
178
+ Facemock::Config.load_users(@path)
179
+ expect(Facemock::Database::Application.all.count).to eq app_count
180
+ expect(Facemock::Database::User.all.count).to eq user_count
181
+ end
182
+ end
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::Database::Application do
4
+ let(:table_name) { :applications }
5
+ let(:column_names) { [:id, :secret, :created_at] }
6
+
7
+ let(:id) { 1 }
8
+ let(:secret) { "test_secret" }
9
+ let(:created_at) { Time.now }
10
+ let(:options) { { id: id, secret: secret, created_at: created_at } }
11
+
12
+ describe '::TABLE_NAME' do
13
+ subject { Facemock::Database::Application::TABLE_NAME }
14
+ it { is_expected.to eq table_name }
15
+ end
16
+
17
+ describe '::COLUMN_NAMES' do
18
+ subject { Facemock::Database::Application::COLUMN_NAMES }
19
+ it { is_expected.to eq column_names }
20
+ end
21
+
22
+ describe '#initialize' do
23
+ context 'without option' do
24
+ subject { Facemock::Database::Application.new }
25
+ it { is_expected.to be_kind_of Facemock::Database::Application }
26
+
27
+ describe '.id' do
28
+ subject { Facemock::Database::Application.new.id }
29
+ it { is_expected.to be > 0 }
30
+ end
31
+
32
+ describe '.secret' do
33
+ subject { Facemock::Database::Application.new.secret }
34
+ it { is_expected.to be_kind_of String }
35
+
36
+ describe '.size' do
37
+ subject { Facemock::Database::Application.new.secret.size }
38
+ it { is_expected.to eq 128 }
39
+ end
40
+ end
41
+
42
+ describe '.created_at' do
43
+ subject { Facemock::Database::Application.new.created_at }
44
+ it { is_expected.to be_nil }
45
+ end
46
+ end
47
+
48
+ context 'with id option but it is not integer' do
49
+ before { @opts = { id: "test_id" } }
50
+ subject { Facemock::Database::Application.new(@opts) }
51
+ it { is_expected.to be_kind_of Facemock::Database::Application }
52
+
53
+ describe '.id' do
54
+ subject { Facemock::Database::Application.new(@opts).id }
55
+ it { is_expected.to be > 0 }
56
+ end
57
+ end
58
+
59
+ context 'with all options' do
60
+ subject { Facemock::Database::Application.new(options) }
61
+ it { is_expected.to be_kind_of Facemock::Database::Application }
62
+
63
+ context 'then attributes' do
64
+ it 'should set specified values by option' do
65
+ column_names.each do |column_name|
66
+ value = Facemock::Database::Application.new(options).send(column_name)
67
+ expect(value).to eq options[column_name]
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::Database::Permission do
4
+ let(:table_name) { :permissions }
5
+ let(:column_names) { [ :id, :name, :user_id, :created_at ] }
6
+
7
+ let(:id) { 1 }
8
+ let(:name) { "read_stream" }
9
+ let(:user_id) { 1 }
10
+ let(:created_at) { Time.now }
11
+ let(:options) { { id: id, name: name, user_id: user_id, created_at: created_at } }
12
+
13
+ describe '::TABLE_NAME' do
14
+ subject { Facemock::Database::Permission::TABLE_NAME }
15
+ it { is_expected.to eq table_name }
16
+ end
17
+
18
+ describe '::COLUMN_NAMES' do
19
+ subject { Facemock::Database::Permission::COLUMN_NAMES }
20
+ it { is_expected.to eq column_names }
21
+ end
22
+
23
+ describe '#initialize' do
24
+ context 'without option' do
25
+ subject { Facemock::Database::Permission.new }
26
+ it { is_expected.to be_kind_of Facemock::Database::Permission }
27
+
28
+ context 'then attributes' do
29
+ it 'should be nil' do
30
+ column_names.each do |column_name|
31
+ value = Facemock::Database::Permission.new.send(column_name)
32
+ expect(value).to be_nil
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ context 'with all options' do
39
+ subject { Facemock::Database::Permission.new(options) }
40
+ it { is_expected.to be_kind_of Facemock::Database::Permission }
41
+
42
+ context 'then attributes' do
43
+ it 'should set specified value by option' do
44
+ column_names.each do |column_name|
45
+ value = Facemock::Database::Permission.new(options).send(column_name)
46
+ expect(value).to eq options[column_name]
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end