my_obfuscate 0.4.2 → 0.5.0

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.
@@ -0,0 +1,276 @@
1
+ require 'spec_helper'
2
+
3
+ describe MyObfuscate::ConfigApplicator do
4
+
5
+ describe ".apply_table_config" do
6
+ it "should work on email addresses" do
7
+ 100.times do
8
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else"], {:a => {:type => :email}}, [:a, :b])
9
+ new_row.length.should == 2
10
+ new_row.first.should =~ /^[\w\.]+\@(\w+\.){2,3}[a-f0-9]{5}\.example\.com$/
11
+ end
12
+ end
13
+
14
+ it "should work on strings" do
15
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "something crazy"], {:b => {:type => :string, :length => 7}}, [:a, :b, :c])
16
+ new_row.length.should == 3
17
+ new_row[1].length.should == 7
18
+ new_row[1].should_not == "something_else"
19
+ end
20
+
21
+ describe "conditional directives" do
22
+ it "should honor :unless conditionals" do
23
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :unless => lambda { |row| row[:a] == "blah" }}}, [:a, :b, :c])
24
+ new_row[0].should_not == "123"
25
+ new_row[0].should == "blah"
26
+
27
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :unless => lambda { |row| row[:a] == "not blah" }}}, [:a, :b, :c])
28
+ new_row[0].should == "123"
29
+
30
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config([nil, "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :unless => :nil}, :b=> {:type => :fixed, :string => "123", :unless => :nil}}, [:a, :b, :c])
31
+ new_row[0].should == nil
32
+ new_row[1].should == "123"
33
+
34
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(['', "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :unless => :blank}, :b=> {:type => :fixed, :string => "123", :unless => :blank}}, [:a, :b, :c])
35
+ new_row[0].should == ''
36
+ new_row[1].should == "123"
37
+ end
38
+
39
+ it "should honor :if conditionals" do
40
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if => lambda { |row| row[:a] == "blah" }}}, [:a, :b, :c])
41
+ new_row[0].should == "123"
42
+
43
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if=> lambda { |row| row[:a] == "not blah" }}}, [:a, :b, :c])
44
+ new_row[0].should_not == "123"
45
+ new_row[0].should == "blah"
46
+
47
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config([nil, "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if => :nil}, :b=> {:type => :fixed, :string => "123", :if => :nil}}, [:a, :b, :c])
48
+ new_row[0].should == "123"
49
+ new_row[1].should == "something_else"
50
+
51
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(['', "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if => :blank}, :b=> {:type => :fixed, :string => "123", :if => :blank}}, [:a, :b, :c])
52
+ new_row[0].should == "123"
53
+ new_row[1].should == "something_else"
54
+ end
55
+
56
+ it "should supply the original row values to the conditional" do
57
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else"], {:a => {:type => :fixed, :string => "123"}, :b => {:type => :fixed, :string => "yup", :if => lambda { |row| row[:a] == "blah" }}}, [:a, :b])
58
+ new_row[0].should == "123"
59
+ new_row[1].should == "yup"
60
+ end
61
+
62
+ it "should honor combined :unless and :if conditionals" do
63
+ #both true
64
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if => lambda { |row| row[:a] == "blah" }, :unless => lambda { |row| row[:b] == "something_else" }}}, [:a, :b, :c])
65
+ new_row[0].should == "blah"
66
+
67
+ #both false
68
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if => lambda { |row| row[:a] == "not blah" }, :unless => lambda { |row| row[:b] == "not something_else" }}}, [:a, :b, :c])
69
+ new_row[0].should == "blah"
70
+
71
+ #if true, #unless false
72
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if => lambda { |row| row[:a] == "blah" }, :unless => lambda { |row| row[:b] == "not something_else" }}}, [:a, :b, :c])
73
+ new_row[0].should == "123"
74
+
75
+ #if false, #unless true
76
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a=> {:type => :fixed, :string => "123", :if => lambda { |row| row[:a] == "not blah" }, :unless => lambda { |row| row[:b] == "something_else" }}}, [:a, :b, :c])
77
+ new_row[0].should == "blah"
78
+ end
79
+ end
80
+
81
+ it "should be able to generate random integers in ranges" do
82
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:c => {:type => :integer, :between => 10..100}}, [:a, :b, :c])
83
+ new_row.length.should == 3
84
+ new_row[2].to_i.to_s.should == new_row[2] # It should be an integer.
85
+ new_row[2].should_not == "5"
86
+ end
87
+
88
+ it "should be able to substitute fixed strings" do
89
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:b => {:type => :fixed, :string => "hello"}}, [:a, :b, :c])
90
+ new_row.length.should == 3
91
+ new_row[1].should == "hello"
92
+ end
93
+
94
+ it "should be able to substitute a proc that returns a string" do
95
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:b => {:type => :fixed, :string => proc { "Hello World" }}}, [:a, :b, :c])
96
+ new_row.length.should == 3
97
+ new_row[1].should == "Hello World"
98
+ end
99
+
100
+ it "should provide the row to the proc" do
101
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:b => {:type => :fixed, :string => proc { |a| a[:b] }}}, [:a, :b, :c])
102
+ new_row.length.should == 3
103
+ new_row[1].should == "something_else"
104
+ end
105
+
106
+ it "should be able to substitute fixed strings from a random set" do
107
+ looking_for = ["hello", "world"]
108
+ original_looking_for = looking_for.dup
109
+ guard = 0
110
+ while !looking_for.empty? && guard < 1000
111
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => {:type => :fixed, :one_of => ["hello", "world"]}}, [:a, :b, :c])
112
+ new_row.length.should == 3
113
+ original_looking_for.should include(new_row[0])
114
+ looking_for.delete new_row[0]
115
+ guard += 1
116
+ end
117
+ looking_for.should be_empty
118
+ end
119
+
120
+ it "should treat a symbol in the column definition as an implicit { :type => symbol }" do
121
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:b => :null, :a => :keep}, [:a, :b, :c])
122
+ new_row.length.should == 3
123
+ new_row[0].should == "blah"
124
+ new_row[1].should == nil
125
+ end
126
+
127
+ it "should be able to set things NULL" do
128
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:b => {:type => :null}}, [:a, :b, :c])
129
+ new_row.length.should == 3
130
+ new_row[1].should == nil
131
+ end
132
+
133
+ it "should be able to :keep the value the same" do
134
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:b => {:type => :keep}}, [:a, :b, :c])
135
+ new_row.length.should == 3
136
+ new_row[1].should == "something_else"
137
+ end
138
+
139
+ it "should keep the value when given an unknown type, but should display a warning" do
140
+ $stderr = error_output = StringIO.new
141
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:b => {:type => :unknown_type}}, [:a, :b, :c])
142
+ $stderr = STDERR
143
+ new_row.length.should == 3
144
+ new_row[1].should == "something_else"
145
+ error_output.rewind
146
+ error_output.read.should =~ /Keeping a column value by.*?unknown_type/
147
+ end
148
+
149
+ it "should be able to substitute lorem ipsum text" do
150
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :lorem, :b => {:type => :lorem, :number => 2}}, [:a, :b, :c])
151
+ new_row.length.should == 3
152
+ new_row[0].should_not == "blah"
153
+ new_row[0].should_not =~ /\w\.(?!\Z)/
154
+ new_row[1].should_not == "something_else"
155
+ new_row[1].should =~ /\w\.(?!\Z)/
156
+ end
157
+
158
+ it "should be able to generate an :company" do
159
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["Smith and Sons", "something_else", "5"], {:a => :company}, [:a, :b, :c])
160
+ new_row.length.should == 3
161
+ new_row[0].should_not == "Smith and Sons"
162
+ new_row[0].should =~ /\w+/
163
+ end
164
+
165
+ it "should be able to generate an :url" do
166
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["http://mystuff.blogger.com", "something_else", "5"], {:a => :url}, [:a, :b, :c])
167
+ new_row.length.should == 3
168
+ new_row[0].should_not == "http://mystuff.blogger.com"
169
+ new_row[0].should =~ /http:\/\/\w+/
170
+ end
171
+
172
+ it "should be able to generate an :ipv4" do
173
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["1.2.3.4", "something_else", "5"], {:a => :ipv4}, [:a, :b, :c])
174
+ new_row.length.should == 3
175
+ new_row[0].should_not == "1.2.3.4"
176
+ new_row[0].should =~ /\d+\.\d+\.\d+\.\d+/
177
+ end
178
+
179
+ it "should be able to generate an :ipv6" do
180
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["fe80:0000:0000:0000:0202:b3ff:fe1e:8329", "something_else", "5"], {:a => :ipv6}, [:a, :b, :c])
181
+ new_row.length.should == 3
182
+ new_row[0].should_not == "fe80:0000:0000:0000:0202:b3ff:fe1e:8329"
183
+ new_row[0].should =~ /[0-9a-f:]+/
184
+ end
185
+
186
+ it "should be able to generate an :address" do
187
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :address}, [:a, :b, :c])
188
+ new_row.length.should == 3
189
+ new_row[0].should_not == "blah"
190
+ new_row[0].should =~ /\d+ \w/
191
+ end
192
+
193
+ it "should be able to generate a :name" do
194
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :name}, [:a, :b, :c])
195
+ new_row.length.should == 3
196
+ new_row[0].should_not == "blah"
197
+ new_row[0].should =~ / /
198
+ end
199
+
200
+ it "should be able to generate just a street address" do
201
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :street_address}, [:a, :b, :c])
202
+ new_row.length.should == 3
203
+ new_row[0].should_not == "blah"
204
+ new_row[0].should =~ /\d+ \w/
205
+ end
206
+
207
+ it "should be able to generate a city" do
208
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :city}, [:a, :b, :c])
209
+ new_row.length.should == 3
210
+ new_row[0].should_not == "blah"
211
+ end
212
+
213
+ it "should be able to generate a state" do
214
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :state}, [:a, :b, :c])
215
+ new_row.length.should == 3
216
+ new_row[0].should_not == "blah"
217
+ end
218
+
219
+ it "should be able to generate a zip code" do
220
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :zip_code}, [:a, :b, :c])
221
+ new_row.length.should == 3
222
+ new_row[0].should_not == "blah"
223
+ new_row[0].should =~ /\d+/
224
+ end
225
+
226
+ it "should be able to generate a phone number" do
227
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["blah", "something_else", "5"], {:a => :phone}, [:a, :b, :c])
228
+ new_row.length.should == 3
229
+ new_row[0].should_not == "blah"
230
+ new_row[0].should =~ /\d+/
231
+ end
232
+
233
+ describe "when faker generates values with quotes in them" do
234
+ before do
235
+ Faker::Address.stub(:city).and_return("O'ReillyTown")
236
+ Faker::Name.stub(:name).and_return("Foo O'Reilly")
237
+ Faker::Name.stub(:first_name).and_return("O'Foo")
238
+ Faker::Name.stub(:last_name).and_return("O'Reilly")
239
+ Faker::Lorem.stub(:sentences).with(any_args).and_return(["Foo bar O'Thingy"])
240
+ end
241
+
242
+ it "should remove single quotes from the value" do
243
+ new_row = MyObfuscate::ConfigApplicator.apply_table_config(["address", "city", "first", "last", "fullname", "some text"],
244
+ {:a => :address, :b => :city, :c => :first_name, :d => :last_name, :e => :name, :f => :lorem},
245
+ [:a, :b, :c, :d, :e, :f])
246
+ new_row.each {|value| value.should_not include("'")}
247
+ end
248
+ end
249
+ end
250
+
251
+ describe ".row_as_hash" do
252
+ it "will map row values into a hash with column names as keys" do
253
+ MyObfuscate::ConfigApplicator.row_as_hash([1, 2, 3, 4], [:a, :b, :c, :d]).should == {:a => 1, :b => 2, :c => 3, :d => 4}
254
+ end
255
+ end
256
+
257
+ describe ".random_english_sentences" do
258
+ before do
259
+ File.should_receive(:read).once.and_return("hello 2")
260
+ end
261
+
262
+ after do
263
+ MyObfuscate::ConfigApplicator.class_variable_set(:@@walker_method, nil)
264
+ end
265
+
266
+ it "should only load file data once" do
267
+ MyObfuscate::ConfigApplicator.random_english_sentences(1)
268
+ MyObfuscate::ConfigApplicator.random_english_sentences(1)
269
+ end
270
+
271
+ it "should make random sentences" do
272
+ MyObfuscate::ConfigApplicator.random_english_sentences(2).should =~ /^(Hello( hello)+\.\s*){2}$/
273
+ end
274
+ end
275
+
276
+ end
@@ -1,9 +1,66 @@
1
1
  require 'spec_helper'
