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 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  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_for_logged_users
  37. @request.session[:user_id] = 2
  38. get(:index, :params => {:project_id => 999})
  39. assert_response 404
  40. end
  41. def test_index_with_invalid_project_should_respond_with_302_for_anonymous
  42. Role.anonymous.remove_permission! :view_news
  43. with_settings :login_required => '0' do
  44. get(:index, :params => {:project_id => 999})
  45. assert_response 302
  46. end
  47. end
  48. def test_index_without_permission_should_fail
  49. Role.all.each {|r| r.remove_permission! :view_news}
  50. @request.session[:user_id] = 2
  51. get :index
  52. assert_response 403
  53. end
  54. def test_index_without_manage_news_permission_should_not_display_add_news_link
  55. user = User.find(2)
  56. @request.session[:user_id] = user.id
  57. Role.all.each {|r| r.remove_permission! :manage_news}
  58. get :index
  59. assert_select '.add-news-link', count: 0
  60. user.members.first.roles.first.add_permission! :manage_news
  61. get :index
  62. assert_select '.add-news-link', count: 1
  63. end
  64. def test_show
  65. get(:show, :params => {:id => 1})
  66. assert_response :success
  67. assert_select 'p.breadcrumb a[href=?]', '/projects/ecookbook/news', :text => 'News'
  68. assert_select 'h2', :text => 'eCookbook first release !'
  69. end
  70. def test_show_should_show_attachments
  71. attachment = Attachment.first
  72. attachment.container = News.find(1)
  73. attachment.save!
  74. get(:show, :params => {:id => 1})
  75. assert_response :success
  76. assert_select 'a', :text => attachment.filename
  77. end
  78. def test_show_with_comments_in_reverse_order
  79. user = User.find(1)
  80. user.pref[:comments_sorting] = 'desc'
  81. user.pref.save!
  82. @request.session[:user_id] = 1
  83. get(:show, :params => {:id => 1})
  84. assert_response :success
  85. comments = css_select('#comments .wiki').map {|e| e.text.strip}
  86. assert_equal ["This is an other comment", "my first comment"], comments
  87. end
  88. def test_show_not_found
  89. get(:show, :params => {:id => 999})
  90. assert_response 404
  91. end
  92. def test_get_new_with_project_id
  93. @request.session[:user_id] = 2
  94. get(:new, :params => {:project_id => 1})
  95. assert_response :success
  96. assert_select 'select[name=project_id]', false
  97. assert_select 'input[name=?]', 'news[title]'
  98. end
  99. def test_get_new_without_project_id
  100. @request.session[:user_id] = 2
  101. get(:new)
  102. assert_response :success
  103. assert_select 'select[name=project_id]'
  104. assert_select 'input[name=?]', 'news[title]'
  105. end
  106. def test_get_new_if_user_does_not_have_permission
  107. @request.session[:user_id] = 2
  108. User.find(2).roles.each{|u| u.remove_permission! :manage_news }
  109. get(:new)
  110. assert_response :forbidden
  111. assert_select 'select[name=project_id]', false
  112. assert_select 'input[name=?]', 'news[title]', count: 0
  113. end
  114. def test_post_create
  115. ActionMailer::Base.deliveries.clear
  116. @request.session[:user_id] = 2
  117. with_settings :notified_events => %w(news_added) do
  118. post(
  119. :create,
  120. :params => {
  121. :project_id => 1,
  122. :news => {
  123. :title => 'NewsControllerTest',
  124. :description => 'This is the description',
  125. :summary => ''
  126. }
  127. }
  128. )
  129. end
  130. assert_redirected_to '/projects/ecookbook/news'
  131. news = News.find_by_title('NewsControllerTest')
  132. assert_not_nil news
  133. assert_equal 'This is the description', news.description
  134. assert_equal User.find(2), news.author
  135. assert_equal Project.find(1), news.project
  136. assert_equal 2, ActionMailer::Base.deliveries.size
  137. end
  138. def test_post_create_with_cross_project_param
  139. ActionMailer::Base.deliveries.clear
  140. @request.session[:user_id] = 2
  141. with_settings :notified_events => %w(news_added) do
  142. post(
  143. :create,
  144. :params => {
  145. :project_id => 1,
  146. :cross_project => '1',
  147. :news => {
  148. :title => 'NewsControllerTest',
  149. :description => 'This is the description',
  150. :summary => ''
  151. }
  152. }
  153. )
  154. end
  155. assert_redirected_to '/news'
  156. news = News.find_by(title: 'NewsControllerTest')
  157. assert_not_nil news
  158. assert_equal 'This is the description', news.description
  159. assert_equal User.find(2), news.author
  160. assert_equal Project.find(1), news.project
  161. assert_equal 2, ActionMailer::Base.deliveries.size
  162. end
  163. def test_post_create_with_attachment
  164. set_tmp_attachments_directory
  165. ActionMailer::Base.deliveries.clear
  166. @request.session[:user_id] = 2
  167. assert_difference 'News.count' do
  168. assert_difference 'Attachment.count' do
  169. with_settings :notified_events => %w(news_added) do
  170. post(
  171. :create,
  172. :params => {
  173. :project_id => 1,
  174. :news => {
  175. :title => 'Test',
  176. :description => 'This is the description'
  177. },
  178. :attachments => {
  179. '1' => {
  180. 'file' => uploaded_test_file('testfile.txt', 'text/plain')
  181. }
  182. }
  183. }
  184. )
  185. end
  186. end
  187. end
  188. attachment = Attachment.order('id DESC').first
  189. news = News.order('id DESC').first
  190. assert_equal news, attachment.container
  191. assert_select_email do
  192. # link to the attachments download
  193. assert_select 'fieldset.attachments' do
  194. assert_select 'a[href=?]',
  195. "http://localhost:3000/attachments/download/#{attachment.id}/testfile.txt",
  196. :text => 'testfile.txt'
  197. end
  198. end
  199. end
  200. def test_post_create_with_validation_failure
  201. @request.session[:user_id] = 2
  202. post(
  203. :create,
  204. :params => {
  205. :project_id => 1,
  206. :news => {
  207. :title => '',
  208. :description => 'This is the description',
  209. :summary => ''
  210. }
  211. }
  212. )
  213. assert_response :success
  214. assert_select_error /title cannot be blank/i
  215. end
  216. def test_get_edit
  217. @request.session[:user_id] = 2
  218. get(:edit, :params => {:id => 1})
  219. assert_response :success
  220. assert_select 'input[name=?][value=?]', 'news[title]', 'eCookbook first release !'
  221. end
  222. def test_put_update
  223. @request.session[:user_id] = 2
  224. put(
  225. :update,
  226. :params => {
  227. :id => 1,
  228. :news => {
  229. :description => 'Description changed by test_post_edit'
  230. }
  231. }
  232. )
  233. assert_redirected_to '/news/1'
  234. news = News.find(1)
  235. assert_equal 'Description changed by test_post_edit', news.description
  236. end
  237. def test_put_update_with_attachment
  238. set_tmp_attachments_directory
  239. @request.session[:user_id] = 2
  240. assert_no_difference 'News.count' do
  241. assert_difference 'Attachment.count' do
  242. put(
  243. :update,
  244. :params => {
  245. :id => 1,
  246. :news => {
  247. :description => 'This is the description'
  248. },
  249. :attachments => {
  250. '1' => {
  251. 'file' => uploaded_test_file('testfile.txt', 'text/plain')
  252. }
  253. }
  254. }
  255. )
  256. end
  257. end
  258. attachment = Attachment.order('id DESC').first
  259. assert_equal News.find(1), attachment.container
  260. end
  261. def test_update_with_failure
  262. @request.session[:user_id] = 2
  263. put(
  264. :update,
  265. :params => {
  266. :id => 1,
  267. :news => {
  268. :description => ''
  269. }
  270. }
  271. )
  272. assert_response :success
  273. assert_select_error /description cannot be blank/i
  274. end
  275. def test_destroy
  276. @request.session[:user_id] = 2
  277. delete(:destroy, :params => {:id => 1})
  278. assert_redirected_to '/projects/ecookbook/news'
  279. assert_equal 'Successful deletion.', flash[:notice]
  280. assert_nil News.find_by_id(1)
  281. end
  282. end