test/refcode_test.rb in refcode-0.0.1 vs test/refcode_test.rb in refcode-0.1.0

- old
+ new

@@ -5,10 +5,17 @@ class RefcodeTest < Test::Unit::TestCase SECRET = "686cb0461769ebed556f56bc88c956bfdf786646ab1920f58201723c557ccbaeb57112fd53302c06475e6e19a176ba617ee284b091d375858cb259e8578c620f" SALT = "dcc74de5efdab0d80b5d763f8a63bef3d37c7453ee45446fbbccd26648af8ca6a112576dafbf5475f3cd7b968703cc9e0682ee45b8b622045cec287908f536e8" + class BareEncodableObject < OpenStruct + include Refcode::Encodable + end + class EncodableObject < BareEncodableObject + has_refcode :secret => SECRET, :salt => :record_id + end + def test_encoder_against_hash test_hash = { :channel => 'email', :record_id => 49203, :action => 'forward' } assert_equal test_hash, encode_and_decode(test_hash) end @@ -24,10 +31,42 @@ def test_encoder_against_open_struct test_struct = OpenStruct.new({:test => 42}) assert_equal test_struct, encode_and_decode(test_struct) end + def test_should_raise_argument_error_if_generate_refcode_called_without_val + assert_raise ArgumentError do + encodable_object.generate_refcode + end + end + + def test_should_raise_error_if_generate_refcode_called_before_has_refcode + assert_raise ArgumentError do + obj = BareEncodableObject.new :some => 'value' + obj.generate_refcode + end + end + + def test_should_raise_an_error_if_salt_is_empty + encodable_object.record_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) + 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) + end + private + + def encodable_object + @encodable_object ||= EncodableObject.new :name => 'Unimportant Value', :record_id => 381598 + end def encoder @encoder ||= Refcode::Encoder.new do |e| e.secret = SECRET e.salt = SALT