symmetric-encryption 3.6.0 → 3.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,539 +1,542 @@
1
- $LOAD_PATH.unshift File.dirname(__FILE__)
2
- require 'mongo_mapper'
3
- require 'test_helper'
4
- require 'symmetric_encryption/extensions/mongo_mapper/plugins/encrypted_key'
5
-
6
- # Initialize MongoMapper
7
- config_file = File.join('test', 'config', 'mongo_mapper.yml')
8
- config = YAML.load(ERB.new(File.read(config_file)).result)
9
- MongoMapper.setup(config, 'test', logger: SemanticLogger['Mongo'])
10
-
11
- class MongoMapperUser
12
- include MongoMapper::Document
13
-
14
- key :name, String
15
- encrypted_key :bank_account_number, String
16
- encrypted_key :social_security_number, String
17
- encrypted_key :string, String, encrypted: { random_iv: true }
18
- encrypted_key :long_string, String, encrypted: { random_iv: true, compress: true }
19
-
20
- # Valid Types: String, Integer, Float, BigDecimal, DateTime, Time, Date, Hash
21
- encrypted_key :integer_value, Integer
22
- encrypted_key :aliased_integer_value, Integer, encrypted: { encrypt_as: :aiv }
23
- encrypted_key :float_value, Float
24
- encrypted_key :decimal_value, BigDecimal
25
- encrypted_key :datetime_value, DateTime
26
- encrypted_key :time_value, Time
27
- encrypted_key :date_value, Date
28
- encrypted_key :true_value, Boolean
29
- encrypted_key :false_value, Boolean
30
- encrypted_key :data_json, Hash, encrypted: {random_iv: true, compress: true}
31
- encrypted_key :data_yaml, Hash, encrypted: {random_iv: true, compress: true, type: :yaml}
32
-
33
- validates :encrypted_bank_account_number, symmetric_encryption: true
34
- validates :encrypted_social_security_number, symmetric_encryption: true
35
- end
36
-
37
- #
38
- # Unit Tests for MongoMapper
39
- #
40
- class MongoMapperTest < Test::Unit::TestCase
41
- context 'MongoMapperUser' do
42
- setup do
43
- @bank_account_number = "1234567890"
44
- @bank_account_number_encrypted = "QEVuQwIAL94ArJeFlJrZp6SYsvoOGA=="
45
-
46
- @social_security_number = "987654321"
47
- @social_security_number_encrypted = "QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA=="
48
-
49
- @integer = 32768
50
- @integer_encrypted = "FA3smFQEKqB/ITv+A0xACg=="
51
-
52
- @float = 0.9867
53
- @float_encrypted = "z7Pwt2JDp74d+u0IXFAdrQ=="
54
-
55
- @date = Date.parse('20120320')
56
- @date_encrypted = "WTkSPHo5ApSSHBJMxxWt2A=="
57
-
58
- @string = "A string containing some data to be encrypted with a random initialization vector"
59
- @long_string = "A string containing some data to be encrypted with a random initialization vector and compressed since it takes up so much space in plain text form"
60
-
61
- @integer_value = 12
62
- @float_value = 88.12345
63
- @decimal_value = BigDecimal.new("22.51")
64
- @datetime_value = DateTime.new(2001, 11, 26, 20, 55, 54, "-5")
65
- @time_value = Time.new(2013, 01, 01, 22, 30, 00, "-04:00")
66
- @date_value = Date.new(1927, 04, 02)
67
- @h = { a: 'A', b: 'B' }
68
-
69
- @user = MongoMapperUser.new(
70
- encrypted_bank_account_number: @bank_account_number_encrypted,
71
- encrypted_social_security_number: @social_security_number_encrypted,
72
- name: "Joe Bloggs",
73
- # data type specific fields
74
- integer_value: @integer_value,
75
- aliased_integer_value: @integer_value,
76
- float_value: @float_value,
77
- decimal_value: @decimal_value,
78
- datetime_value: @datetime_value,
79
- time_value: @time_value,
80
- date_value: @date_value,
81
- true_value: true,
82
- false_value: false,
83
- data_yaml: @h.dup,
84
- data_json: @h.dup
85
- )
86
- end
87
-
88
- should "have encrypted methods" do
89
- assert_equal true, @user.respond_to?(:encrypted_bank_account_number)
90
- assert_equal true, @user.respond_to?(:encrypted_social_security_number)
91
- assert_equal true, @user.respond_to?(:encrypted_string)
92
- assert_equal true, @user.respond_to?(:encrypted_long_string)
93
- assert_equal false, @user.respond_to?(:encrypted_name)
94
-
95
- assert_equal true, @user.respond_to?(:encrypted_bank_account_number=)
96
- assert_equal true, @user.respond_to?(:encrypted_social_security_number=)
97
- assert_equal true, @user.respond_to?(:encrypted_string=)
98
- assert_equal true, @user.respond_to?(:encrypted_long_string=)
99
- assert_equal false, @user.respond_to?(:encrypted_name=)
100
- end
1
+ begin
2
+ require 'mongo_mapper'
3
+ require_relative 'test_helper'
4
+ require_relative '../lib/symmetric_encryption/extensions/mongo_mapper/plugins/encrypted_key'
5
+
6
+ # Initialize MongoMapper
7
+ config_file = File.join('test', 'config', 'mongo_mapper.yml')
8
+ config = YAML.load(ERB.new(File.read(config_file)).result)
9
+ MongoMapper.setup(config, 'test', logger: SemanticLogger['Mongo'])
10
+
11
+ class MongoMapperUser
12
+ include MongoMapper::Document
13
+
14
+ key :name, String
15
+ encrypted_key :bank_account_number, String
16
+ encrypted_key :social_security_number, String
17
+ encrypted_key :string, String, encrypted: { random_iv: true }
18
+ encrypted_key :long_string, String, encrypted: { random_iv: true, compress: true }
19
+
20
+ # Valid Types: String, Integer, Float, BigDecimal, DateTime, Time, Date, Hash
21
+ encrypted_key :integer_value, Integer
22
+ encrypted_key :aliased_integer_value, Integer, encrypted: { encrypt_as: :aiv }
23
+ encrypted_key :float_value, Float
24
+ encrypted_key :decimal_value, BigDecimal
25
+ encrypted_key :datetime_value, DateTime
26
+ encrypted_key :time_value, Time
27
+ encrypted_key :date_value, Date
28
+ encrypted_key :true_value, Boolean
29
+ encrypted_key :false_value, Boolean
30
+ encrypted_key :data_json, Hash, encrypted: {random_iv: true, compress: true}
31
+ encrypted_key :data_yaml, Hash, encrypted: {random_iv: true, compress: true, type: :yaml}
32
+
33
+ validates :encrypted_bank_account_number, symmetric_encryption: true
34
+ validates :encrypted_social_security_number, symmetric_encryption: true
35
+ end
101
36
 
