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