##// END OF EJS Templates
Rails4: replace deprecated Relation#last with finder options at MessagesControllerTest...
Toshi MARUYAMA -
r12351:65ba233c620a
parent child
Show More
@@ -1,220 +1,220
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 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
19
20 class MessagesControllerTest < ActionController::TestCase
20 class MessagesControllerTest < ActionController::TestCase
21 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
21 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
22
22
23 def setup
23 def setup
24 User.current = nil
24 User.current = nil
25 end
25 end
26
26
27 def test_show
27 def test_show
28 get :show, :board_id => 1, :id => 1
28 get :show, :board_id => 1, :id => 1
29 assert_response :success
29 assert_response :success
30 assert_template 'show'
30 assert_template 'show'
31 assert_not_nil assigns(:board)
31 assert_not_nil assigns(:board)
32 assert_not_nil assigns(:project)
32 assert_not_nil assigns(:project)
33 assert_not_nil assigns(:topic)
33 assert_not_nil assigns(:topic)
34 end
34 end
35
35
36 def test_show_should_contain_reply_field_tags_for_quoting
36 def test_show_should_contain_reply_field_tags_for_quoting
37 @request.session[:user_id] = 2
37 @request.session[:user_id] = 2
38 get :show, :board_id => 1, :id => 1
38 get :show, :board_id => 1, :id => 1
39 assert_response :success
39 assert_response :success
40
40
41 # tags required by MessagesController#quote
41 # tags required by MessagesController#quote
42 assert_tag 'input', :attributes => {:id => 'message_subject'}
42 assert_tag 'input', :attributes => {:id => 'message_subject'}
43 assert_tag 'textarea', :attributes => {:id => 'message_content'}
43 assert_tag 'textarea', :attributes => {:id => 'message_content'}
44 assert_tag 'div', :attributes => {:id => 'reply'}
44 assert_tag 'div', :attributes => {:id => 'reply'}
45 end
45 end
46
46
47 def test_show_with_pagination
47 def test_show_with_pagination
48 message = Message.find(1)
48 message = Message.find(1)
49 assert_difference 'Message.count', 30 do
49 assert_difference 'Message.count', 30 do
50 30.times do
50 30.times do
51 message.children << Message.new(:subject => 'Reply',
51 message.children << Message.new(:subject => 'Reply',
52 :content => 'Reply body',
52 :content => 'Reply body',
53 :author_id => 2,
53 :author_id => 2,
54 :board_id => 1)
54 :board_id => 1)
55 end
55 end
56 end
56 end
57 get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
57 get :show, :board_id => 1, :id => 1, :r => message.children.order('id').last.id
58 assert_response :success
58 assert_response :success
59 assert_template 'show'
59 assert_template 'show'
60 replies = assigns(:replies)
60 replies = assigns(:replies)
61 assert_not_nil replies
61 assert_not_nil replies
62 assert !replies.include?(message.children.first(:order => 'id'))
62 assert !replies.include?(message.children.first(:order => 'id'))
63 assert replies.include?(message.children.last(:order => 'id'))
63 assert replies.include?(message.children.order('id').last)
64 end
64 end
65
65
66 def test_show_with_reply_permission
66 def test_show_with_reply_permission
67 @request.session[:user_id] = 2
67 @request.session[:user_id] = 2
68 get :show, :board_id => 1, :id => 1
68 get :show, :board_id => 1, :id => 1
69 assert_response :success
69 assert_response :success
70 assert_template 'show'
70 assert_template 'show'
71 assert_tag :div, :attributes => { :id => 'reply' },
71 assert_tag :div, :attributes => { :id => 'reply' },
72 :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
72 :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
73 end
73 end
74
74
75 def test_show_message_not_found
75 def test_show_message_not_found
76 get :show, :board_id => 1, :id => 99999
76 get :show, :board_id => 1, :id => 99999
77 assert_response 404
77 assert_response 404
78 end
78 end
79
79
80 def test_show_message_from_invalid_board_should_respond_with_404
80 def test_show_message_from_invalid_board_should_respond_with_404
81 get :show, :board_id => 999, :id => 1
81 get :show, :board_id => 999, :id => 1
82 assert_response 404
82 assert_response 404
83 end
83 end
84
84
85 def test_get_new
85 def test_get_new
86 @request.session[:user_id] = 2
86 @request.session[:user_id] = 2
87 get :new, :board_id => 1
87 get :new, :board_id => 1
88 assert_response :success
88 assert_response :success
89 assert_template 'new'
89 assert_template 'new'
90 end
90 end
91
91
92 def test_get_new_with_invalid_board
92 def test_get_new_with_invalid_board
93 @request.session[:user_id] = 2
93 @request.session[:user_id] = 2
94 get :new, :board_id => 99
94 get :new, :board_id => 99
95 assert_response 404
95 assert_response 404
96 end
96 end
97
97
98 def test_post_new
98 def test_post_new
99 @request.session[:user_id] = 2
99 @request.session[:user_id] = 2
100 ActionMailer::Base.deliveries.clear
100 ActionMailer::Base.deliveries.clear
101
101
102 with_settings :notified_events => %w(message_posted) do
102 with_settings :notified_events => %w(message_posted) do
103 post :new, :board_id => 1,
103 post :new, :board_id => 1,
104 :message => { :subject => 'Test created message',
104 :message => { :subject => 'Test created message',
105 :content => 'Message body'}
105 :content => 'Message body'}
106 end
106 end
107 message = Message.find_by_subject('Test created message')
107 message = Message.find_by_subject('Test created message')
108 assert_not_nil message
108 assert_not_nil message
109 assert_redirected_to "/boards/1/topics/#{message.to_param}"
109 assert_redirected_to "/boards/1/topics/#{message.to_param}"
110 assert_equal 'Message body', message.content
110 assert_equal 'Message body', message.content
111 assert_equal 2, message.author_id
111 assert_equal 2, message.author_id
112 assert_equal 1, message.board_id
112 assert_equal 1, message.board_id
113
113
114 mail = ActionMailer::Base.deliveries.last
114 mail = ActionMailer::Base.deliveries.last
115 assert_not_nil mail
115 assert_not_nil mail
116 assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
116 assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
117 assert_mail_body_match 'Message body', mail
117 assert_mail_body_match 'Message body', mail
118 # author
118 # author
119 assert mail.bcc.include?('jsmith@somenet.foo')
119 assert mail.bcc.include?('jsmith@somenet.foo')
120 # project member
120 # project member
121 assert mail.bcc.include?('dlopper@somenet.foo')
121 assert mail.bcc.include?('dlopper@somenet.foo')
122 end
122 end
123
123
124 def test_get_edit
124 def test_get_edit
125 @request.session[:user_id] = 2
125 @request.session[:user_id] = 2
126 get :edit, :board_id => 1, :id => 1
126 get :edit, :board_id => 1, :id => 1
127 assert_response :success
127 assert_response :success
128 assert_template 'edit'
128 assert_template 'edit'
129 end
129 end
130
130
131 def test_post_edit
131 def test_post_edit
132 @request.session[:user_id] = 2
132 @request.session[:user_id] = 2
133 post :edit, :board_id => 1, :id => 1,
133 post :edit, :board_id => 1, :id => 1,
134 :message => { :subject => 'New subject',
134 :message => { :subject => 'New subject',
135 :content => 'New body'}
135 :content => 'New body'}
136 assert_redirected_to '/boards/1/topics/1'
136 assert_redirected_to '/boards/1/topics/1'
137 message = Message.find(1)
137 message = Message.find(1)
138 assert_equal 'New subject', message.subject
138 assert_equal 'New subject', message.subject
139 assert_equal 'New body', message.content
139 assert_equal 'New body', message.content
140 end
140 end
141
141
142 def test_post_edit_sticky_and_locked
142 def test_post_edit_sticky_and_locked
143 @request.session[:user_id] = 2
143 @request.session[:user_id] = 2
144 post :edit, :board_id => 1, :id => 1,
144 post :edit, :board_id => 1, :id => 1,
145 :message => { :subject => 'New subject',
145 :message => { :subject => 'New subject',
146 :content => 'New body',
146 :content => 'New body',
147 :locked => '1',
147 :locked => '1',
148 :sticky => '1'}
148 :sticky => '1'}
149 assert_redirected_to '/boards/1/topics/1'
149 assert_redirected_to '/boards/1/topics/1'
150 message = Message.find(1)
150 message = Message.find(1)
151 assert_equal true, message.sticky?
151 assert_equal true, message.sticky?
152 assert_equal true, message.locked?
152 assert_equal true, message.locked?
153 end
153 end
154
154
155 def test_post_edit_should_allow_to_change_board
155 def test_post_edit_should_allow_to_change_board
156 @request.session[:user_id] = 2
156 @request.session[:user_id] = 2
157 post :edit, :board_id => 1, :id => 1,
157 post :edit, :board_id => 1, :id => 1,
158 :message => { :subject => 'New subject',
158 :message => { :subject => 'New subject',
159 :content => 'New body',
159 :content => 'New body',
160 :board_id => 2}
160 :board_id => 2}
161 assert_redirected_to '/boards/2/topics/1'
161 assert_redirected_to '/boards/2/topics/1'
162 message = Message.find(1)
162 message = Message.find(1)
163 assert_equal Board.find(2), message.board
163 assert_equal Board.find(2), message.board
164 end
164 end
165
165
166 def test_reply
166 def test_reply
167 @request.session[:user_id] = 2
167 @request.session[:user_id] = 2
168 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
168 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
169 reply = Message.order('id DESC').first
169 reply = Message.order('id DESC').first
170 assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
170 assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
171 assert Message.find_by_subject('Test reply')
171 assert Message.find_by_subject('Test reply')
172 end
172 end
173
173
174 def test_destroy_topic
174 def test_destroy_topic
175 @request.session[:user_id] = 2
175 @request.session[:user_id] = 2
176 assert_difference 'Message.count', -3 do
176 assert_difference 'Message.count', -3 do
177 post :destroy, :board_id => 1, :id => 1
177 post :destroy, :board_id => 1, :id => 1
178 end
178 end
179 assert_redirected_to '/projects/ecookbook/boards/1'
179 assert_redirected_to '/projects/ecookbook/boards/1'
180 assert_nil Message.find_by_id(1)
180 assert_nil Message.find_by_id(1)
181 end
181 end
182
182
183 def test_destroy_reply
183 def test_destroy_reply
184 @request.session[:user_id] = 2
184 @request.session[:user_id] = 2
185 assert_difference 'Message.count', -1 do
185 assert_difference 'Message.count', -1 do
186 post :destroy, :board_id => 1, :id => 2
186 post :destroy, :board_id => 1, :id => 2
187 end
187 end
188 assert_redirected_to '/boards/1/topics/1?r=2'
188 assert_redirected_to '/boards/1/topics/1?r=2'
189 assert_nil Message.find_by_id(2)
189 assert_nil Message.find_by_id(2)
190 end
190 end
191
191
192 def test_quote
192 def test_quote
193 @request.session[:user_id] = 2
193 @request.session[:user_id] = 2
194 xhr :get, :quote, :board_id => 1, :id => 3
194 xhr :get, :quote, :board_id => 1, :id => 3
195 assert_response :success
195 assert_response :success
196 assert_equal 'text/javascript', response.content_type
196 assert_equal 'text/javascript', response.content_type
197 assert_template 'quote'
197 assert_template 'quote'
198 assert_include 'RE: First post', response.body
198 assert_include 'RE: First post', response.body
199 assert_include '> An other reply', response.body
199 assert_include '> An other reply', response.body
200 end
200 end
201
201
202 def test_preview_new
202 def test_preview_new
203 @request.session[:user_id] = 2
203 @request.session[:user_id] = 2
204 post :preview,
204 post :preview,
205 :board_id => 1,
205 :board_id => 1,
206 :message => {:subject => "", :content => "Previewed text"}
206 :message => {:subject => "", :content => "Previewed text"}
207 assert_response :success
207 assert_response :success
208 assert_template 'common/_preview'
208 assert_template 'common/_preview'
209 end
209 end
210
210
211 def test_preview_edit
211 def test_preview_edit
212 @request.session[:user_id] = 2
212 @request.session[:user_id] = 2
213 post :preview,
213 post :preview,
214 :id => 4,
214 :id => 4,
215 :board_id => 1,
215 :board_id => 1,
216 :message => {:subject => "", :content => "Previewed text"}
216 :message => {:subject => "", :content => "Previewed text"}
217 assert_response :success
217 assert_response :success
218 assert_template 'common/_preview'
218 assert_template 'common/_preview'
219 end
219 end
220 end
220 end
General Comments 0
You need to be logged in to leave comments. Login now