diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index c8ac4f394..ce1d969c7 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -43,7 +43,21 @@ 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', + 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 @@ -172,4 +186,63 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do expect(RepositoryRow.where(id: deleted_id)).to exist end end + + describe 'POST inventory_item, #create' do + 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 + + 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