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