##// END OF EJS Templates
Adds a test for when updating a news fails....
Jean-Philippe Lang -
r8934:9634fe054ab8
parent child
Show More
@@ -1,158 +1,166
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_show
49 def test_show
50 get :show, :id => 1
50 get :show, :id => 1
51 assert_response :success
51 assert_response :success
52 assert_template 'show'
52 assert_template 'show'
53 assert_tag :tag => 'h2', :content => /eCookbook first release/
53 assert_tag :tag => 'h2', :content => /eCookbook first release/
54 end
54 end
55
55
56 def test_show_should_show_attachments
56 def test_show_should_show_attachments
57 attachment = Attachment.first
57 attachment = Attachment.first
58 attachment.container = News.find(1)
58 attachment.container = News.find(1)
59 attachment.save!
59 attachment.save!
60
60
61 get :show, :id => 1
61 get :show, :id => 1
62 assert_response :success
62 assert_response :success
63 assert_tag 'a', :content => attachment.filename
63 assert_tag 'a', :content => attachment.filename
64 end
64 end
65
65
66 def test_show_not_found
66 def test_show_not_found
67 get :show, :id => 999
67 get :show, :id => 999
68 assert_response 404
68 assert_response 404
69 end
69 end
70
70
71 def test_get_new
71 def test_get_new
72 @request.session[:user_id] = 2
72 @request.session[:user_id] = 2
73 get :new, :project_id => 1
73 get :new, :project_id => 1
74 assert_response :success
74 assert_response :success
75 assert_template 'new'
75 assert_template 'new'
76 end
76 end
77
77
78 def test_post_create
78 def test_post_create
79 ActionMailer::Base.deliveries.clear
79 ActionMailer::Base.deliveries.clear
80 Setting.notified_events << 'news_added'
80 Setting.notified_events << 'news_added'
81
81
82 @request.session[:user_id] = 2
82 @request.session[:user_id] = 2
83 post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
83 post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
84 :description => 'This is the description',
84 :description => 'This is the description',
85 :summary => '' }
85 :summary => '' }
86 assert_redirected_to '/projects/ecookbook/news'
86 assert_redirected_to '/projects/ecookbook/news'
87
87
88 news = News.find_by_title('NewsControllerTest')
88 news = News.find_by_title('NewsControllerTest')
89 assert_not_nil news
89 assert_not_nil news
90 assert_equal 'This is the description', news.description
90 assert_equal 'This is the description', news.description
91 assert_equal User.find(2), news.author
91 assert_equal User.find(2), news.author
92 assert_equal Project.find(1), news.project
92 assert_equal Project.find(1), news.project
93 assert_equal 1, ActionMailer::Base.deliveries.size
93 assert_equal 1, ActionMailer::Base.deliveries.size
94 end
94 end
95
95
96 def test_post_create_with_attachment
96 def test_post_create_with_attachment
97 set_tmp_attachments_directory
97 set_tmp_attachments_directory
98 @request.session[:user_id] = 2
98 @request.session[:user_id] = 2
99 assert_difference 'News.count' do
99 assert_difference 'News.count' do
100 assert_difference 'Attachment.count' do
100 assert_difference 'Attachment.count' do
101 post :create, :project_id => 1,
101 post :create, :project_id => 1,
102 :news => { :title => 'Test', :description => 'This is the description' },
102 :news => { :title => 'Test', :description => 'This is the description' },
103 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
103 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
104 end
104 end
105 end
105 end
106 attachment = Attachment.first(:order => 'id DESC')
106 attachment = Attachment.first(:order => 'id DESC')
107 news = News.first(:order => 'id DESC')
107 news = News.first(:order => 'id DESC')
108 assert_equal news, attachment.container
108 assert_equal news, attachment.container
109 end
109 end
110
110
111 def test_post_create_with_validation_failure
111 def test_post_create_with_validation_failure
112 @request.session[:user_id] = 2
112 @request.session[:user_id] = 2
113 post :create, :project_id => 1, :news => { :title => '',
113 post :create, :project_id => 1, :news => { :title => '',
114 :description => 'This is the description',
114 :description => 'This is the description',
115 :summary => '' }
115 :summary => '' }
116 assert_response :success
116 assert_response :success
117 assert_template 'new'
117 assert_template 'new'
118 assert_not_nil assigns(:news)
118 assert_not_nil assigns(:news)
119 assert assigns(:news).new_record?
119 assert assigns(:news).new_record?
120 assert_error_tag :content => /title can't be blank/i
120 assert_error_tag :content => /title can't be blank/i
121 end
121 end
122
122
123 def test_get_edit
123 def test_get_edit
124 @request.session[:user_id] = 2
124 @request.session[:user_id] = 2
125 get :edit, :id => 1
125 get :edit, :id => 1
126 assert_response :success
126 assert_response :success
127 assert_template 'edit'
127 assert_template 'edit'
128 end
128 end
129
129
130 def test_put_update
130 def test_put_update
131 @request.session[:user_id] = 2
131 @request.session[:user_id] = 2
132 put :update, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
132 put :update, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
133 assert_redirected_to '/news/1'
133 assert_redirected_to '/news/1'
134 news = News.find(1)
134 news = News.find(1)
135 assert_equal 'Description changed by test_post_edit', news.description
135 assert_equal 'Description changed by test_post_edit', news.description
136 end
136 end
137
137
138 def test_put_update_with_attachment
138 def test_put_update_with_attachment
139 set_tmp_attachments_directory
139 set_tmp_attachments_directory
140 @request.session[:user_id] = 2
140 @request.session[:user_id] = 2
141 assert_no_difference 'News.count' do
141 assert_no_difference 'News.count' do
142 assert_difference 'Attachment.count' do
142 assert_difference 'Attachment.count' do
143 put :update, :id => 1,
143 put :update, :id => 1,
144 :news => { :description => 'This is the description' },
144 :news => { :description => 'This is the description' },
145 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
145 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
146 end
146 end
147 end
147 end
148 attachment = Attachment.first(:order => 'id DESC')
148 attachment = Attachment.first(:order => 'id DESC')
149 assert_equal News.find(1), attachment.container
149 assert_equal News.find(1), attachment.container
150 end
150 end
151
151
152 def test_update_with_failure
153 @request.session[:user_id] = 2
154 put :update, :id => 1, :news => { :description => '' }
155 assert_response :success
156 assert_template 'edit'
157 assert_error_tag :content => /description can't be blank/i
158 end
159
152 def test_destroy
160 def test_destroy
153 @request.session[:user_id] = 2
161 @request.session[:user_id] = 2
154 delete :destroy, :id => 1
162 delete :destroy, :id => 1
155 assert_redirected_to '/projects/ecookbook/news'
163 assert_redirected_to '/projects/ecookbook/news'
156 assert_nil News.find_by_id(1)
164 assert_nil News.find_by_id(1)
157 end
165 end
158 end
166 end
General Comments 0
You need to be logged in to leave comments. Login now