Adds support for adding attachments to issues through the REST API (#8171).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8928 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2012-02-23 10:01:16 +00:00
parent d086683b17
commit 77626ef6fb
10 changed files with 175 additions and 19 deletions

View File

@@ -82,4 +82,39 @@ class ApiTest::AttachmentsTest < ActionController::IntegrationTest
end
end
end
context "POST /uploads" do
should "return the token" do
set_tmp_attachments_directory
assert_difference 'Attachment.count' do
post '/uploads.xml', 'File content', {'Content-Type' => 'application/octet-stream'}.merge(credentials('jsmith'))
assert_response :created
assert_equal 'application/xml', response.content_type
xml = Hash.from_xml(response.body)
assert_kind_of Hash, xml['upload']
token = xml['upload']['token']
assert_not_nil token
attachment = Attachment.first(:order => 'id DESC')
assert_equal token, attachment.token
assert_nil attachment.container
assert_equal 2, attachment.author_id
assert_equal 'File content'.size, attachment.filesize
assert attachment.content_type.blank?
assert attachment.filename.present?
assert_match /\d+_[0-9a-z]+/, attachment.diskfile
assert File.exist?(attachment.diskfile)
assert_equal 'File content', File.read(attachment.diskfile)
end
end
should "not accept other content types" do
set_tmp_attachments_directory
assert_no_difference 'Attachment.count' do
post '/uploads.xml', 'PNG DATA', {'Content-Type' => 'image/png'}.merge(credentials('jsmith'))
assert_response 406
end
end
end
end

View File

@@ -707,4 +707,72 @@ class ApiTest::IssuesTest < ActionController::IntegrationTest
assert_nil Issue.find_by_id(6)
end
end
def test_create_issue_with_uploaded_file
set_tmp_attachments_directory
# upload the file
assert_difference 'Attachment.count' do
post '/uploads.xml', 'test_create_with_upload', {'Content-Type' => 'application/octet-stream'}.merge(credentials('jsmith'))
assert_response :created
end
xml = Hash.from_xml(response.body)
token = xml['upload']['token']
attachment = Attachment.first(:order => 'id DESC')
# create the issue with the upload's token
assert_difference 'Issue.count' do
post '/issues.xml',
{:issue => {:project_id => 1, :subject => 'Uploaded file', :uploads => [{:token => token, :filename => 'test.txt', :content_type => 'text/plain'}]}},
credentials('jsmith')
assert_response :created
end
issue = Issue.first(:order => 'id DESC')
assert_equal 1, issue.attachments.count
assert_equal attachment, issue.attachments.first
attachment.reload
assert_equal 'test.txt', attachment.filename
assert_equal 'text/plain', attachment.content_type
assert_equal 'test_create_with_upload'.size, attachment.filesize
assert_equal 2, attachment.author_id
# get the issue with its attachments
get "/issues/#{issue.id}.xml", :include => 'attachments'
assert_response :success
xml = Hash.from_xml(response.body)
attachments = xml['issue']['attachments']
assert_kind_of Array, attachments
assert_equal 1, attachments.size
url = attachments.first['content_url']
assert_not_nil url
# download the attachment
get url
assert_response :success
end
def test_update_issue_with_uploaded_file
set_tmp_attachments_directory
# upload the file
assert_difference 'Attachment.count' do
post '/uploads.xml', 'test_upload_with_upload', {'Content-Type' => 'application/octet-stream'}.merge(credentials('jsmith'))
assert_response :created
end
xml = Hash.from_xml(response.body)
token = xml['upload']['token']
attachment = Attachment.first(:order => 'id DESC')
# update the issue with the upload's token
assert_difference 'Journal.count' do
put '/issues/1.xml',
{:issue => {:notes => 'Attachment added', :uploads => [{:token => token, :filename => 'test.txt', :content_type => 'text/plain'}]}},
credentials('jsmith')
assert_response :ok
end
issue = Issue.find(1)
assert_include attachment, issue.attachments
end
end