mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-08 04:47:22 +08:00
Refactor FileColumnController
This commit is contained in:
parent
1720623665
commit
28d7a4f284
3 changed files with 287 additions and 0 deletions
|
@ -0,0 +1,52 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module RepositoryColumns
|
||||||
|
class AssetColumnsController < BaseColumnsController
|
||||||
|
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::CreateStatusColumnService
|
||||||
|
.call(user: current_user, repository: @repository, team: current_team, params: repository_column_params)
|
||||||
|
|
||||||
|
if service.succeed?
|
||||||
|
render json: service.column, status: :created
|
||||||
|
else
|
||||||
|
render json: service.errors, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
service = RepositoryColumns::UpdateStatusColumnService
|
||||||
|
.call(user: current_user,
|
||||||
|
team: current_team,
|
||||||
|
column: @repository_column,
|
||||||
|
params: repository_column_params)
|
||||||
|
|
||||||
|
if service.succeed?
|
||||||
|
render json: service.column, status: :ok
|
||||||
|
else
|
||||||
|
render json: service.errors, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
service = RepositoryColumns::DeleteStatusColumnService
|
||||||
|
.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
|
||||||
|
params.require(:repository_column).permit(:name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -585,6 +585,7 @@ Rails.application.routes.draw do
|
||||||
resources :date_columns, only: %i(create update destroy)
|
resources :date_columns, only: %i(create update destroy)
|
||||||
resources :status_columns, only: %i(create update destroy)
|
resources :status_columns, only: %i(create update destroy)
|
||||||
resources :list_columns, only: %i(create update destroy)
|
resources :list_columns, only: %i(create update destroy)
|
||||||
|
resources :asset_columns, only: %i(create update destroy)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,234 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do
|
||||||
|
login_user
|
||||||
|
|
||||||
|
let(:user) { subject.current_user }
|
||||||
|
let(:team) { create :team, created_by: user }
|
||||||
|
let!(:user_team) { create :user_team, :admin, user: user, team: team }
|
||||||
|
let(:repository) { create :repository, created_by: user, team: team }
|
||||||
|
let(:repository_column) { create(:repository_column, :asset_type, repository: repository) }
|
||||||
|
|
||||||
|
describe 'POST repository_asset_columns, #create' do
|
||||||
|
let(:action) { post :create, params: params }
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
repository_id: repository.id,
|
||||||
|
repository_column: {
|
||||||
|
name: 'name'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
service = double('success_service')
|
||||||
|
allow(service).to(receive(:succeed?)).and_return(true)
|
||||||
|
allow(service).to(receive(:column)).and_return(:repository_column)
|
||||||
|
|
||||||
|
allow_any_instance_of(RepositoryColumns::CreateStatusColumnService).to(receive(:call)).and_return(service)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when columnd is created' do
|
||||||
|
it 'respons with status 201' do
|
||||||
|
action
|
||||||
|
|
||||||
|
expect(response).to(have_http_status(201))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when repository is not found' do
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
repository_id: -1,
|
||||||
|
repository_column: {
|
||||||
|
name: 'name'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 created' 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::CreateStatusColumnService).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 'PUT repository_status_column, #update' do
|
||||||
|
let(:action) { patch :update, params: params }
|
||||||
|
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
repository_id: repository.id,
|
||||||
|
id: repository_column.id,
|
||||||
|
repository_column: {
|
||||||
|
name: 'name'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
service = double('success_service')
|
||||||
|
allow(service).to(receive(:succeed?)).and_return(true)
|
||||||
|
allow(service).to(receive(:column)).and_return(:repository_column)
|
||||||
|
|
||||||
|
allow_any_instance_of(RepositoryColumns::UpdateStatusColumnService).to(receive(:call)).and_return(service)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when columnd is updated' 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,
|
||||||
|
repository_column: {
|
||||||
|
name: 'name'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 updated' 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::UpdateStatusColumnService).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 '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::DeleteStatusColumnService).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::DeleteStatusColumnService).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
|
Loading…
Add table
Reference in a new issue