##// 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 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
21 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 22 :enumerations, :users, :issue_categories,
23 23 :projects_trackers,
24 24 :roles,
25 25 :member_roles,
26 26 :members,
27 27 :enabled_modules,
28 28 :attachments
29 29
30 30 def setup
31 31 Setting.rest_api_enabled = '1'
32 32 set_fixtures_attachments_directory
33 33 end
34 34
35 35 def teardown
36 36 set_tmp_attachments_directory
37 37 end
38 38
39 39 test "GET /attachments/:id.xml should return the attachment" do
40 40 get '/attachments/7.xml', {}, credentials('jsmith')
41 41 assert_response :success
42 42 assert_equal 'application/xml', @response.content_type
43 43 assert_tag :tag => 'attachment',
44 44 :child => {
45 45 :tag => 'id',
46 46 :content => '7',
47 47 :sibling => {
48 48 :tag => 'filename',
49 49 :content => 'archive.zip',
50 50 :sibling => {
51 51 :tag => 'content_url',
52 52 :content => 'http://www.example.com/attachments/download/7/archive.zip'
53 53 }
54 54 }
55 55 }
56 56 end
57 57
58 58 test "GET /attachments/:id.xml should deny access without credentials" do
59 59 get '/attachments/7.xml'
60 60 assert_response 401
61 61 set_tmp_attachments_directory
62 62 end
63 63
64 64 test "GET /attachments/download/:id/:filename should return the attachment content" do
65 65 get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
66 66 assert_response :success
67 67 assert_equal 'application/octet-stream', @response.content_type
68 68 set_tmp_attachments_directory
69 69 end
70 70
71 71 test "GET /attachments/download/:id/:filename should deny access without credentials" do
72 72 get '/attachments/download/7/archive.zip'
73 73 assert_response 302
74 74 set_tmp_attachments_directory
75 75 end
76 76
77 77 test "POST /uploads.xml should return the token" do
78 78 set_tmp_attachments_directory
79 79 assert_difference 'Attachment.count' do
80 80 post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
81 81 assert_response :created
82 82 assert_equal 'application/xml', response.content_type
83 83 end
84 84
85 85 xml = Hash.from_xml(response.body)
86 86 assert_kind_of Hash, xml['upload']
87 87 token = xml['upload']['token']
88 88 assert_not_nil token
89 89
90 attachment = Attachment.first(:order => 'id DESC')
90 attachment = Attachment.order('id DESC').first
91 91 assert_equal token, attachment.token
92 92 assert_nil attachment.container
93 93 assert_equal 2, attachment.author_id
94 94 assert_equal 'File content'.size, attachment.filesize
95 95 assert attachment.content_type.blank?
96 96 assert attachment.filename.present?
97 97 assert_match /\d+_[0-9a-z]+/, attachment.diskfile
98 98 assert File.exist?(attachment.diskfile)
99 99 assert_equal 'File content', File.read(attachment.diskfile)
100 100 end
101 101
102 102 test "POST /uploads.json should return the token" do
103 103 set_tmp_attachments_directory
104 104 assert_difference 'Attachment.count' do
105 105 post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
106 106 assert_response :created
107 107 assert_equal 'application/json', response.content_type
108 108 end
109 109
110 110 json = ActiveSupport::JSON.decode(response.body)
111 111 assert_kind_of Hash, json['upload']
112 112 token = json['upload']['token']
113 113 assert_not_nil token
114 114
115 attachment = Attachment.first(:order => 'id DESC')
115 attachment = Attachment.order('id DESC').first
116 116 assert_equal token, attachment.token
117 117 end
118 118
119 119 test "POST /uploads.xml should accept :filename param as the attachment filename" do
120 120 set_tmp_attachments_directory
121 121 assert_difference 'Attachment.count' do
122 122 post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
123 123 assert_response :created
124 124 end
125 125
126 126 attachment = Attachment.order('id DESC').first
127 127 assert_equal 'test.txt', attachment.filename
128 128 assert_match /_test\.txt$/, attachment.diskfile
129 129 end
130 130
131 131 test "POST /uploads.xml should not accept other content types" do
132 132 set_tmp_attachments_directory
133 133 assert_no_difference 'Attachment.count' do
134 134 post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
135 135 assert_response 406
136 136 end
137 137 end
138 138
139 139 test "POST /uploads.xml should return errors if file is too big" do
140 140 set_tmp_attachments_directory
141 141 with_settings :attachment_max_size => 1 do
142 142 assert_no_difference 'Attachment.count' do
143 143 post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
144 144 assert_response 422
145 145 assert_tag 'error', :content => /exceeds the maximum allowed file size/
146 146 end
147 147 end
148 148 end
149 149 end
General Comments 0
You need to be logged in to leave comments. Login now