102
- should "have unencrypted methods" do
103
- assert_equal true, @user.respond_to?(:bank_account_number)
104
- assert_equal true, @user.respond_to?(:social_security_number)
105
- assert_equal true, @user.respond_to?(:string)
106
- assert_equal true, @user.respond_to?(:long_string)
107
- assert_equal true, @user.respond_to?(:name)
108
-
109
- assert_equal true, @user.respond_to?(:bank_account_number=)
110
- assert_equal true, @user.respond_to?(:social_security_number=)
111
- assert_equal true, @user.respond_to?(:string=)
112
- assert_equal true, @user.respond_to?(:long_string=)
113
- assert_equal true, @user.respond_to?(:name=)
114
- end
37
+ #
38
+ # Unit Tests for MongoMapper
39
+ #
40
+ class MongoMapperTest < Minitest::Test
41
+ context 'MongoMapperUser' do
42
+ setup do
43
+ @bank_account_number = "1234567890"
44
+ @bank_account_number_encrypted = "QEVuQwIAL94ArJeFlJrZp6SYsvoOGA=="
45
+
46
+ @social_security_number = "987654321"
47
+ @social_security_number_encrypted = "QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA=="
48
+
49
+ @integer = 32768
50
+ @integer_encrypted = "FA3smFQEKqB/ITv+A0xACg=="
51
+
52
+ @float = 0.9867
53
+ @float_encrypted = "z7Pwt2JDp74d+u0IXFAdrQ=="
54
+
55
+ @date = Date.parse('20120320')
56
+ @date_encrypted = "WTkSPHo5ApSSHBJMxxWt2A=="
57
+
58
+ @string = "A string containing some data to be encrypted with a random initialization vector"
59
+ @long_string = "A string containing some data to be encrypted with a random initialization vector and compressed since it takes up so much space in plain text form"
60
+
61
+ @integer_value = 12
62
+ @float_value = 88.12345
63
+ @decimal_value = BigDecimal.new("22.51")
64
+ @datetime_value = DateTime.new(2001, 11, 26, 20, 55, 54, "-5")
65
+ @time_value = Time.new(2013, 01, 01, 22, 30, 00, "-04:00")
66
+ @date_value = Date.new(1927, 04, 02)
67
+ @h = { a: 'A', b: 'B' }
68
+
69
+ @user = MongoMapperUser.new(
70
+ encrypted_bank_account_number: @bank_account_number_encrypted,
71
+ encrypted_social_security_number: @social_security_number_encrypted,
72
+ name: "Joe Bloggs",
73
+ # data type specific fields
74
+ integer_value: @integer_value,
75
+ aliased_integer_value: @integer_value,
76
+ float_value: @float_value,
77
+ decimal_value: @decimal_value,
78
+ datetime_value: @datetime_value,
79
+ time_value: @time_value,
80
+ date_value: @date_value,
81
+ true_value: true,
82
+ false_value: false,
83
+ data_yaml: @h.dup,
84
+ data_json: @h.dup
85
+ )
86
+ end
115
87
 