2
- require 'my_obfuscate/database_helper_shared_examples'
3
2
 
4
3
  describe MyObfuscate::Mysql do
5
4
 
6
- it_behaves_like MyObfuscate::DatabaseHelperShared
5
+ describe "#rows_to_be_inserted" do
6
+ it "should split a mysql string into fields" do
7
+ string = "INSERT INTO `some_table` (thing1,thing2) VALUES ('bob@bob.com','bob', 'somethingelse1', 25, '2', 10, 'hi') ; "
8
+ fields = [['bob@bob.com', 'bob', 'somethingelse1', '25', '2', '10', "hi"]]
9
+ subject.rows_to_be_inserted(string).should == fields
10
+ end
11
+
12
+ it "should work ok with escaped characters" do
13
+ string = "INSERT INTO `some_table` (thing1,thing2) VALUES ('bob,@bob.c , om', 'bo\\', b', 'some\"thin\\gel\\\\\\'se1', 25, '2', 10, 'hi', 5) ; "
14
+ fields = [['bob,@bob.c , om', 'bo\\\', b', 'some"thin\\gel\\\\\\\'se1', '25', '2', '10', "hi", "5"]]
15
+ subject.rows_to_be_inserted(string).should == fields
16
+ end
17
+
18
+ it "should work with multiple subinserts" do
19
+ string = "INSERT INTO `some_table` (thing1,thing2) VALUES (1,2,3, '((m))(oo()s,e'), ('bob,@bob.c , om', 'bo\\', b', 'some\"thin\\gel\\\\\\'se1', 25, '2', 10, 'hi', 5) ;"
20
+ fields = [["1", "2", "3", "((m))(oo()s,e"], ['bob,@bob.c , om', 'bo\\\', b', 'some"thin\\gel\\\\\\\'se1', '25', '2', '10', "hi", "5"]]
21
+ subject.rows_to_be_inserted(string).should == fields
22
+ end
23
+
24
+ it "should work ok with NULL values" do
25
+ string = "INSERT INTO `some_table` (thing1,thing2) VALUES (NULL , 'bob@bob.com','bob', NULL, 25, '2', NULL, 'hi', NULL ); "
26
+ fields = [[nil, 'bob@bob.com', 'bob', nil, '25', '2', nil, "hi", nil]]
27
+ subject.rows_to_be_inserted(string).should == fields
28
+ end
29
+
30
+ it "should work with empty strings" do
31
+ string = "INSERT INTO `some_table` (thing1,thing2) VALUES (NULL , '', '' , '', 25, '2','', 'hi','') ;"
32
+ fields = [[nil, '', '', '', '25', '2', '', "hi", '']]
33
+ subject.rows_to_be_inserted(string).should == fields
34
+ end
35
+
36
+ it "should work with hex encoded blobs" do
37
+ string = "INSERT INTO `some_table` (thing1,thing2) VALUES ('bla' , 'blobdata', 'blubb' , 0xACED00057372001F6A6176612E7574696C2E436F6C6C656) ;"
38
+ fields = [['bla', 'blobdata', 'blubb', '0xACED00057372001F6A6176612E7574696C2E436F6C6C656']]
39
+ subject.rows_to_be_inserted(string).should == fields
40
+ end
41
+ end
42
+
43
+ describe "#make_valid_value_string" do
44
+ it "should work with nil values" do
45
+ value = nil
46
+ subject.make_valid_value_string(value).should == 'NULL'
47
+ end
48
+
49
+ it "should work with hex-encoded blob data" do
50
+ value = "0xACED00057372001F6A6176612E7574696C2E436F6C6C656"
51
+ subject.make_valid_value_string(value).should == '0xACED00057372001F6A6176612E7574696C2E436F6C6C656'
52
+ end
53
+
54
+ it "should quote hex-encoded ALIKE data" do
55
+ value = "40x17x7"
56
+ subject.make_valid_value_string(value).should == "'40x17x7'"
57
+ end
58
+
59
+ it "should quote all other values" do
60
+ value = "hello world"
61
+ subject.make_valid_value_string(value).should == "'hello world'"
62
+ end
63
+ end
7
64
 
