mirror of
https://github.com/redmine/redmine.git
synced 2026-03-06 12:31:40 +01:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user