-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed tests, the tables need to be created before declaring the class…
…es, attr_encrypted uses `columns_hash` to identify column type in table.
- Loading branch information
1 parent
e3418c2
commit 739c3fd
Showing
5 changed files
with
139 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
require_relative 'test_helper' | ||
|
||
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | ||
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'test.db') | ||
|
||
def create_tables | ||
ActiveRecord::Schema.define(version: 1) do | ||
|
@@ -41,6 +41,17 @@ def create_tables | |
end | ||
end | ||
|
||
def drop_tables | ||
ActiveRecord::Schema.define(version: 1) do | ||
self.verbose = false | ||
drop_table :people, if_exists: true | ||
drop_table :accounts, if_exists: true | ||
drop_table :users, if_exists: true | ||
drop_table :prime_ministers, if_exists: true | ||
drop_table :addresses, if_exists: true | ||
end | ||
end | ||
|
||
ActiveRecord::MissingAttributeError = ActiveModel::MissingAttributeError unless defined?(ActiveRecord::MissingAttributeError) | ||
|
||
if ::ActiveRecord::VERSION::STRING > "4.0" | ||
|
@@ -53,7 +64,12 @@ class UploadedFile; end | |
require 'action_controller/metal/strong_parameters' | ||
end | ||
|
||
class Person < ActiveRecord::Base | ||
class ActiveRecordTest < Minitest::Test | ||
|
||
drop_tables | ||
create_tables | ||
|
||
class Person < ActiveRecord::Base | ||
self.attr_encrypted_options[:mode] = :per_attribute_iv_and_salt | ||
attr_encrypted :email, key: SECRET_KEY | ||
attr_encrypted :credentials, key: Proc.new { |user| Encryptor.encrypt(value: user.salt, key: SECRET_KEY, iv: user.key_iv) }, marshal: true | ||
|
@@ -67,67 +83,60 @@ def initialize_salt_and_credentials | |
self.salt ||= Digest::SHA256.hexdigest((Time.now.to_i * rand(1000)).to_s)[0..15] | ||
self.credentials ||= { username: 'example', password: 'test' } | ||
end | ||
end | ||
end | ||
|
||
class PersonWithValidation < Person | ||
validates_presence_of :email | ||
end | ||
class PersonWithValidation < Person | ||
validates_presence_of :email | ||
end | ||
|
||
class PersonWithProcMode < Person | ||
attr_encrypted :email, key: SECRET_KEY, mode: Proc.new { :per_attribute_iv_and_salt } | ||
attr_encrypted :credentials, key: SECRET_KEY, mode: Proc.new { :single_iv_and_salt }, insecure_mode: true | ||
end | ||
class PersonWithProcMode < Person | ||
attr_encrypted :email, key: SECRET_KEY, mode: Proc.new { :per_attribute_iv_and_salt } | ||
attr_encrypted :credentials, key: SECRET_KEY, mode: Proc.new { :single_iv_and_salt }, insecure_mode: true | ||
end | ||
|
||
class Account < ActiveRecord::Base | ||
ACCOUNT_ENCRYPTION_KEY = SecureRandom.urlsafe_base64(24) | ||
attr_encrypted :password, key: :password_encryption_key | ||
class Account < ActiveRecord::Base | ||
ACCOUNT_ENCRYPTION_KEY = SecureRandom.urlsafe_base64(24) | ||
attr_encrypted :password, key: :password_encryption_key | ||
|
||
def encrypting?(attr) | ||
encrypted_attributes[attr][:operation] == :encrypting | ||
end | ||
def encrypting?(attr) | ||
encrypted_attributes[attr][:operation] == :encrypting | ||
end | ||
|
||
def password_encryption_key | ||
if encrypting?(:password) | ||
self.key = ACCOUNT_ENCRYPTION_KEY | ||
else | ||
self.key | ||
def password_encryption_key | ||
if encrypting?(:password) | ||
self.key = ACCOUNT_ENCRYPTION_KEY | ||
else | ||
self.key | ||
end | ||
end | ||
end | ||
end | ||
|
||
class PersonWithSerialization < ActiveRecord::Base | ||
self.table_name = 'people' | ||
attr_encrypted :email, key: SECRET_KEY | ||
serialize :password | ||
end | ||
|
||
class UserWithProtectedAttribute < ActiveRecord::Base | ||
self.table_name = 'users' | ||
attr_encrypted :password, key: SECRET_KEY | ||
attr_protected :is_admin if ::ActiveRecord::VERSION::STRING < "4.0" | ||
end | ||
|
||
class PersonUsingAlias < ActiveRecord::Base | ||
self.table_name = 'people' | ||
attr_encryptor :email, key: SECRET_KEY | ||
end | ||
class PersonWithSerialization < ActiveRecord::Base | ||
self.table_name = 'people' | ||
attr_encrypted :email, key: SECRET_KEY | ||
serialize :password | ||
end | ||
|
||
class PrimeMinister < ActiveRecord::Base | ||
attr_encrypted :name, marshal: true, key: SECRET_KEY | ||
end | ||
class UserWithProtectedAttribute < ActiveRecord::Base | ||
self.table_name = 'users' | ||
attr_encrypted :password, key: SECRET_KEY | ||
attr_protected :is_admin if ::ActiveRecord::VERSION::STRING < "4.0" | ||
end | ||
|
||
class Address < ActiveRecord::Base | ||
self.attr_encrypted_options[:marshal] = false | ||
self.attr_encrypted_options[:encode] = false | ||
attr_encrypted :street, encode_iv: false, key: SECRET_KEY | ||
attr_encrypted :zipcode, key: SECRET_KEY, mode: Proc.new { |address| address.mode.to_sym }, insecure_mode: true | ||
end | ||
class PersonUsingAlias < ActiveRecord::Base | ||
self.table_name = 'people' | ||
attr_encryptor :email, key: SECRET_KEY | ||
end | ||
|
||
class ActiveRecordTest < Minitest::Test | ||
class PrimeMinister < ActiveRecord::Base | ||
attr_encrypted :name, marshal: true, key: SECRET_KEY | ||
end | ||
|
||
def setup | ||
drop_all_tables | ||
create_tables | ||
class Address < ActiveRecord::Base | ||
self.attr_encrypted_options[:marshal] = false | ||
self.attr_encrypted_options[:encode] = false | ||
attr_encrypted :street, encode_iv: false, key: SECRET_KEY | ||
attr_encrypted :zipcode, key: SECRET_KEY, mode: Proc.new { |address| address.mode.to_sym }, insecure_mode: true | ||
end | ||
|
||
def test_should_encrypt_email | ||
|
@@ -162,6 +171,7 @@ def test_should_encrypt_decrypt_with_iv | |
end | ||
|
||
def test_should_ensure_attributes_can_be_deserialized | ||
debugger | ||
@person = PersonWithSerialization.new(email: '[email protected]', password: %w(an array of strings)) | ||
@person.save | ||
assert_equal @person.password, %w(an array of strings) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# -*- encoding: utf-8 -*- | ||
require_relative 'test_helper' | ||
|
||
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:' | ||
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'test.db' | ||
|
||
def create_people_table | ||
ActiveRecord::Schema.define(:version => 1) do | ||
|
@@ -14,7 +14,14 @@ def create_people_table | |
end | ||
end | ||
|
||
def drop_tables | ||
ActiveRecord::Schema.define(:version => 1) do | ||
drop_table :legacy_people, if_exists: true | ||
end | ||
end | ||
|
||
# The table needs to exist before defining the class | ||
drop_tables | ||
create_people_table | ||
|
||
ActiveRecord::MissingAttributeError = ActiveModel::MissingAttributeError unless defined?(ActiveRecord::MissingAttributeError) | ||
|
@@ -49,11 +56,6 @@ class LegacyPersonWithValidation < LegacyPerson | |
|
||
class LegacyActiveRecordTest < Minitest::Test | ||
|
||
def setup | ||
drop_all_tables | ||
create_people_table | ||
end | ||
|
||
def test_should_decrypt_with_correct_encoding | ||
if defined?(Encoding) | ||
@person = LegacyPerson.create :email => '[email protected]' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters