Skip to content

Commit

Permalink
Clear up Person vs. EagerPerson when testing vault_persist_before_save!
Browse files Browse the repository at this point in the history
We use `Person` in the "when not used" context because that model class
has not had `vault_persist_before_save!` called on it.  `EagerPerson` has
and that's why we use it in the "when used" context.  It'd be clearer if
we could explicitly call it in the spec, but there's no clean up option to
unset it so we need separate models.
  • Loading branch information
h-lame committed Nov 12, 2018
1 parent bdb80c8 commit f99aebd
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions spec/unit/encrypted_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@

describe '#vault_persist_before_save!' do
context "when not used" do
# Person hasn't had `vault_persist_before_save!` called on it
let(:model_class) { Person }

it "the model has an after_save callback" do
save_callbacks = Person._save_callbacks.select do |cb|
save_callbacks = model_class._save_callbacks.select do |cb|
cb.filter == :__vault_persist_attributes!
end

Expand All @@ -104,34 +107,37 @@
expect(persist_callback.kind).to eq :after
end

it 'calls the correnct callback' do
person = Person.new(ssn: '123-45-6789')
expect(person).to receive(:__vault_persist_attributes!)
it 'calls the correct callback' do
record = model_class.new(ssn: '123-45-6789')
expect(record).to receive(:__vault_persist_attributes!)

person.save
record.save
end

it 'encrypts the attribute if it has been saved' do
person = Person.new(ssn: '123-45-6789')
record = model_class.new(ssn: '123-45-6789')
expect(Vault::Rails).to receive(:encrypt).with('transit', 'dummy_people_ssn', anything, anything, anything).and_call_original

person.save
record.save

expect(person.ssn_encrypted).not_to be_nil
expect(record.ssn_encrypted).not_to be_nil
end
end

context "when used" do
# EagerPerson has had `vault_persist_before_save!` called on it
let(:model_class) { EagerPerson }

it "the model does not have an after_save callback" do
save_callbacks = EagerPerson._save_callbacks.select do |cb|
save_callbacks = model_class._save_callbacks.select do |cb|
cb.filter == :__vault_persist_attributes!
end

expect(save_callbacks.length).to eq 0
end

it "the model has a before_save callback" do
save_callbacks = EagerPerson._save_callbacks.select do |cb|
save_callbacks = model_class._save_callbacks.select do |cb|
cb.filter == :__vault_encrypt_attributes!
end

Expand All @@ -144,20 +150,20 @@
end

it 'calls the correct callback' do
eager_person = EagerPerson.new(ssn: '123-45-6789')
expect(eager_person).not_to receive(:__vault_persist_attributes!)
expect(eager_person).to receive(:__vault_encrypt_attributes!)
record = model_class.new(ssn: '123-45-6789')
expect(record).not_to receive(:__vault_persist_attributes!)
expect(record).to receive(:__vault_encrypt_attributes!)

eager_person.save
record.save
end

it 'encrypts the attribute if it has been saved' do
eager_person = EagerPerson.new(ssn: '123-45-6789')
record = model_class.new(ssn: '123-45-6789')
expect(Vault::Rails).to receive(:encrypt).with('transit', 'dummy_people_ssn',anything,anything,anything).and_call_original

eager_person.save
record.save

expect(eager_person.ssn_encrypted).not_to be_nil
expect(record.ssn_encrypted).not_to be_nil
end
end
end
Expand Down

0 comments on commit f99aebd

Please sign in to comment.