116
- should "support aliased fields" do
117
- assert_equal true, @user.respond_to?(:aliased_integer_value=)
118
- assert_equal true, @user.respond_to?(:aliased_integer_value)
119
- end
88
+ should "have encrypted methods" do
89
+ assert_equal true, @user.respond_to?(:encrypted_bank_account_number)
90
+ assert_equal true, @user.respond_to?(:encrypted_social_security_number)
91
+ assert_equal true, @user.respond_to?(:encrypted_string)
92
+ assert_equal true, @user.respond_to?(:encrypted_long_string)
93
+ assert_equal false, @user.respond_to?(:encrypted_name)
94
+
95
+ assert_equal true, @user.respond_to?(:encrypted_bank_account_number=)
96
+ assert_equal true, @user.respond_to?(:encrypted_social_security_number=)
97
+ assert_equal true, @user.respond_to?(:encrypted_string=)
98
+ assert_equal true, @user.respond_to?(:encrypted_long_string=)
99
+ assert_equal false, @user.respond_to?(:encrypted_name=)
100
+ end
120
101
 
121
- should "have unencrypted values" do
122
- assert_equal @bank_account_number, @user.bank_account_number
123
- assert_equal @social_security_number, @user.social_security_number
124
- end
102
+ should "have unencrypted methods" do
103
+ assert_equal true, @user.respond_to?(:bank_account_number)
104
+ assert_equal true, @user.respond_to?(:social_security_number)
105
+ assert_equal true, @user.respond_to?(:string)
106
+ assert_equal true, @user.respond_to?(:long_string)
107
+ assert_equal true, @user.respond_to?(:name)
108
+
109
+ assert_equal true, @user.respond_to?(:bank_account_number=)
110
+ assert_equal true, @user.respond_to?(:social_security_number=)
111
+ assert_equal true, @user.respond_to?(:string=)
112
+ assert_equal true, @user.respond_to?(:long_string=)
113
+ assert_equal true, @user.respond_to?(:name=)
114
+ end
125
115
 
126
- should "have encrypted values" do
127
- assert_equal @bank_account_number_encrypted, @user.encrypted_bank_account_number
128
- assert_equal @social_security_number_encrypted, @user.encrypted_social_security_number
129
- end
116
+ should "support aliased fields" do
117
+ assert_equal true, @user.respond_to?(:aliased_integer_value=)
118
+ assert_equal true, @user.respond_to?(:aliased_integer_value)
119
+ end
130
120
 
131
- should "support same iv" do
132
- @user.social_security_number = @social_security_number
133
- assert first_value = @user.social_security_number
134
- # Assign the same value
135
- @user.social_security_number = @social_security_number
136
- assert_equal first_value, @user.social_security_number
137
- end
121
+ should "have unencrypted values" do
122
+ assert_equal @bank_account_number, @user.bank_account_number
123
+ assert_equal @social_security_number, @user.social_security_number
124
+ end
138
125
 
139
- should "support a random iv" do
140
- @user.string = @string
141
- assert first_value = @user.encrypted_string
142
- # Assign the same value
143
- @user.string = @string.dup
144
- assert_equal true, first_value != @user.encrypted_string
145
- end
126
+ should "have encrypted values" do
127
+ assert_equal @bank_account_number_encrypted, @user.encrypted_bank_account_number
128
+ assert_equal @social_security_number_encrypted, @user.encrypted_social_security_number
129
+ end
146
130
 
147
- should "support a random iv and compress" do
148
- @user.string = @long_string
149
- @user.long_string = @long_string
131
+ should "support same iv" do
132
+ @user.social_security_number = @social_security_number
133
+ assert first_value = @user.social_security_number
134
+ # Assign the same value
135
+ @user.social_security_number = @social_security_number
136
+ assert_equal first_value, @user.social_security_number
137
+ end
150
138
 
151
- assert_equal true, (@user.encrypted_long_string.length.to_f / @user.encrypted_string.length) < 0.8
152
- end
139
+ should "support a random iv" do
140
+ @user.string = @string
141
+ assert first_value = @user.encrypted_string
142
+ # Assign the same value
143
+ @user.string = @string.dup
144
+ assert_equal true, first_value != @user.encrypted_string
145
+ end
153
146
 
154
- should "encrypt" do
155
- user = MongoMapperUser.new
156
- user.bank_account_number = @bank_account_number
157
- assert_equal @bank_account_number, user.bank_account_number
158
- assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
159
- end
147
+ should "support a random iv and compress" do
148
+ @user.string = @long_string
149
+ @user.long_string = @long_string
160
150
 
161
- should "all paths should lead to the same result" do
162
- assert_equal @bank_account_number_encrypted, (@user.encrypted_social_security_number = @bank_account_number_encrypted)
163
- assert_equal @bank_account_number, @user.social_security_number
164
- end
151
+ assert_equal true, (@user.encrypted_long_string.length.to_f / @user.encrypted_string.length) < 0.8
152
+ end
165
153
 
166
- should "all paths should lead to the same result 2" do
167
- assert_equal @bank_account_number, (@user.social_security_number = @bank_account_number)
168
- assert_equal @bank_account_number_encrypted, @user.encrypted_social_security_number
169
- end
154
+ should "encrypt" do
155
+ user = MongoMapperUser.new
156
+ user.bank_account_number = @bank_account_number
157
+ assert_equal @bank_account_number, user.bank_account_number
158
+ assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
159
+ end
170
160
 
