##// END OF EJS Templates
Small test refactoring, extract method....
Eric Davis -
r3837:fc6e7f12b70e
parent child
Show More
@@ -1,26 +1,24
1 1 require "#{File.dirname(__FILE__)}/../test_helper"
2 2
3 3 class LayoutTest < ActionController::IntegrationTest
4 4 fixtures :all
5 5
6 6 test "browsing to a missing page should render the base layout" do
7 7 get "/users/100000000"
8 8
9 9 assert_response :not_found
10 10
11 11 # UsersController uses the admin layout by default
12 12 assert_select "#admin-menu", :count => 0
13 13 end
14 14
15 15 test "browsing to an unauthorized page should render the base layout" do
16 user = User.find(9)
17 user.password, user.password_confirmation = 'test', 'test'
18 user.save!
16 change_user_password('miscuser9', 'test')
19 17
20 18 log_user('miscuser9','test')
21 19
22 20 get "/admin"
23 21 assert_response :forbidden
24 22 assert_select "#admin-menu", :count => 0
25 23 end
26 24 end
@@ -1,174 +1,180
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 ENV["RAILS_ENV"] = "test"
19 19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 20 require 'test_help'
21 21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
23 23
24 24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
25 25 include ObjectDaddyHelpers
26 26
27 27 class ActiveSupport::TestCase
28 28 # Transactional fixtures accelerate your tests by wrapping each test method
29 29 # in a transaction that's rolled back on completion. This ensures that the
30 30 # test database remains unchanged so your fixtures don't have to be reloaded
31 31 # between every test method. Fewer database queries means faster tests.
32 32 #
33 33 # Read Mike Clark's excellent walkthrough at
34 34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
35 35 #
36 36 # Every Active Record database supports transactions except MyISAM tables
37 37 # in MySQL. Turn off transactional fixtures in this case; however, if you
38 38 # don't care one way or the other, switching from MyISAM to InnoDB tables
39 39 # is recommended.
40 40 self.use_transactional_fixtures = true
41 41
42 42 # Instantiated fixtures are slow, but give you @david where otherwise you
43 43 # would need people(:david). If you don't want to migrate your existing
44 44 # test cases which use the @david style and don't mind the speed hit (each
45 45 # instantiated fixtures translates to a database query per test method),
46 46 # then set this back to true.
47 47 self.use_instantiated_fixtures = false
48 48
49 49 # Add more helper methods to be used by all tests here...
50 50
51 51 def log_user(login, password)
52 52 User.anonymous
53 53 get "/login"
54 54 assert_equal nil, session[:user_id]
55 55 assert_response :success
56 56 assert_template "account/login"
57 57 post "/login", :username => login, :password => password
58 58 assert_equal login, User.find(session[:user_id]).login
59 59 end
60 60
61 61 def uploaded_test_file(name, mime)
62 62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
63 63 end
64 64
65 65 # Mock out a file
66 66 def mock_file
67 67 file = 'a_file.png'
68 68 file.stubs(:size).returns(32)
69 69 file.stubs(:original_filename).returns('a_file.png')
70 70 file.stubs(:content_type).returns('image/png')
71 71 file.stubs(:read).returns(false)
72 72 file
73 73 end
74 74
75 75 # Use a temporary directory for attachment related tests
76 76 def set_tmp_attachments_directory
77 77 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
78 78 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
79 79 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
80 80 end
81 81
82 82 def with_settings(options, &block)
83 83 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
84 84 options.each {|k, v| Setting[k] = v}
85 85 yield
86 86 saved_settings.each {|k, v| Setting[k] = v}
87 87 end
88 88
89 def change_user_password(login, new_password)
90 user = User.first(:conditions => {:login => login})
91 user.password, user.password_confirmation = new_password, new_password
92 user.save!
93 end
94
89 95 def self.ldap_configured?
90 96 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
91 97 return @test_ldap.bind
92 98 rescue Exception => e
93 99 # LDAP is not listening
94 100 return nil
95 101 end
96 102
97 103 # Returns the path to the test +vendor+ repository
98 104 def self.repository_path(vendor)
99 105 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
100 106 end
101 107
102 108 # Returns true if the +vendor+ test repository is configured
103 109 def self.repository_configured?(vendor)
104 110 File.directory?(repository_path(vendor))
105 111 end
106 112
107 113 # Shoulda macros
108 114 def self.should_render_404
109 115 should_respond_with :not_found
110 116 should_render_template 'common/404'
111 117 end
112 118
113 119 def self.should_have_before_filter(expected_method, options = {})
114 120 should_have_filter('before', expected_method, options)
115 121 end
116 122
117 123 def self.should_have_after_filter(expected_method, options = {})
118 124 should_have_filter('after', expected_method, options)
119 125 end
120 126
121 127 def self.should_have_filter(filter_type, expected_method, options)
122 128 description = "have #{filter_type}_filter :#{expected_method}"
123 129 description << " with #{options.inspect}" unless options.empty?
124 130
125 131 should description do
126 132 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
127 133 expected = klass.new(:filter, expected_method.to_sym, options)
128 134 assert_equal 1, @controller.class.filter_chain.select { |filter|
129 135 filter.method == expected.method && filter.kind == expected.kind &&
130 136 filter.options == expected.options && filter.class == expected.class
131 137 }.size
132 138 end
133 139 end
134 140
135 141 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
136 142 context "" do
137 143 setup do
138 144 if block_given?
139 145 instance_eval &block
140 146 else
141 147 @old_value = model.generate!
142 148 @new_value = model.generate!
143 149 end
144 150 end
145 151
146 152 should "use the new value's name" do
147 153 @detail = JournalDetail.generate!(:property => 'attr',
148 154 :old_value => @old_value.id,
149 155 :value => @new_value.id,
150 156 :prop_key => prop_key)
151 157
152 158 assert_match @new_value.name, show_detail(@detail, true)
153 159 end
154 160
155 161 should "use the old value's name" do
156 162 @detail = JournalDetail.generate!(:property => 'attr',
157 163 :old_value => @old_value.id,
158 164 :value => @new_value.id,
159 165 :prop_key => prop_key)
160 166
161 167 assert_match @old_value.name, show_detail(@detail, true)
162 168 end
163 169 end
164 170 end
165 171
166 172 def self.should_create_a_new_user(&block)
167 173 should "create a new user" do
168 174 user = instance_eval &block
169 175 assert user
170 176 assert_kind_of User, user
171 177 assert !user.new_record?
172 178 end
173 179 end
174 180 end
General Comments 0
You need to be logged in to leave comments. Login now