@@ -1,55 +1,62 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class FilesController < ApplicationController |
|
18 | class FilesController < ApplicationController | |
19 | menu_item :files |
|
19 | menu_item :files | |
20 |
|
20 | |||
21 | before_filter :find_project_by_project_id |
|
21 | before_filter :find_project_by_project_id | |
22 | before_filter :authorize |
|
22 | before_filter :authorize | |
23 |
|
23 | |||
24 | helper :sort |
|
24 | helper :sort | |
25 | include SortHelper |
|
25 | include SortHelper | |
26 |
|
26 | |||
27 | def index |
|
27 | def index | |
28 | sort_init 'filename', 'asc' |
|
28 | sort_init 'filename', 'asc' | |
29 | sort_update 'filename' => "#{Attachment.table_name}.filename", |
|
29 | sort_update 'filename' => "#{Attachment.table_name}.filename", | |
30 | 'created_on' => "#{Attachment.table_name}.created_on", |
|
30 | 'created_on' => "#{Attachment.table_name}.created_on", | |
31 | 'size' => "#{Attachment.table_name}.filesize", |
|
31 | 'size' => "#{Attachment.table_name}.filesize", | |
32 | 'downloads' => "#{Attachment.table_name}.downloads" |
|
32 | 'downloads' => "#{Attachment.table_name}.downloads" | |
33 |
|
33 | |||
34 | @containers = [Project.includes(:attachments). |
|
34 | @containers = [Project.includes(:attachments). | |
35 | references(:attachments).reorder(sort_clause).find(@project.id)] |
|
35 | references(:attachments).reorder(sort_clause).find(@project.id)] | |
36 | @containers += @project.versions.includes(:attachments). |
|
36 | @containers += @project.versions.includes(:attachments). | |
37 | references(:attachments).reorder(sort_clause).to_a.sort.reverse |
|
37 | references(:attachments).reorder(sort_clause).to_a.sort.reverse | |
38 | render :layout => !request.xhr? |
|
38 | render :layout => !request.xhr? | |
39 | end |
|
39 | end | |
40 |
|
40 | |||
41 | def new |
|
41 | def new | |
42 | @versions = @project.versions.sort |
|
42 | @versions = @project.versions.sort | |
43 | end |
|
43 | end | |
44 |
|
44 | |||
45 | def create |
|
45 | def create | |
46 | container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id])) |
|
46 | container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id])) | |
47 | attachments = Attachment.attach_files(container, params[:attachments]) |
|
47 | attachments = Attachment.attach_files(container, params[:attachments]) | |
48 | render_attachment_warning_if_needed(container) |
|
48 | render_attachment_warning_if_needed(container) | |
49 |
|
49 | |||
50 | if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added') |
|
50 | if attachments[:files].present? | |
|
51 | if Setting.notified_events.include?('file_added') | |||
51 | Mailer.attachments_added(attachments[:files]).deliver |
|
52 | Mailer.attachments_added(attachments[:files]).deliver | |
52 | end |
|
53 | end | |
|
54 | flash[:notice] = l(:label_file_added) | |||
53 | redirect_to project_files_path(@project) |
|
55 | redirect_to project_files_path(@project) | |
|
56 | else | |||
|
57 | flash.now[:error] = l(:label_attachment) + " " + l('activerecord.errors.messages.invalid') | |||
|
58 | new | |||
|
59 | render :action => 'new' | |||
|
60 | end | |||
54 | end |
|
61 | end | |
55 | end |
|
62 | end |
@@ -1,109 +1,120 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class FilesControllerTest < ActionController::TestCase |
|
20 | class FilesControllerTest < ActionController::TestCase | |
21 | fixtures :projects, :trackers, :issue_statuses, :issues, |
|
21 | fixtures :projects, :trackers, :issue_statuses, :issues, | |
22 | :enumerations, :users, |
|
22 | :enumerations, :users, | |
23 | :email_addresses, |
|
23 | :email_addresses, | |
24 | :issue_categories, |
|
24 | :issue_categories, | |
25 | :projects_trackers, |
|
25 | :projects_trackers, | |
26 | :roles, |
|
26 | :roles, | |
27 | :member_roles, |
|
27 | :member_roles, | |
28 | :members, |
|
28 | :members, | |
29 | :enabled_modules, |
|
29 | :enabled_modules, | |
30 | :journals, :journal_details, |
|
30 | :journals, :journal_details, | |
31 | :attachments, |
|
31 | :attachments, | |
32 | :versions |
|
32 | :versions | |
33 |
|
33 | |||
34 | def setup |
|
34 | def setup | |
35 | @request.session[:user_id] = nil |
|
35 | @request.session[:user_id] = nil | |
36 | Setting.default_language = 'en' |
|
36 | Setting.default_language = 'en' | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def test_index |
|
39 | def test_index | |
40 | get :index, :project_id => 1 |
|
40 | get :index, :project_id => 1 | |
41 | assert_response :success |
|
41 | assert_response :success | |
42 | assert_template 'index' |
|
42 | assert_template 'index' | |
43 | assert_not_nil assigns(:containers) |
|
43 | assert_not_nil assigns(:containers) | |
44 |
|
44 | |||
45 | # file attached to the project |
|
45 | # file attached to the project | |
46 | assert_select 'a[href=?]', '/attachments/download/8/project_file.zip', :text => 'project_file.zip' |
|
46 | assert_select 'a[href=?]', '/attachments/download/8/project_file.zip', :text => 'project_file.zip' | |
47 |
|
47 | |||
48 | # file attached to a project's version |
|
48 | # file attached to a project's version | |
49 | assert_select 'a[href=?]', '/attachments/download/9/version_file.zip', :text => 'version_file.zip' |
|
49 | assert_select 'a[href=?]', '/attachments/download/9/version_file.zip', :text => 'version_file.zip' | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def test_new |
|
52 | def test_new | |
53 | @request.session[:user_id] = 2 |
|
53 | @request.session[:user_id] = 2 | |
54 | get :new, :project_id => 1 |
|
54 | get :new, :project_id => 1 | |
55 | assert_response :success |
|
55 | assert_response :success | |
56 | assert_template 'new' |
|
56 | assert_template 'new' | |
57 |
|
57 | |||
58 | assert_select 'select[name=?]', 'version_id' |
|
58 | assert_select 'select[name=?]', 'version_id' | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def test_new_without_versions |
|
61 | def test_new_without_versions | |
62 | Version.delete_all |
|
62 | Version.delete_all | |
63 | @request.session[:user_id] = 2 |
|
63 | @request.session[:user_id] = 2 | |
64 | get :new, :project_id => 1 |
|
64 | get :new, :project_id => 1 | |
65 | assert_response :success |
|
65 | assert_response :success | |
66 | assert_template 'new' |
|
66 | assert_template 'new' | |
67 |
|
67 | |||
68 | assert_select 'select[name=?]', 'version_id', 0 |
|
68 | assert_select 'select[name=?]', 'version_id', 0 | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | def test_create_file |
|
71 | def test_create_file | |
72 | set_tmp_attachments_directory |
|
72 | set_tmp_attachments_directory | |
73 | @request.session[:user_id] = 2 |
|
73 | @request.session[:user_id] = 2 | |
74 | ActionMailer::Base.deliveries.clear |
|
74 | ActionMailer::Base.deliveries.clear | |
75 |
|
75 | |||
76 | with_settings :notified_events => %w(file_added) do |
|
76 | with_settings :notified_events => %w(file_added) do | |
77 | assert_difference 'Attachment.count' do |
|
77 | assert_difference 'Attachment.count' do | |
78 | post :create, :project_id => 1, :version_id => '', |
|
78 | post :create, :project_id => 1, :version_id => '', | |
79 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} |
|
79 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} | |
80 | assert_response :redirect |
|
80 | assert_response :redirect | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 | assert_redirected_to '/projects/ecookbook/files' |
|
83 | assert_redirected_to '/projects/ecookbook/files' | |
84 | a = Attachment.order('created_on DESC').first |
|
84 | a = Attachment.order('created_on DESC').first | |
85 | assert_equal 'testfile.txt', a.filename |
|
85 | assert_equal 'testfile.txt', a.filename | |
86 | assert_equal Project.find(1), a.container |
|
86 | assert_equal Project.find(1), a.container | |
87 |
|
87 | |||
88 | mail = ActionMailer::Base.deliveries.last |
|
88 | mail = ActionMailer::Base.deliveries.last | |
89 | assert_not_nil mail |
|
89 | assert_not_nil mail | |
90 | assert_equal "[eCookbook] New file", mail.subject |
|
90 | assert_equal "[eCookbook] New file", mail.subject | |
91 | assert_mail_body_match 'testfile.txt', mail |
|
91 | assert_mail_body_match 'testfile.txt', mail | |
92 | end |
|
92 | end | |
93 |
|
93 | |||
94 | def test_create_version_file |
|
94 | def test_create_version_file | |
95 | set_tmp_attachments_directory |
|
95 | set_tmp_attachments_directory | |
96 | @request.session[:user_id] = 2 |
|
96 | @request.session[:user_id] = 2 | |
97 |
|
97 | |||
98 | assert_difference 'Attachment.count' do |
|
98 | assert_difference 'Attachment.count' do | |
99 | post :create, :project_id => 1, :version_id => '2', |
|
99 | post :create, :project_id => 1, :version_id => '2', | |
100 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} |
|
100 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} | |
101 | assert_response :redirect |
|
101 | assert_response :redirect | |
102 | end |
|
102 | end | |
103 | assert_redirected_to '/projects/ecookbook/files' |
|
103 | assert_redirected_to '/projects/ecookbook/files' | |
104 | a = Attachment.order('created_on DESC').first |
|
104 | a = Attachment.order('created_on DESC').first | |
105 | assert_equal 'testfile.txt', a.filename |
|
105 | assert_equal 'testfile.txt', a.filename | |
106 | assert_equal Version.find(2), a.container |
|
106 | assert_equal Version.find(2), a.container | |
107 | end |
|
107 | end | |
108 |
|
108 | |||
|
109 | def test_create_without_file | |||
|
110 | set_tmp_attachments_directory | |||
|
111 | @request.session[:user_id] = 2 | |||
|
112 | ||||
|
113 | assert_no_difference 'Attachment.count' do | |||
|
114 | post :create, :project_id => 1, :version_id => '' | |||
|
115 | assert_response 200 | |||
|
116 | assert_template 'new' | |||
|
117 | end | |||
|
118 | assert_select 'div.error', 'File is invalid' | |||
|
119 | end | |||
109 | end |
|
120 | end |
General Comments 0
You need to be logged in to leave comments.
Login now