Add endpoint to remove project boards

parent 27326e61
......@@ -2,7 +2,8 @@ class Projects::BoardsController < Projects::ApplicationController
include IssuableCollections
before_action :authorize_read_board!, only: [:index, :show]
before_action :authorize_admin_board!, only: [:create, :update]
before_action :authorize_admin_board!, only: [:create, :update, :destroy]
before_action :find_board, only: [:show, :update, :destroy]
def index
@boards = ::Boards::ListService.new(project, current_user).execute
......@@ -16,8 +17,6 @@ class Projects::BoardsController < Projects::ApplicationController
end
def show
@board = project.boards.find(params[:id])
respond_to do |format|
format.html
format.json do
......@@ -41,22 +40,31 @@ class Projects::BoardsController < Projects::ApplicationController
end
def update
board = project.boards.find(params[:id])
service = ::Boards::UpdateService.new(project, current_user, board_params)
service.execute(board)
service.execute(@board)
respond_to do |format|
format.json do
if board.valid?
render json: serialize_as_json(board)
if @board.valid?
render json: serialize_as_json(@board)
else
render json: board.errors, status: :unprocessable_entity
render json: @board.errors, status: :unprocessable_entity
end
end
end
end
def destroy
service = ::Boards::DestroyService.new(project, current_user)
if service.execute(@board)
head :ok
else
head :unprocessable_entity
end
end
private
def authorize_admin_board!
......@@ -71,6 +79,10 @@ class Projects::BoardsController < Projects::ApplicationController
params.require(:board).permit(:name)
end
def find_board
@board = project.boards.find(params[:id])
end
def serialize_as_json(resource)
resource.as_json(only: [:id])
end
......
......@@ -459,7 +459,7 @@ resources :namespaces, path: '/', constraints: { id: /[a-zA-Z.0-9_\-]+/ }, only:
end
end
resources :boards, only: [:index, :show, :create, :update] do
resources :boards, only: [:index, :show, :create, :update, :destroy] do
scope module: :boards do
resources :issues, only: [:update]
......
......@@ -237,4 +237,48 @@ describe Projects::BoardsController do
format: :json
end
end
describe 'DELETE destroy' do
let!(:board) { create(:board, project: project) }
context 'with valid board id' do
it 'returns a successful 200 response' do
remove_board board: board
expect(response).to have_http_status(200)
end
it 'removes board from project' do
expect { remove_board board: board }.to change(project.boards, :size).by(-1)
end
end
context 'with invalid board id' do
it 'returns a not found 404 response' do
remove_board board: 999
expect(response).to have_http_status(404)
end
end
context 'with unauthorized user' do
before do
allow(Ability).to receive(:allowed?).with(user, :read_project, project).and_return(true)
allow(Ability).to receive(:allowed?).with(user, :admin_board, project).and_return(false)
end
it 'returns a not found 404 response' do
remove_board board: board
expect(response).to have_http_status(404)
end
end
def remove_board(board:)
delete :destroy, namespace_id: project.namespace.to_param,
project_id: project.to_param,
id: board.to_param,
format: :json
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment