##// END OF EJS Templates
Adds a routing test for deleting attachment via the API....
Jean-Philippe Lang -
r15480:30fc03ac3259
parent child
Show More
@@ -1,159 +1,160
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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 Redmine::ApiTest::ApiRoutingTest < Redmine::ApiTest::Routing
20 class Redmine::ApiTest::ApiRoutingTest < Redmine::ApiTest::Routing
21
21
22 def test_attachments
22 def test_attachments
23 should_route 'GET /attachments/1' => 'attachments#show', :id => '1'
23 should_route 'GET /attachments/1' => 'attachments#show', :id => '1'
24 should_route 'PATCH /attachments/1' => 'attachments#update', :id => '1'
24 should_route 'PATCH /attachments/1' => 'attachments#update', :id => '1'
25 should_route 'DELETE /attachments/1' => 'attachments#destroy', :id => '1'
25 should_route 'POST /uploads' => 'attachments#upload'
26 should_route 'POST /uploads' => 'attachments#upload'
26 end
27 end
27
28
28 def test_custom_fields
29 def test_custom_fields
29 should_route 'GET /custom_fields' => 'custom_fields#index'
30 should_route 'GET /custom_fields' => 'custom_fields#index'
30 end
31 end
31
32
32 def test_enumerations
33 def test_enumerations
33 should_route 'GET /enumerations/issue_priorities' => 'enumerations#index', :type => 'issue_priorities'
34 should_route 'GET /enumerations/issue_priorities' => 'enumerations#index', :type => 'issue_priorities'
34 end
35 end
35
36
36 def test_groups
37 def test_groups
37 should_route 'GET /groups' => 'groups#index'
38 should_route 'GET /groups' => 'groups#index'
38 should_route 'POST /groups' => 'groups#create'
39 should_route 'POST /groups' => 'groups#create'
39
40
40 should_route 'GET /groups/1' => 'groups#show', :id => '1'
41 should_route 'GET /groups/1' => 'groups#show', :id => '1'
41 should_route 'PUT /groups/1' => 'groups#update', :id => '1'
42 should_route 'PUT /groups/1' => 'groups#update', :id => '1'
42 should_route 'DELETE /groups/1' => 'groups#destroy', :id => '1'
43 should_route 'DELETE /groups/1' => 'groups#destroy', :id => '1'
43 end
44 end
44
45
45 def test_group_users
46 def test_group_users
46 should_route 'POST /groups/567/users' => 'groups#add_users', :id => '567'
47 should_route 'POST /groups/567/users' => 'groups#add_users', :id => '567'
47 should_route 'DELETE /groups/567/users/12' => 'groups#remove_user', :id => '567', :user_id => '12'
48 should_route 'DELETE /groups/567/users/12' => 'groups#remove_user', :id => '567', :user_id => '12'
48 end
49 end
49
50
50 def test_issue_categories
51 def test_issue_categories
51 should_route 'GET /projects/foo/issue_categories' => 'issue_categories#index', :project_id => 'foo'
52 should_route 'GET /projects/foo/issue_categories' => 'issue_categories#index', :project_id => 'foo'
52 should_route 'POST /projects/foo/issue_categories' => 'issue_categories#create', :project_id => 'foo'
53 should_route 'POST /projects/foo/issue_categories' => 'issue_categories#create', :project_id => 'foo'
53
54
54 should_route 'GET /issue_categories/1' => 'issue_categories#show', :id => '1'
55 should_route 'GET /issue_categories/1' => 'issue_categories#show', :id => '1'
55 should_route 'PUT /issue_categories/1' => 'issue_categories#update', :id => '1'
56 should_route 'PUT /issue_categories/1' => 'issue_categories#update', :id => '1'
56 should_route 'DELETE /issue_categories/1' => 'issue_categories#destroy', :id => '1'
57 should_route 'DELETE /issue_categories/1' => 'issue_categories#destroy', :id => '1'
57 end
58 end
58
59
59 def test_issue_relations
60 def test_issue_relations
60 should_route 'GET /issues/1/relations' => 'issue_relations#index', :issue_id => '1'
61 should_route 'GET /issues/1/relations' => 'issue_relations#index', :issue_id => '1'
61 should_route 'POST /issues/1/relations' => 'issue_relations#create', :issue_id => '1'
62 should_route 'POST /issues/1/relations' => 'issue_relations#create', :issue_id => '1'
62
63
63 should_route 'GET /relations/23' => 'issue_relations#show', :id => '23'
64 should_route 'GET /relations/23' => 'issue_relations#show', :id => '23'
64 should_route 'DELETE /relations/23' => 'issue_relations#destroy', :id => '23'
65 should_route 'DELETE /relations/23' => 'issue_relations#destroy', :id => '23'
65 end
66 end
66
67
67 def test_issue_statuses
68 def test_issue_statuses
68 should_route 'GET /issue_statuses' => 'issue_statuses#index'
69 should_route 'GET /issue_statuses' => 'issue_statuses#index'
69 end
70 end
70
71
71 def test_issues
72 def test_issues
72 should_route 'GET /issues' => 'issues#index'
73 should_route 'GET /issues' => 'issues#index'
73 should_route 'POST /issues' => 'issues#create'
74 should_route 'POST /issues' => 'issues#create'
74
75
75 should_route 'GET /issues/64' => 'issues#show', :id => '64'
76 should_route 'GET /issues/64' => 'issues#show', :id => '64'
76 should_route 'PUT /issues/64' => 'issues#update', :id => '64'
77 should_route 'PUT /issues/64' => 'issues#update', :id => '64'
77 should_route 'DELETE /issues/64' => 'issues#destroy', :id => '64'
78 should_route 'DELETE /issues/64' => 'issues#destroy', :id => '64'
78 end
79 end
79
80
80 def test_issue_watchers
81 def test_issue_watchers
81 should_route 'POST /issues/12/watchers' => 'watchers#create', :object_type => 'issue', :object_id => '12'
82 should_route 'POST /issues/12/watchers' => 'watchers#create', :object_type => 'issue', :object_id => '12'
82 should_route 'DELETE /issues/12/watchers/3' => 'watchers#destroy', :object_type => 'issue', :object_id => '12', :user_id => '3'
83 should_route 'DELETE /issues/12/watchers/3' => 'watchers#destroy', :object_type => 'issue', :object_id => '12', :user_id => '3'
83 end
84 end
84
85
85 def test_memberships
86 def test_memberships
86 should_route 'GET /projects/5234/memberships' => 'members#index', :project_id => '5234'
87 should_route 'GET /projects/5234/memberships' => 'members#index', :project_id => '5234'
87 should_route 'POST /projects/5234/memberships' => 'members#create', :project_id => '5234'
88 should_route 'POST /projects/5234/memberships' => 'members#create', :project_id => '5234'
88
89
89 should_route 'GET /memberships/5234' => 'members#show', :id => '5234'
90 should_route 'GET /memberships/5234' => 'members#show', :id => '5234'
90 should_route 'PUT /memberships/5234' => 'members#update', :id => '5234'
91 should_route 'PUT /memberships/5234' => 'members#update', :id => '5234'
91 should_route 'DELETE /memberships/5234' => 'members#destroy', :id => '5234'
92 should_route 'DELETE /memberships/5234' => 'members#destroy', :id => '5234'
92 end
93 end
93
94
94 def test_news
95 def test_news
95 should_route 'GET /news' => 'news#index'
96 should_route 'GET /news' => 'news#index'
96 should_route 'GET /projects/567/news' => 'news#index', :project_id => '567'
97 should_route 'GET /projects/567/news' => 'news#index', :project_id => '567'
97 end
98 end
98
99
99 def test_projects
100 def test_projects
100 should_route 'GET /projects' => 'projects#index'
101 should_route 'GET /projects' => 'projects#index'
101 should_route 'POST /projects' => 'projects#create'
102 should_route 'POST /projects' => 'projects#create'
102
103
103 should_route 'GET /projects/1' => 'projects#show', :id => '1'
104 should_route 'GET /projects/1' => 'projects#show', :id => '1'
104 should_route 'PUT /projects/1' => 'projects#update', :id => '1'
105 should_route 'PUT /projects/1' => 'projects#update', :id => '1'
105 should_route 'DELETE /projects/1' => 'projects#destroy', :id => '1'
106 should_route 'DELETE /projects/1' => 'projects#destroy', :id => '1'
106 end
107 end
107
108
108 def test_queries
109 def test_queries
109 should_route 'GET /queries' => 'queries#index'
110 should_route 'GET /queries' => 'queries#index'
110 end
111 end
111
112
112 def test_roles
113 def test_roles
113 should_route 'GET /roles' => 'roles#index'
114 should_route 'GET /roles' => 'roles#index'
114 should_route 'GET /roles/2' => 'roles#show', :id => '2'
115 should_route 'GET /roles/2' => 'roles#show', :id => '2'
115 end
116 end
116
117
117 def test_time_entries
118 def test_time_entries
118 should_route 'GET /time_entries' => 'timelog#index'
119 should_route 'GET /time_entries' => 'timelog#index'
119 should_route 'POST /time_entries' => 'timelog#create'
120 should_route 'POST /time_entries' => 'timelog#create'
120
121
121 should_route 'GET /time_entries/1' => 'timelog#show', :id => '1'
122 should_route 'GET /time_entries/1' => 'timelog#show', :id => '1'
122 should_route 'PUT /time_entries/1' => 'timelog#update', :id => '1'
123 should_route 'PUT /time_entries/1' => 'timelog#update', :id => '1'
123 should_route 'DELETE /time_entries/1' => 'timelog#destroy', :id => '1'
124 should_route 'DELETE /time_entries/1' => 'timelog#destroy', :id => '1'
124 end
125 end
125
126
126 def test_trackers
127 def test_trackers
127 should_route 'GET /trackers' => 'trackers#index'
128 should_route 'GET /trackers' => 'trackers#index'
128 end
129 end
129
130
130 def test_users
131 def test_users
131 should_route 'GET /users' => 'users#index'
132 should_route 'GET /users' => 'users#index'
132 should_route 'POST /users' => 'users#create'
133 should_route 'POST /users' => 'users#create'
133
134
134 should_route 'GET /users/44' => 'users#show', :id => '44'
135 should_route 'GET /users/44' => 'users#show', :id => '44'
135 should_route 'GET /users/current' => 'users#show', :id => 'current'
136 should_route 'GET /users/current' => 'users#show', :id => 'current'
136 should_route 'PUT /users/44' => 'users#update', :id => '44'
137 should_route 'PUT /users/44' => 'users#update', :id => '44'
137 should_route 'DELETE /users/44' => 'users#destroy', :id => '44'
138 should_route 'DELETE /users/44' => 'users#destroy', :id => '44'
138 end
139 end
139
140
140 def test_versions
141 def test_versions
141 should_route 'GET /projects/foo/versions' => 'versions#index', :project_id => 'foo'
142 should_route 'GET /projects/foo/versions' => 'versions#index', :project_id => 'foo'
142 should_route 'POST /projects/foo/versions' => 'versions#create', :project_id => 'foo'
143 should_route 'POST /projects/foo/versions' => 'versions#create', :project_id => 'foo'
143
144
144 should_route 'GET /versions/1' => 'versions#show', :id => '1'
145 should_route 'GET /versions/1' => 'versions#show', :id => '1'
145 should_route 'PUT /versions/1' => 'versions#update', :id => '1'
146 should_route 'PUT /versions/1' => 'versions#update', :id => '1'
146 should_route 'DELETE /versions/1' => 'versions#destroy', :id => '1'
147 should_route 'DELETE /versions/1' => 'versions#destroy', :id => '1'
147 end
148 end
148
149
149 def test_wiki
150 def test_wiki
150 should_route 'GET /projects/567/wiki/index' => 'wiki#index', :project_id => '567'
151 should_route 'GET /projects/567/wiki/index' => 'wiki#index', :project_id => '567'
151
152
152 should_route 'GET /projects/567/wiki/my_page' => 'wiki#show', :project_id => '567', :id => 'my_page'
153 should_route 'GET /projects/567/wiki/my_page' => 'wiki#show', :project_id => '567', :id => 'my_page'
153 should_route 'GET /projects/567/wiki/my_page' => 'wiki#show', :project_id => '567', :id => 'my_page'
154 should_route 'GET /projects/567/wiki/my_page' => 'wiki#show', :project_id => '567', :id => 'my_page'
154 should_route 'GET /projects/1/wiki/my_page/2' => 'wiki#show', :project_id => '1', :id => 'my_page', :version => '2'
155 should_route 'GET /projects/1/wiki/my_page/2' => 'wiki#show', :project_id => '1', :id => 'my_page', :version => '2'
155
156
156 should_route 'PUT /projects/567/wiki/my_page' => 'wiki#update', :project_id => '567', :id => 'my_page'
157 should_route 'PUT /projects/567/wiki/my_page' => 'wiki#update', :project_id => '567', :id => 'my_page'
157 should_route 'DELETE /projects/567/wiki/my_page' => 'wiki#destroy', :project_id => '567', :id => 'my_page'
158 should_route 'DELETE /projects/567/wiki/my_page' => 'wiki#destroy', :project_id => '567', :id => 'my_page'
158 end
159 end
159 end
160 end
@@ -1,200 +1,200
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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 Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
21 fixtures :projects, :trackers, :issue_statuses, :issues,
21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 :enumerations, :users, :issue_categories,
22 :enumerations, :users, :issue_categories,
23 :projects_trackers,
23 :projects_trackers,
24 :roles,
24 :roles,
25 :member_roles,
25 :member_roles,
26 :members,
26 :members,
27 :enabled_modules,
27 :enabled_modules,
28 :attachments
28 :attachments
29
29
30 def setup
30 def setup
31 super
31 super
32 set_fixtures_attachments_directory
32 set_fixtures_attachments_directory
33 end
33 end
34
34
35 def teardown
35 def teardown
36 super
36 super
37 set_tmp_attachments_directory
37 set_tmp_attachments_directory
38 end
38 end
39
39
40 test "GET /attachments/:id.xml should return the attachment" do
40 test "GET /attachments/:id.xml should return the attachment" do
41 get '/attachments/7.xml', {}, credentials('jsmith')
41 get '/attachments/7.xml', {}, credentials('jsmith')
42 assert_response :success
42 assert_response :success
43 assert_equal 'application/xml', @response.content_type
43 assert_equal 'application/xml', @response.content_type
44 assert_select 'attachment id', :text => '7' do
44 assert_select 'attachment id', :text => '7' do
45 assert_select '~ filename', :text => 'archive.zip'
45 assert_select '~ filename', :text => 'archive.zip'
46 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/7/archive.zip'
46 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/7/archive.zip'
47 end
47 end
48 end
48 end
49
49
50 test "GET /attachments/:id.xml for image should include thumbnail_url" do
50 test "GET /attachments/:id.xml for image should include thumbnail_url" do
51 get '/attachments/16.xml', {}, credentials('jsmith')
51 get '/attachments/16.xml', {}, credentials('jsmith')
52 assert_response :success
52 assert_response :success
53 assert_equal 'application/xml', @response.content_type
53 assert_equal 'application/xml', @response.content_type
54 assert_select 'attachment id:contains(16)' do
54 assert_select 'attachment id:contains(16)' do
55 assert_select '~ thumbnail_url', :text => 'http://www.example.com/attachments/thumbnail/16'
55 assert_select '~ thumbnail_url', :text => 'http://www.example.com/attachments/thumbnail/16'
56 end
56 end
57 end
57 end
58
58
59 test "GET /attachments/:id.xml should deny access without credentials" do
59 test "GET /attachments/:id.xml should deny access without credentials" do
60 get '/attachments/7.xml'
60 get '/attachments/7.xml'
61 assert_response 401
61 assert_response 401
62 set_tmp_attachments_directory
62 set_tmp_attachments_directory
63 end
63 end
64
64
65 test "GET /attachments/download/:id/:filename should return the attachment content" do
65 test "GET /attachments/download/:id/:filename should return the attachment content" do
66 get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
66 get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
67 assert_response :success
67 assert_response :success
68 assert_equal 'application/zip', @response.content_type
68 assert_equal 'application/zip', @response.content_type
69 set_tmp_attachments_directory
69 set_tmp_attachments_directory
70 end
70 end
71
71
72 test "GET /attachments/download/:id/:filename should deny access without credentials" do
72 test "GET /attachments/download/:id/:filename should deny access without credentials" do
73 get '/attachments/download/7/archive.zip'
73 get '/attachments/download/7/archive.zip'
74 assert_response 302
74 assert_response 302
75 set_tmp_attachments_directory
75 set_tmp_attachments_directory
76 end
76 end
77
77
78 test "GET /attachments/thumbnail/:id should return the thumbnail" do
78 test "GET /attachments/thumbnail/:id should return the thumbnail" do
79 skip unless convert_installed?
79 skip unless convert_installed?
80 get '/attachments/thumbnail/16', {}, credentials('jsmith')
80 get '/attachments/thumbnail/16', {}, credentials('jsmith')
81 assert_response :success
81 assert_response :success
82 end
82 end
83
83
84 test "Destroy /attachments/:id.xml should return ok and deleted Attachment" do
84 test "DELETE /attachments/:id.xml should return ok and delete Attachment" do
85 assert_difference 'Attachment.count', -1 do
85 assert_difference 'Attachment.count', -1 do
86 delete '/attachments/7.xml', {}, credentials('jsmith')
86 delete '/attachments/7.xml', {}, credentials('jsmith')
87 assert_response :ok
87 assert_response :ok
88 assert_equal '', response.body
88 assert_equal '', response.body
89 end
89 end
90 assert_nil Attachment.find_by_id(7)
90 assert_nil Attachment.find_by_id(7)
91 end
91 end
92
92
93 test "Destroy /attachments/:id.json should return ok and deleted Attachment" do
93 test "DELETE /attachments/:id.json should return ok and delete Attachment" do
94 assert_difference 'Attachment.count', -1 do
94 assert_difference 'Attachment.count', -1 do
95 delete '/attachments/7.json', {}, credentials('jsmith')
95 delete '/attachments/7.json', {}, credentials('jsmith')
96 assert_response :ok
96 assert_response :ok
97 assert_equal '', response.body
97 assert_equal '', response.body
98 end
98 end
99 assert_nil Attachment.find_by_id(7)
99 assert_nil Attachment.find_by_id(7)
100 end
100 end
101
101
102 test "PATCH /attachments/:id.json should update the attachment" do
102 test "PATCH /attachments/:id.json should update the attachment" do
103 patch '/attachments/7.json',
103 patch '/attachments/7.json',
104 {:attachment => {:filename => 'renamed.zip', :description => 'updated'}},
104 {:attachment => {:filename => 'renamed.zip', :description => 'updated'}},
105 credentials('jsmith')
105 credentials('jsmith')
106
106
107 assert_response :ok
107 assert_response :ok
108 assert_equal 'application/json', response.content_type
108 assert_equal 'application/json', response.content_type
109 attachment = Attachment.find(7)
109 attachment = Attachment.find(7)
110 assert_equal 'renamed.zip', attachment.filename
110 assert_equal 'renamed.zip', attachment.filename
111 assert_equal 'updated', attachment.description
111 assert_equal 'updated', attachment.description
112 end
112 end
113
113
114 test "PATCH /attachments/:id.json with failure should return the errors" do
114 test "PATCH /attachments/:id.json with failure should return the errors" do
115 patch '/attachments/7.json',
115 patch '/attachments/7.json',
116 {:attachment => {:filename => '', :description => 'updated'}},
116 {:attachment => {:filename => '', :description => 'updated'}},
117 credentials('jsmith')
117 credentials('jsmith')
118
118
119 assert_response 422
119 assert_response 422
120 assert_equal 'application/json', response.content_type
120 assert_equal 'application/json', response.content_type
121 json = ActiveSupport::JSON.decode(response.body)
121 json = ActiveSupport::JSON.decode(response.body)
122 assert_include "File cannot be blank", json['errors']
122 assert_include "File cannot be blank", json['errors']
123 end
123 end
124
124
125 test "POST /uploads.xml should return the token" do
125 test "POST /uploads.xml should return the token" do
126 set_tmp_attachments_directory
126 set_tmp_attachments_directory
127 assert_difference 'Attachment.count' do
127 assert_difference 'Attachment.count' do
128 post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
128 post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
129 assert_response :created
129 assert_response :created
130 assert_equal 'application/xml', response.content_type
130 assert_equal 'application/xml', response.content_type
131 end
131 end
132
132
133 xml = Hash.from_xml(response.body)
133 xml = Hash.from_xml(response.body)
134 assert_kind_of Hash, xml['upload']
134 assert_kind_of Hash, xml['upload']
135 token = xml['upload']['token']
135 token = xml['upload']['token']
136 assert_not_nil token
136 assert_not_nil token
137 attachment_id = xml['upload']['id']
137 attachment_id = xml['upload']['id']
138 assert_not_nil attachment_id
138 assert_not_nil attachment_id
139
139
140 attachment = Attachment.order('id DESC').first
140 attachment = Attachment.order('id DESC').first
141 assert_equal token, attachment.token
141 assert_equal token, attachment.token
142 assert_equal attachment_id, attachment.id.to_s
142 assert_equal attachment_id, attachment.id.to_s
143 assert_nil attachment.container
143 assert_nil attachment.container
144 assert_equal 2, attachment.author_id
144 assert_equal 2, attachment.author_id
145 assert_equal 'File content'.size, attachment.filesize
145 assert_equal 'File content'.size, attachment.filesize
146 assert attachment.content_type.blank?
146 assert attachment.content_type.blank?
147 assert attachment.filename.present?
147 assert attachment.filename.present?
148 assert_match /\d+_[0-9a-z]+/, attachment.diskfile
148 assert_match /\d+_[0-9a-z]+/, attachment.diskfile
149 assert File.exist?(attachment.diskfile)
149 assert File.exist?(attachment.diskfile)
150 assert_equal 'File content', File.read(attachment.diskfile)
150 assert_equal 'File content', File.read(attachment.diskfile)
151 end
151 end
152
152
153 test "POST /uploads.json should return the token" do
153 test "POST /uploads.json should return the token" do
154 set_tmp_attachments_directory
154 set_tmp_attachments_directory
155 assert_difference 'Attachment.count' do
155 assert_difference 'Attachment.count' do
156 post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
156 post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
157 assert_response :created
157 assert_response :created
158 assert_equal 'application/json', response.content_type
158 assert_equal 'application/json', response.content_type
159 end
159 end
160
160
161 json = ActiveSupport::JSON.decode(response.body)
161 json = ActiveSupport::JSON.decode(response.body)
162 assert_kind_of Hash, json['upload']
162 assert_kind_of Hash, json['upload']
163 token = json['upload']['token']
163 token = json['upload']['token']
164 assert_not_nil token
164 assert_not_nil token
165
165
166 attachment = Attachment.order('id DESC').first
166 attachment = Attachment.order('id DESC').first
167 assert_equal token, attachment.token
167 assert_equal token, attachment.token
168 end
168 end
169
169
170 test "POST /uploads.xml should accept :filename param as the attachment filename" do
170 test "POST /uploads.xml should accept :filename param as the attachment filename" do
171 set_tmp_attachments_directory
171 set_tmp_attachments_directory
172 assert_difference 'Attachment.count' do
172 assert_difference 'Attachment.count' do
173 post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
173 post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
174 assert_response :created
174 assert_response :created
175 end
175 end
176
176
177 attachment = Attachment.order('id DESC').first
177 attachment = Attachment.order('id DESC').first
178 assert_equal 'test.txt', attachment.filename
178 assert_equal 'test.txt', attachment.filename
179 assert_match /_test\.txt$/, attachment.diskfile
179 assert_match /_test\.txt$/, attachment.diskfile
180 end
180 end
181
181
182 test "POST /uploads.xml should not accept other content types" do
182 test "POST /uploads.xml should not accept other content types" do
183 set_tmp_attachments_directory
183 set_tmp_attachments_directory
184 assert_no_difference 'Attachment.count' do
184 assert_no_difference 'Attachment.count' do
185 post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
185 post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
186 assert_response 406
186 assert_response 406
187 end
187 end
188 end
188 end
189
189
190 test "POST /uploads.xml should return errors if file is too big" do
190 test "POST /uploads.xml should return errors if file is too big" do
191 set_tmp_attachments_directory
191 set_tmp_attachments_directory
192 with_settings :attachment_max_size => 1 do
192 with_settings :attachment_max_size => 1 do
193 assert_no_difference 'Attachment.count' do
193 assert_no_difference 'Attachment.count' do
194 post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
194 post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
195 assert_response 422
195 assert_response 422
196 assert_select 'error', :text => /exceeds the maximum allowed file size/
196 assert_select 'error', :text => /exceeds the maximum allowed file size/
197 end
197 end
198 end
198 end
199 end
199 end
200 end
200 end
General Comments 0
You need to be logged in to leave comments. Login now