171
- should "all paths should lead to the same result, check uninitialized" do
172
- user = MongoMapperUser.new
173
- assert_equal nil, user.social_security_number
174
- assert_equal @bank_account_number, (user.social_security_number = @bank_account_number)
175
- assert_equal @bank_account_number, user.social_security_number
176
- assert_equal @bank_account_number_encrypted, user.encrypted_social_security_number
161
+ should "all paths should lead to the same result" do
162
+ assert_equal @bank_account_number_encrypted, (@user.encrypted_social_security_number = @bank_account_number_encrypted)
163
+ assert_equal @bank_account_number, @user.social_security_number
164
+ end
177
165
 
178
- assert_equal nil, (user.social_security_number = nil)
179
- assert_equal nil, user.social_security_number
180
- assert_equal nil, user.encrypted_social_security_number
181
- end
166
+ should "all paths should lead to the same result 2" do
167
+ assert_equal @bank_account_number, (@user.social_security_number = @bank_account_number)
168
+ assert_equal @bank_account_number_encrypted, @user.encrypted_social_security_number
169
+ end
182
170
 
183
- should "allow unencrypted values to be passed to the constructor" do
184
- user = MongoMapperUser.new(bank_account_number: @bank_account_number, social_security_number: @social_security_number)
185
- assert_equal @bank_account_number, user.bank_account_number
186
- assert_equal @social_security_number, user.social_security_number
187
- assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
188
- assert_equal @social_security_number_encrypted, user.encrypted_social_security_number
189
- end
171
+ should "all paths should lead to the same result, check uninitialized" do
172
+ user = MongoMapperUser.new
173
+ assert_equal nil, user.social_security_number
174
+ assert_equal @bank_account_number, (user.social_security_number = @bank_account_number)
175
+ assert_equal @bank_account_number, user.social_security_number
176
+ assert_equal @bank_account_number_encrypted, user.encrypted_social_security_number
190
177
 
191
- should "allow both encrypted and unencrypted values to be passed to the constructor" do
192
- user = MongoMapperUser.new(encrypted_bank_account_number: @bank_account_number_encrypted, social_security_number: @social_security_number)
193
- assert_equal @bank_account_number, user.bank_account_number
194
- assert_equal @social_security_number, user.social_security_number
195
- assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
196
- assert_equal @social_security_number_encrypted, user.encrypted_social_security_number
197
- end
178
+ assert_equal nil, (user.social_security_number = nil)
179
+ assert_equal nil, user.social_security_number
180
+ assert_equal nil, user.encrypted_social_security_number
181
+ end
198
182
 
199
- context "data types" do
200
- setup do
201
- @user.save!
202
- @user_clone = MongoMapperUser.find(@user.id)
183
+ should "allow unencrypted values to be passed to the constructor" do
184
+ user = MongoMapperUser.new(bank_account_number: @bank_account_number, social_security_number: @social_security_number)
185
+ assert_equal @bank_account_number, user.bank_account_number
186
+ assert_equal @social_security_number, user.social_security_number
187
+ assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
188
+ assert_equal @social_security_number_encrypted, user.encrypted_social_security_number
203
189
  end
204
190
 
205
- context "aliased fields" do
206
- should "return correct data type" do
207
- @user_clone.aliased_integer_value = "5"
208
- assert_equal 5, @user_clone.aliased_integer_value
209
- end
191
+ should "allow both encrypted and unencrypted values to be passed to the constructor" do
192
+ user = MongoMapperUser.new(encrypted_bank_account_number: @bank_account_number_encrypted, social_security_number: @social_security_number)
193
+ assert_equal @bank_account_number, user.bank_account_number
194
+ assert_equal @social_security_number, user.social_security_number
195
+ assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
196
+ assert_equal @social_security_number_encrypted, user.encrypted_social_security_number
210
197
  end
211
198
 
212
- context "integer values" do
213
- should "return correct data type" do
214
- assert_equal @integer_value, @user_clone.integer_value
215
- assert @user.clone.integer_value.kind_of?(Integer)
199
+ context "data types" do
200
+ setup do
201
+ @user.save!
202
+ @user_clone = MongoMapperUser.find(@user.id)
216
203
  end
217
204
 
218
- should "coerce data type before save" do
219
- u = MongoMapperUser.new(integer_value: "5")
220
- assert_equal 5, u.integer_value
221
- assert u.integer_value.kind_of?(Integer)
205
+ context "aliased fields" do
206
+ should "return correct data type" do
207
+ @user_clone.aliased_integer_value = "5"
208
+ assert_equal 5, @user_clone.aliased_integer_value
209
+ end
222
210
  end
223
211
 
224
- should "permit replacing value with nil" do
225
- @user_clone.integer_value = nil
226
- @user_clone.save!
212
+ context "integer values" do
213
+ should "return correct data type" do
214
+ assert_equal @integer_value, @user_clone.integer_value
215
+ assert @user.clone.integer_value.kind_of?(Integer)
216
+ end
227
217
 
228
- @user.reload
229
- assert_nil @user.integer_value
230
- assert_nil @user.encrypted_integer_value
231
- end
218
+ should "coerce data type before save" do
219
+ u = MongoMapperUser.new(integer_value: "5")
220
+ assert_equal 5, u.integer_value
221
+ assert u.integer_value.kind_of?(Integer)
222
+ end
232
223
 
233
- should "permit replacing value" do
234
- new_integer_value = 98
235
- @user_clone.integer_value = new_integer_value
236
- @user_clone.save!
224
+ should "permit replacing value with nil" do
225
+ @user_clone.integer_value = nil
226
+ @user_clone.save!
237
227
 
238
- @user.reload
239
- assert_equal new_integer_value, @user.integer_value
240
- end
241
- end
228
+ @user.reload
229
+ assert_nil @user.integer_value
230
+ assert_nil @user.encrypted_integer_value
231
+ end
242
232
 
243
- context "float values" do
244
- should "return correct data type" do
245
- assert_equal @float_value, @user_clone.float_value
246
- assert @user.clone.float_value.kind_of?(Float)
247
- end
233
+ should "permit replacing value" do
234
+ new_integer_value = 98
235
+ @user_clone.integer_value = new_integer_value
236
+ @user_clone.save!
248
237
 
249
- should "coerce data type before save" do
250
- u = MongoMapperUser.new(float_value: "5.6")
251
- assert_equal 5.6, u.float_value
252
- assert u.float_value.kind_of?(Float)
238
+ @user.reload
239
+ assert_equal new_integer_value, @user.integer_value
240
+ end
253
241
  end
254
242
 
255
- should "permit replacing value with nil" do
256
- @user_clone.float_value = nil
257
- @user_clone.save!
243
+ context "float values" do
244
+ should "return correct data type" do
245
+ assert_equal @float_value, @user_clone.float_value
246
+ assert @user.clone.float_value.kind_of?(Float)
247
+ end
258
248
 
259
- @user.reload
260
- assert_nil @user.float_value
261
- assert_nil @user.encrypted_float_value
262
- end
249
+ should "coerce data type before save" do
250
+ u = MongoMapperUser.new(float_value: "5.6")
251
+ assert_equal 5.6, u.float_value
252
+ assert u.float_value.kind_of?(Float)
253
+ end
263
254
 
264
- should "permit replacing value" do
265
- new_float_value = 45.4321
266
- @user_clone.float_value = new_float_value
267
- @user_clone.save!
255
+ should "permit replacing value with nil" do
256
+ @user_clone.float_value = nil
257
+ @user_clone.save!
268
258
 
269
- @user.reload
270
- assert_equal new_float_value, @user.float_value
271
- end
272
- end
259
+ @user.reload
260
+ assert_nil @user.float_value
261
+ assert_nil @user.encrypted_float_value
262
+ end
273
263
 
274
- context "decimal values" do
275
- should "return correct data type" do
276
- assert_equal @decimal_value, @user_clone.decimal_value
277
- assert @user.clone.decimal_value.kind_of?(BigDecimal)
278
- end
264
+ should "permit replacing value" do
265
+ new_float_value = 45.4321
266
+ @user_clone.float_value = new_float_value
267
+ @user_clone.save!
279
268
 
280
- should "coerce data type before save" do
281
- u = MongoMapperUser.new(decimal_value: "99.95")
282
- assert_equal BigDecimal.new("99.95"), u.decimal_value
283
- assert u.decimal_value.kind_of?(BigDecimal)
269
+ @user.reload
270
+ assert_equal new_float_value, @user.float_value
271
+ end
284
272
  end
285
273
 
286
- should "permit replacing value with nil" do
287
- @user_clone.decimal_value = nil
288
- @user_clone.save!
274
+ context "decimal values" do
275
+ should "return correct data type" do
276
+ assert_equal @decimal_value, @user_clone.decimal_value
277
+ assert @user.clone.decimal_value.kind_of?(BigDecimal)
278
+ end
289
279
 
290
- @user.reload
291
- assert_nil @user.decimal_value
292
- assert_nil @user.encrypted_decimal_value
293
- end
280
+ should "coerce data type before save" do
281
+ u = MongoMapperUser.new(decimal_value: "99.95")
282
+ assert_equal BigDecimal.new("99.95"), u.decimal_value
283
+ assert u.decimal_value.kind_of?(BigDecimal)
284
+ end
294
285
 
295
- should "permit replacing value" do
296
- new_decimal_value = BigDecimal.new("99.95")
297
- @user_clone.decimal_value = new_decimal_value
298
- @user_clone.save!
286
+ should "permit replacing value with nil" do
287
+ @user_clone.decimal_value = nil
288
+ @user_clone.save!
299
289
 
300
- @user.reload
301
- assert_equal new_decimal_value, @user.decimal_value
302
- end
303
- end
290
+ @user.reload
291
+ assert_nil @user.decimal_value
292
+ assert_nil @user.encrypted_decimal_value
293
+ end
304
294
 
