I''m trying to get a test to pass and keep getting a "no method
error"
I''m using the routines from Chapter 8 of "Security on Rails"
and can''t
get the test to pass. I keep getting "no method error" for decrypt.
Can someone help me with the correct "address" for decrypt so I can
call it directly.
Here is the routine definition:
module Encryptor
module Routines
def decrypt(cipher_text, key, opts)
cipher = OpenSSL::Cipher::Cipher.new(algorithm(opts))
decoded_cipher_text = Base64.decode64(cipher_text)
#cipher.decrypt(key, decoded_cipher_text.slice!(0..15))
cipher.decrypt
cipher.pkcs5_keyivgen(key)
# DW 11/3/10 Above two lines per deprecation warning and
stackoverflow site
# TODO Use a real Salt instead of Thoracic1
out = cipher.update(decoded_cipher_text)
out << cipher.final
end
end
end
Here is the "after save" hook:
module Encryptor
module ActiveRecord
class Encryptor
include ::Encryptor::Routines
def after_save(model)
unless model[@field].blank?
key = model.class.encryption_key
model[@field] = decrypt(model[@field], key, @options)
end
end
end
end
end
Here is the test:
require ''test_helper''
class UserTest < ActiveSupport::TestCase
fixtures :users
test "the_encrypter" do
@user = users(:bob)
@user.secret_answer = "Volvo"
assert @user.save
@answer = User.connection.select_all(
"select secret_answer from users where id = 1"
)
cipher_text = @answer.first[''secret_answer'']
opts = Encryptor::ActiveRecord::Encryptor.default_options
@plain_text = decrypt(cipher_text, User.encryption_key, opts)
assert_equal @plain_text, "Volvo"
end
end
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Nov-06 00:41 UTC
Re: Calling a method directly from a test => no method error
On Nov 4, 10:33 pm, dwormuth <dworm...-cIpcPs7DjqbWs/AcZQh2Cw@public.gmane.org> wrote:> I''m trying to get a test to pass and keep getting a "no method error" > I''m using the routines from Chapter 8 of "Security on Rails" and can''t > get the test to pass. I keep getting "no method error" for decrypt. > Can someone help me with the correct "address" for decrypt so I can > call it directly.Depending on what it is you''re trying to test you could either create an instance of your Encryptor class (decrypt is an instance method there) or mixin your Routines module into the test case. Fred> > Here is the routine definition: > > module Encryptor > module Routines > def decrypt(cipher_text, key, opts) > cipher = OpenSSL::Cipher::Cipher.new(algorithm(opts)) > decoded_cipher_text = Base64.decode64(cipher_text) > #cipher.decrypt(key, decoded_cipher_text.slice!(0..15)) > cipher.decrypt > cipher.pkcs5_keyivgen(key) > # DW 11/3/10 Above two lines per deprecation warning and > stackoverflow site > # TODO Use a real Salt instead of Thoracic1 > out = cipher.update(decoded_cipher_text) > out << cipher.final > end > end > end > > Here is the "after save" hook: > > module Encryptor > module ActiveRecord > class Encryptor > include ::Encryptor::Routines > > def after_save(model) > unless model[@field].blank? > key = model.class.encryption_key > model[@field] = decrypt(model[@field], key, @options) > end > end > end > end > end > > Here is the test: > > require ''test_helper'' > class UserTest < ActiveSupport::TestCase > > fixtures :users > > test "the_encrypter" do > @user = users(:bob) > @user.secret_answer = "Volvo" > assert @user.save > @answer = User.connection.select_all( > "select secret_answer from users where id = 1" > ) > cipher_text = @answer.first[''secret_answer''] > opts = Encryptor::ActiveRecord::Encryptor.default_options > @plain_text = decrypt(cipher_text, User.encryption_key, opts) > assert_equal @plain_text, "Volvo" > end > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.