Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encryption stubbing #112

Merged
merged 1 commit into from
Apr 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/crypt_keeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@
require 'crypt_keeper/provider/postgres_pgp_public_key'

module CryptKeeper
class << self
attr_accessor :stub_encryption
alias_method :stub_encryption?, :stub_encryption
end
end

CryptKeeper.stub_encryption = false
4 changes: 2 additions & 2 deletions lib/crypt_keeper/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def digest_passphrase(key, salt)

module Serializer
def dump(value)
if value.blank?
if value.blank? || CryptKeeper.stub_encryption?
value
else
encrypt(value.to_s)
end
end

def load(value)
if value.blank?
if value.blank? || CryptKeeper.stub_encryption?
value
else
decrypt(value)
Expand Down
18 changes: 18 additions & 0 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module CryptKeeper

subject { create_model }

after do
CryptKeeper.stub_encryption = false
end

describe "#crypt_keeper" do
context "Fields" do
it "enables encryption for the given fields" do
Expand Down Expand Up @@ -77,6 +81,20 @@ module CryptKeeper
data = subject.create!(storage: 1)
data.reload.storage.should == "1"
end

it "does not decrypt when stubbing is enabled" do
CryptKeeper.stub_encryption = true
record = subject.create!(storage: "testing")
CryptKeeper::Provider::Encryptor.any_instance.should_not_receive(:decrypt)
subject.find(record.id).storage
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than using should_not_receive, how about just asserting that the plaintext of storage is unchanged.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit tricky skirting deserialization, but I suppose it could work. I was following the pattern in other tests.

end

it "does not decrypt when stubbing is enabled after model is created" do
record = subject.create!(storage: "testing")
CryptKeeper.stub_encryption = true
CryptKeeper::Provider::Encryptor.any_instance.should_not_receive(:decrypt)
subject.find(record.id).storage
end
end

context "Search" do
Expand Down