Commit 02ed1a17 authored by Jarka Kadlecová's avatar Jarka Kadlecová

Improve code & add tests

parent 8d91df9d
......@@ -28,7 +28,6 @@ module EpicIssues
def issues_to_move
@issues_to_move ||= epic.epic_issues
.where('position >= ? AND position <= ? AND id != ?', from, to, epic_issue.id)
.order(:position)
end
def from
......@@ -45,8 +44,11 @@ module EpicIssues
def new_position
@new_position ||= begin
replacing_issue = epic.epic_issues.order(:position).limit(1).offset(params[:position])[0]
replacing_issue.position
replacing_issue = epic.epic_issues.order(:position).limit(1).offset(params[:position])
return replacing_issue[0].position if replacing_issue.present?
epic.epic_issues.order(:position).last.position
end
end
end
......
......@@ -16,6 +16,11 @@ module IssuableLinks
private
def relation_path(issue)
raise NotImplementedError
end
def reference(issue)
issue.to_reference(issuable.project)
end
......
......@@ -2,12 +2,22 @@ module API
class EpicIssues < Grape::API
before do
authenticate!
check_epics!
authorize_can_admin!
end
helpers do
def check_epics!
forbidden! unless ::License.feature_available?(:epics) # TODO: check for group feature instead
def authorize_can_admin!
forbidden! unless user_group.feature_available?(:epics) # TODO: check for group feature instead
authorize!(:admin_epic, epic)
forbidden! if link.epic != epic
end
def epic
@epic ||= user_group.epics.find_by(iid: params[:epic_iid])
end
def link
@link ||= EpicIssue.find(params[:epic_issue_id])
end
end
......@@ -24,15 +34,10 @@ module API
requires :position, type: Integer, desc: 'The new position of the issue in the epic (index starting with 0)'
end
put ':id/-/epics/:epic_iid/issues/:epic_issue_id' do
epic = user_group.epics.find_by(iid: params[:epic_iid])
authorize!(:admin_epic, epic)
link = EpicIssue.find(params[:epic_issue_id])
forbidden! if link.epic != epic
result = ::EpicIssues::UpdateService
.new(link, current_user, { position: params[:position].to_i }).execute
result = ::EpicIssues::UpdateService.new(link, current_user, { position: params[:position].to_i }).execute
# For now we return empty body
# The issues list in the correct order in body will be returned as part of #4250
render_api_error!({ error: "Issue could not be moved!" }, 400) unless result
end
end
......
......@@ -13,8 +13,8 @@ describe 'Epic Issues', :js do
let!(:epic_issues) do
[
create(:epic_issue, epic: epic, issue: public_issue),
create(:epic_issue, epic: epic, issue: private_issue)
create(:epic_issue, epic: epic, issue: public_issue, position: 1),
create(:epic_issue, epic: epic, issue: private_issue, position: 2)
]
end
......
......@@ -3,7 +3,7 @@ require 'spec_helper'
describe API::EpicIssues do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:project) { create(:project, group: group) }
let(:project) { create(:project, :public, group: group) }
let(:epic) { create(:epic, group: group) }
let(:issues) { create_list(:issue, 2, project: project) }
let!(:epic_issue1) { create(:epic_issue, epic: epic, issue: issues[0], position: 1) }
......@@ -11,53 +11,68 @@ describe API::EpicIssues do
describe 'PUT /groups/:id/-/epics/:epic_iid/issues/:epic_issue_id' do
let(:url) { "/groups/#{group.path}/-/epics/#{epic.iid}/issues/#{epic_issue1.id}?position=1" }
context 'when an error occurs' do
it 'returns 401 unauthorized error for non autehnticated user' do
put api(url)
expect(response).to have_gitlab_http_status(401)
end
it 'returns 404 not found error for user who does not have permissions to see the group' do
group.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
put api(url, user)
expect(response).to have_gitlab_http_status(404)
end
it 'returns 403 forbidden error for user who does can not move the issue' do
group.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
put api(url, user)
expect(response).to have_gitlab_http_status(404)
end
it 'returns 403 forbidden error for the link of another epic' do
context 'when epics feature is disabled' do
it 'returns 403 forbidden error' do
group.add_developer(user)
another_epic = create(:epic, group: group)
url = "/groups/#{group.path}/-/epics/#{another_epic.iid}/issues/#{epic_issue1.id}?position=1"
put api(url, user)
expect(response).to have_gitlab_http_status(403)
end
end
context 'when the request is correct' do
let!(:epic_issue2) { create(:epic_issue, epic: epic, issue: issues[1], position: 2) }
context 'when epics feature is enabled' do
before do
stub_licensed_features(epics: true)
group.add_developer(user)
put api(url, user)
end
it 'returns 200 status' do
expect(response).to have_gitlab_http_status(200)
context 'when an error occurs' do
it 'returns 401 unauthorized error for non authenticated user' do
put api(url)
expect(response).to have_gitlab_http_status(401)
end
it 'returns 404 not found error for a user without permissions to see the group' do
project.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
group.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
put api(url, user)
expect(response).to have_gitlab_http_status(404)
end
it 'returns 403 forbidden error for a user who can not move the issue' do
put api(url, user)
expect(response).to have_gitlab_http_status(403)
end
it 'returns 403 forbidden error for the link of another epic' do
group.add_developer(user)
another_epic = create(:epic, group: group)
url = "/groups/#{group.path}/-/epics/#{another_epic.iid}/issues/#{epic_issue1.id}?position=1"
put api(url, user)
expect(response).to have_gitlab_http_status(403)
end
end
it 'updates the positions values' do
expect(epic_issue1.reload.position).to eq(2)
expect(epic_issue2.reload.position).to eq(1)
context 'when the request is correct' do
let!(:epic_issue2) { create(:epic_issue, epic: epic, issue: issues[1], position: 2) }
before do
group.add_developer(user)
put api(url, user)
end
it 'returns 200 status' do
expect(response).to have_gitlab_http_status(200)
end
it 'updates the positions values' do
expect(epic_issue1.reload.position).to eq(2)
expect(epic_issue2.reload.position).to eq(1)
end
end
end
end
......
......@@ -187,7 +187,7 @@ describe EpicIssues::CreateService do
expect(subject).to eq(status: :success)
end
it 'creates 2 system notes for each assiues' do
it 'creates 2 system notes for each issue' do
expect { subject }.to change { Note.count }.from(0).to(4)
end
end
......
......@@ -104,5 +104,27 @@ describe EpicIssues::UpdateService do
end
end
end
context 'moving issues to the last position' do
context 'when index of the last possition is correct' do
before do
order_issue(epic_issue1, 3)
end
it 'orders issues correctly' do
expect(ordered_epics).to eq([epic_issue2, epic_issue3, epic_issue4, epic_issue1])
end
end
context 'when index of the last possition is too high' do
before do
order_issue(epic_issue1, 100)
end
it 'orders issues correctly' do
expect(ordered_epics).to eq([epic_issue2, epic_issue3, epic_issue4, epic_issue1])
end
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