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.1KB

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