Merge pull request #2384 from mlorb/ml-sci-4261

Clean up controllers for manage repository columns [SCI-4261]
This commit is contained in:
mlorb 2020-07-09 09:18:23 +02:00 committed by GitHub
commit efbef2c876
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 52 additions and 881 deletions

View file

@ -1,11 +1,8 @@
# frozen_string_literal: true
module RepositoryColumns
class AssetColumnsController < BaseColumnsController
class AssetColumnsController < RepositoryColumnsController
include InputSanitizeHelper
before_action :load_column, only: %i(update destroy)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def create
service = RepositoryColumns::CreateColumnService
@ -34,17 +31,6 @@ module RepositoryColumns
end
end
def destroy
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
private
def repository_column_params

View file

@ -1,28 +0,0 @@
# frozen_string_literal: true
module RepositoryColumns
class BaseColumnsController < ApplicationController
include InputSanitizeHelper
before_action :load_repository
private
def load_repository
@repository = Repository.accessible_by_teams(current_team).find_by(id: params[:repository_id])
render_404 unless @repository
end
def load_column
@repository_column = @repository.repository_columns.find_by(id: params[:id])
render_404 unless @repository_column
end
def check_create_permissions
render_403 unless can_create_repository_columns?(@repository)
end
def check_manage_permissions
render_403 unless can_manage_repository_column?(@repository_column)
end
end
end

View file

