Update endpoints to handle with board list changes

parent 5de4fba6
...@@ -5,11 +5,11 @@ module Projects ...@@ -5,11 +5,11 @@ module Projects
before_action :authorize_read_list!, only: [:index] before_action :authorize_read_list!, only: [:index]
def index def index
render json: serialize_as_json(project.board.lists) render json: serialize_as_json(board.lists)
end end
def create def create
list = ::Boards::Lists::CreateService.new(project, current_user, list_params).execute list = ::Boards::Lists::CreateService.new(project, current_user, list_params).execute(board)
if list.valid? if list.valid?
render json: serialize_as_json(list) render json: serialize_as_json(list)
...@@ -19,7 +19,7 @@ module Projects ...@@ -19,7 +19,7 @@ module Projects
end end
def update def update
list = project.board.lists.movable.find(params[:id]) list = board.lists.movable.find(params[:id])
service = ::Boards::Lists::MoveService.new(project, current_user, move_params) service = ::Boards::Lists::MoveService.new(project, current_user, move_params)
if service.execute(list) if service.execute(list)
...@@ -30,8 +30,8 @@ module Projects ...@@ -30,8 +30,8 @@ module Projects
end end
def destroy def destroy
list = project.board.lists.destroyable.find(params[:id]) list = board.lists.destroyable.find(params[:id])
service = ::Boards::Lists::DestroyService.new(project, current_user, params) service = ::Boards::Lists::DestroyService.new(project, current_user)
if service.execute(list) if service.execute(list)
head :ok head :ok
...@@ -43,8 +43,8 @@ module Projects ...@@ -43,8 +43,8 @@ module Projects
def generate def generate
service = ::Boards::Lists::GenerateService.new(project, current_user) service = ::Boards::Lists::GenerateService.new(project, current_user)
if service.execute if service.execute(board)
render json: serialize_as_json(project.board.lists.movable) render json: serialize_as_json(board.lists.movable)
else else
head :unprocessable_entity head :unprocessable_entity
end end
...@@ -60,6 +60,10 @@ module Projects ...@@ -60,6 +60,10 @@ module Projects
return render_403 unless can?(current_user, :read_list, project) return render_403 unless can?(current_user, :read_list, project)
end end
def board
@board ||= project.boards.find(params[:board_id])
end
def list_params def list_params
params.require(:list).permit(:label_id) params.require(:list).permit(:label_id)
end end
......
...@@ -13,7 +13,7 @@ describe Projects::Boards::ListsController do ...@@ -13,7 +13,7 @@ describe Projects::Boards::ListsController do
describe 'GET index' do describe 'GET index' do
it 'returns a successful 200 response' do it 'returns a successful 200 response' do
read_board_list user: user read_board_list user: user, board: board
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(response.content_type).to eq 'application/json' expect(response.content_type).to eq 'application/json'
...@@ -22,7 +22,7 @@ describe Projects::Boards::ListsController do ...@@ -22,7 +22,7 @@ describe Projects::Boards::ListsController do
it 'returns a list of board lists' do it 'returns a list of board lists' do
create(:list, board: board) create(:list, board: board)
read_board_list user: user read_board_list user: user, board: board
parsed_response = JSON.parse(response.body) parsed_response = JSON.parse(response.body)
...@@ -37,17 +37,18 @@ describe Projects::Boards::ListsController do ...@@ -37,17 +37,18 @@ describe Projects::Boards::ListsController do
end end
it 'returns a forbidden 403 response' do it 'returns a forbidden 403 response' do
read_board_list user: user read_board_list user: user, board: board
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
end end
end end
def read_board_list(user:) def read_board_list(user:, board:)
sign_in(user) sign_in(user)
get :index, namespace_id: project.namespace.to_param, get :index, namespace_id: project.namespace.to_param,
project_id: project.to_param, project_id: project.to_param,
board_id: board.to_param,
format: :json format: :json
end end
end end
...@@ -57,13 +58,13 @@ describe Projects::Boards::ListsController do ...@@ -57,13 +58,13 @@ describe Projects::Boards::ListsController do
let(:label) { create(:label, project: project, name: 'Development') } let(:label) { create(:label, project: project, name: 'Development') }
it 'returns a successful 200 response' do it 'returns a successful 200 response' do
create_board_list user: user, label_id: label.id create_board_list user: user, board: board, label_id: label.id
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'returns the created list' do it 'returns the created list' do
create_board_list user: user, label_id: label.id create_board_list user: user, board: board, label_id: label.id
expect(response).to match_response_schema('list') expect(response).to match_response_schema('list')
end end
...@@ -72,7 +73,7 @@ describe Projects::Boards::ListsController do ...@@ -72,7 +73,7 @@ describe Projects::Boards::ListsController do
context 'with invalid params' do context 'with invalid params' do
context 'when label is nil' do context 'when label is nil' do
it 'returns a not found 404 response' do it 'returns a not found 404 response' do
create_board_list user: user, label_id: nil create_board_list user: user, board: board, label_id: nil
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
...@@ -82,7 +83,7 @@ describe Projects::Boards::ListsController do ...@@ -82,7 +83,7 @@ describe Projects::Boards::ListsController do
it 'returns a not found 404 response' do it 'returns a not found 404 response' do
label = create(:label, name: 'Development') label = create(:label, name: 'Development')
create_board_list user: user, label_id: label.id create_board_list user: user, board: board, label_id: label.id
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
...@@ -93,17 +94,18 @@ describe Projects::Boards::ListsController do ...@@ -93,17 +94,18 @@ describe Projects::Boards::ListsController do
it 'returns a forbidden 403 response' do it 'returns a forbidden 403 response' do
label = create(:label, project: project, name: 'Development') label = create(:label, project: project, name: 'Development')
create_board_list user: guest, label_id: label.id create_board_list user: guest, board: board, label_id: label.id
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
end end
end end
def create_board_list(user:, label_id:) def create_board_list(user:, board:, label_id:)
sign_in(user) sign_in(user)
post :create, namespace_id: project.namespace.to_param, post :create, namespace_id: project.namespace.to_param,
project_id: project.to_param, project_id: project.to_param,
board_id: board.to_param,
list: { label_id: label_id }, list: { label_id: label_id },
format: :json format: :json
end end
...@@ -115,13 +117,13 @@ describe Projects::Boards::ListsController do ...@@ -115,13 +117,13 @@ describe Projects::Boards::ListsController do
context 'with valid position' do context 'with valid position' do
it 'returns a successful 200 response' do it 'returns a successful 200 response' do
move user: user, list: planning, position: 1 move user: user, board: board, list: planning, position: 1
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'moves the list to the desired position' do it 'moves the list to the desired position' do
move user: user, list: planning, position: 1 move user: user, board: board, list: planning, position: 1
expect(planning.reload.position).to eq 1 expect(planning.reload.position).to eq 1
end end
...@@ -129,7 +131,7 @@ describe Projects::Boards::ListsController do ...@@ -129,7 +131,7 @@ describe Projects::Boards::ListsController do
context 'with invalid position' do context 'with invalid position' do
it 'returns an unprocessable entity 422 response' do it 'returns an unprocessable entity 422 response' do
move user: user, list: planning, position: 6 move user: user, board: board, list: planning, position: 6
expect(response).to have_http_status(422) expect(response).to have_http_status(422)
end end
...@@ -137,7 +139,7 @@ describe Projects::Boards::ListsController do ...@@ -137,7 +139,7 @@ describe Projects::Boards::ListsController do
context 'with invalid list id' do context 'with invalid list id' do
it 'returns a not found 404 response' do it 'returns a not found 404 response' do
move user: user, list: 999, position: 1 move user: user, board: board, list: 999, position: 1
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
...@@ -145,17 +147,18 @@ describe Projects::Boards::ListsController do ...@@ -145,17 +147,18 @@ describe Projects::Boards::ListsController do
context 'with unauthorized user' do context 'with unauthorized user' do
it 'returns a forbidden 403 response' do it 'returns a forbidden 403 response' do
move user: guest, list: planning, position: 6 move user: guest, board: board, list: planning, position: 6
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
end end
end end
def move(user:, list:, position:) def move(user:, board:, list:, position:)
sign_in(user) sign_in(user)
patch :update, namespace_id: project.namespace.to_param, patch :update, namespace_id: project.namespace.to_param,
project_id: project.to_param, project_id: project.to_param,
board_id: board.to_param,
id: list.to_param, id: list.to_param,
list: { position: position }, list: { position: position },
format: :json format: :json
...@@ -167,19 +170,19 @@ describe Projects::Boards::ListsController do ...@@ -167,19 +170,19 @@ describe Projects::Boards::ListsController do
context 'with valid list id' do context 'with valid list id' do
it 'returns a successful 200 response' do it 'returns a successful 200 response' do
remove_board_list user: user, list: planning remove_board_list user: user, board: board, list: planning
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'removes list from board' do it 'removes list from board' do
expect { remove_board_list user: user, list: planning }.to change(board.lists, :size).by(-1) expect { remove_board_list user: user, board: board, list: planning }.to change(board.lists, :size).by(-1)
end end
end end
context 'with invalid list id' do context 'with invalid list id' do
it 'returns a not found 404 response' do it 'returns a not found 404 response' do
remove_board_list user: user, list: 999 remove_board_list user: user, board: board, list: 999
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
...@@ -187,17 +190,18 @@ describe Projects::Boards::ListsController do ...@@ -187,17 +190,18 @@ describe Projects::Boards::ListsController do
context 'with unauthorized user' do context 'with unauthorized user' do
it 'returns a forbidden 403 response' do it 'returns a forbidden 403 response' do
remove_board_list user: guest, list: planning remove_board_list user: guest, board: board, list: planning
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
end end
end end
def remove_board_list(user:, list:) def remove_board_list(user:, board:, list:)
sign_in(user) sign_in(user)
delete :destroy, namespace_id: project.namespace.to_param, delete :destroy, namespace_id: project.namespace.to_param,
project_id: project.to_param, project_id: project.to_param,
board_id: board.to_param,
id: list.to_param, id: list.to_param,
format: :json format: :json
end end
...@@ -206,13 +210,13 @@ describe Projects::Boards::ListsController do ...@@ -206,13 +210,13 @@ describe Projects::Boards::ListsController do
describe 'POST generate' do describe 'POST generate' do
context 'when board lists is empty' do context 'when board lists is empty' do
it 'returns a successful 200 response' do it 'returns a successful 200 response' do
generate_default_board_lists user: user generate_default_lists user: user, board: board
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'returns the defaults lists' do it 'returns the defaults lists' do
generate_default_board_lists user: user generate_default_lists user: user, board: board
expect(response).to match_response_schema('lists') expect(response).to match_response_schema('lists')
end end
...@@ -222,7 +226,7 @@ describe Projects::Boards::ListsController do ...@@ -222,7 +226,7 @@ describe Projects::Boards::ListsController do
it 'returns an unprocessable entity 422 response' do it 'returns an unprocessable entity 422 response' do
create(:list, board: board) create(:list, board: board)
generate_default_board_lists user: user generate_default_lists user: user, board: board
expect(response).to have_http_status(422) expect(response).to have_http_status(422)
end end
...@@ -230,17 +234,18 @@ describe Projects::Boards::ListsController do ...@@ -230,17 +234,18 @@ describe Projects::Boards::ListsController do
context 'with unauthorized user' do context 'with unauthorized user' do
it 'returns a forbidden 403 response' do it 'returns a forbidden 403 response' do
generate_default_board_lists user: guest generate_default_lists user: guest, board: board
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
end end
end end
def generate_default_board_lists(user:) def generate_default_lists(user:, board: board)
sign_in(user) sign_in(user)
post :generate, namespace_id: project.namespace.to_param, post :generate, namespace_id: project.namespace.to_param,
project_id: project.to_param, project_id: project.to_param,
board_id: board.to_param,
format: :json format: :json
end 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