Project

General

Profile

Patch #22356 » update_attachment_api_with_tests.diff

Oliver Luethi, 2016-06-23 14:47

View differences:

app/controllers/attachments_controller.rb
before_filter :delete_authorize, :only => :destroy
before_filter :authorize_global, :only => :upload
accept_api_auth :show, :download, :thumbnail, :upload, :destroy
accept_api_auth :show, :download, :thumbnail, :upload, :update, :destroy
def show
respond_to do |format|
......
def update
if params[:attachments].is_a?(Hash)
if Attachment.update_attachments(@attachments, params[:attachments])
redirect_back_or_default home_path
return
respond_to do |format|
format.html { redirect_back_or_default home_path }
format.api { render_api_ok }
end
return
end
end
render :action => 'edit'
respond_to do |format|
format.html { render :action => 'edit' }
format.api { render_validation_errors(@attachments) }
end
end
def destroy
test/integration/api_test/attachments_test.rb
assert_nil Attachment.find_by_id(7)
end
test "PATCH /attachments/issues/:id.json should return ok and updated Attachment" do
token = json_upload('File content 1', credentials('jsmith'))
payload = <<-JSON
{
"issue": {
"project_id": "1",
"tracker_id": "1",
"subject": "Issue with attachment",
"uploads": [
{"token": "#{token}", "filename": "test1.txt", "description": "test1"}
]
}
}
JSON
assert_difference 'Issue.count' do
post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
assert_response :created
end
issue = Issue.order('id DESC').first
assert_equal 1, issue.attachments.count
attachment = issue.attachments.first
attachment.filename = "test1_updated.txt"
attachment.description = "test1_updated"
data = {:attachments => {attachment.id => {:filename => attachment.filename, :description => attachment.description}}}
patch "/attachments/issues/#{issue.id}.json", data, credentials('jsmith')
assert_response :ok
updatedIssue = Issue.find(issue.id)
updatedAttachment = updatedIssue.attachments.first;
assert_equal "Issue with attachment", updatedIssue.subject
assert_equal "test1_updated.txt", updatedAttachment.filename
assert_equal "test1_updated", updatedAttachment.description
end
test "POST /uploads.xml should return the token" do
set_tmp_attachments_directory
assert_difference 'Attachment.count' do
(2-2/2)