2018-08-07 21:19:24 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-07 20:19:49 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2018-08-07 21:19:24 +08:00
|
|
|
RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
|
2018-08-07 20:19:49 +08:00
|
|
|
before :all do
|
|
|
|
@user = create(:user)
|
|
|
|
@teams = create_list(:team, 2, created_by: @user)
|
|
|
|
create(:user_team, user: @user, team: @teams.first, role: 2)
|
|
|
|
|
|
|
|
# valid_inventory
|
|
|
|
@valid_inventory = create(:repository, name: Faker::Name.unique.name,
|
|
|
|
created_by: @user, team: @teams.first)
|
|
|
|
|
|
|
|
# unaccessable_inventory
|
|
|
|
create(:repository, name: Faker::Name.unique.name,
|
|
|
|
created_by: @user, team: @teams.second)
|
|
|
|
|
|
|
|
text_column = create(:repository_column, name: Faker::Name.unique.name,
|
|
|
|
repository: @valid_inventory, data_type: :RepositoryTextValue)
|
|
|
|
list_column = create(:repository_column, name: Faker::Name.unique.name,
|
|
|
|
repository: @valid_inventory, data_type: :RepositoryListValue)
|
|
|
|
list_item =
|
2020-04-09 23:11:58 +08:00
|
|
|
create(:repository_list_item, repository_column: list_column, data: Faker::Name.unique.name)
|
2018-08-07 20:19:49 +08:00
|
|
|
file_column = create(:repository_column, name: Faker::Name.unique.name,
|
|
|
|
repository: @valid_inventory, data_type: :RepositoryAssetValue)
|
|
|
|
asset = create(:asset)
|
|
|
|
|
|
|
|
create_list(:repository_row, 100, repository: @valid_inventory)
|
|
|
|
|
|
|
|
@valid_inventory.repository_rows.each do |row|
|
|
|
|
create(:repository_text_value,
|
|
|
|
data: Faker::Name.name,
|
|
|
|
repository_cell_attributes:
|
|
|
|
{ repository_row: row, repository_column: text_column })
|
|
|
|
create(:repository_list_value, repository_list_item: list_item,
|
|
|
|
repository_cell_attributes:
|
|
|
|
{ repository_row: row, repository_column: list_column })
|
|
|
|
create(:repository_asset_value, asset: asset,
|
|
|
|
repository_cell_attributes:
|
|
|
|
{ repository_row: row, repository_column: file_column })
|
|
|
|
end
|
|
|
|
|
|
|
|
@valid_headers =
|
2018-09-27 00:13:10 +08:00
|
|
|
{ 'Authorization': 'Bearer ' + generate_token(@user.id),
|
|
|
|
'Content-Type': 'application/json' }
|
2018-09-07 04:45:28 +08:00
|
|
|
|
|
|
|
@valid_hash_body = { data:
|
|
|
|
{ type: 'inventory_items',
|
|
|
|
attributes: {
|
|
|
|
name: Faker::Name.unique.name
|
|
|
|
} },
|
2020-06-23 16:23:22 +08:00
|
|
|
included: [
|
2018-09-07 04:45:28 +08:00
|
|
|
{ type: 'inventory_cells',
|
|
|
|
attributes: {
|
|
|
|
column_id: text_column.id,
|
|
|
|
value: Faker::Name.unique.name
|
|
|
|
} }
|
|
|
|
] }
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET inventory_items, #index' do
|
|
|
|
it 'Response with correct inventory items, default per page' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_inventory_items_path(
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.first.repositories.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data]).to match(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@valid_inventory.repository_rows.limit(10),
|
|
|
|
each_serializer: Api::V1::InventoryItemSerializer,
|
|
|
|
include: :inventory_cells)
|
|
|
|
.as_json[:data]
|
|
|
|
)
|
2018-10-07 00:37:40 +08:00
|
|
|
expect(hash_body).not_to include('included')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Response with correct inventory items, included cells' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_inventory_items_path(
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.first.repositories.first.id,
|
|
|
|
include: 'inventory_cells'
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data]).to match(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@valid_inventory.repository_rows.limit(10),
|
|
|
|
each_serializer: Api::V1::InventoryItemSerializer,
|
|
|
|
include: :inventory_cells)
|
|
|
|
.as_json[:data]
|
|
|
|
)
|
2018-08-07 20:19:49 +08:00
|
|
|
expect(hash_body[:included]).to match(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@valid_inventory.repository_rows.limit(10),
|
|
|
|
each_serializer: Api::V1::InventoryItemSerializer,
|
|
|
|
include: :inventory_cells)
|
|
|
|
.as_json[:included]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-08-07 22:02:14 +08:00
|
|
|
it 'Response with correct inventory items, 100 per page' do
|
2018-08-07 20:19:49 +08:00
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_inventory_items_path(
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.first.repositories.first.id
|
2018-08-23 22:48:27 +08:00
|
|
|
), params: { page: { size: 100 } }, headers: @valid_headers
|
2018-08-07 20:19:49 +08:00
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data]).to match(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@valid_inventory.repository_rows.limit(100),
|
|
|
|
each_serializer: Api::V1::InventoryItemSerializer,
|
|
|
|
include: :inventory_cells)
|
|
|
|
.as_json[:data]
|
|
|
|
)
|
2018-10-07 00:37:40 +08:00
|
|
|
expect(hash_body).not_to include('included')
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, user in not member of the team' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_inventory_items_path(
|
|
|
|
team_id: @teams.second.id,
|
|
|
|
inventory_id: @teams.second.repositories.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-10-23 23:52:48 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 403)
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, non existing inventory' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_inventory_items_path(
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: 123
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-10-23 23:52:48 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, repository from another team' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_inventory_items_path(
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.second.repositories.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-10-23 23:52:48 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
2020-06-23 16:23:22 +08:00
|
|
|
|
|
|
|
context 'when have some archived rows' do
|
|
|
|
before do
|
|
|
|
create(:repository_row, :archived,
|
|
|
|
name: Faker::Name.unique.name, created_by: @user, repository: @teams.first.repositories.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'will ignore them' do
|
|
|
|
hash_body = nil
|
|
|
|
|
|
|
|
get api_v1_team_inventory_items_path(
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.first.repositories.first.id
|
|
|
|
), params: { page: { size: 200 } }, headers: @valid_headers
|
|
|
|
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body['data'].count).to be_eql 100
|
|
|
|
end
|
|
|
|
end
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
2018-09-08 06:11:53 +08:00
|
|
|
|
|
|
|
describe 'DELETE inventory_items, #destroy' do
|
|
|
|
it 'Destroys inventory item' do
|
|
|
|
deleted_id = @teams.first.repositories.first.repository_rows.last.id
|
|
|
|
delete api_v1_team_inventory_item_path(
|
|
|
|
id: deleted_id,
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.first.repositories.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(RepositoryRow.where(id: deleted_id)).to_not exist
|
|
|
|
expect(RepositoryCell.where(repository_row: deleted_id).count).to equal 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Invalid request, non existing inventory item' do
|
|
|
|
deleted_id = RepositoryRow.last.id + 1
|
|
|
|
delete api_v1_team_inventory_item_path(
|
|
|
|
id: deleted_id,
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.first.repositories.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, incorrect repository' do
|
|
|
|
deleted_id = @teams.first.repositories.first.repository_rows.last.id
|
|
|
|
delete api_v1_team_inventory_item_path(
|
|
|
|
id: deleted_id,
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @teams.second.repositories.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect(RepositoryRow.where(id: deleted_id)).to exist
|
|
|
|
end
|
|
|
|
|
2018-09-12 00:55:38 +08:00
|
|
|
it 'When invalid request, repository from another team' do
|
2018-09-08 06:11:53 +08:00
|
|
|
deleted_id = @teams.first.repositories.first.repository_rows.last.id
|
|
|
|
delete api_v1_team_inventory_item_path(
|
|
|
|
id: deleted_id,
|
|
|
|
team_id: @teams.second.id,
|
|
|
|
inventory_id: @teams.first.repositories.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
expect(RepositoryRow.where(id: deleted_id)).to exist
|
|
|
|
end
|
|
|
|
end
|
2018-09-27 00:16:25 +08:00
|
|
|
|
2018-09-07 04:45:28 +08:00
|
|
|
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
|
2018-09-07 08:54:24 +08:00
|
|
|
|
|
|
|
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
|
2018-10-23 23:52:48 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-09-07 08:54:24 +08:00
|
|
|
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
|
2018-10-23 23:52:48 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 403)
|
2018-09-07 08:54:24 +08:00
|
|
|
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
|
2018-10-23 23:52:48 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-09-07 08:54:24 +08:00
|
|
|
end
|
2018-09-07 04:45:28 +08:00
|
|
|
end
|
2018-10-01 20:59:06 +08:00
|
|
|
|
|
|
|
describe 'PATCH inventory_items, #update' do
|
|
|
|
before :all do
|
|
|
|
@valid_headers['Content-Type'] = 'application/json'
|
|
|
|
@inventory_item = ActiveModelSerializers::SerializableResource.new(
|
|
|
|
RepositoryRow.last,
|
|
|
|
serializer: Api::V1::InventoryItemSerializer,
|
|
|
|
include: :inventory_cells
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Response with correctly updated inventory item for name field' do
|
|
|
|
hash_body = nil
|
|
|
|
updated_inventory_item = @inventory_item.as_json[:data]
|
|
|
|
updated_inventory_item[:attributes][:name] = Faker::Name.unique.name
|
|
|
|
patch api_v1_team_inventory_item_path(
|
|
|
|
id: RepositoryRow.last.id,
|
|
|
|
team_id: @teams.first.id,
|
|
|
|
inventory_id: @valid_inventory.id
|
|
|
|
), params: { data: updated_inventory_item }.to_json,
|
|
|
|
headers: @valid_headers
|
|
|
|
expect(response).to have_http_status 200
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data].to_json).to match(updated_inventory_item.to_json)
|
|
|
|
end
|
|
|
|
end
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|