-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathrails_spec.rb
More file actions
786 lines (617 loc) · 23.7 KB
/
rails_spec.rb
File metadata and controls
786 lines (617 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# encoding: utf-8
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
require "spec_helper"
describe Vault::Rails do
before(:all) do
Vault::Rails.sys.mount("transit", :transit)
end
context "with default options" do
before(:all) do
Vault::Rails.logical.write("transit/keys/dummy_people_ssn")
end
it "encrypts attributes" do
person = Person.create!(ssn: "123-45-6789")
expect(person.ssn_encrypted).to be
expect(person.ssn_encrypted.encoding).to eq(Encoding::UTF_8)
end
it "decrypts attributes" do
person = Person.create!(ssn: "123-45-6789")
person.reload
expect(person.ssn).to eq("123-45-6789")
expect(person.ssn.encoding).to eq(Encoding::UTF_8)
end
it "tracks dirty attributes" do
person = Person.create!(ssn: "123-45-6789")
expect(person.ssn_changed?).to be(false)
expect(person.ssn_change).to be(nil)
expect(person.ssn_was).to eq("123-45-6789")
person.ssn = "111-11-1111"
expect(person.ssn_changed?).to be(true)
expect(person.ssn_change).to eq(["123-45-6789", "111-11-1111"])
expect(person.ssn_was).to eq("123-45-6789")
end
it "allows attributes to be unset" do
person = Person.create!(ssn: "123-45-6789")
person.update!(ssn: nil)
person.reload
expect(person.ssn).to be(nil)
end
it "allows dirty attributes to be unset" do
person = Person.create!(ssn: "123-45-6789")
person.ssn = nil
expect(person.ssn).to be_nil
person2 = Person.create!(ssn: "123-45-6789")
person2.assign_attributes(ssn: nil)
expect(person2.ssn).to be_nil
end
it "allows saving without validations" do
person = Person.new(ssn: "123-456-7890")
person.save(validate: false)
expect(person.ssn_encrypted).to match("vault:")
end
it "allows attributes to be unset after reload" do
person = Person.create!(ssn: "123-45-6789")
person.reload
person.update!(ssn: nil)
person.reload
expect(person.ssn).to be(nil)
end
it "allows attributes to be blank" do
person = Person.create!(ssn: "123-45-6789")
person.update!(ssn: "")
person.reload
expect(person.ssn).to eq("")
expect(person.ssn_encrypted).to eq("")
end
it "allows attributes to be null" do
person = Person.create!(ssn: "123-45-6789")
person.update!(ssn: nil)
person.reload
expect(person.ssn).to eq(nil)
expect(person.ssn_encrypted).to eq(nil)
end
it "reloads instance variables on reload" do
person = Person.create!(ssn: "123-45-6789")
expect(person.instance_variable_get(:@ssn)).to eq("123-45-6789")
person.ssn = "111-11-1111"
person.reload
expect(person.instance_variable_get(:@ssn)).to eq("123-45-6789")
end
it "does not try to encrypt unchanged attributes" do
person = Person.create!(ssn: "123-45-6789")
expect(Vault::Rails).to_not receive(:encrypt)
person.name = "Cinderella"
person.save!
end
it "does not register a Vault attribute as necessarily being backed by a column" do
expect(Person.attribute_names).to include("ssn")
expect(Person.column_names).not_to include("ssn")
end
it "does not reload encrypted attributes on destroy" do
person = Person.create!(ssn: "123-45-6789")
expect(Vault::Rails).to_not receive(:decrypt)
person.destroy
end
end
context "lazy decrypt" do
before(:all) do
Vault::Rails.logical.write("transit/keys/dummy_people_ssn")
end
it "encrypts attributes" do
person = LazyPerson.create!(ssn: "123-45-6789")
expect(person.ssn_encrypted).to be
expect(person.ssn_encrypted.encoding).to eq(Encoding::UTF_8)
end
it "decrypts attributes" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.reload
expect(person.ssn).to eq("123-45-6789")
expect(person.ssn.encoding).to eq(Encoding::UTF_8)
end
it "does not decrypt on initialization" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.reload
p2 = LazyPerson.find(person.id)
expect(p2.instance_variable_get("@ssn")).to eq(nil)
expect(p2.ssn).to eq("123-45-6789")
end
it "tracks dirty attributes" do
person = LazyPerson.create!(ssn: "123-45-6789")
expect(person.ssn_changed?).to be(false)
expect(person.ssn_change).to be(nil)
expect(person.ssn_was).to eq("123-45-6789")
person.ssn = "111-11-1111"
expect(person.ssn_changed?).to be(true)
expect(person.ssn_change).to eq(["123-45-6789", "111-11-1111"])
expect(person.ssn_was).to eq("123-45-6789")
person.assign_attributes(ssn: "222-22-2222")
expect(person.ssn_changed?).to be(true)
expect(person.ssn_change).to eq(["123-45-6789", "222-22-2222"])
expect(person.ssn_was).to eq("123-45-6789")
end
it "allows attributes to be unset" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.update!(ssn: nil)
person.reload
expect(person.ssn).to be(nil)
end
it "allows dirty attributes to be unset" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.ssn = nil
expect(person.ssn).to be_nil
person2 = LazyPerson.create!(ssn: "123-45-6789")
person2.assign_attributes(ssn: nil)
expect(person2.ssn).to be_nil
end
it "allows saving without validations" do
person = LazyPerson.new(ssn: "123-456-7890")
expect(person.save(validate: false)).to be(true)
expect(person.ssn_encrypted).to match("vault:")
end
it "allows attributes to be unset after reload" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.reload
person.update!(ssn: nil)
person.reload
expect(person.ssn).to be(nil)
end
it "allows attributes to be blank" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.update!(ssn: "")
person.reload
expect(person.ssn).to eq("")
end
it "reloads instance variables on reload" do
person = LazyPerson.create!(ssn: "123-45-6789")
expect(person.instance_variable_get(:@ssn)).to eq("123-45-6789")
person.ssn = "111-11-1111"
person.reload
expect(person.ssn).to eq("123-45-6789")
end
it "does not try to encrypt unchanged attributes" do
person = LazyPerson.create!(ssn: "123-45-6789")
expect(Vault::Rails).to_not receive(:encrypt)
person.name = "Cinderella"
person.save!
end
it "allows attributes to be accessed after a destroy" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.destroy
expect { person.ssn }.not_to raise_error
end
end
context "lazy single decrypt" do
before(:all) do
Vault::Rails.logical.write("transit/keys/dummy_people_ssn")
end
it "encrypts attributes" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
expect(person.ssn_encrypted.length).to eq(61)
expect(person.ssn_encrypted).to start_with("vault:v1:")
expect(person.ssn_encrypted.encoding).to eq(Encoding::UTF_8)
end
it "decrypts attributes" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
person.reload
expect(person.ssn).to eq("123-45-6789")
expect(person.ssn.encoding).to eq(Encoding::UTF_8)
end
it "does not decrypt on initialization" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
person.reload
p2 = LazySinglePerson.find(person.id)
expect(p2.instance_variable_get("@ssn")).to eq(nil)
expect(p2.ssn).to eq("123-45-6789")
end
it "does not decrypt all attributes on single read" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
person.update!(credit_card: "abcd-efgh-hijk-lmno")
expect(person.credit_card).to eq("abcd-efgh-hijk-lmno")
person.reload
p2 = LazySinglePerson.find(person.id)
expect(p2.instance_variable_get("@ssn")).to eq(nil)
expect(p2.ssn).to eq("123-45-6789")
expect(p2.instance_variable_get("@credit_card")).to eq(nil)
expect(p2.credit_card).to eq("abcd-efgh-hijk-lmno")
end
it "does not decrypt all attributes on single write" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
person.update!(credit_card: "abcd-efgh-hijk-lmno")
expect(person.credit_card).to eq("abcd-efgh-hijk-lmno")
person.reload
p2 = LazySinglePerson.find(person.id)
expect(p2.instance_variable_get("@ssn")).to eq(nil)
expect(p2.ssn).to eq("123-45-6789")
person.ssn = "111-11-1111"
expect(p2.instance_variable_get("@credit_card")).to eq(nil)
expect(p2.credit_card).to eq("abcd-efgh-hijk-lmno")
end
it "tracks dirty attributes" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
expect(person.ssn_changed?).to be(false)
expect(person.ssn_change).to be(nil)
expect(person.ssn_was).to eq("123-45-6789")
person.ssn = "111-11-1111"
expect(person.ssn_changed?).to be(true)
expect(person.ssn_change).to eq(["123-45-6789", "111-11-1111"])
expect(person.ssn_was).to eq("123-45-6789")
end
it "allows attributes to be unset" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
person.update!(ssn: nil)
person.reload
expect(person.ssn).to be(nil)
end
it "allows saving without validations" do
person = LazySinglePerson.new(ssn: "123-456-7890")
expect(person.save(validate: false)).to be(true)
expect(person.ssn_encrypted).to match("vault:")
end
it "allows attributes to be unset after reload" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
person.reload
person.update!(ssn: nil)
person.reload
expect(person.ssn).to be(nil)
end
it "allows attributes to be blank" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
person.update!(ssn: "")
person.reload
expect(person.ssn).to eq("")
end
it "reloads instance variables on reload" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
expect(person.instance_variable_get(:@ssn)).to eq("123-45-6789")
person.ssn = "111-11-1111"
person.reload
expect(person.ssn).to eq("123-45-6789")
end
it "does not try to encrypt unchanged attributes" do
person = LazySinglePerson.create!(ssn: "123-45-6789")
expect(Vault::Rails).to_not receive(:encrypt)
person.name = "Cinderella"
person.save!
end
it "allows attributes to be accessed after a destroy" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.destroy
expect { person.ssn }.not_to raise_error
end
end
context "with custom options" do
before(:all) do
Vault::Rails.sys.mount("credit-secrets", :transit)
Vault::Rails.logical.write("credit-secrets/keys/people_credit_cards")
end
it "encrypts attributes" do
person = Person.create!(credit_card: "1234567890111213")
expect(person.cc_encrypted).to be
expect(person.cc_encrypted.encoding).to eq(Encoding::UTF_8)
end
it "decrypts attributes" do
person = Person.create!(credit_card: "1234567890111213")
person.reload
expect(person.credit_card).to eq("1234567890111213")
expect(person.credit_card.encoding).to eq(Encoding::UTF_8)
end
it "tracks dirty attributes" do
person = Person.create!(credit_card: "1234567890111213")
expect(person.credit_card_changed?).to be(false)
expect(person.credit_card_change).to eq(nil)
expect(person.credit_card_was).to eq("1234567890111213")
person.credit_card = "123456789010"
expect(person.credit_card_changed?).to be(true)
expect(person.credit_card_change).to eq(["1234567890111213", "123456789010"])
expect(person.credit_card_was).to eq("1234567890111213")
end
it "allows attributes to be unset" do
person = Person.create!(credit_card: "1234567890111213")
person.update!(credit_card: nil)
person.reload
expect(person.credit_card).to be(nil)
end
it "allows attributes to be blank" do
person = Person.create!(credit_card: "1234567890111213")
person.update!(credit_card: "")
person.reload
expect(person.credit_card).to eq("")
end
end
context "with non-ASCII characters" do
before(:all) do
Vault::Rails.sys.mount("non-ascii", :transit)
Vault::Rails.logical.write("non-ascii/keys/people_non_ascii")
end
it "encrypts attributes" do
person = Person.create!(non_ascii: "dás ümlaut")
expect(person.non_ascii_encrypted).to be
expect(person.non_ascii_encrypted.encoding).to eq(Encoding::UTF_8)
end
it "decrypts attributes" do
person = Person.create!(non_ascii: "dás ümlaut")
person.reload
expect(person.non_ascii).to eq("dás ümlaut")
expect(person.non_ascii.encoding).to eq(Encoding::UTF_8)
end
it "tracks dirty attributes" do
person = Person.create!(non_ascii: "dás ümlaut")
expect(person.non_ascii_changed?).to be(false)
expect(person.non_ascii_change).to eq(nil)
expect(person.non_ascii_was).to eq("dás ümlaut")
person.non_ascii = "él ñiñô"
expect(person.non_ascii_changed?).to be(true)
expect(person.non_ascii_change).to eq(["dás ümlaut", "él ñiñô"])
expect(person.non_ascii_was).to eq("dás ümlaut")
end
it "allows attributes to be unset" do
person = Person.create!(non_ascii: "dás ümlaut")
person.update!(non_ascii: nil)
person.reload
expect(person.non_ascii).to be(nil)
end
it "allows attributes to be blank" do
person = Person.create!(non_ascii: "dás ümlaut")
person.update!(non_ascii: "")
person.reload
expect(person.non_ascii).to eq("")
end
end
context "with a default" do
%i[new create].each do |creation_method|
context "on #{creation_method}" do
context "without an initial attribute" do
it "sets the default" do
person = Person.public_send(creation_method)
expect(person.default).to eq("abc123")
person.save!
person.reload
expect(person.default).to eq("abc123")
end
end
context "with an initial attribute" do
it "does not set the default" do
person = Person.public_send(creation_method, default: "another")
expect(person.default).to eq("another")
person.save!
person.reload
expect(person.default).to eq("another")
end
end
end
end
end
context "with a default and serializer" do
%i[new create].each do |creation_method|
context "on #{creation_method}" do
context "without an initial attribute" do
it "sets the default" do
person = Person.public_send(creation_method)
expect(person.default_with_serializer).to eq({})
person.save!
person.reload
expect(person.default_with_serializer).to eq({})
end
end
context "with an initial attribute" do
it "does not set the default" do
person = Person.public_send(
creation_method,
default_with_serializer: { "foo" => "bar" }
)
expect(person.default_with_serializer).to eq({ "foo" => "bar" })
person.save!
person.reload
expect(person.default_with_serializer).to eq({ "foo" => "bar" })
end
end
end
end
end
context "with the :json serializer" do
before(:all) do
Vault::Rails.logical.write("transit/keys/dummy_people_details")
end
it "does not default to a hash" do
person = Person.new
expect(person.details).to eq(nil)
person.save!
person.reload
expect(person.details).to eq(nil)
end
it "tracks dirty attributes" do
person = Person.create!(details: { "foo" => "bar" })
expect(person.details_changed?).to be(false)
expect(person.details_change).to be(nil)
expect(person.details_was).to eq({ "foo" => "bar" })
person.details = { "zip" => "zap" }
expect(person.details_changed?).to be(true)
expect(person.details_change).to eq([{ "foo" => "bar" }, { "zip" => "zap" }])
expect(person.details_was).to eq({ "foo" => "bar" })
end
it "encodes and decodes attributes" do
person = Person.create!(details: { "foo" => "bar", "zip" => 1 })
person.reload
raw = Vault::Rails.decrypt("transit", "dummy_people_details", person.details_encrypted)
expect(raw).to eq("{\"foo\":\"bar\",\"zip\":1}")
expect(person.details).to eq("foo" => "bar", "zip" => 1)
end
end
context "with a custom serializer" do
before(:all) do
Vault::Rails.logical.write("transit/keys/dummy_people_business_card")
end
it "encodes and decodes attributes" do
person = Person.create!(business_card: "data")
person.reload
raw = Vault::Rails.decrypt("transit", "dummy_people_business_card", person.business_card_encrypted)
expect(raw).to eq("01100100011000010111010001100001")
expect(person.business_card).to eq("data")
end
end
context "with custom encode/decode proc" do
before(:all) do
Vault::Rails.logical.write("transit/keys/dummy_people_favorite_color")
end
it "encodes and decodes attributes" do
person = Person.create!(favorite_color: "blue")
person.reload
raw = Vault::Rails.decrypt("transit", "dummy_people_favorite_color", person.favorite_color_encrypted)
expect(raw).to eq("xxxbluexxx")
expect(person.favorite_color).to eq("blue")
end
end
xcontext "with context" do
it "encodes and decodes with a string context" do
person = Person.create!(context_string: "foobar")
person.reload
raw = Vault::Rails.decrypt(
"transit", "dummy_people_context_string",
person.context_string_encrypted, context: "production")
expect(raw).to eq("foobar")
expect(person.context_string).to eq("foobar")
# Decrypting without the correct context fails
expect {
Vault::Rails.decrypt(
"transit", "dummy_people_context_string",
person.context_string_encrypted, context: "wrongcontext")
}.to raise_error(Vault::HTTPClientError, /invalid ciphertext/)
# Decrypting without a context fails
expect {
Vault::Rails.decrypt(
"transit", "dummy_people_context_string",
person.context_string_encrypted)
}.to raise_error(Vault::HTTPClientError, /context/)
end
it "encodes and decodes with a symbol context" do
person = Person.create!(context_symbol: "foobar")
person.reload
raw = Vault::Rails.decrypt(
"transit", "dummy_people_context_symbol",
person.context_symbol_encrypted, context: person.encryption_context)
expect(raw).to eq("foobar")
expect(person.context_symbol).to eq("foobar")
# Decrypting without the correct context fails
expect {
Vault::Rails.decrypt(
"transit", "dummy_people_context_symbol",
person.context_symbol_encrypted, context: "wrongcontext")
}.to raise_error(Vault::HTTPClientError, /invalid ciphertext/)
# Decrypting without a context fails
expect {
Vault::Rails.decrypt(
"transit", "dummy_people_context_symbol",
person.context_symbol_encrypted)
}.to raise_error(Vault::HTTPClientError, /context/)
end
it "encodes and decodes with a proc context" do
person = Person.create!(context_proc: "foobar")
person.reload
raw = Vault::Rails.decrypt(
"transit", "dummy_people_context_proc",
person.context_proc_encrypted, context: person.encryption_context)
expect(raw).to eq("foobar")
expect(person.context_proc).to eq("foobar")
# Decrypting without the correct context fails
expect {
Vault::Rails.decrypt(
"transit", "dummy_people_context_proc",
person.context_proc_encrypted, context: "wrongcontext")
}.to raise_error(Vault::HTTPClientError, /invalid ciphertext/)
# Decrypting without a context fails
expect {
Vault::Rails.decrypt(
"transit", "dummy_people_context_proc",
person.context_proc_encrypted)
}.to raise_error(Vault::HTTPClientError, /context/)
end
end
xcontext 'with transform_secret', ent_vault: ">= 1.4" do
before(:all) do
Vault::Rails.sys.mount("transform", :transform)
Vault::Rails.client.transform.create_transformation(
"social_sec",
template: "builtin/socialsecuritynumber",
tweak_source: "internal",
type: "fpe",
allowed_roles: [Vault::Rails.application]
)
Vault::Rails.client.transform.create_role(Vault::Rails.application, transformations: ["social_sec"])
Vault::Rails.client.transform.create_role("foobar_role", transformations: ["social_sec"])
end
it "encrypts the attribute using the given transformation" do
person = Person.create!(transform_ssn: "123-45-6789")
expect(person[:transform_ssn_encrypted]).not_to eq("123-45-6789")
expect(person[:transform_ssn_encrypted]).to match(/\d{3}-\d{2}-\d{4}/)
expect(person.transform_ssn).to eq("123-45-6789")
end
it "raises an error if the format is incorrect" do
expect{ Person.create!(transform_ssn: "1234-5678-90") }.to(
raise_error(Vault::HTTPClientError, /unable to find matching expression/)
)
end
it "raises an error if the transformation does not exist" do
expect{ Person.create!(bad_transform: "nope") }.to(
raise_error(Vault::HTTPClientError, /unable to find transformation/)
)
end
it "raises an error if the provided role doesn't have the ability to use the transformation" do
expect{ Person.create!(bad_role_transform: "123-45-6789") }.to(
raise_error(Vault::HTTPClientError, /is not an allowed role for the transformation/)
)
end
end
context 'with errors' do
it 'raises the appropriate exception' do
expect {
Vault::Rails.encrypt('/bogus/path', 'bogus', 'bogus')
}.to raise_error(Vault::HTTPClientError)
end
end
context "without a server" do
it "encrypts attributes with a dev prefix" do
allow(Vault::Rails).to receive(:enabled?).and_return(false)
person = Person.create!(credit_card: "1234567890111213")
expect(person.cc_encrypted).to start_with(Vault::Rails::DEV_PREFIX)
end
it "decrypts attributes" do
allow(Vault::Rails).to receive(:enabled?).and_return(false)
person = Person.create!(credit_card: "1234567890111213")
person.reload
expect(person.credit_card).to eq("1234567890111213")
end
end
context "manual encryption" do
describe "#vault_encrypt_attributes!" do
it "encrypts vault attributes without saving" do
person = Person.new(ssn: "123-45-6789", favorite_color: "green")
expect {
person.vault_encrypt_attributes!
}.to change {
person.ssn_encrypted
}.from(nil).to(be_a(String))
expect(person.favorite_color).to eq("green")
expect(person.favorite_color_encrypted).to be_present
end
it "returns self" do
person = Person.new
result = person.vault_encrypt_attributes!
expect(result).to be(person)
end
it "encrypts attributes with a default option" do
person = Person.new
expect(person.default).to eq("abc123")
expect(person.default_with_serializer).to eq({})
expect {
person.vault_encrypt_attributes!
}.to change {
person.default_encrypted
}.from(nil).to(be_a(String))
.and change {
person.default_with_serializer_encrypted
}.from(nil).to(be_a(String))
end
end
end
end