Skip to content

Commit f334dab

Browse files
committed
Fixed tests, the tables need to be created before declaring the classes, attr_encrypted uses columns_hash to identify column type in table.
1 parent e3418c2 commit f334dab

File tree

5 files changed

+138
-116
lines changed

5 files changed

+138
-116
lines changed

lib/attr_encrypted/adapters/active_record.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ def attr_encrypted(*attrs)
5151
super
5252
options = attrs.extract_options!
5353
attr = attrs.pop
54-
# if ::ActiveRecord::VERSION::STRING >= "5.1.0"
55-
# column_type = columns_hash[attr.to_s].type unless columns_hash[attr.to_s].nil?
56-
# column_type.nil? ? attribute(attr) : attribute(attr, column_type)
57-
# end
54+
if ::ActiveRecord::VERSION::STRING >= "5.1.0"
55+
column_type = columns_hash[attr.to_s].type unless columns_hash[attr.to_s].nil?
56+
column_type.nil? ? attribute(attr) : attribute(attr, column_type)
57+
end
5858
options.merge! encrypted_attributes[attr]
5959

6060
define_method("#{attr}_was") do

test/active_record_test.rb

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require_relative 'test_helper'
22

3-
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
3+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'test.db')
44

55
def create_tables
66
ActiveRecord::Schema.define(version: 1) do
@@ -41,6 +41,17 @@ def create_tables
4141
end
4242
end
4343

44+
def drop_tables
45+
ActiveRecord::Schema.define(version: 1) do
46+
self.verbose = false
47+
drop_table :people, if_exists: true
48+
drop_table :accounts, if_exists: true
49+
drop_table :users, if_exists: true
50+
drop_table :prime_ministers, if_exists: true
51+
drop_table :addresses, if_exists: true
52+
end
53+
end
54+
4455
ActiveRecord::MissingAttributeError = ActiveModel::MissingAttributeError unless defined?(ActiveRecord::MissingAttributeError)
4556

4657
if ::ActiveRecord::VERSION::STRING > "4.0"
@@ -53,7 +64,12 @@ class UploadedFile; end
5364
require 'action_controller/metal/strong_parameters'
5465
end
5566

56-
class Person < ActiveRecord::Base
67+
class ActiveRecordTest < Minitest::Test
68+
69+
drop_tables
70+
create_tables
71+
72+
class Person < ActiveRecord::Base
5773
self.attr_encrypted_options[:mode] = :per_attribute_iv_and_salt
5874
attr_encrypted :email, key: SECRET_KEY
5975
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
6783
self.salt ||= Digest::SHA256.hexdigest((Time.now.to_i * rand(1000)).to_s)[0..15]
6884
self.credentials ||= { username: 'example', password: 'test' }
6985
end
70-
end
86+
end
7187

72-
class PersonWithValidation < Person
73-
validates_presence_of :email
74-
end
88+
class PersonWithValidation < Person
89+
validates_presence_of :email
90+
end
7591

76-
class PersonWithProcMode < Person
77-
attr_encrypted :email, key: SECRET_KEY, mode: Proc.new { :per_attribute_iv_and_salt }
78-
attr_encrypted :credentials, key: SECRET_KEY, mode: Proc.new { :single_iv_and_salt }, insecure_mode: true
79-
end
92+
class PersonWithProcMode < Person
93+
attr_encrypted :email, key: SECRET_KEY, mode: Proc.new { :per_attribute_iv_and_salt }
94+
attr_encrypted :credentials, key: SECRET_KEY, mode: Proc.new { :single_iv_and_salt }, insecure_mode: true
95+
end
8096

81-
class Account < ActiveRecord::Base
82-
ACCOUNT_ENCRYPTION_KEY = SecureRandom.urlsafe_base64(24)
83-
attr_encrypted :password, key: :password_encryption_key
97+
class Account < ActiveRecord::Base
98+
ACCOUNT_ENCRYPTION_KEY = SecureRandom.urlsafe_base64(24)
99+
attr_encrypted :password, key: :password_encryption_key
84100

85-
def encrypting?(attr)
86-
encrypted_attributes[attr][:operation] == :encrypting
87-
end
101+
def encrypting?(attr)
102+
encrypted_attributes[attr][:operation] == :encrypting
103+
end
88104

89-
def password_encryption_key
90-
if encrypting?(:password)
91-
self.key = ACCOUNT_ENCRYPTION_KEY
92-
else
93-
self.key
105+
def password_encryption_key
106+
if encrypting?(:password)
107+
self.key = ACCOUNT_ENCRYPTION_KEY
108+
else
109+
self.key
110+
end
94111
end
95112
end
96-
end
97-
98-
class PersonWithSerialization < ActiveRecord::Base
99-
self.table_name = 'people'
100-
attr_encrypted :email, key: SECRET_KEY
101-
serialize :password
102-
end
103-
104-
class UserWithProtectedAttribute < ActiveRecord::Base
105-
self.table_name = 'users'
106-
attr_encrypted :password, key: SECRET_KEY
107-
attr_protected :is_admin if ::ActiveRecord::VERSION::STRING < "4.0"
108-
end
109113

