##// END OF EJS Templates
More functional tests for NewsController....
Jean-Philippe Lang -
r9053:33cb9c87a9fa
parent child
Show More
@@ -1,166 +1,171
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'news_controller'
19 require 'news_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class NewsController; def rescue_action(e) raise e end; end
22 class NewsController; def rescue_action(e) raise e end; end
23
23
24 class NewsControllerTest < ActionController::TestCase
24 class NewsControllerTest < ActionController::TestCase
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
26
26
27 def setup
27 def setup
28 @controller = NewsController.new
28 @controller = NewsController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_index
34 def test_index
35 get :index
35 get :index
36 assert_response :success
36 assert_response :success
37 assert_template 'index'
37 assert_template 'index'
38 assert_not_nil assigns(:newss)
38 assert_not_nil assigns(:newss)
39 assert_nil assigns(:project)
39 assert_nil assigns(:project)
40 end
40 end
41
41
42 def test_index_with_project
42 def test_index_with_project
43 get :index, :project_id => 1
43 get :index, :project_id => 1
44 assert_response :success
44 assert_response :success
45 assert_template 'index'
45 assert_template 'index'
46 assert_not_nil assigns(:newss)
46 assert_not_nil assigns(:newss)
47 end
47 end
48
48
49 def test_index_with_invalid_project_should_respond_with_404
50 get :index, :project_id => 999
51 assert_response 404
52 end
53
49 def test_show
54 def test_show
50 get :show, :id => 1
55 get :show, :id => 1
51 assert_response :success
56 assert_response :success
52 assert_template 'show'
57 assert_template 'show'
53 assert_tag :tag => 'h2', :content => /eCookbook first release/
58 assert_tag :tag => 'h2', :content => /eCookbook first release/
54 end
59 end
55
60
56 def test_show_should_show_attachments
61 def test_show_should_show_attachments
57 attachment = Attachment.first
62 attachment = Attachment.first
58 attachment.container = News.find(1)
63 attachment.container = News.find(1)
59 attachment.save!
64 attachment.save!
60
65
61 get :show, :id => 1
66 get :show, :id => 1
62 assert_response :success
67 assert_response :success
63 assert_tag 'a', :content => attachment.filename
68 assert_tag 'a', :content => attachment.filename
64 end
69 end
65
70
66 def test_show_not_found
71 def test_show_not_found
67 get :show, :id => 999
72 get :show, :id => 999
68 assert_response 404
73 assert_response 404
69 end
74 end
70
75
71 def test_get_new
76 def test_get_new
72 @request.session[:user_id] = 2
77 @request.session[:user_id] = 2
73 get :new, :project_id => 1
78 get :new, :project_id => 1
74 assert_response :success
79 assert_response :success
75 assert_template 'new'
80 assert_template 'new'
76 end
81 end
77
82
78 def test_post_create
83 def test_post_create
79 ActionMailer::Base.deliveries.clear
84 ActionMailer::Base.deliveries.clear
80 Setting.notified_events << 'news_added'
85 Setting.notified_events << 'news_added'
81
86
82 @request.session[:user_id] = 2
87 @request.session[:user_id] = 2
83 post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
88 post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
84 :description => 'This is the description',
89 :description => 'This is the description',
85 :summary => '' }
90 :summary => '' }
86 assert_redirected_to '/projects/ecookbook/news'
91 assert_redirected_to '/projects/ecookbook/news'
87
92
88 news = News.find_by_title('NewsControllerTest')
93 news = News.find_by_title('NewsControllerTest')
89 assert_not_nil news
94 assert_not_nil news
90 assert_equal 'This is the description', news.description
95 assert_equal 'This is the description', news.description
91 assert_equal User.find(2), news.author
96 assert_equal User.find(2), news.author
92 assert_equal Project.find(1), news.project
97 assert_equal Project.find(1), news.project
93 assert_equal 1, ActionMailer::Base.deliveries.size
98 assert_equal 1, ActionMailer::Base.deliveries.size
94 end
99 end
95
100
96 def test_post_create_with_attachment
101 def test_post_create_with_attachment
97 set_tmp_attachments_directory
102 set_tmp_attachments_directory
98 @request.session[:user_id] = 2
103 @request.session[:user_id] = 2
99 assert_difference 'News.count' do
104 assert_difference 'News.count' do
100 assert_difference 'Attachment.count' do
105 assert_difference 'Attachment.count' do
101 post :create, :project_id => 1,
106 post :create, :project_id => 1,
102 :news => { :title => 'Test', :description => 'This is the description' },
107 :news => { :title => 'Test', :description => 'This is the description' },
103 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
108 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
104 end
109 end
105 end
110 end
106 attachment = Attachment.first(:order => 'id DESC')
111 attachment = Attachment.first(:order => 'id DESC')
107 news = News.first(:order => 'id DESC')
112 news = News.first(:order => 'id DESC')
108 assert_equal news, attachment.container
113 assert_equal news, attachment.container
109 end
114 end
110
115
111 def test_post_create_with_validation_failure
116 def test_post_create_with_validation_failure
112 @request.session[:user_id] = 2
117 @request.session[:user_id] = 2
113 post :create, :project_id => 1, :news => { :title => '',
118 post :create, :project_id => 1, :news => { :title => '',
114 :description => 'This is the description',
119 :description => 'This is the description',
115 :summary => '' }
120 :summary => '' }
116 assert_response :success
121 assert_response :success
117 assert_template 'new'
122 assert_template 'new'
118 assert_not_nil assigns(:news)
123 assert_not_nil assigns(:news)
119 assert assigns(:news).new_record?
124 assert assigns(:news).new_record?
120 assert_error_tag :content => /title can't be blank/i
125 assert_error_tag :content => /title can't be blank/i
121 end
126 end
122
127
123 def test_get_edit
128 def test_get_edit
124 @request.session[:user_id] = 2
129 @request.session[:user_id] = 2
125 get :edit, :id => 1
130 get :edit, :id => 1
126 assert_response :success
131 assert_response :success
127 assert_template 'edit'
132 assert_template 'edit'
128 end
133 end
129
134
130 def test_put_update
135 def test_put_update
131 @request.session[:user_id] = 2
136 @request.session[:user_id] = 2
132 put :update, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
137 put :update, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
133 assert_redirected_to '/news/1'
138 assert_redirected_to '/news/1'
134 news = News.find(1)
139 news = News.find(1)
135 assert_equal 'Description changed by test_post_edit', news.description
140 assert_equal 'Description changed by test_post_edit', news.description
136 end
141 end
137
142
138 def test_put_update_with_attachment
143 def test_put_update_with_attachment
139 set_tmp_attachments_directory
144 set_tmp_attachments_directory
140 @request.session[:user_id] = 2
145 @request.session[:user_id] = 2
141 assert_no_difference 'News.count' do
146 assert_no_difference 'News.count' do
142 assert_difference 'Attachment.count' do
147 assert_difference 'Attachment.count' do
143 put :update, :id => 1,
148 put :update, :id => 1,
144 :news => { :description => 'This is the description' },
149 :news => { :description => 'This is the description' },
145 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
150 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
146 end
151 end
147 end
152 end
148 attachment = Attachment.first(:order => 'id DESC')
153 attachment = Attachment.first(:order => 'id DESC')
149 assert_equal News.find(1), attachment.container
154 assert_equal News.find(1), attachment.container
150 end
155 end
151
156
152 def test_update_with_failure
157 def test_update_with_failure
153 @request.session[:user_id] = 2
158 @request.session[:user_id] = 2
154 put :update, :id => 1, :news => { :description => '' }
159 put :update, :id => 1, :news => { :description => '' }
155 assert_response :success
160 assert_response :success
156 assert_template 'edit'
161 assert_template 'edit'
157 assert_error_tag :content => /description can't be blank/i
162 assert_error_tag :content => /description can't be blank/i
158 end
163 end
159
164
160 def test_destroy
165 def test_destroy
161 @request.session[:user_id] = 2
166 @request.session[:user_id] = 2
162 delete :destroy, :id => 1
167 delete :destroy, :id => 1
163 assert_redirected_to '/projects/ecookbook/news'
168 assert_redirected_to '/projects/ecookbook/news'
164 assert_nil News.find_by_id(1)
169 assert_nil News.find_by_id(1)
165 end
170 end
166 end
171 end
General Comments 0
You need to be logged in to leave comments. Login now