#--
# 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 'firm_controller'

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

class FirmControllerTest < Test::Unit::TestCase

  use_all_fixtures

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

    log_in_as users(:universal_grant_guy)
  end

  def test_get_index
    get :index
    assert_response :success
    assert_template 'index'
    assert_not_nil assigns(:new_firm)
  end

  def test_create
    assert_nil Firm.find_by_name('simpson')
    post :index, 
      :create => 1,
      :new_firm => { :name => 'simpson',  
                     :full_name => 'Bank of the Simpsons' }
    assert_redirected_to :action => 'index'
    assert_not_nil Firm.find_by_name('simpson')
  end

  def test_create_bad
    post :index, 
      :create => 1,
      :new_firm => { :name => 'simpson',  
                     :full_name => '' }
    assert_response :success
    assert !assigns(:new_firm).valid?
    assert_nil Firm.find_by_name('simpson')
  end

  def test_delete
    post :index, :destroy => { firms(:shire).id.to_s => 1 }
    assert_redirected_to :action => 'index'
    assert_nil Firm.find_by_id( firms(:shire).id )
  end

  def test_post_index_administer
    post :index, :administer => { firms(:shire).id.to_s => 1 }
    assert_redirected_to :action => 'administer', :id => firms(:shire)
  end

  def test_get_administer
    get :administer, :id => firms(:shire).id
    assert_response :success
    assert_template 'administer'
    assert_equal firms(:shire), assigns(:firm)
  end

  def test_post_administer_good
    post :administer, :id => firms(:shire).id, 
      :firm => { :password_lifetime_days => '56'}
    assert_redirected_to :action => 'administer', :id => firms(:shire)
    assert_equal 56, firms(:shire).reload.password_lifetime_days
  end

  def test_post_administer_bad
    post :administer, :id => firms(:shire).id, 
      :firm => { :password_lifetime_days => '-2'}
    assert_response :success
    assert_template 'administer'
    assert_tag :attributes => { :class => 'errorExplanation' }
    assert_equal firms(:shire), assigns(:firm)
    assert_not_nil assigns(:firm).errors.on(:password_lifetime_days)
  end

end