305
- context "datetime values" do
306
- should "return correct data type" do
307
- assert_equal @datetime_value, @user_clone.datetime_value
308
- assert @user.clone.datetime_value.kind_of?(DateTime)
309
- end
295
+ should "permit replacing value" do
296
+ new_decimal_value = BigDecimal.new("99.95")
297
+ @user_clone.decimal_value = new_decimal_value
298
+ @user_clone.save!
310
299
 
311
- should "coerce data type before save" do
312
- now = Time.now
313
- u = MongoMapperUser.new(datetime_value: now)
314
- assert_equal now, u.datetime_value
315
- assert u.datetime_value.kind_of?(DateTime)
300
+ @user.reload
301
+ assert_equal new_decimal_value, @user.decimal_value
302
+ end
316
303
  end
317
304
 
318
- should "permit replacing value with nil" do
319
- @user_clone.datetime_value = nil
320
- @user_clone.save!
305
+ context "datetime values" do
306
+ should "return correct data type" do
307
+ assert_equal @datetime_value, @user_clone.datetime_value
308
+ assert @user.clone.datetime_value.kind_of?(DateTime)
309
+ end
321
310
 
322
- @user.reload
323
- assert_nil @user.datetime_value
324
- assert_nil @user.encrypted_datetime_value
325
- end
311
+ should "coerce data type before save" do
312
+ now = Time.now
313
+ u = MongoMapperUser.new(datetime_value: now)
314
+ assert_equal now, u.datetime_value
315
+ assert u.datetime_value.kind_of?(DateTime)
316
+ end
326
317
 
327
- should "permit replacing value" do
328
- new_datetime_value = DateTime.new(1998, 10, 21, 8, 33, 28, "+5")
329
- @user_clone.datetime_value = new_datetime_value
330
- @user_clone.save!
318
+ should "permit replacing value with nil" do
319
+ @user_clone.datetime_value = nil
320
+ @user_clone.save!
331
321
 
332
- @user.reload
333
- assert_equal new_datetime_value, @user.datetime_value
334
- end
335
- end
322
+ @user.reload
323
+ assert_nil @user.datetime_value
324
+ assert_nil @user.encrypted_datetime_value
325
+ end
336
326
 
337
- context "time values" do
338
- should "return correct data type" do
339
- assert_equal @time_value, @user_clone.time_value
340
- assert @user.clone.time_value.kind_of?(Time)
341
- end
327
+ should "permit replacing value" do
328
+ new_datetime_value = DateTime.new(1998, 10, 21, 8, 33, 28, "+5")
329
+ @user_clone.datetime_value = new_datetime_value
330
+ @user_clone.save!
342
331
 
343
- should "coerce data type before save" do
344
- now = Time.now
345
- u = MongoMapperUser.new(time_value: now)
346
- assert_equal now, u.time_value
347
- assert u.time_value.kind_of?(Time)
332
+ @user.reload
333
+ assert_equal new_datetime_value, @user.datetime_value
334
+ end
348
335
  end
349
336
 
350
- should "permit replacing value with nil" do
351
- @user_clone.time_value = nil
352
- @user_clone.save!
337
+ context "time values" do
338
+ should "return correct data type" do
339
+ assert_equal @time_value, @user_clone.time_value
340
+ assert @user.clone.time_value.kind_of?(Time)
341
+ end
353
342
 
354
- @user.reload
355
- assert_nil @user.time_value
356
- assert_nil @user.encrypted_time_value
357
- end
343
+ should "coerce data type before save" do
344
+ now = Time.now
345
+ u = MongoMapperUser.new(time_value: now)
346
+ assert_equal now, u.time_value
347
+ assert u.time_value.kind_of?(Time)
348
+ end
358
349
 
359
- should "permit replacing value" do
360
- new_time_value = Time.new(1998, 10, 21, 8, 33, 28, "+04:00")
361
- @user_clone.time_value = new_time_value
362
- @user_clone.save!
350
+ should "permit replacing value with nil" do
351
+ @user_clone.time_value = nil
352
+ @user_clone.save!
363
353
 
364
- @user.reload
365
- assert_equal new_time_value, @user.time_value
366
- end
367
- end
354
+ @user.reload
355
+ assert_nil @user.time_value
356
+ assert_nil @user.encrypted_time_value
357
+ end
368
358
 
369
- context "date values" do
370
- should "return correct data type" do
371
- assert_equal @date_value, @user_clone.date_value
372
- assert @user.clone.date_value.kind_of?(Date)
373
- end
359
+ should "permit replacing value" do
360
+ new_time_value = Time.new(1998, 10, 21, 8, 33, 28, "+04:00")
361
+ @user_clone.time_value = new_time_value
362
+ @user_clone.save!
374
363
 
375
- should "coerce data type before save" do
376
- now = Time.now
377
- u = MongoMapperUser.new(date_value: now)
378
- assert_equal now.to_date, u.date_value
379
- assert u.date_value.kind_of?(Date)
364
+ @user.reload
365
+ assert_equal new_time_value, @user.time_value
366
+ end
380
367
  end
381
368
 
