categories_controller_test.rb
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require File.dirname(__FILE__) + '/../test_helper'
require 'categories_controller'
# Re-raise errors caught by the controller.
class CategoriesController; def rescue_action(e) raise e end; end
class CategoriesControllerTest < Test::Unit::TestCase
  all_fixtures
  def setup
    @controller = CategoriesController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
   
    @env = Environment.create!(:name => "My test environment")
    Environment.stubs(:default).returns(env)
    assert (@cat1 = env.categories.create(:name => 'a test category'))
    assert (@cat1 = env.categories.create(:name => 'another category'))
    login_as(create_admin_user(@env))
  end
  attr_reader :env, :cat1, :cat2
  def test_local_files_reference
    assert_local_files_reference
  end
  
  def test_valid_xhtml
    assert_valid_xhtml
  end
  
  def test_index
    assert user =  login_as(create_admin_user(Environment.default))
    assert user.person.has_permission?('manage_environment_categories',Environment.default ), "#{user.login} don't have permission to manage_environment_categories in #{Environment.default.name}"
    get :index
    assert_response :success
    assert_template 'index'
    assert_kind_of Array, assigns(:categories)
    assert_tag :tag => 'a', :attributes => { :href => '/admin/categories/new'}
  end
  def test_edit
    cat = Category.new
    env.categories.expects(:find).with('1').returns(cat)
    get :edit, :id => '1'
    assert_response :success
    assert_template 'edit'
    assert_equal cat, assigns(:category)
  end
  def test_edit_save
    post :edit, :id => cat1.id, :category => { :name => 'new name for category' }
    assert_redirected_to :action => 'index'
    assert_equal 'new name for category', Category.find(cat1.id).name
  end
  def test_new_category
    cat = Category.new
    Category.expects(:new).returns(cat)
    get :new
  end
  def test_new_product_category
    cat = ProductCategory.new
    ProductCategory.expects(:new).returns(cat)
    get :new, :type => 'ProductCategory'
  end
  def test_new_save
    assert_difference Category, :count do
      post :new, :category => { :name => 'a new category' }
      assert_redirected_to :action => 'index'
    end
  end
  def test_remove
    cat = Category.create!(:name => 'a category to be removed', :environment_id => env.id)
    post :remove, :id => cat.id
    assert_redirected_to :action => 'index'
    assert_raise ActiveRecord::RecordNotFound do
      Category.find(cat.id)
    end
  end
  should 'be able to upload a file' do
    assert_difference Category, :count do
      post :new, :category => { :name => 'new category', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
      assert_equal assigns(:category).image.filename, 'rails.png'
    end
  end
end