You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

news_controller_test.rb 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class NewsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
  21. :enabled_modules, :news, :comments,
  22. :attachments, :user_preferences
  23. def setup
  24. User.current = nil
  25. end
  26. def test_index
  27. get :index
  28. assert_response :success
  29. assert_select 'h3 a', :text => 'eCookbook first release !'
  30. end
  31. def test_index_with_project
  32. get(:index, :params => {:project_id => 1})
  33. assert_response :success
  34. assert_select 'h3 a', :text => 'eCookbook first release !'
  35. end
  36. def test_index_with_invalid_project_should_respond_with_404
  37. get(:index, :params => {:project_id => 999})
  38. assert_response 404
  39. end
  40. def test_index_without_permission_should_fail
  41. Role.all.each {|r| r.remove_permission! :view_news}
  42. @request.session[:user_id] = 2
  43. get :index
  44. assert_response 403
  45. end
  46. def test_index_without_manage_news_permission_should_not_display_add_news_link
  47. user = User.find(2)
  48. @request.session[:user_id] = user.id
  49. Role.all.each {|r| r.remove_permission! :manage_news}
  50. get :index
  51. assert_select '.add-news-link', count: 0
  52. user.members.first.roles.first.add_permission! :manage_news
  53. get :index
  54. assert_select '.add-news-link', count: 1
  55. end
  56. def test_show
  57. get(:show, :params => {:id => 1})
  58. assert_response :success
  59. assert_select 'h2', :text => 'eCookbook first release !'
  60. end
  61. def test_show_should_show_attachments
  62. attachment = Attachment.first
  63. attachment.container = News.find(1)
  64. attachment.save!
  65. get(:show, :params => {:id => 1})
  66. assert_response :success
  67. assert_select 'a', :text => attachment.filename
  68. end
  69. def test_show_with_comments_in_reverse_order
  70. user = User.find(1)
  71. user.pref[:comments_sorting] = 'desc'
  72. user.pref.save!
  73. @request.session[:user_id] = 1
  74. get(:show, :params => {:id => 1})
  75. assert_response :success
  76. comments = css_select('#comments .wiki').map(&:text).map(&:strip)
  77. assert_equal ["This is an other comment", "my first comment"], comments
  78. end
  79. def test_show_not_found
  80. get(:show, :params => {:id => 999})
  81. assert_response 404
  82. end
  83. def test_get_new_with_project_id
  84. @request.session[:user_id] = 2
  85. get(:new, :params => {:project_id => 1})
  86. assert_response :success
  87. assert_select 'select[name=project_id]', false
  88. assert_select 'input[name=?]', 'news[title]'
  89. end
  90. def test_get_new_without_project_id
  91. @request.session[:user_id] = 2
  92. get(:new)
  93. assert_response :success
  94. assert_select 'select[name=project_id]'
  95. assert_select 'input[name=?]', 'news[title]'
  96. end
  97. def test_get_new_if_user_does_not_have_permission
  98. @request.session[:user_id] = 2
  99. User.find(2).roles.each{|u| u.remove_permission! :manage_news }
  100. get(:new)
  101. assert_response :forbidden
  102. assert_select 'select[name=project_id]', false
  103. assert_select 'input[name=?]', 'news[title]', count: 0
  104. end
  105. def test_post_create
  106. ActionMailer::Base.deliveries.clear
  107. @request.session[:user_id] = 2
  108. with_settings :notified_events => %w(news_added) do
  109. post(
  110. :create,
  111. :params => {
  112. :project_id => 1,
  113. :news => {
  114. :title => 'NewsControllerTest',
  115. :description => 'This is the description',
  116. :summary => ''
  117. }
  118. }
  119. )
  120. end
  121. assert_redirected_to '/projects/ecookbook/news'
  122. news = News.find_by_title('NewsControllerTest')
  123. assert_not_nil news
  124. assert_equal 'This is the description', news.description
  125. assert_equal User.find(2), news.author
  126. assert_equal Project.find(1), news.project
  127. assert_equal 2, ActionMailer::Base.deliveries.size
  128. end
  129. def test_post_create_with_cross_project_param
  130. ActionMailer::Base.deliveries.clear
  131. @request.session[:user_id] = 2
  132. with_settings :notified_events => %w(news_added) do
  133. post(
  134. :create,
  135. :params => {
  136. :project_id => 1,
  137. :cross_project => '1',
  138. :news => {
  139. :title => 'NewsControllerTest',
  140. :description => 'This is the description',
  141. :summary => ''
  142. }
  143. }
  144. )
  145. end
  146. assert_redirected_to '/news'
  147. news = News.find_by(title: 'NewsControllerTest')
  148. assert_not_nil news
  149. assert_equal 'This is the description', news.description
  150. assert_equal User.find(2), news.author
  151. assert_equal Project.find(1), news.project
  152. assert_equal 2, ActionMailer::Base.deliveries.size
  153. end
  154. def test_post_create_with_attachment
  155. set_tmp_attachments_directory
  156. ActionMailer::Base.deliveries.clear
  157. @request.session[:user_id] = 2
  158. assert_difference 'News.count' do
  159. assert_difference 'Attachment.count' do
  160. with_settings :notified_events => %w(news_added) do
  161. post(
  162. :create,
  163. :params => {
  164. :project_id => 1,
  165. :news => {
  166. :title => 'Test',
  167. :description => 'This is the description'
  168. },
  169. :attachments => {
  170. '1' => {
  171. 'file' => uploaded_test_file('testfile.txt', 'text/plain')
  172. }
  173. }
  174. }
  175. )
  176. end
  177. end
  178. end
  179. attachment = Attachment.order('id DESC').first
  180. news = News.order('id DESC').first
  181. assert_equal news, attachment.container
  182. assert_select_email do
  183. # link to the attachments download
  184. assert_select 'fieldset.attachments' do
  185. assert_select 'a[href=?]',
  186. "http://localhost:3000/attachments/download/#{attachment.id}/testfile.txt",
  187. :text => 'testfile.txt'
  188. end
  189. end
  190. end
  191. def test_post_create_with_validation_failure
  192. @request.session[:user_id] = 2
  193. post(
  194. :create,
  195. :params => {
  196. :project_id => 1,
  197. :news => {
  198. :title => '',
  199. :description => 'This is the description',
  200. :summary => ''
  201. }
  202. }
  203. )
  204. assert_response :success
  205. assert_select_error /title cannot be blank/i
  206. end
  207. def test_get_edit
  208. @request.session[:user_id] = 2
  209. get(:edit, :params => {:id => 1})
  210. assert_response :success
  211. assert_select 'input[name=?][value=?]', 'news[title]', 'eCookbook first release !'
  212. end
  213. def test_put_update
  214. @request.session[:user_id] = 2
  215. put(
  216. :update,
  217. :params => {
  218. :id => 1,
  219. :news => {
  220. :description => 'Description changed by test_post_edit'
  221. }
  222. }
  223. )
  224. assert_redirected_to '/news/1'
  225. news = News.find(1)
  226. assert_equal 'Description changed by test_post_edit', news.description
  227. end
  228. def test_put_update_with_attachment
  229. set_tmp_attachments_directory
  230. @request.session[:user_id] = 2
  231. assert_no_difference 'News.count' do
  232. assert_difference 'Attachment.count' do
  233. put(
  234. :update,
  235. :params => {
  236. :id => 1,
  237. :news => {
  238. :description => 'This is the description'
  239. },
  240. :attachments => {
  241. '1' => {
  242. 'file' => uploaded_test_file('testfile.txt', 'text/plain')
  243. }
  244. }
  245. }
  246. )
  247. end
  248. end
  249. attachment = Attachment.order('id DESC').first
  250. assert_equal News.find(1), attachment.container
  251. end
  252. def test_update_with_failure
  253. @request.session[:user_id] = 2
  254. put(
  255. :update,
  256. :params => {
  257. :id => 1,
  258. :news => {
  259. :description => ''
  260. }
  261. }
  262. )
  263. assert_response :success
  264. assert_select_error /description cannot be blank/i
  265. end
  266. def test_destroy
  267. @request.session[:user_id] = 2
  268. delete(:destroy, :params => {:id => 1})
  269. assert_redirected_to '/projects/ecookbook/news'
  270. assert_equal 'Successful deletion.', flash[:notice]
  271. assert_nil News.find_by_id(1)
  272. end
  273. end