382
- should "permit replacing value with nil" do
383
- @user_clone.date_value = nil
384
- @user_clone.save!
369
+ context "date values" do
370
+ should "return correct data type" do
371
+ assert_equal @date_value, @user_clone.date_value
372
+ assert @user.clone.date_value.kind_of?(Date)
373
+ end
385
374
 
386
- @user.reload
387
- assert_nil @user.date_value
388
- assert_nil @user.encrypted_date_value
389
- end
375
+ should "coerce data type before save" do
376
+ now = Time.now
377
+ u = MongoMapperUser.new(date_value: now)
378
+ assert_equal now.to_date, u.date_value
379
+ assert u.date_value.kind_of?(Date)
380
+ end
390
381
 
391
- should "permit replacing value" do
392
- new_date_value = Date.new(1998, 10, 21)
393
- @user_clone.date_value = new_date_value
394
- @user_clone.save!
382
+ should "permit replacing value with nil" do
383
+ @user_clone.date_value = nil
384
+ @user_clone.save!
395
385
 
396
- @user.reload
397
- assert_equal new_date_value, @user.date_value
398
- end
399
- end
386
+ @user.reload
387
+ assert_nil @user.date_value
388
+ assert_nil @user.encrypted_date_value
389
+ end
400
390
 
401
- context "true values" do
402
- should "return correct data type" do
403
- assert_equal true, @user_clone.true_value
404
- assert @user.clone.true_value.kind_of?(TrueClass)
405
- end
391
+ should "permit replacing value" do
392
+ new_date_value = Date.new(1998, 10, 21)
393
+ @user_clone.date_value = new_date_value
394
+ @user_clone.save!
406
395
 
407
- should "coerce data type before save" do
408
- u = MongoMapperUser.new(true_value: "1")
409
- assert_equal true, u.true_value
410
- assert u.true_value.kind_of?(TrueClass)
396
+ @user.reload
397
+ assert_equal new_date_value, @user.date_value
398
+ end
411
399
  end
412
400
 
413
- should "permit replacing value with nil" do
414
- @user_clone.true_value = nil
415
- @user_clone.save!
401
+ context "true values" do
402
+ should "return correct data type" do
403
+ assert_equal true, @user_clone.true_value
404
+ assert @user.clone.true_value.kind_of?(TrueClass)
405
+ end
416
406
 
417
- @user.reload
418
- assert_nil @user.true_value
419
- assert_nil @user.encrypted_true_value
420
- end
407
+ should "coerce data type before save" do
408
+ u = MongoMapperUser.new(true_value: "1")
409
+ assert_equal true, u.true_value
410
+ assert u.true_value.kind_of?(TrueClass)
411
+ end
421
412
 
422
- should "permit replacing value" do
423
- new_value = false
424
- @user_clone.true_value = new_value
425
- @user_clone.save!
413
+ should "permit replacing value with nil" do
414
+ @user_clone.true_value = nil
415
+ @user_clone.save!
426
416
 
427
- @user.reload
428
- assert_equal new_value, @user.true_value
429
- end
430
- end
417
+ @user.reload
418
+ assert_nil @user.true_value
419
+ assert_nil @user.encrypted_true_value
420
+ end
431
421
 
432
- context "false values" do
433
- should "return correct data type" do
434
- assert_equal false, @user_clone.false_value
435
- assert @user.clone.false_value.kind_of?(FalseClass)
436
- end
422
+ should "permit replacing value" do
423
+ new_value = false
424
+ @user_clone.true_value = new_value
425
+ @user_clone.save!
437
426
 
438
- should "coerce data type before save" do
439
- u = MongoMapperUser.new(false_value: "0")
440
- assert_equal false, u.false_value
441
- assert u.false_value.kind_of?(FalseClass)
427
+ @user.reload
428
+ assert_equal new_value, @user.true_value
429
+ end
442
430
  end
443
431
 
444
- should "permit replacing value with nil" do
445
- @user_clone.false_value = nil
446
- @user_clone.save!
432
+ context "false values" do
433
+ should "return correct data type" do
434
+ assert_equal false, @user_clone.false_value
435
+ assert @user.clone.false_value.kind_of?(FalseClass)
436
+ end
447
437
 
448
- @user.reload
449
- assert_nil @user.false_value
450
- assert_nil @user.encrypted_false_value
451
- end
438
+ should "coerce data type before save" do
439
+ u = MongoMapperUser.new(false_value: "0")
440
+ assert_equal false, u.false_value
441
+ assert u.false_value.kind_of?(FalseClass)
442
+ end
452
443
 
453
- should "permit replacing value" do
454
- new_value = true
455
- @user_clone.false_value = new_value
456
- @user_clone.save!
444
+ should "permit replacing value with nil" do
445
+ @user_clone.false_value = nil
446
+ @user_clone.save!
457
447
 
458
- @user.reload
459
- assert_equal new_value, @user.false_value
460
- end
461
- end
448
+ @user.reload
449
+ assert_nil @user.false_value
450
+ assert_nil @user.encrypted_false_value
451
+ end
462
452
 
