debian-mirror-gitlab/spec/controllers/projects/labels_controller_spec.rb

304 lines
11 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::LabelsController do
2021-01-03 14:25:43 +05:30
let_it_be(:group) { create(:group) }
let_it_be(:project, reload: true) { create(:project, namespace: group) }
let_it_be(:user) { create(:user) }
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2016-11-03 12:29:30 +05:30
sign_in(user)
end
describe 'GET #index' do
2021-01-03 14:25:43 +05:30
let_it_be(:label_1) { create(:label, project: project, priority: 1, title: 'Label 1') }
let_it_be(:label_2) { create(:label, project: project, priority: 3, title: 'Label 2') }
let_it_be(:label_3) { create(:label, project: project, priority: 1, title: 'Label 3') }
let_it_be(:label_4) { create(:label, project: project, title: 'Label 4') }
let_it_be(:label_5) { create(:label, project: project, title: 'Label 5') }
let_it_be(:group_label_1) { create(:group_label, group: group, title: 'Group Label 1') }
let_it_be(:group_label_2) { create(:group_label, group: group, title: 'Group Label 2') }
let_it_be(:group_label_3) { create(:group_label, group: group, title: 'Group Label 3') }
let_it_be(:group_label_4) { create(:group_label, group: group, title: 'Group Label 4') }
let_it_be(:group_labels) { [group_label_3, group_label_4]}
let_it_be(:project_labels) { [label_4, label_5]}
let_it_be(:group_priority_labels) { [group_label_1, group_label_2]}
let_it_be(:project_priority_labels) { [label_1, label_2, label_3]}
2016-11-03 12:29:30 +05:30
before do
create(:label_priority, project: project, label: group_label_1, priority: 3)
create(:label_priority, project: project, label: group_label_2, priority: 1)
end
context '@prioritized_labels' do
2016-11-03 12:29:30 +05:30
before do
list_labels
end
2016-11-03 12:29:30 +05:30
it 'does not include labels without priority' do
list_labels
expect(assigns(:prioritized_labels)).not_to include(group_label_3, group_label_4, label_4, label_5)
end
it 'is sorted by priority, then label title' do
2016-11-03 12:29:30 +05:30
expect(assigns(:prioritized_labels)).to eq [group_label_2, label_1, label_3, group_label_1, label_2]
end
end
context '@labels' do
2016-11-03 12:29:30 +05:30
it 'is sorted by label title' do
list_labels
2016-11-03 12:29:30 +05:30
expect(assigns(:labels)).to eq [group_label_3, group_label_4, label_4, label_5]
end
2016-11-03 12:29:30 +05:30
it 'does not include labels with priority' do
list_labels
expect(assigns(:labels)).not_to include(group_label_2, label_1, label_3, group_label_1, label_2)
end
it 'does not include group labels when project does not belong to a group' do
2021-04-29 21:17:54 +05:30
project.update!(namespace: create(:namespace))
2016-11-03 12:29:30 +05:30
list_labels
expect(assigns(:labels)).not_to include(group_label_3, group_label_4)
end
end
2021-01-03 14:25:43 +05:30
context 'with subgroups' do
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:subgroup_label_1) { create(:group_label, group: subgroup, title: 'subgroup_label_1') }
let_it_be(:subgroup_label_2) { create(:group_label, group: subgroup, title: 'subgroup_label_2') }
before do
project.update!(namespace: subgroup)
subgroup.add_owner(user)
create(:label_priority, project: project, label: subgroup_label_2, priority: 1)
end
2021-03-08 18:12:59 +05:30
it 'returns ancestor group labels', :aggregate_failures do
params = { namespace_id: project.namespace.to_param, project_id: project }
get :index, params: params
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
expect(assigns(:labels)).to match_array([subgroup_label_1] + group_labels + project_labels)
expect(assigns(:prioritized_labels)).to match_array([subgroup_label_2] + group_priority_labels + project_priority_labels)
2021-01-03 14:25:43 +05:30
end
end
2021-04-29 21:17:54 +05:30
context 'with views rendered' do
render_views
before do
list_labels
end
it 'avoids N+1 queries' do
control = ActiveRecord::QueryRecorder.new(skip_cached: false) { list_labels }
create_list(:label, 3, project: project)
create_list(:group_label, 3, group: group)
# some n+1 queries still exist
# calls to get max project authorization access level
expect { list_labels }.not_to exceed_all_query_limit(control.count).with_threshold(25)
expect(assigns(:labels).count).to eq(10)
end
end
2016-11-03 12:29:30 +05:30
def list_labels
2019-02-15 15:39:39 +05:30
get :index, params: { namespace_id: project.namespace.to_param, project_id: project }
2016-11-03 12:29:30 +05:30
end
end
describe 'POST #generate' do
context 'personal project' do
2021-01-03 14:25:43 +05:30
let_it_be(:personal_project) { create(:project, namespace: user.namespace) }
2016-11-03 12:29:30 +05:30
it 'creates labels' do
2019-02-15 15:39:39 +05:30
post :generate, params: { namespace_id: personal_project.namespace.to_param, project_id: personal_project }
2016-11-03 12:29:30 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2016-11-03 12:29:30 +05:30
end
end
context 'project belonging to a group' do
it 'creates labels' do
2019-02-15 15:39:39 +05:30
post :generate, params: { namespace_id: project.namespace.to_param, project_id: project }
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST #toggle_subscription' do
it 'allows user to toggle subscription on project labels' do
label = create(:label, project: project)
toggle_subscription(label)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
it 'allows user to toggle subscription on group labels' do
group_label = create(:group_label, group: group)
toggle_subscription(group_label)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
def toggle_subscription(label)
2019-02-15 15:39:39 +05:30
post :toggle_subscription, params: { namespace_id: project.namespace.to_param, project_id: project, id: label.to_param }
2017-08-17 22:00:37 +05:30
end
end
describe 'POST #promote' do
2021-01-03 14:25:43 +05:30
let_it_be(:promoted_label_name) { "Promoted Label" }
let_it_be(:label_1) { create(:label, title: promoted_label_name, project: project) }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
context 'not group reporters' do
2017-08-17 22:00:37 +05:30
it 'denies access' do
2019-02-15 15:39:39 +05:30
post :promote, params: { namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'group reporter' do
2017-08-17 22:00:37 +05:30
before do
2017-09-10 17:25:29 +05:30
group.add_reporter(user)
2017-08-17 22:00:37 +05:30
end
it 'gives access' do
2019-02-15 15:39:39 +05:30
post :promote, params: { namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param }
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(namespace_project_labels_path)
end
it 'promotes the label' do
2019-02-15 15:39:39 +05:30
post :promote, params: { namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param }
2017-08-17 22:00:37 +05:30
expect(Label.where(id: label_1.id)).to be_empty
expect(GroupLabel.find_by(title: promoted_label_name)).not_to be_nil
end
2018-10-15 14:42:47 +05:30
it 'renders label name without parsing it as HTML' do
label_1.update!(name: 'CCC<img src=x onerror=alert(document.domain)>')
2019-02-15 15:39:39 +05:30
post :promote, params: { namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param }
2018-10-15 14:42:47 +05:30
expect(flash[:notice]).to eq("CCC&lt;img src=x onerror=alert(document.domain)&gt; promoted to <a href=\"#{group_labels_path(project.group)}\"><u>group label</u></a>.")
end
2017-08-17 22:00:37 +05:30
context 'service raising InvalidRecord' do
before do
expect_any_instance_of(Labels::PromoteService).to receive(:execute) do |label|
2021-06-08 01:23:25 +05:30
raise ActiveRecord::RecordInvalid, label_1
2017-08-17 22:00:37 +05:30
end
end
it 'returns to label list' do
2019-02-15 15:39:39 +05:30
post :promote, params: { namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param }
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(namespace_project_labels_path)
end
end
end
end
describe '#ensure_canonical_path' do
before do
sign_in(user)
end
context 'for a GET request' do
context 'when requesting the canonical path' do
context 'non-show path' do
context 'with exactly matching casing' do
it 'does not redirect' do
2019-02-15 15:39:39 +05:30
get :index, params: { namespace_id: project.namespace, project_id: project.to_param }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).not_to have_gitlab_http_status(:moved_permanently)
2017-08-17 22:00:37 +05:30
end
end
context 'with different casing' do
it 'redirects to the correct casing' do
2019-02-15 15:39:39 +05:30
get :index, params: { namespace_id: project.namespace, project_id: project.to_param.upcase }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(response).to redirect_to(project_labels_path(project))
2017-08-17 22:00:37 +05:30
expect(controller).not_to set_flash[:notice]
end
end
end
end
context 'when requesting a redirected path' do
2021-04-29 21:17:54 +05:30
let_it_be(:redirect_route) { project.redirect_routes.create!(path: project.full_path + 'old') }
2017-08-17 22:00:37 +05:30
it 'redirects to the canonical path' do
2019-02-15 15:39:39 +05:30
get :index, params: { namespace_id: project.namespace, project_id: project.to_param + 'old' }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(response).to redirect_to(project_labels_path(project))
2017-08-17 22:00:37 +05:30
expect(controller).to set_flash[:notice].to(project_moved_message(redirect_route, project))
end
2019-12-26 22:10:19 +05:30
context 'with an AJAX request' do
it 'redirects to the canonical path but does not set flash message' do
get :index, params: { namespace_id: project.namespace, project_id: project.to_param + 'old' }, xhr: true
expect(response).to redirect_to(project_labels_path(project))
expect(controller).not_to set_flash[:notice]
end
end
context 'with JSON format' do
it 'redirects to the canonical path but does not set flash message' do
get :index, params: { namespace_id: project.namespace, project_id: project.to_param + 'old' }, format: :json
expect(response).to redirect_to(project_labels_path(project, format: :json))
expect(controller).not_to set_flash[:notice]
end
end
2017-08-17 22:00:37 +05:30
end
end
end
context 'for a non-GET request' do
context 'when requesting the canonical path with different casing' do
it 'does not 404' do
2019-02-15 15:39:39 +05:30
post :generate, params: { namespace_id: project.namespace, project_id: project }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).not_to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
it 'does not redirect to the correct casing' do
2019-02-15 15:39:39 +05:30
post :generate, params: { namespace_id: project.namespace, project_id: project }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).not_to have_gitlab_http_status(:moved_permanently)
2017-08-17 22:00:37 +05:30
end
end
context 'when requesting a redirected path' do
2021-04-29 21:17:54 +05:30
let_it_be(:redirect_route) { project.redirect_routes.create!(path: project.full_path + 'old') }
2017-08-17 22:00:37 +05:30
it 'returns not found' do
2019-02-15 15:39:39 +05:30
post :generate, params: { namespace_id: project.namespace, project_id: project.to_param + 'old' }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
end
end
def project_moved_message(redirect_route, project)
"Project '#{redirect_route.path}' was moved to '#{project.full_path}'. Please update any links and bookmarks that may still have the old path."
end
end