require 'pathname' require Pathname.new(__FILE__).dirname + 'test_helper' class UserTest < Test::Unit::TestCase #:nodoc: def test_should_authenticate_user assert Lypp::User.respond_to?(:authenticate) assert_kind_of Lypp::User, current_user assert_valid_user current_user, :id => user_id, :email => username end def test_should_create_user attributes = { :name => 'John Doe', :phone_number => '6045555555', :address => { :street_address => '123 Main Street', :postal_code => '12345', :location => 'Anytown', :subdivision => 'BC', :country => 'CA', }, :credit_card => { :name => 'John Doe', :number => '4387751111011', # not a real number :) :security_code => 123, :expiry_month => 1, :expiry_year => Time.now.year + 1 }, } new_user = Lypp::User.create attributes.merge(:password => password, :password_confirmation => password, :terms_of_service => true) assert_kind_of Lypp::User, new_user assert new_user.id assert_equal_attributes new_user, attributes end def test_should_show_user user = Lypp::User.find user_id assert_valid_user user, :id => user_id, :email => username end def test_should_update_user attributes = { :phone_number => '6045555555' } # set the attributes current_user.attributes = attributes # save and reload the user assert current_user.save current_user.reload # check to make sure the attributes were updated attributes.each do |attribute,value| assert_equal value, current_user.send(attribute) end end { :conferences => Lypp::Conference, :contacts => Lypp::Contact }.each do |association_name,klass| define_method "test_should_have_many_#{association_name}" do assert current_user.respond_to?(association_name) assert_kind_of ActiveResource::AssociationProxy, current_user.send(association_name) assert_equal klass, current_user.send(association_name).klass end end private def current_user @current_user ||= Lypp::User.authenticate end def username Lypp::Base.site.user end def password Lypp::Base.site.password end def user_id Lypp::Base.user_id.to_i end def assert_equal_attributes(object, attributes) attributes.each do |attribute,expected| value = object.send(attribute) if expected.kind_of?(Hash) && !value.nil? assert_equal_attributes value, expected else assert_equal expected, value end end end def assert_valid_user(user, attributes) if id = attributes[:id] attributes[:id] = id.to_i end assert_equal_attributes user, attributes %w[ id created_at updated_at lock_version balance_in_seconds ].each do |attribute| assert_not_nil user.send(attribute) end end end