463
- context "JSON Serialization" do
464
- setup do
465
- # JSON Does not support symbols, so they will come back as strings
466
- # Convert symbols to string in the test
467
- @h.keys.each do |k|
468
- @h[k.to_s] = @h[k]
469
- @h.delete(k)
453
+ should "permit replacing value" do
454
+ new_value = true
455
+ @user_clone.false_value = new_value
456
+ @user_clone.save!
457
+
458
+ @user.reload
459
+ assert_equal new_value, @user.false_value
470
460
  end
471
461
  end
472
462
 
473
- should "return correct data type" do
474
- assert_equal @h, @user_clone.data_json
475
- assert @user.clone.data_json.kind_of?(Hash)
476
- end
463
+ context "JSON Serialization" do
464
+ setup do
465
+ # JSON Does not support symbols, so they will come back as strings
466
+ # Convert symbols to string in the test
467
+ @h.keys.each do |k|
468
+ @h[k.to_s] = @h[k]
469
+ @h.delete(k)
470
+ end
471
+ end
477
472
 
478
- should "not coerce data type (leaves as hash) before save" do
479
- u = MongoMapperUser.new(data_json: @h)
480
- assert_equal @h, u.data_json
481
- assert u.data_json.kind_of?(Hash)
482
- end
473
+ should "return correct data type" do
474
+ assert_equal @h, @user_clone.data_json
475
+ assert @user.clone.data_json.kind_of?(Hash)
476
+ end
477
+
478
+ should "not coerce data type (leaves as hash) before save" do
479
+ u = MongoMapperUser.new(data_json: @h)
480
+ assert_equal @h, u.data_json
481
+ assert u.data_json.kind_of?(Hash)
482
+ end
483
483
 
484
- should "permit replacing value with nil" do
485
- @user_clone.data_json = nil
486
- @user_clone.save!
484
+ should "permit replacing value with nil" do
485
+ @user_clone.data_json = nil
486
+ @user_clone.save!
487
487
 
488
- @user.reload
489
- assert_nil @user.data_json
490
- assert_nil @user.encrypted_data_json
491
- end
488
+ @user.reload
489
+ assert_nil @user.data_json
490
+ assert_nil @user.encrypted_data_json
491
+ end
492
492
 
493
- should "permit replacing value" do
494
- new_value = @h.clone
495
- new_value['c'] = 'C'
496
- @user_clone.data_json = new_value
497
- @user_clone.save!
493
+ should "permit replacing value" do
494
+ new_value = @h.clone
495
+ new_value['c'] = 'C'
496
+ @user_clone.data_json = new_value
497
+ @user_clone.save!
498
498
 
499
- @user.reload
500
- assert_equal new_value, @user.data_json
499
+ @user.reload
500
+ assert_equal new_value, @user.data_json
501
+ end
501
502
  end
502
- end
503
503
 
504
- context "YAML Serialization" do
505
- should "return correct data type" do
506
- assert_equal @h, @user_clone.data_yaml
507
- assert @user.clone.data_yaml.kind_of?(Hash)
508
- end
504
+ context "YAML Serialization" do
505
+ should "return correct data type" do
506
+ assert_equal @h, @user_clone.data_yaml
507
+ assert @user.clone.data_yaml.kind_of?(Hash)
508
+ end
509
509
 
510
- should "not coerce data type (leaves as hash) before save" do
511
- u = MongoMapperUser.new(data_yaml: @h)
512
- assert_equal @h, u.data_yaml
513
- assert u.data_yaml.kind_of?(Hash)
514
- end
510
+ should "not coerce data type (leaves as hash) before save" do
511
+ u = MongoMapperUser.new(data_yaml: @h)
512
+ assert_equal @h, u.data_yaml
513
+ assert u.data_yaml.kind_of?(Hash)
514
+ end
515
515
 
516
- should "permit replacing value with nil" do
517
- @user_clone.data_yaml = nil
518
- @user_clone.save!
516
+ should "permit replacing value with nil" do
517
+ @user_clone.data_yaml = nil
518
+ @user_clone.save!
519
519
 
520
- @user.reload
521
- assert_nil @user.data_yaml
522
- assert_nil @user.encrypted_data_yaml
523
- end
520
+ @user.reload
521
+ assert_nil @user.data_yaml
522
+ assert_nil @user.encrypted_data_yaml
523
+ end
524
524
 
525
- should "permit replacing value" do
526
- new_value = @h.clone
527
- new_value[:c] = 'C'
528
- @user_clone.data_yaml = new_value
529
- @user_clone.save!
525
+ should "permit replacing value" do
526
+ new_value = @h.clone
527
+ new_value[:c] = 'C'
528
+ @user_clone.data_yaml = new_value
529
+ @user_clone.save!
530
530
 
531
- @user.reload
532
- assert_equal new_value, @user.data_yaml
531
+ @user.reload
532
+ assert_equal new_value, @user.data_yaml
533
+ end
533
534
  end
535
+
534
536
  end
535
537
 
536
538
  end
537
-
538
539
  end
539
- end
540
+ rescue LoadError
541
+ puts "Not running MongoMapper tests because mongo_mapper gem is not installed!!!"
542
+ end