#--
# Copyright (c) 2007 Robert S. Thau, Smartleaf, Inc.
# 
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
require File.dirname(__FILE__) + '/../test_helper'
require 'payments_controller'

# Re-raise errors caught by the controller.
class PaymentsController; def rescue_action(e) raise e end; end

class PaymentsControllerTest < Test::Unit::TestCase

  use_all_fixtures

  def setup
    @controller = PaymentsController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new

    log_in_as users(:ricky)
  end

  def test_get_new
    get :new, :order_id => orders(:lucys_order).id.to_s
    assert_response :success
    assert_equal orders(:lucys_order), assigns(:order)
  end

  def test_post_create
    post :create, :order_id => orders(:lucys_order).id.to_s,
         :order => { :shipping_address => '6 christopher walken lane', 
                     :payment_authenticator => '01234' }
    assert_valid assigns(:order)
    assert_equal '6 christopher walken lane', assigns(:order).shipping_address
    assert_redirected_to order_payment_url( orders(:lucys_order) )

    orders(:lucys_order).reload

    assert orders(:lucys_order).paid?
    assert_equal '01234', orders(:lucys_order).payment_authenticator

    get :show, :order_id => orders(:lucys_order).id.to_s
    assert_response :success
    assert_template 'show'
  end

  def test_post_create_bad
    post :create, :order_id => orders(:lucys_order).id.to_s,
         :order => { :shipping_address => '', 
                     :payment_authenticator => '012345' }

    assert_response :success
    assert_template 'new'
    assert_equal orders(:lucys_order), assigns(:order)

    orders(:lucys_order).reload

    assert !orders(:lucys_order).paid?
  end

end

