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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require File.expand_path('../../test_helper', __FILE__)
  2. class FilesControllerTest < ActionController::TestCase
  3. fixtures :projects, :trackers, :issue_statuses, :issues,
  4. :enumerations, :users, :issue_categories,
  5. :projects_trackers,
  6. :roles,
  7. :member_roles,
  8. :members,
  9. :enabled_modules,
  10. :workflows,
  11. :journals, :journal_details,
  12. :attachments,
  13. :versions
  14. def setup
  15. @controller = FilesController.new
  16. @request = ActionController::TestRequest.new
  17. @response = ActionController::TestResponse.new
  18. @request.session[:user_id] = nil
  19. Setting.default_language = 'en'
  20. end
  21. def test_index
  22. get :index, :project_id => 1
  23. assert_response :success
  24. assert_template 'index'
  25. assert_not_nil assigns(:containers)
  26. # file attached to the project
  27. assert_tag :a, :content => 'project_file.zip',
  28. :attributes => { :href => '/attachments/download/8/project_file.zip' }
  29. # file attached to a project's version
  30. assert_tag :a, :content => 'version_file.zip',
  31. :attributes => { :href => '/attachments/download/9/version_file.zip' }
  32. end
  33. def test_new
  34. @request.session[:user_id] = 2
  35. get :new, :project_id => 1
  36. assert_response :success
  37. assert_template 'new'
  38. assert_tag 'select', :attributes => {:name => 'version_id'}
  39. end
  40. def test_new_without_versions
  41. Version.delete_all
  42. @request.session[:user_id] = 2
  43. get :new, :project_id => 1
  44. assert_response :success
  45. assert_template 'new'
  46. assert_no_tag 'select', :attributes => {:name => 'version_id'}
  47. end
  48. def test_create_file
  49. set_tmp_attachments_directory
  50. @request.session[:user_id] = 2
  51. Setting.notified_events = ['file_added']
  52. ActionMailer::Base.deliveries.clear
  53. assert_difference 'Attachment.count' do
  54. post :create, :project_id => 1, :version_id => '',
  55. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
  56. assert_response :redirect
  57. end
  58. assert_redirected_to '/projects/ecookbook/files'
  59. a = Attachment.find(:first, :order => 'created_on DESC')
  60. assert_equal 'testfile.txt', a.filename
  61. assert_equal Project.find(1), a.container
  62. mail = ActionMailer::Base.deliveries.last
  63. assert_not_nil mail
  64. assert_equal "[eCookbook] New file", mail.subject
  65. assert_mail_body_match 'testfile.txt', mail
  66. end
  67. def test_create_version_file
  68. set_tmp_attachments_directory
  69. @request.session[:user_id] = 2
  70. Setting.notified_events = ['file_added']
  71. assert_difference 'Attachment.count' do
  72. post :create, :project_id => 1, :version_id => '2',
  73. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
  74. assert_response :redirect
  75. end
  76. assert_redirected_to '/projects/ecookbook/files'
  77. a = Attachment.find(:first, :order => 'created_on DESC')
  78. assert_equal 'testfile.txt', a.filename
  79. assert_equal Version.find(2), a.container
  80. end
  81. end