@ -1,10 +1,7 @@
# frozen_string_literal: true
module RepositoryColumns
class ChecklistColumnsController < BaseColumnsController
before_action :load_column, only: %i(update destroy items)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
class ChecklistColumnsController < RepositoryColumnsController
helper_method :delimiters
def create
@ -34,17 +31,6 @@ module RepositoryColumns
end
end
def destroy
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
def items
column_checklist_items = @repository_column.repository_checklist_items
.where('data ILIKE ?',
@ -58,7 +44,7 @@ module RepositoryColumns
private
def search_params
params.permit(:query, :column_id)
params.permit(:query, :repository_id, :id)
end
def repository_column_params

View file

@ -1,11 +1,8 @@
# frozen_string_literal: true
module RepositoryColumns
class DateTimeColumnsController < BaseColumnsController
class DateTimeColumnsController < RepositoryColumnsController
include InputSanitizeHelper
before_action :load_column, only: %i(update destroy)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def create
service = RepositoryColumns::CreateColumnService
@ -34,17 +31,6 @@ module RepositoryColumns
end
end
def destroy
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
private
def repository_column_params

View file

@ -1,10 +1,7 @@
# frozen_string_literal: true
module RepositoryColumns
class ListColumnsController < BaseColumnsController
before_action :load_column, only: %i(update destroy items)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
class ListColumnsController < RepositoryColumnsController
helper_method :delimiters
def create
@ -34,17 +31,6 @@ module RepositoryColumns
end
end
def destroy
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
def items
column_list_items = @repository_column.repository_list_items
.where('data ILIKE ?',
@ -58,7 +44,7 @@ module RepositoryColumns
private
def search_params
params.permit(:query, :column_id)
params.permit(:query, :repository_id, :id)
end
def repository_column_params

View file

@ -1,11 +1,8 @@
# frozen_string_literal: true
module RepositoryColumns
class NumberColumnsController < BaseColumnsController
class NumberColumnsController < RepositoryColumnsController
include InputSanitizeHelper
before_action :load_column, only: %i(update destroy)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def create
service = RepositoryColumns::CreateColumnService
@ -34,17 +31,6 @@ module RepositoryColumns
end
end
def destroy
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
private
def repository_column_params

View file

@ -1,11 +1,8 @@
# frozen_string_literal: true
module RepositoryColumns
class StatusColumnsController < BaseColumnsController
class StatusColumnsController < RepositoryColumnsController
include InputSanitizeHelper
before_action :load_column, only: %i(update destroy items)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def create
service = RepositoryColumns::CreateColumnService
@ -34,17 +31,6 @@ module RepositoryColumns
end
end
def destroy
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
def items
column_status_items = @repository_column.repository_status_items
.where('status ILIKE ?',
@ -58,7 +44,7 @@ module RepositoryColumns
private
def search_params
params.permit(:query, :column_id)
params.permit(:query, :repository_id, :id)
end
def repository_column_params

View file

@ -1,11 +1,8 @@
# frozen_string_literal: true
module RepositoryColumns
class TextColumnsController < BaseColumnsController
class TextColumnsController < RepositoryColumnsController
include InputSanitizeHelper
before_action :load_column, only: %i(update destroy)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def create
service = RepositoryColumns::CreateColumnService
@ -34,17 +31,6 @@ module RepositoryColumns
end
end
def destroy
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
private
def repository_column_params

View file

@ -2,19 +2,13 @@ class RepositoryColumnsController < ApplicationController
include InputSanitizeHelper
include RepositoryColumnsHelper
ACTIONS = %i(
create index_html create_html available_asset_type_columns available_columns
).freeze
before_action :load_vars,
except: ACTIONS
before_action :load_vars_nested,
only: ACTIONS
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions,
except: ACTIONS
before_action :load_repository
before_action :load_column, only: %i(edit update destroy_html destroy items)
before_action :check_create_permissions, only: %i(new create)
before_action :check_manage_permissions, only: %i(edit update destroy_html destroy)
before_action :load_asset_type_columns, only: :available_asset_type_columns
def index_html
def index
render json: {
id: @repository.id,
html: render_to_string(
@ -23,7 +17,7 @@ class RepositoryColumnsController < ApplicationController
}
end
def create_html
def new
@repository_column = RepositoryColumn.new
respond_to do |format|
format.json do
@ -37,82 +31,19 @@ class RepositoryColumnsController < ApplicationController
end
def create
@repository_column = RepositoryColumn.new(repository_column_params)
@repository_column.repository = @repository
@repository_column.created_by = current_user
respond_to do |format|
format.json do
if @repository_column.save
log_activity(:create_column_inventory)
if generate_repository_list_items(params[:list_items])
render json: {
id: @repository_column.id,
name: escape_input(@repository_column.name),
message: t('libraries.repository_columns.create.success_flash',
name: @repository_column.name),
edit_url:
edit_repository_repository_column_path(@repository,
@repository_column),
update_url:
repository_repository_column_path(@repository,
@repository_column),
destroy_html_url:
repository_columns_destroy_html_path(@repository,
@repository_column)
},
status: :ok
else
render json: {
message: {
repository_list_items:
t('libraries.repository_columns.repository_list_items_limit',
limit: Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN)
}
}, status: :unprocessable_entity
end
else
render json: { message: @repository_column.errors.full_messages },
status: :unprocessable_entity
end
end
end
raise NotImplementedError
end
def edit
render json: { html: render_to_string(partial: 'repository_columns/manage_column_modal_content.html.erb') }
render json: {
html: render_to_string(
partial: 'repository_columns/manage_column_modal_content.html.erb'
)
}
end
def update
respond_to do |format|
format.json do
@repository_column.update(repository_column_params)
if @repository_column.save
log_activity(:edit_column_inventory)
if update_repository_list_items(params[:list_items])
render json: {
id: @repository_column.id,
name: escape_input(@repository_column.name),
message: t('libraries.repository_columns.update.success_flash',
name: escape_input(@repository_column.name))
}, status: :ok
else
render json: {
message: {
repository_list_items:
t('libraries.repository_columns.repository_list_items_limit',
limit: Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN)
}
}, status: :unprocessable_entity
end
else
render json: { message: @repository_column.errors.full_messages },
status: :unprocessable_entity
end
end
end
raise NotImplementedError
end
def destroy_html
@ -151,6 +82,10 @@ class RepositoryColumnsController < ApplicationController
end
end
def items
raise NotImplementedError
end
def available_asset_type_columns
if @asset_columns.empty?
render json: {
@ -172,16 +107,14 @@ class RepositoryColumnsController < ApplicationController
include StringUtility
AvailableRepositoryColumn = Struct.new(:id, :name)
def load_vars
@repository = Repository.accessible_by_teams(current_team).find_by_id(params[:repository_id])
def load_repository
@repository = Repository.accessible_by_teams(current_team).find_by(id: params[:repository_id])
render_404 unless @repository
@repository_column = @repository.repository_columns.find_by_id(params[:id])
render_404 unless @repository_column
end
def load_vars_nested
@repository = Repository.accessible_by_teams(current_team).find_by_id(params[:repository_id])
render_404 unless @repository
def load_column
@repository_column = @repository.repository_columns.find_by(id: params[:id])
render_404 unless @repository_column
end
def load_asset_type_columns
@ -197,10 +130,6 @@ class RepositoryColumnsController < ApplicationController
render_403 unless can_manage_repository_column?(@repository_column)
end
def repository_column_params
params.require(:repository_column).permit(:name, :data_type)
end
def search_params
params.permit(:q, :repository_id)
end
@ -218,63 +147,6 @@ class RepositoryColumnsController < ApplicationController
end
end
def generate_repository_list_items(item_names)
return true unless @repository_column.data_type == 'RepositoryListValue'
column_items = @repository_column.repository_list_items.size
success = true
item_names.split(',').uniq.each do |name|
if column_items >= Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN
success = false
next
end
RepositoryListItem.create(
repository: @repository,
repository_column: @repository_column,
data: name,
created_by: current_user,
last_modified_by: current_user
)
column_items += 1
end
success
end
def update_repository_list_items(item_names)
return true unless @repository_column.data_type == 'RepositoryListValue'
column_items = @repository_column.repository_list_items.size
items_list = item_names.split(',').uniq
existing = @repository_column.repository_list_items.pluck(:data)
existing.each do |name|
next if items_list.include? name
list_item_id = @repository_column.repository_list_items
.find_by_data(name)
.destroy
.id
RepositoryCell.where(
'value_type = ? AND value_id = ?',
'RepositoryListValue',
list_item_id
).destroy_all
end
success = true
items_list.each do |name|
next if @repository_column.repository_list_items.find_by_data(name)
if column_items >= Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN
success = false
next
end
RepositoryListItem.create(
repository: @repository,
repository_column: @repository_column,
data: name,
created_by: current_user,
last_modified_by: current_user
)
column_items += 1
end
success
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,

View file

@ -1,22 +0,0 @@
# frozen_string_literal: true
module RepositoryColumns
class DeleteColumnService < RepositoryColumns::ColumnService
def initialize(user:, team:, column:)
super(user: user, team: team, column_name: nil, repository: column.repository)
@column = column
end
def call
ActiveRecord::Base.transaction do
log_activity(:delete_column_inventory)
@column.destroy!
rescue ActiveRecord::RecordNotDestroyed
errors[:repository_column] = 'record cannot be destroyed'
raise ActiveRecord::Rollback
end
self
end
end
end

View file

@ -19,7 +19,7 @@
<div class="repo-datatables-buttons pull-right" style="display: inline;">
<button class="btn btn-secondary manage-repo-column-index"
data-modal-url="<%= repository_columns_index_html_path(@repository) %>"
data-modal-url="<%= repository_repository_columns_path(@repository) %>"
data-action="new">
<span class="fas fa-wrench"></span>&nbsp;<%= t('repositories.index.columns') %>
</button>

View file

@ -54,7 +54,7 @@
</div>
<button class="btn btn-secondary manage-repo-column-index"
data-modal-url="<%= repository_columns_index_html_path(@repository) %>"
data-modal-url="<%= repository_repository_columns_path(@repository) %>"
data-action="new">
<span class="fas fa-wrench"></span>&nbsp;<%= t('repositories.index.columns') %>
</button>

View file

@ -1,7 +1,7 @@
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<button class="btn icon-btn btn-light manage-repo-column-index back-to-column-modal"
data-modal-url="<%= repository_columns_index_html_path(@repository) %>"
data-modal-url="<%= repository_repository_columns_path(@repository) %>"
data-action="new">
<span class="fas fa-arrow-left"></span>
</button>
@ -23,7 +23,7 @@
</div>
<div class="modal-footer delete-footer">
<button class="btn btn-secondary manage-repo-column-index back-to-column-modal"
data-modal-url="<%= repository_columns_index_html_path(@repository) %>"
data-modal-url="<%= repository_repository_columns_path(@repository) %>"
data-action="new">
<%= t("general.cancel")%>
</button>

View file

@ -1,7 +1,7 @@
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<button class="btn icon-btn btn-light manage-repo-column-index back-to-column-modal"
data-modal-url="<%= repository_columns_index_html_path(@repository) %>"
data-modal-url="<%= repository_repository_columns_path(@repository) %>"
data-action="new">
<span class="fas fa-arrow-left"></span>
</button>

View file

@ -16,7 +16,7 @@
<button id="new-repo-column-modal"
data-view-mode="active"
class="btn btn-secondary manage-repo-column"
data-modal-url="<%= repository_columns_create_html_path(@repository) %>"
data-modal-url="<%= new_repository_repository_column_path(@repository) %>"
data-action="new">
<span class="fas fa-plus"></span>&nbsp;<%=t 'libraries.manange_modal_column_index.add_column' %>
</button>

View file

@ -577,14 +577,6 @@ Rails.application.routes.draw do
get 'repository_columns/:id/destroy_html',
to: 'repository_columns#destroy_html',
as: 'columns_destroy_html'
get 'index_html',
to: 'repository_columns#index_html',
as: 'columns_index_html',
defaults: { format: 'json' }
get 'create_html',
to: 'repository_columns#create_html',
as: 'columns_create_html',
defaults: { format: 'json' }
get 'available_columns',
to: 'repository_columns#available_columns',
as: 'available_columns',
@ -592,7 +584,7 @@ Rails.application.routes.draw do
get :table_toolbar
get :status
resources :repository_columns, only: %i(create edit update destroy)
resources :repository_columns, only: %i(index new edit destroy)
resources :repository_rows, only: %i(create show update) do
member do
get :assigned_task_list
@ -609,22 +601,22 @@ Rails.application.routes.draw do
post 'import_records'
end
namespace :repository_columns do
resources :text_columns, only: %i(create update destroy)
resources :number_columns, only: %i(create update destroy)
resources :asset_columns, only: %i(create update destroy)
resources :date_columns, only: %i(create update destroy)
resources :date_time_columns, only: %i(create update destroy)
resources :list_columns, only: %i(create update destroy) do
resources :text_columns, only: %i(create update)
resources :number_columns, only: %i(create update)
resources :asset_columns, only: %i(create update)
resources :date_columns, only: %i(create update)
resources :date_time_columns, only: %i(create update)
resources :list_columns, only: %i(create update) do
member do
get 'items'
end
end
resources :checklist_columns, only: %i(create update destroy) do
resources :checklist_columns, only: %i(create update) do
member do
get 'items'
end
end
resources :status_columns, only: %i(create update destroy) do
resources :status_columns, only: %i(create update) do
member do
get 'items'
end

View file

@ -161,74 +161,4 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do
end
end
end
describe 'DELETE repository_status_column, #delete' do
let(:action) { delete :destroy, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
context 'when column deleted' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be deleted fails' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
end

View file

@ -161,74 +161,4 @@ RSpec.describe RepositoryColumns::ChecklistColumnsController, type: :controller
end
end
end
describe 'DELETE repository_checkbox_column, #delete' do
let(:action) { delete :destroy, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
context 'when column deleted' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be deleted fails' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
end

View file

@ -162,74 +162,4 @@ RSpec.describe RepositoryColumns::DateTimeColumnsController, type: :controller d
end
end
end
describe 'DELETE repository_date_time_column, #delete' do
let(:action) { delete :destroy, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
context 'when column deleted' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be deleted fails' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
end

View file

@ -161,74 +161,4 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do
end
end
end
describe 'DELETE repository_list_column, #delete' do
let(:action) { delete :destroy, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
context 'when column deleted' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be deleted fails' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
end

View file

@ -162,76 +162,6 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do
end
end
describe 'DELETE repository_status_column, #delete' do
let(:action) { delete :destroy, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
context 'when column deleted' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be deleted fails' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
describe 'GET repository_status_column, #items' do
let(:action) { get :items, params: params }

View file

@ -161,74 +161,4 @@ RSpec.describe RepositoryColumns::TextColumnsController, type: :controller do
end
end
end
describe 'DELETE repository_text_column, #delete' do
let(:action) { delete :destroy, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
context 'when column deleted' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be deleted fails' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
end

View file

@ -13,58 +13,6 @@ describe RepositoryColumnsController, type: :controller do
create :repository_column, created_by: user, repository: repository
end
describe 'POST create' do
let(:action) { post :create, params: params, format: :json }
let(:params) do
{
repository_id: repository.id,
repository_column: {
name: 'repository_column',
data_type: 'RepositoryTextValue'
}
}
end
it 'calls create activity for creating inventory column' do
expect(Activities::CreateActivityService)
.to(receive(:call)
.with(hash_including(activity_type: :create_column_inventory)))
action
end
it 'adds activity in DB' do
expect { action }
.to(change { Activity.count })
end
end
describe 'PUT update' do
let(:action) { put :update, params: params, format: :json }
let(:params) do
{
id: repository_column.id,
repository_id: repository.id,
repository_column: {
name: 'new_repository_column'
}
}
end
it 'calls create activity for editing intentory column' do
expect(Activities::CreateActivityService)
.to(receive(:call)
.with(hash_including(activity_type: :edit_column_inventory)))
action
end
it 'adds activity in DB' do
expect { action }
.to(change { Activity.count })
end
end
describe 'DELETE destroy' do
let(:action) { delete :destroy, params: params, format: :json }
let(:params) do

View file

@ -1,73 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryColumns::DeleteColumnService do
let(:user) { create :user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:team) { create :team }
let(:repository) { create :repository, team: team }
let(:repository_column) { create :repository_column, :list_type }
context 'when deletes column' do
let(:service_call) do
RepositoryColumns::DeleteColumnService.call(user: user, team: team, column: repository_column)
end
it 'removes RepositoryColumn record' do
repository_column
expect { service_call }.to change { RepositoryColumn.count }.by(-1)
end
it 'adds Activity record' do
expect { service_call }.to(change { Activity.count }.by(1))
end
context 'when RepositoryColumn has RepositoryListItems' do
before do
3.times do
create(:repository_list_item, repository_column: repository_column)
end
end
it 'removes RepositoryListItem records as well' do
expect { service_call }.to(change { RepositoryListItem.count }.by(-3))
end
end
context 'when RepositoryColumn has RepositoryStatusItems' do
before do
3.times do
create(:repository_status_item, repository_column: repository_column)
end
end
it 'removes RepositoryStatusItem records as well' do
expect { service_call }.to(change { RepositoryStatusItem.count }.by(-3))
end
end
end
context 'when column cannot be deleted' do
let(:service_call) do
RepositoryColumns::DeleteColumnService.call(user: user, team: team, column: repository_column)
end
before do
allow_any_instance_of(RepositoryColumn).to receive(:destroy!).and_raise(ActiveRecord::RecordNotDestroyed)
end
it 'returns errors' do
expect(service_call.errors).to have_key(:repository_column)
end
it 'returns succeed false' do
expect(service_call.succeed?).to be_falsey
end
it 'does not add Activity record' do
expect { service_call }.not_to(change { Activity.count })
end
end
end