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