2019-03-07 14:38:34 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-27 23:21:46 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe RepositoryRowsController, type: :controller do
|
|
|
|
login_user
|
|
|
|
render_views
|
2019-03-07 14:38:34 +08:00
|
|
|
let!(:user) { controller.current_user }
|
2018-02-27 23:21:46 +08:00
|
|
|
let!(:team) { create :team, created_by: user }
|
2022-06-07 00:21:57 +08:00
|
|
|
let!(:viewer_role) { create :viewer_role }
|
2018-02-27 23:21:46 +08:00
|
|
|
let!(:repository) { create :repository, team: team, created_by: user }
|
2018-03-09 21:22:00 +08:00
|
|
|
let!(:repository_state) do
|
|
|
|
RepositoryTableState.create(
|
|
|
|
repository: repository,
|
|
|
|
user: user,
|
2022-03-09 21:12:16 +08:00
|
|
|
state: repository.default_table_state
|
2018-03-09 21:22:00 +08:00
|
|
|
)
|
|
|
|
end
|
2018-02-27 23:21:46 +08:00
|
|
|
let!(:repository_row) do
|
|
|
|
create :repository_row, repository: repository,
|
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:user_two) { create :user, email: 'new@user.com' }
|
2022-06-07 00:21:57 +08:00
|
|
|
let!(:team_two) { create :team, created_by: user_two }
|
2018-02-27 23:21:46 +08:00
|
|
|
let!(:repository_two) do
|
|
|
|
create :repository, team: team_two, created_by: user_two
|
|
|
|
end
|
|
|
|
let!(:repository_row_two) do
|
|
|
|
create :repository_row, repository: repository_two,
|
|
|
|
created_by: user_two,
|
|
|
|
last_modified_by: user_two
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show' do
|
|
|
|
it 'unsuccessful response with non existing id' do
|
2020-05-18 20:12:06 +08:00
|
|
|
get :show, format: :json, params: { repository_id: repository.id, id: -1 }
|
2018-02-27 23:21:46 +08:00
|
|
|
expect(response).to have_http_status(:not_found)
|
|
|
|
end
|
|
|
|
|
2020-05-18 20:12:06 +08:00
|
|
|
it 'unsuccessful response with id from another repository' do
|
|
|
|
get :show, format: :json, params: { repository_id: repository.id, id: repository_row_two.id }
|
|
|
|
expect(response).to have_http_status(:not_found)
|
2018-02-27 23:21:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'successful response' do
|
2020-05-18 20:12:06 +08:00
|
|
|
get :show, format: :json, params: { repository_id: repository.id, id: repository_row.id }
|
2018-02-27 23:21:46 +08:00
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
end
|
2018-03-09 21:22:00 +08:00
|
|
|
|
|
|
|
context '#index' do
|
|
|
|
before do
|
|
|
|
repository.repository_rows.destroy_all
|
|
|
|
110.times do |index|
|
|
|
|
create :repository_row, name: "row (#{index})",
|
|
|
|
repository: repository,
|
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'json object' do
|
|
|
|
it 'returns a valid object' do
|
2022-03-09 21:12:16 +08:00
|
|
|
params = { order: [{ column: '4', dir: 'asc' }],
|
2018-03-09 21:22:00 +08:00
|
|
|
drow: '0',
|
|
|
|
search: { value: '' },
|
|
|
|
length: '10',
|
|
|
|
start: '1',
|
|
|
|
repository_id: repository.id }
|
|
|
|
get :index, params: params, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
expect(response).to match_response_schema('repository_row_datatables')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'pagination' do
|
|
|
|
it 'returns first 10 records' do
|
2022-03-09 21:12:16 +08:00
|
|
|
params = { order: [{ column: '4', dir: 'asc' }],
|
2018-03-09 21:22:00 +08:00
|
|
|
drow: '0',
|
|
|
|
search: { value: '' },
|
|
|
|
length: '10',
|
|
|
|
start: '1',
|
2020-06-24 17:22:28 +08:00
|
|
|
repository_id: repository.id,
|
|
|
|
archived: false }
|
2018-03-09 21:22:00 +08:00
|
|
|
get :index, params: params, format: :json
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body['data'].length).to eq 10
|
2018-04-04 20:44:32 +08:00
|
|
|
expect(response_body['data'].first['3']).to eq 'row (0)'
|
2018-03-09 21:22:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns next 10 records' do
|
2022-03-09 21:12:16 +08:00
|
|
|
params = { order: [{ column: '4', dir: 'asc' }],
|
2018-03-09 21:22:00 +08:00
|
|
|
drow: '0',
|
|
|
|
search: { value: '' },
|
|
|
|
length: '10',
|
|
|
|
start: '11',
|
2020-06-24 17:22:28 +08:00
|
|
|
repository_id: repository.id,
|
|
|
|
archived: false }
|
2018-03-09 21:22:00 +08:00
|
|
|
get :index, params: params, format: :json
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body['data'].length).to eq 10
|
2018-04-04 20:44:32 +08:00
|
|
|
expect(response_body['data'].first['3']).to eq 'row (10)'
|
2018-03-09 21:22:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns first 25 records' do
|
2022-03-09 21:12:16 +08:00
|
|
|
params = { order: [{ column: '4', dir: 'desc' }],
|
2018-03-09 21:22:00 +08:00
|
|
|
drow: '0',
|
|
|
|
search: { value: '' },
|
|
|
|
length: '25',
|
|
|
|
start: '1',
|
2020-06-24 17:22:28 +08:00
|
|
|
repository_id: repository.id,
|
|
|
|
archived: false }
|
2018-03-09 21:22:00 +08:00
|
|
|
get :index, params: params, format: :json
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body['data'].length).to eq 25
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-11 23:17:19 +08:00
|
|
|
|
|
|
|
describe 'POST #copy_records' do
|
|
|
|
it 'returns a success response' do
|
|
|
|
post :copy_records, params: { repository_id: repository.id,
|
|
|
|
selected_rows: [repository_row.id] }
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
end
|
2019-03-07 14:38:34 +08:00
|
|
|
|
|
|
|
describe 'POST create' do
|
2019-03-13 16:06:48 +08:00
|
|
|
let(:action) { post :create, params: params, format: :json }
|
2019-03-07 14:38:34 +08:00
|
|
|
let(:params) do
|
2019-12-10 15:43:26 +08:00
|
|
|
{ repository_id: repository.id, repository_row: { name: 'row_name' } }
|
2019-03-07 14:38:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for creating inventory item' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :create_item_inventory)))
|
|
|
|
|
2019-03-13 16:06:48 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
2019-03-07 14:38:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT update' do
|
2019-03-13 16:06:48 +08:00
|
|
|
let(:action) { put :update, params: params, format: :json }
|
2019-03-07 14:38:34 +08:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
repository_id: repository.id,
|
|
|
|
id: repository_row.id,
|
2019-12-10 15:43:26 +08:00
|
|
|
repository_row: { name: 'row_name' }
|
2019-03-07 14:38:34 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for editing intentory item' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :edit_item_inventory)))
|
|
|
|
|
2019-03-13 16:06:48 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
2019-03-07 14:38:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST delete_records' do
|
2019-03-13 16:06:48 +08:00
|
|
|
let(:action) { post :delete_records, params: params, format: :json }
|
2019-03-07 14:38:34 +08:00
|
|
|
let(:params) do
|
|
|
|
{ repository_id: repository.id, selected_rows: [repository_row.id] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for deleting inventory items' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :delete_item_inventory)))
|
|
|
|
|
2019-03-13 16:06:48 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
2019-03-07 14:38:34 +08:00
|
|
|
end
|
|
|
|
end
|
2020-06-10 01:18:30 +08:00
|
|
|
|
|
|
|
describe 'POST archive_rows' do
|
|
|
|
let(:action) { post :archive_records, params: params, format: :json }
|
|
|
|
let(:params) do
|
|
|
|
{ repository_id: repository.id, selected_rows: [repository_row.id] }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has permission' do
|
|
|
|
context 'when archiving passes' do
|
|
|
|
it 'change archived boolean to true' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(repository_row.reload.archived).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 200' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when archiving fails' do
|
|
|
|
before do
|
|
|
|
# Make invalid row record
|
|
|
|
repository_row.name = ''
|
|
|
|
repository_row.save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not change archived boolean to true' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(repository_row.reload.archived).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 422' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when does not have permission' do
|
|
|
|
context 'when guest' do
|
|
|
|
it 'renders 403' do
|
2022-06-07 00:21:57 +08:00
|
|
|
repository.user_assignments.update(user_role: viewer_role)
|
2020-06-10 01:18:30 +08:00
|
|
|
action
|
|
|
|
expect(response).to have_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when does not see repository' do
|
2022-06-07 00:21:57 +08:00
|
|
|
let(:repository) { create :repository, team: team_two, created_by: user_two }
|
2020-06-10 01:18:30 +08:00
|
|
|
|
|
|
|
it 'renders 404' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST restore_rows' do
|
|
|
|
let(:repository_row) { create :repository_row, :archived, repository: repository, created_by: user }
|
|
|
|
let(:action) { post :restore_records, params: params, format: :json }
|
|
|
|
let(:params) do
|
|
|
|
{ repository_id: repository.id, selected_rows: [repository_row.id] }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has permission' do
|
|
|
|
context 'when restoring passes' do
|
|
|
|
it 'change archived boolean to false' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(repository_row.reload.archived).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 200' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when restoring fails' do
|
|
|
|
before do
|
|
|
|
# Make invalid row record
|
|
|
|
repository_row.name = ''
|
|
|
|
repository_row.save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not change archived boolean to false' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(repository_row.reload.archived).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 422' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when does not have permission' do
|
|
|
|
context 'when guest' do
|
|
|
|
it 'renders 403' do
|
2022-06-07 00:21:57 +08:00
|
|
|
repository.user_assignments.update(user_role: viewer_role)
|
2020-06-10 01:18:30 +08:00
|
|
|
action
|
|
|
|
expect(response).to have_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when does not see repository' do
|
2022-06-07 00:21:57 +08:00
|
|
|
let(:repository) { create :repository, team: team_two, created_by: user_two }
|
2020-06-10 01:18:30 +08:00
|
|
|
|
|
|
|
it 'renders 404' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-27 23:21:46 +08:00
|
|
|
end
|