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.

files_controller_test.rb 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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 FilesControllerTest < Redmine::ControllerTest
  19. fixtures :projects, :trackers, :issue_statuses, :issues,
  20. :enumerations, :users,
  21. :email_addresses,
  22. :issue_categories,
  23. :projects_trackers,
  24. :roles,
  25. :member_roles,
  26. :members,
  27. :enabled_modules,
  28. :journals, :journal_details,
  29. :attachments,
  30. :versions
  31. def setup
  32. @request.session[:user_id] = nil
  33. Setting.default_language = 'en'
  34. end
  35. def test_index
  36. get :index, :params => {
  37. :project_id => 1
  38. }
  39. assert_response :success
  40. # file attached to the project
  41. assert_select 'a[href=?]', '/attachments/download/8/project_file.zip', :text => 'project_file.zip'
  42. # file attached to a project's version
  43. assert_select 'a[href=?]', '/attachments/download/9/version_file.zip', :text => 'version_file.zip'
  44. end
  45. def test_new
  46. @request.session[:user_id] = 2
  47. get :new, :params => {
  48. :project_id => 1
  49. }
  50. assert_response :success
  51. assert_select 'select[name=?]', 'version_id'
  52. end
  53. def test_new_without_versions
  54. Version.delete_all
  55. @request.session[:user_id] = 2
  56. get :new, :params => {
  57. :project_id => 1
  58. }
  59. assert_response :success
  60. assert_select 'select[name=?]', 'version_id', 0
  61. end
  62. def test_create_file
  63. set_tmp_attachments_directory
  64. @request.session[:user_id] = 2
  65. ActionMailer::Base.deliveries.clear
  66. with_settings :notified_events => %w(file_added) do
  67. assert_difference 'Attachment.count' do
  68. post :create, :params => {
  69. :project_id => 1,
  70. :version_id => '',
  71. :attachments => {
  72. '1' => {
  73. 'file' => uploaded_test_file('testfile.txt', 'text/plain')}
  74. }
  75. }
  76. assert_response :redirect
  77. end
  78. end
  79. assert_redirected_to '/projects/ecookbook/files'
  80. a = Attachment.order('created_on DESC').first
  81. assert_equal 'testfile.txt', a.filename
  82. assert_equal Project.find(1), a.container
  83. mail = ActionMailer::Base.deliveries.last
  84. assert_not_nil mail
  85. assert_equal "[eCookbook] New file", mail.subject
  86. assert_mail_body_match 'testfile.txt', mail
  87. end
  88. def test_create_version_file
  89. set_tmp_attachments_directory
  90. @request.session[:user_id] = 2
  91. assert_difference 'Attachment.count' do
  92. post :create, :params => {
  93. :project_id => 1,
  94. :version_id => '2',
  95. :attachments => {
  96. '1' => {
  97. 'file' => uploaded_test_file('testfile.txt', 'text/plain')}
  98. }
  99. }
  100. assert_response :redirect
  101. end
  102. assert_redirected_to '/projects/ecookbook/files'
  103. a = Attachment.order('created_on DESC').first
  104. assert_equal 'testfile.txt', a.filename
  105. assert_equal Version.find(2), a.container
  106. end
  107. def test_create_without_file
  108. set_tmp_attachments_directory
  109. @request.session[:user_id] = 2
  110. assert_no_difference 'Attachment.count' do
  111. post :create, :params => {
  112. :project_id => 1,
  113. :version_id => ''
  114. }
  115. assert_response :success
  116. end
  117. assert_select 'div.error', 'File is invalid'
  118. end
  119. end