11require_relative 'test_helper'
22
3- ActiveRecord ::Base . establish_connection ( adapter : 'sqlite3' , database : ':memory: ' )
3+ ActiveRecord ::Base . establish_connection ( adapter : 'sqlite3' , database : 'test.db ' )
44
55def create_tables
66 ActiveRecord ::Schema . define ( version : 1 ) do
@@ -41,6 +41,17 @@ def create_tables
4141 end
4242end
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+
4455ActiveRecord ::MissingAttributeError = ActiveModel ::MissingAttributeError unless defined? ( ActiveRecord ::MissingAttributeError )
4556
4657if ::ActiveRecord ::VERSION ::STRING > "4.0"
@@ -53,7 +64,12 @@ class UploadedFile; end
5364 require 'action_controller/metal/strong_parameters'
5465end
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 )
0 commit comments