Merge pull request #1715 from opf/feature/api-work-package-update

Feature/api work package update
pull/1719/merge
manwithtwowatches 10 years ago
commit 4e2a579282
  1. 16
      lib/api/v3/work_packages/work_package_model.rb
  2. 5
      lib/api/v3/work_packages/work_package_representer.rb
  3. 11
      lib/api/v3/work_packages/work_packages_api.rb
  4. 69
      spec/api/work_package_resource_spec.rb

@ -53,6 +53,7 @@ module API
property :updated_at, on: :work_package, type: DateTime
property :author, on: :work_package, type: String
property :project_id, on: :work_package, type: Integer
property :parent_id, on: :work_package, type: Integer
property :responsible_id, on: :work_package, type: Integer
property :assigned_to_id, on: :work_package, type: Integer
property :fixed_version_id, on: :work_package, type: Integer
@ -78,7 +79,7 @@ module API
end
def type=(value)
type = Type.find(:first, conditions: ['name ilike ?', value])
type = Type.find(:first, conditions: ['UPPER(name) = UPPER(?)', value])
work_package.type = type
end
@ -87,7 +88,7 @@ module API
end
def status=(value)
status = Status.find(:first, conditions: ['name ilike ?', value])
status = Status.find(:first, conditions: ['UPPER(name) = UPPER(?)', value])
work_package.status = status
end
@ -96,7 +97,7 @@ module API
end
def priority=(value)
priority = IssuePriority.find(:first, conditions: ['name ilike ?', value])
priority = IssuePriority.find(:first, conditions: ['UPPER(name) = UPPER(?)', value])
work_package.priority = priority
end
@ -160,6 +161,15 @@ module API
validates_presence_of :subject, :project_id, :type, :author, :status
validates_length_of :subject, maximum: 255
validate :validate_parent_constraint
private
def validate_parent_constraint
if work_package.parent
errors.add :parent_id, :cannot_be_milestone if work_package.parent.is_milestone?
end
end
end
end
end

@ -146,14 +146,15 @@ module API
property :status, render_nil: true
property :is_closed
property :priority, render_nil: true
property :start_date, getter: -> (*) { work_package.start_date }, render_nil: true
property :due_date, getter: -> (*) { work_package.due_date }, render_nil: true
property :start_date, getter: -> (*) { work_package.start_date.to_datetime.utc.iso8601 unless work_package.start_date.nil? }, render_nil: true
property :due_date, getter: -> (*) { work_package.due_date.to_datetime.utc.iso8601 unless work_package.due_date.nil? }, render_nil: true
property :estimated_time, render_nil: true
property :percentage_done, render_nil: true
property :version_id, getter: -> (*) { work_package.fixed_version.try(:id) }, render_nil: true
property :version_name, getter: -> (*) { work_package.fixed_version.try(:name) }, render_nil: true
property :project_id, getter: -> (*) { work_package.project.id }
property :project_name, getter: -> (*) { work_package.project.try(:name) }
property :parent_id, render_nil: true
property :created_at, getter: -> (*) { work_package.created_at.utc.iso8601}, render_nil: true
property :updated_at, getter: -> (*) { work_package.updated_at.utc.iso8601}, render_nil: true

@ -49,6 +49,17 @@ module API
@representer.to_json
end
patch do
authorize(:edit_work_packages, context: @work_package.project)
@representer.from_json(request.POST.to_json)
@representer.represented.sync
if @representer.represented.work_package.valid? && @representer.represented.save
@representer.to_json
else
fail Errors::Validation.new(@representer.represented.work_package)
end
end
resource :activities do
helpers do

@ -64,7 +64,7 @@ h4. things we like
}}
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project', :is_public => false) }
let(:role) { FactoryGirl.create(:role, permissions: [:view_work_packages, :view_timelines]) }
let(:role) { FactoryGirl.create(:role, permissions: [:view_work_packages, :view_timelines, :edit_work_packages]) }
let(:current_user) { FactoryGirl.create(:user, member_in_project: project, member_through_role: role) }
let(:watcher) do
FactoryGirl
@ -205,4 +205,71 @@ h4. things we like
end
end
describe '#patch' do
let(:patch_path) { "/api/v3/work_packages/#{work_package.id}" }
before(:each) do
allow(User).to receive(:current).and_return current_user
patch patch_path, params
end
subject(:response) { last_response }
context 'user with needed permissions' do
context 'valid update' do
let(:params) do
{
subject: 'Updated subject',
rawDescription: '<h1>Updated description</h1>',
priority: FactoryGirl.create(:priority).name,
startDate: (Date.yesterday - 1.week).to_datetime.utc.iso8601,
dueDate: (Date.yesterday + 2.weeks).to_datetime.utc.iso8601,
percentageDone: 90,
}
end
it 'should respond with 200' do
expect(response.status).to eq(200)
end
it 'should respond with updated work package' do
expect(subject.body).to be_json_eql('Updated subject'.to_json).at_path('subject')
expect(subject.body).to be_json_eql(params[:priority].to_json).at_path('priority')
end
it 'should update the dates in iso8601 format' do
expect(subject.body).to be_json_eql(params[:startDate].to_json).at_path('startDate')
expect(subject.body).to be_json_eql(params[:dueDate].to_json).at_path('dueDate')
end
it 'should allow html in raw description' do
expect(subject.body).to be_json_eql('<h1>Updated description</h1>'.to_json).at_path('rawDescription')
end
end
context 'invalid update' do
let(:params) do
{
subject: ' ',
type: FactoryGirl.create(:type).name,
rawDescription: '<h1>Updated description</h1>',
status: FactoryGirl.create(:status).name,
priority: FactoryGirl.create(:priority).name,
startDate: (Date.new - 1.week).to_datetime.utc.iso8601,
dueDate: (Date.new + 2.weeks).to_datetime.utc.iso8601,
percentageDone: 90,
}
end
it 'should respond with 422' do
expect(response.status).to eq 422
end
it 'should respond with explanatory error message' do
parsed_errors = JSON.parse(last_response.body)['errors']
parsed_errors.should eq(["Subject can't be blank", "Type is not included in the list"])
end
end
end
end
end

Loading…
Cancel
Save