##// END OF EJS Templates
Rails4: replace deprecated Relation#first with finder options at ApiTest::AttachmentsTest...
Toshi MARUYAMA -
r12320:c020578820ec
parent child
Show More
@@ -1,149 +1,149
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 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 Setting.rest_api_enabled = '1'
31 Setting.rest_api_enabled = '1'
32 set_fixtures_attachments_directory
32 set_fixtures_attachments_directory
33 end
33 end
34
34
35 def teardown
35 def teardown
36 set_tmp_attachments_directory
36 set_tmp_attachments_directory
37 end
37 end
38
38
39 test "GET /attachments/:id.xml should return the attachment" do
39 test "GET /attachments/:id.xml should return the attachment" do
40 get '/attachments/7.xml', {}, credentials('jsmith')
40 get '/attachments/7.xml', {}, credentials('jsmith')
41 assert_response :success
41 assert_response :success
42 assert_equal 'application/xml', @response.content_type
42 assert_equal 'application/xml', @response.content_type
43 assert_tag :tag => 'attachment',
43 assert_tag :tag => 'attachment',
44 :child => {
44 :child => {
45 :tag => 'id',
45 :tag => 'id',
46 :content => '7',
46 :content => '7',
47 :sibling => {
47 :sibling => {
48 :tag => 'filename',
48 :tag => 'filename',
49 :content => 'archive.zip',
49 :content => 'archive.zip',
50 :sibling => {
50 :sibling => {
51 :tag => 'content_url',
51 :tag => 'content_url',
52 :content => 'http://www.example.com/attachments/download/7/archive.zip'
52 :content => 'http://www.example.com/attachments/download/7/archive.zip'
53 }
53 }
54 }
54 }
55 }
55 }
56 end
56 end
57
57
58 test "GET /attachments/:id.xml should deny access without credentials" do
58 test "GET /attachments/:id.xml should deny access without credentials" do
59 get '/attachments/7.xml'
59 get '/attachments/7.xml'
60 assert_response 401
60 assert_response 401
61 set_tmp_attachments_directory
61 set_tmp_attachments_directory
62 end
62 end
63
63
64 test "GET /attachments/download/:id/:filename should return the attachment content" do
64 test "GET /attachments/download/:id/:filename should return the attachment content" do
65 get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
65 get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
66 assert_response :success
66 assert_response :success
67 assert_equal 'application/octet-stream', @response.content_type
67 assert_equal 'application/octet-stream', @response.content_type
68 set_tmp_attachments_directory
68 set_tmp_attachments_directory
69 end
69 end
70
70
71 test "GET /attachments/download/:id/:filename should deny access without credentials" do
71 test "GET /attachments/download/:id/:filename should deny access without credentials" do
72 get '/attachments/download/7/archive.zip'
72 get '/attachments/download/7/archive.zip'
73 assert_response 302
73 assert_response 302
74 set_tmp_attachments_directory
74 set_tmp_attachments_directory
75 end
75 end
76
76
77 test "POST /uploads.xml should return the token" do
77 test "POST /uploads.xml should return the token" do
78 set_tmp_attachments_directory
78 set_tmp_attachments_directory
79 assert_difference 'Attachment.count' do
79 assert_difference 'Attachment.count' do
80 post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
80 post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
81 assert_response :created
81 assert_response :created
82 assert_equal 'application/xml', response.content_type
82 assert_equal 'application/xml', response.content_type
83 end
83 end
84
84
85 xml = Hash.from_xml(response.body)
85 xml = Hash.from_xml(response.body)
86 assert_kind_of Hash, xml['upload']
86 assert_kind_of Hash, xml['upload']
87 token = xml['upload']['token']
87 token = xml['upload']['token']
88 assert_not_nil token
88 assert_not_nil token
89
89
90 attachment = Attachment.first(:order => 'id DESC')
90 attachment = Attachment.order('id DESC').first
91 assert_equal token, attachment.token
91 assert_equal token, attachment.token
92 assert_nil attachment.container
92 assert_nil attachment.container
93 assert_equal 2, attachment.author_id
93 assert_equal 2, attachment.author_id
94 assert_equal 'File content'.size, attachment.filesize
94 assert_equal 'File content'.size, attachment.filesize
95 assert attachment.content_type.blank?
95 assert attachment.content_type.blank?
96 assert attachment.filename.present?
96 assert attachment.filename.present?
97 assert_match /\d+_[0-9a-z]+/, attachment.diskfile
97 assert_match /\d+_[0-9a-z]+/, attachment.diskfile
98 assert File.exist?(attachment.diskfile)
98 assert File.exist?(attachment.diskfile)
99 assert_equal 'File content', File.read(attachment.diskfile)
99 assert_equal 'File content', File.read(attachment.diskfile)
100 end
100 end
101
101
102 test "POST /uploads.json should return the token" do
102 test "POST /uploads.json should return the token" do
103 set_tmp_attachments_directory
103 set_tmp_attachments_directory
104 assert_difference 'Attachment.count' do
104 assert_difference 'Attachment.count' do
105 post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
105 post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
106 assert_response :created
106 assert_response :created
107 assert_equal 'application/json', response.content_type
107 assert_equal 'application/json', response.content_type
108 end
108 end
109
109
110 json = ActiveSupport::JSON.decode(response.body)
110 json = ActiveSupport::JSON.decode(response.body)
111 assert_kind_of Hash, json['upload']
111 assert_kind_of Hash, json['upload']
112 token = json['upload']['token']
112 token = json['upload']['token']
113 assert_not_nil token
113 assert_not_nil token
114
114
115 attachment = Attachment.first(:order => 'id DESC')
115 attachment = Attachment.order('id DESC').first
116 assert_equal token, attachment.token
116 assert_equal token, attachment.token
117 end
117 end
118
118
119 test "POST /uploads.xml should accept :filename param as the attachment filename" do
119 test "POST /uploads.xml should accept :filename param as the attachment filename" do
120 set_tmp_attachments_directory
120 set_tmp_attachments_directory
121 assert_difference 'Attachment.count' do
121 assert_difference 'Attachment.count' do
122 post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
122 post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
123 assert_response :created
123 assert_response :created
124 end
124 end
125
125
126 attachment = Attachment.order('id DESC').first
126 attachment = Attachment.order('id DESC').first
127 assert_equal 'test.txt', attachment.filename
127 assert_equal 'test.txt', attachment.filename
128 assert_match /_test\.txt$/, attachment.diskfile
128 assert_match /_test\.txt$/, attachment.diskfile
129 end
129 end
130
130
131 test "POST /uploads.xml should not accept other content types" do
131 test "POST /uploads.xml should not accept other content types" do
132 set_tmp_attachments_directory
132 set_tmp_attachments_directory
133 assert_no_difference 'Attachment.count' do
133 assert_no_difference 'Attachment.count' do
134 post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
134 post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
135 assert_response 406
135 assert_response 406
136 end
136 end
137 end
137 end
138
138
139 test "POST /uploads.xml should return errors if file is too big" do
139 test "POST /uploads.xml should return errors if file is too big" do
140 set_tmp_attachments_directory
140 set_tmp_attachments_directory
141 with_settings :attachment_max_size => 1 do
141 with_settings :attachment_max_size => 1 do
142 assert_no_difference 'Attachment.count' do
142 assert_no_difference 'Attachment.count' do
143 post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
143 post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
144 assert_response 422
144 assert_response 422
145 assert_tag 'error', :content => /exceeds the maximum allowed file size/
145 assert_tag 'error', :content => /exceeds the maximum allowed file size/
146 end
146 end
147 end
147 end
148 end
148 end
149 end
149 end
General Comments 0
You need to be logged in to leave comments. Login now