From c22ad028584e353dc47abec59b5c2d7eb5cf89ae Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 6 Sep 2018 13:45:28 -0700 Subject: [PATCH 1/3] inventory_item #create correct response unit test --- .../api/v1/inventory_items_controller_spec.rb | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index 5da4f1cae..f80136f08 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -44,6 +44,19 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do @valid_headers = { 'Authorization': 'Bearer ' + generate_token(@user.id) } + + @valid_hash_body = { data: + { type: 'inventory_items', + attributes: { + name: Faker::Name.unique.name + } }, + included: [ + { type: 'inventory_cells', + attributes: { + column_id: text_column.id, + value: Faker::Name.unique.name + } } + ] } end describe 'GET inventory_items, #index' do @@ -126,4 +139,34 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do expect(hash_body).to match({}) end end + + describe 'POST inventory_item, #create' do + before :all do + @valid_headers['Content-Type'] = 'application/json' + end + + it 'Response with correct inventory item' do + hash_body = nil + post api_v1_team_inventory_items_path( + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), params: @valid_hash_body.to_json, headers: @valid_headers + expect(response).to have_http_status 201 + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(RepositoryRow.last, + serializer: Api::V1::InventoryItemSerializer, + include: :inventory_cells) + .as_json[:data] + ) + expect(hash_body[:included]).to match( + ActiveModelSerializers::SerializableResource + .new(RepositoryRow.last, + serializer: Api::V1::InventoryItemSerializer, + include: :inventory_cells) + .as_json[:included] + ) + end + end end From 212bdfeffe85abf4d35939e09bc9ab9900069fad Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 6 Sep 2018 17:54:24 -0700 Subject: [PATCH 2/3] close SCI-2696 --- .../api/v1/inventory_items_controller_spec.rb | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index f80136f08..13b28278c 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -168,5 +168,38 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do .as_json[:included] ) end + + it 'When invalid request, non existing inventory' do + hash_body = nil + post api_v1_team_inventory_items_path( + team_id: @teams.first.id, + inventory_id: -1 + ), params: @valid_hash_body.to_json, headers: @valid_headers + expect(response).to have_http_status(404) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + + it 'When invalid request, user in not member of the team' do + hash_body = nil + post api_v1_team_inventory_items_path( + team_id: @teams.second.id, + inventory_id: @valid_inventory.id + ), params: @valid_hash_body.to_json, headers: @valid_headers + expect(response).to have_http_status(403) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + + it 'When invalid request, repository from another team' do + hash_body = nil + post api_v1_team_inventory_items_path( + team_id: @teams.first.id, + inventory_id: @teams.second.repositories.first.id + ), params: @valid_hash_body.to_json, headers: @valid_headers + expect(response).to have_http_status(404) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end end end From d0d0939afdf49d8d4f1593d25adf58b7af3afc52 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Wed, 26 Sep 2018 18:13:10 +0200 Subject: [PATCH 3/3] Move @valid_headers content type to global @valid_headers def --- spec/requests/api/v1/inventory_items_controller_spec.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index 13b28278c..6f5d35e5d 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -43,7 +43,8 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do end @valid_headers = - { 'Authorization': 'Bearer ' + generate_token(@user.id) } + { 'Authorization': 'Bearer ' + generate_token(@user.id), + 'Content-Type': 'application/json' } @valid_hash_body = { data: { type: 'inventory_items', @@ -141,10 +142,6 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do end describe 'POST inventory_item, #create' do - before :all do - @valid_headers['Content-Type'] = 'application/json' - end - it 'Response with correct inventory item' do hash_body = nil post api_v1_team_inventory_items_path(