110-
class PersonUsingAlias < ActiveRecord::Base
111-
self.table_name = 'people'
112-
attr_encryptor :email, key: SECRET_KEY
113-
end
114+
class PersonWithSerialization < ActiveRecord::Base
115+
self.table_name = 'people'
116+
attr_encrypted :email, key: SECRET_KEY
117+
serialize :password
118+
end
114119

115-
class PrimeMinister < ActiveRecord::Base
116-
attr_encrypted :name, marshal: true, key: SECRET_KEY
117-
end
120+
class UserWithProtectedAttribute < ActiveRecord::Base
121+
self.table_name = 'users'
122+
attr_encrypted :password, key: SECRET_KEY
123+
attr_protected :is_admin if ::ActiveRecord::VERSION::STRING < "4.0"
124+
end
118125

119-
class Address < ActiveRecord::Base
120-
self.attr_encrypted_options[:marshal] = false
121-
self.attr_encrypted_options[:encode] = false
122-
attr_encrypted :street, encode_iv: false, key: SECRET_KEY
123-
attr_encrypted :zipcode, key: SECRET_KEY, mode: Proc.new { |address| address.mode.to_sym }, insecure_mode: true
124-
end
126+
class PersonUsingAlias < ActiveRecord::Base
127+
self.table_name = 'people'
128+
attr_encryptor :email, key: SECRET_KEY
129+
end
125130

126-
class ActiveRecordTest < Minitest::Test
131+
class PrimeMinister < ActiveRecord::Base
132+
attr_encrypted :name, marshal: true, key: SECRET_KEY
133+
end
127134

128-
def setup
129-
drop_all_tables
130-
create_tables
135+
class Address < ActiveRecord::Base
136+
self.attr_encrypted_options[:marshal] = false
137+
self.attr_encrypted_options[:encode] = false
138+
attr_encrypted :street, encode_iv: false, key: SECRET_KEY
139+
attr_encrypted :zipcode, key: SECRET_KEY, mode: Proc.new { |address| address.mode.to_sym }, insecure_mode: true
131140
end
132141

133142
def test_should_encrypt_email
@@ -162,6 +171,7 @@ def test_should_encrypt_decrypt_with_iv
162171
end
163172

164173
def test_should_ensure_attributes_can_be_deserialized
174+
debugger
165175
@person = PersonWithSerialization.new(email: 'test@example.com', password: %w(an array of strings))
166176
@person.save
167177
assert_equal @person.password, %w(an array of strings)

test/compatibility_test.rb

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
11
# -*- encoding: utf-8 -*-
22
require_relative 'test_helper'
33

4+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'test.db'
5+
6+
def create_tables
7+
ActiveRecord::Schema.define(:version => 1) do
8+
create_table :nonmarshalling_pets do |t|
9+
t.string :name
10+
t.string :encrypted_nickname
11+
t.string :encrypted_nickname_iv
12+
t.string :encrypted_nickname_salt
13+
t.string :encrypted_birthdate
14+
t.string :encrypted_birthdate_iv
15+
t.string :encrypted_birthdate_salt
16+
end
17+
create_table :marshalling_pets do |t|
18+
t.string :name
19+
t.string :encrypted_nickname
20+
t.string :encrypted_nickname_iv
21+
t.string :encrypted_nickname_salt
22+
t.string :encrypted_birthdate
23+
t.string :encrypted_birthdate_iv
24+
t.string :encrypted_birthdate_salt
25+
end
26+
end
27+
end
28+
29+
def drop_tables
30+
ActiveRecord::Schema.define(version: 1) do
31+
self.verbose = false
32+
drop_table :nonmarshalling_pets, if_exists: true
33+
drop_table :marshalling_pets, if_exists: true
34+
end
35+
end
36+
437
# Test to ensure that existing representations in database do not break on
538
# migrating to new versions of this gem. This ensures that future versions of
639
# this gem will retain backwards compatibility with data generated by earlier
740
# versions.
841
class CompatibilityTest < Minitest::Test
42+
drop_tables
43+
create_tables
44+
945
class NonmarshallingPet < ActiveRecord::Base
1046
PET_NICKNAME_SALT = Digest::SHA256.hexdigest('my-really-really-secret-pet-nickname-salt')
1147
PET_NICKNAME_KEY = 'my-really-really-secret-pet-nickname-key'
@@ -40,11 +76,6 @@ class MarshallingPet < ActiveRecord::Base
4076
:marshal => true
4177
end
4278