8
65
  describe "#parse_insert_statement" do
9
66
  it "should return nil for other SQL syntaxes (MS SQL Server)" do
@@ -1,23 +1,43 @@
1
1
  require 'spec_helper'
2
- require 'my_obfuscate/database_helper_shared_examples'
3
2
 
4
3
  describe MyObfuscate::Postgres do
5
4
 
6
- it_behaves_like MyObfuscate::DatabaseHelperShared
5
+ let(:helper) { MyObfuscate::Postgres.new }
7
6
 
8
- describe "#parse_insert_statement" do
9
- it "should return nil for other SQL syntaxes (MS SQL Server)" do
10
- subject.parse_insert_statement("INSERT [dbo].[TASKS] ([TaskID], [TaskName]) VALUES (61, N'Report Thing')").should be_nil
7
+ describe "#rows_to_be_inserted" do
8
+ it 'splits tab seperated values' do
9
+ line = "1 2 3 4"
10
+ helper.rows_to_be_inserted(line).should == [["1","2","3","4"]]
11
11
  end
12
12
 
13
- it "should return nil for MySQL non-insert statements" do
14
- subject.parse_insert_statement("CREATE TABLE `some_table`;").should be_nil
13
+ it 'ignores the newline character at the end of string' do
14
+ line = "1 2 3 4\n"
15
+ helper.rows_to_be_inserted(line).should == [["1","2","3","4"]]
15
16
  end
