From 0b68a670c201081ad6c5927993a47bfb8a818e00 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Wed, 12 Sep 2018 13:44:10 +0200 Subject: [PATCH] Fix the inventory_columns_controller#create action Based on unit tests, fix the create inventory columns endpoint to work as intended. Also add another unit test case. --- .../api/v1/inventory_columns_controller.rb | 8 +++++--- .../v1/inventory_columns_controller_spec.rb | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/inventory_columns_controller.rb b/app/controllers/api/v1/inventory_columns_controller.rb index 86ac63544..717d0cbdf 100644 --- a/app/controllers/api/v1/inventory_columns_controller.rb +++ b/app/controllers/api/v1/inventory_columns_controller.rb @@ -6,7 +6,7 @@ module Api before_action :load_team before_action :load_inventory before_action :load_inventory_column, only: %i(show update destroy) - before_action :check_manage_permissions, only: %i(create update destroy) + before_action :check_manage_permissions, only: %i(update destroy) def index columns = @inventory.repository_columns @@ -19,7 +19,7 @@ module Api def create inventory_column = - @inventory.inventory_columns.create!(inventory_column_params) + @inventory.repository_columns.create!(inventory_column_params) render jsonapi: inventory_column, serializer: InventoryColumnSerializer, status: :created @@ -72,7 +72,9 @@ module Api 'Wrong object type within parameters' end params.require(:data).require(:attributes) - params.permit(data: { attributes: %i(name) })[:data] + params + .permit(data: { attributes: %i(name data_type) })[:data] + .merge(created_by: @current_user) end def update_inventory_column_params diff --git a/spec/requests/api/v1/inventory_columns_controller_spec.rb b/spec/requests/api/v1/inventory_columns_controller_spec.rb index 612bf457e..270e334da 100644 --- a/spec/requests/api/v1/inventory_columns_controller_spec.rb +++ b/spec/requests/api/v1/inventory_columns_controller_spec.rb @@ -93,7 +93,9 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do @request_body = { data: { type: 'inventory_columns', attributes: { - name: Faker::Name.unique.name + name: Faker::Name.unique.name, + data_type: + RepositoryColumn.data_types.values.first } } } end @@ -196,5 +198,20 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do expect { hash_body = json }.to_not raise_exception expect(hash_body).to match({}) end + + it 'When invalid request, missing attributes values' do + hash_body = nil + [:name, :data_type].each do |attr| + invalid_request_body = @request_body.deep_dup + invalid_request_body[:data][:attributes].delete(attr) + post api_v1_team_inventory_columns_path( + team_id: @teams.first.id, + inventory_id: @teams.first.repositories.first.id + ), params: invalid_request_body.to_json, headers: @valid_headers + expect(response).to have_http_status(400) + expect { hash_body = json }.to_not raise_exception + expect(hash_body).to match({}) + end + end end end