test/refcode_encodable_test.rb in refcode-0.1.1 vs test/refcode_encodable_test.rb in refcode-0.1.2

- old
+ new

@@ -3,27 +3,38 @@ require File.join(File.dirname(__FILE__),"..","lib","refcode.rb") class RefcodeEncodableTest < Test::Unit::TestCase SECRET = "a short tidy secret, but not too short!" + CODE_CONTENT = {:channel => 'email', :action => 'forward', :user_id => 14325, :unique_hash => 'f'} class BareEncodableObject < OpenStruct include Refcode::Encodable end - class EncodableObjectSymbol < BareEncodableObject - has_refcode :secret => SECRET, :salt => :record_id + class EncodableObjectSaltSymbol < BareEncodableObject + has_refcode :secret => SECRET, :salt => :user_id end - class EncodableObjectString < BareEncodableObject + class EncodableObjectSaltString < BareEncodableObject has_refcode :secret => SECRET, :salt => "123 abc xyz" end - class EncodableObjectSaltyLambda < BareEncodableObject - has_refcode :secret => SECRET, :salt => lambda { |me| "#{me.record_id}-42" } + class EncodableObjectSaltLambda < BareEncodableObject + has_refcode :secret => SECRET, :salt => lambda { |me| "#{me.user_id}-42" } end + class EncodableObjectSecretSymbol < BareEncodableObject + has_refcode :secret => :unique_hash, :salt => :user_id + end + class EncodableObjectSecretString < BareEncodableObject + has_refcode :secret => SECRET, :salt => "123 abc xyz" + end + class EncodableObjectSecretLambda < BareEncodableObject + has_refcode :secret => lambda { |me| me.channel*20 }, :salt => "aaa" + end def test_should_raise_argument_error_if_generate_refcode_called_without_val assert_raise ArgumentError do - encodable_object.generate_refcode + obj = EncodableObjectSaltString.new CODE_CONTENT + obj.generate_refcode end end def test_should_raise_error_if_refcode_methods_called_before_has_refcode [:generate_refcode, :parse_refcode].each do |m| @@ -40,35 +51,53 @@ obj.class.has_refcode :whatever => 'wrong' end end def test_should_raise_an_error_if_salt_is_empty - encodable_object.record_id = nil + obj = EncodableObjectSaltSymbol.new CODE_CONTENT + obj.user_id = nil assert_raise RuntimeError do - referral_code_content = { :channel => 'email', :action => 'forward', :user_id => 14325 } - code = encodable_object.generate_refcode referral_code_content - assert_equal referral_code_content, encodable_object.parse_refcode(code) + code = obj.generate_refcode CODE_CONTENT + assert_equal CODE_CONTENT, obj.parse_refcode(code) end end def test_should_work_with_different_salt_options - [EncodableObjectSaltyLambda, EncodableObjectString].each do |klass| - referral_code_content = { :channel => 'email', :action => 'forward', :user_id => 14325 } - obj = klass.new referral_code_content - code = obj.generate_refcode referral_code_content - assert_equal referral_code_content, obj.parse_refcode(code) + with_all_fixtures do |klass| + obj = klass.new CODE_CONTENT + code = obj.generate_refcode CODE_CONTENT + assert_equal CODE_CONTENT, obj.parse_refcode(code) end end - def test_encodable_against_hash - referral_code_content = { :channel => 'email', :action => 'forward', :user_id => 14325 } - code = encodable_object.generate_refcode referral_code_content - assert_equal referral_code_content, encodable_object.parse_refcode(code) + def test_should_work_with_different_secret_options + with_all_fixtures do |klass| + obj = klass.new CODE_CONTENT + code = obj.generate_refcode CODE_CONTENT + assert_equal CODE_CONTENT, obj.parse_refcode(code) + end end + def test_should_adjust_key_length_to_satisfy_encryption_algorithm + content = CODE_CONTENT + content[:unique_hash] = "x" + obj = EncodableObjectSecretSymbol.new content + code = obj.generate_refcode content + assert_equal content, obj.parse_refcode(code) + end + private - def encodable_object - @encodable_object ||= EncodableObjectSymbol.new :name => 'Unimportant Value', :record_id => 381598 + def with_all_fixtures + [ + EncodableObjectSaltLambda, + EncodableObjectSaltString, + EncodableObjectSaltSymbol, + EncodableObjectSecretLambda, + EncodableObjectSecretString, + EncodableObjectSecretSymbol + ].each do |klass| + yield klass + end end end