implement grid create end point

pull/6834/head
Jens Ulferts 6 years ago
parent 56bf3070c5
commit a2cc3d11a0
No known key found for this signature in database
GPG Key ID: 3CAA4B1182CF5308
  1. 30
      app/contracts/grids/base_contract.rb
  2. 2
      lib/api/v3/grids/grid_representer.rb
  3. 15
      lib/api/v3/grids/grids_api.rb
  4. 43
      spec/contracts/grids/create_contract_spec.rb
  5. 51
      spec/requests/api/v3/grids/grids_resource_spec.rb

@ -32,14 +32,36 @@ require 'model_contract'
module Grids
class BaseContract < ::ModelContract
attribute :row_count
attribute :column_count
# TODO: check how this can be restricted to only MyPage
attribute :user
attribute :row_count do
validate_positive_integer(:row_count)
end
attribute :column_count do
validate_positive_integer(:column_count)
end
attribute :widgets
# TODO: generalize
attribute :user_id,
writeable: -> { model.is_a?(MyPageGrid) }
# TODO: prevent that value from being set
attribute :type
def self.model
Grid
end
private
def validate_positive_integer(attribute)
value = model.send(attribute)
if !value
errors.add(attribute, :blank)
elsif value < 1
errors.add(attribute, :greater_than, count: 0)
end
end
end
end

@ -66,7 +66,7 @@ module API
setter: ->(fragment:, **) do
represented.widgets = fragment.map do |widget_fragment|
WidgetRepresenter
.new(OpenStruct.new, current_user: current_user)
.new(GridWidget.new, current_user: current_user)
.from_hash(widget_fragment.with_indifferent_access)
end
end

@ -52,23 +52,22 @@ module API
end
post do
# TODO: replace mock with actual creation
params = API::V3::ParseResourceParamsService
.new(current_user, representer: GridRepresenter)
.call(request_body)
.result
# TODO: determine grid class based on the page parameter
call = ::Grids::SetAttributesService
.new(user: current_user,
grid: MyPageGrid.new_default(current_user),
contract_class: ::Grids::CreateContract)
.call(params)
call = ::Grids::CreateService
.new(user: current_user)
.call(attributes: params)
status 201
if call.success?
GridRepresenter.create(call.result,
current_user: current_user,
embed_links: true)
else
fail ::API::Errors::ErrorBase.create_and_merge_errors(call.errors)
end
end
mount CreateFormAPI

@ -38,8 +38,16 @@ describe Grids::CreateContract do
let(:instance) { described_class.new(grid, user) }
shared_examples_for 'is writable' do
let(:default_values) do
{
row_count: 6,
column_count: 7
}
end
let(:grid) do
FactoryBot.build_stubbed(:grid, attribute => value)
attributes = default_values.merge(attribute => value)
FactoryBot.build_stubbed(:grid, attributes)
end
it 'is writable' do
@ -48,10 +56,36 @@ describe Grids::CreateContract do
end
end
shared_examples_for 'validates positive integer' do
context 'when the value is negative' do
let(:value) { -1 }
it 'is invalid' do
instance.validate
expect(instance.errors.details[attribute])
.to match_array [{ error: :greater_than, count: 0 }]
end
end
context 'when the value is nil' do
let(:value) { nil }
it 'is invalid' do
instance.validate
expect(instance.errors.details[attribute])
.to match_array [{ error: :blank }]
end
end
end
describe 'row_count' do
it_behaves_like 'is writable' do
let(:attribute) { :row_count }
let(:value) { 5 }
it_behaves_like 'validates positive integer'
end
end
@ -59,13 +93,8 @@ describe Grids::CreateContract do
it_behaves_like 'is writable' do
let(:attribute) { :column_count }
let(:value) { 5 }
end
end
describe 'page' do
it_behaves_like 'is writable' do
let(:attribute) { :page }
let(:value) { 'some/other/page' }
it_behaves_like 'validates positive integer'
end
end

@ -192,7 +192,14 @@ describe 'API v3 Grids resource', type: :request, content_type: :json do
let(:params) do
{
"rowCount": 10,
"columnCount": 15
"columnCount": 15,
"widgets": [{
"identifier": "work_packages_assigned",
"startRow": 4,
"endRow": 8,
"startColumn": 2,
"endColumn": 5
}]
}.with_indifferent_access
end
@ -211,11 +218,53 @@ describe 'API v3 Grids resource', type: :request, content_type: :json do
expect(subject.body)
.to be_json_eql(params['rowCount'].to_json)
.at_path('rowCount')
expect(subject.body)
.to be_json_eql(params['widgets'][0]['identifier'].to_json)
.at_path('widgets/0/identifier')
end
it 'persists the grid' do
expect(Grid.count)
.to eql(1)
end
context 'with invalid params' do
let(:params) do
{
"rowCount": -5,
"columnCount": "sdjfksdfsdfdsf",
"widgets": [{
"identifier": "work_packages_assigned",
"startRow": 4,
"endRow": 8,
"startColumn": 2,
"endColumn": 5
}]
}.with_indifferent_access
end
it 'responds with 422' do
expect(subject.status).to eq(422)
end
it 'does not create a grid' do
expect(Grid.count)
.to eql(0)
end
it 'returns the errors' do
expect(subject.body)
.to be_json_eql('Error'.to_json)
.at_path('_type')
expect(subject.body)
.to be_json_eql("Row count must be greater than 0.".to_json)
.at_path('_embedded/errors/0/message')
expect(subject.body)
.to be_json_eql("Column count must be greater than 0.".to_json)
.at_path('_embedded/errors/1/message')
end
end
end
end

Loading…
Cancel
Save