43-
def setup
44-
drop_all_tables
45-
create_tables
46-
end
47-
4879
def test_nonmarshalling_backwards_compatibility
4980
pet = NonmarshallingPet.create!(
5081
:name => 'Fido',
@@ -78,30 +109,4 @@ def test_marshalling_backwards_compatibility
78109
assert_equal Date.new(2011, 7, 9), pet.birthdate
79110
end
80111

81-
private
82-
83-
def create_tables
84-
ActiveRecord::Schema.define(:version => 1) do
85-
create_table :nonmarshalling_pets do |t|
86-
t.string :name
87-
t.string :encrypted_nickname
88-
t.string :encrypted_nickname_iv
89-
t.string :encrypted_nickname_salt
90-
t.string :encrypted_birthdate
91-
t.string :encrypted_birthdate_iv
92-
t.string :encrypted_birthdate_salt
93-
end
94-
create_table :marshalling_pets do |t|
95-
t.string :name
96-
t.string :encrypted_nickname
97-
t.string :encrypted_nickname_iv
98-
t.string :encrypted_nickname_salt
99-
t.string :encrypted_birthdate
100-
t.string :encrypted_birthdate_iv
101-
t.string :encrypted_birthdate_salt
102-
end
103-
end
104-
end
105112
end
106-
107-
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'

test/legacy_active_record_test.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- encoding: utf-8 -*-
22
require_relative 'test_helper'
33

4-
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
4+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'test.db'
55

66
def create_people_table
77
ActiveRecord::Schema.define(:version => 1) do
@@ -14,7 +14,14 @@ def create_people_table
1414
end
1515
end
1616

17+
def drop_tables
18+
ActiveRecord::Schema.define(:version => 1) do
19+
drop_table :legacy_people, if_exists: true
20+
end
21+
end
22+
1723
# The table needs to exist before defining the class
24+
drop_tables
1825
create_people_table
1926

2027
ActiveRecord::MissingAttributeError = ActiveModel::MissingAttributeError unless defined?(ActiveRecord::MissingAttributeError)
@@ -49,11 +56,6 @@ class LegacyPersonWithValidation < LegacyPerson
4956

5057
class LegacyActiveRecordTest < Minitest::Test
5158

52-
def setup
53-
drop_all_tables
54-
create_people_table
55-
end
56-
5759
def test_should_decrypt_with_correct_encoding
5860
if defined?(Encoding)
5961
@person = LegacyPerson.create :email => 'test@example.com'

test/legacy_compatibility_test.rb

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
# -*- encoding: utf-8 -*-
22
require_relative 'test_helper'
33

4+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'test.db'
5+
6+
def create_tables
7+
ActiveRecord::Schema.define(:version => 1) do
8+
create_table :legacy_nonmarshalling_pets do |t|
9+
t.string :name
10+
t.string :encrypted_nickname
11+
t.string :encrypted_birthdate
12+
t.string :salt
13+
end
14+
create_table :legacy_marshalling_pets do |t|
15+
t.string :name
16+
t.string :encrypted_nickname
17+
t.string :encrypted_birthdate
18+
t.string :salt
19+
end
20+
end
21+
end
22+
23+
def drop_tables
24+
ActiveRecord::Schema.define(version: 1) do
25+
drop_table :legacy_nonmarshalling_pets, if_exists: true
26+
drop_table :legacy_marshalling_pets, if_exists: true
27+
end
28+
end
29+
30+
drop_tables
31+
create_tables
32+
433
# Test to ensure that existing representations in database do not break on
534
# migrating to new versions of this gem. This ensures that future versions of
635
# this gem will retain backwards compatibility with data generated by earlier
@@ -40,11 +69,6 @@ class LegacyMarshallingPet < ActiveRecord::Base
4069
:marshal => true
4170
end
4271

43-
def setup
44-
drop_all_tables
45-
create_tables
46-
end
47-
4872
def test_nonmarshalling_backwards_compatibility
4973
pet = LegacyNonmarshallingPet.create!(
5074
:name => 'Fido',
@@ -70,24 +94,5 @@ def test_marshalling_backwards_compatibility
7094
assert_equal Date.new(2011, 7, 9), pet.birthdate
7195
end
7296

73-
private
74-
75-
def create_tables
76-
ActiveRecord::Schema.define(:version => 1) do
77-
create_table :legacy_nonmarshalling_pets do |t|
78-
t.string :name
79-
t.string :encrypted_nickname
80-
t.string :encrypted_birthdate
81-
t.string :salt
82-
end
83-
create_table :legacy_marshalling_pets do |t|
84-
t.string :name
85-
t.string :encrypted_nickname
86-
t.string :encrypted_birthdate
87-
t.string :salt
88-
end
89-
end
90-
end
9197
end
9298

93-
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'

0 commit comments

Comments
 (0)