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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2015 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class NewsControllerTest < ActionController::TestCase
  19. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
  20. :enabled_modules, :news, :comments,
  21. :attachments
  22. def setup
  23. User.current = nil
  24. end
  25. def test_index
  26. get :index
  27. assert_response :success
  28. assert_template 'index'
  29. assert_not_nil assigns(:newss)
  30. assert_nil assigns(:project)
  31. end
  32. def test_index_with_project
  33. get :index, :project_id => 1
  34. assert_response :success
  35. assert_template 'index'
  36. assert_not_nil assigns(:newss)
  37. end
  38. def test_index_with_invalid_project_should_respond_with_404
  39. get :index, :project_id => 999
  40. assert_response 404
  41. end
  42. def test_show
  43. get :show, :id => 1
  44. assert_response :success
  45. assert_template 'show'
  46. assert_select 'h2', :text => /eCookbook first release/
  47. end
  48. def test_show_should_show_attachments
  49. attachment = Attachment.first
  50. attachment.container = News.find(1)
  51. attachment.save!
  52. get :show, :id => 1
  53. assert_response :success
  54. assert_select 'a', :text => attachment.filename
  55. end
  56. def test_show_with_comments_in_reverse_order
  57. user = User.find(1)
  58. user.pref[:comments_sorting] = 'desc'
  59. user.pref.save!
  60. @request.session[:user_id] = 1
  61. get :show, :id => 1
  62. assert_response :success
  63. assert_equal News.find(1).comments.to_a.sort_by(&:created_on).reverse, assigns(:comments)
  64. end
  65. def test_show_not_found
  66. get :show, :id => 999
  67. assert_response 404
  68. end
  69. def test_get_new
  70. @request.session[:user_id] = 2
  71. get :new, :project_id => 1
  72. assert_response :success
  73. assert_template 'new'
  74. end
  75. def test_post_create
  76. ActionMailer::Base.deliveries.clear
  77. @request.session[:user_id] = 2
  78. with_settings :notified_events => %w(news_added) do
  79. post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
  80. :description => 'This is the description',
  81. :summary => '' }
  82. end
  83. assert_redirected_to '/projects/ecookbook/news'
  84. news = News.find_by_title('NewsControllerTest')
  85. assert_not_nil news
  86. assert_equal 'This is the description', news.description
  87. assert_equal User.find(2), news.author
  88. assert_equal Project.find(1), news.project
  89. assert_equal 1, ActionMailer::Base.deliveries.size
  90. end
  91. def test_post_create_with_attachment
  92. set_tmp_attachments_directory
  93. @request.session[:user_id] = 2
  94. assert_difference 'News.count' do
  95. assert_difference 'Attachment.count' do
  96. post :create, :project_id => 1,
  97. :news => { :title => 'Test', :description => 'This is the description' },
  98. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
  99. end
  100. end
  101. attachment = Attachment.order('id DESC').first
  102. news = News.order('id DESC').first
  103. assert_equal news, attachment.container
  104. end
  105. def test_post_create_with_validation_failure
  106. @request.session[:user_id] = 2
  107. post :create, :project_id => 1, :news => { :title => '',
  108. :description => 'This is the description',
  109. :summary => '' }
  110. assert_response :success
  111. assert_template 'new'
  112. assert_not_nil assigns(:news)
  113. assert assigns(:news).new_record?
  114. assert_select_error /title cannot be blank/i
  115. end
  116. def test_get_edit
  117. @request.session[:user_id] = 2
  118. get :edit, :id => 1
  119. assert_response :success
  120. assert_template 'edit'
  121. end
  122. def test_put_update
  123. @request.session[:user_id] = 2
  124. put :update, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
  125. assert_redirected_to '/news/1'
  126. news = News.find(1)
  127. assert_equal 'Description changed by test_post_edit', news.description
  128. end
  129. def test_put_update_with_attachment
  130. set_tmp_attachments_directory
  131. @request.session[:user_id] = 2
  132. assert_no_difference 'News.count' do
  133. assert_difference 'Attachment.count' do
  134. put :update, :id => 1,
  135. :news => { :description => 'This is the description' },
  136. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
  137. end
  138. end
  139. attachment = Attachment.order('id DESC').first
  140. assert_equal News.find(1), attachment.container
  141. end
  142. def test_update_with_failure
  143. @request.session[:user_id] = 2
  144. put :update, :id => 1, :news => { :description => '' }
  145. assert_response :success
  146. assert_template 'edit'
  147. assert_select_error /description cannot be blank/i
  148. end
  149. def test_destroy
  150. @request.session[:user_id] = 2
  151. delete :destroy, :id => 1
  152. assert_redirected_to '/projects/ecookbook/news'
  153. assert_nil News.find_by_id(1)
  154. end
  155. end