16
17
 
17
- it "should return a hash of table name, column names for MySQL insert statements" do
18
- hash = subject.parse_insert_statement("INSERT INTO some_table (email, name, something, age) VALUES ('bob@honk.com','bob', 'some\\'thin,ge())lse1', 25),('joe@joe.com','joe', 'somethingelse2', 54);")
19
- hash.should == {:table_name => :some_table, :column_names => [:email, :name, :something, :age]}
18
+ it "doesn't ignore newline characters in the string" do
19
+ line = "1 2 3\n4 5"
20
+ helper.rows_to_be_inserted(line).should == [["1","2","3\n4","5"]]
21
+ end
22
+
23
+ it "replaces \\N with nil" do
24
+ line = "1 2 \\N 4"
25
+ helper.rows_to_be_inserted(line).should == [["1","2",nil,"4"]]
20
26
  end
21
27
  end
22
28
 
29
+ describe "#parse_copy_statement" do
30
+ it 'parses table name and column names' do
31
+ line = "COPY some_table (id, email, name, something) FROM stdin;"
32
+ hash = helper.parse_copy_statement(line)
33
+ hash[:table_name].should == :some_table
34
+ hash[:column_names].should == [:id, :email, :name, :something]
35
+ end
36
+ end
37
+
38
+ describe "#make_insert_statement" do
39
+ it 'creates a string with tab delminted' do
40
+ helper.make_insert_statement(:some_table, [:id, :name], ['1', '2']).should == "1 2"
41
+ end
42
+ end
23
43
  end