debian-mirror-gitlab/spec/requests/api/group_labels_spec.rb

459 lines
17 KiB
Ruby
Raw Normal View History

2019-03-02 22:35:43 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe API::GroupLabels do
2021-03-11 19:13:27 +05:30
let_it_be(:valid_group_label_title_1) { 'Label foo & bar:subgroup::v.1' }
let_it_be(:valid_group_label_title_1_esc) { ERB::Util.url_encode(valid_group_label_title_1) }
let_it_be(:valid_group_label_title_2) { 'Bar & foo:subgroup::v.2' }
let_it_be(:valid_subgroup_label_title_1) { 'Support label foobar:sub::v.1' }
let_it_be(:valid_new_label_title) { 'New & foo:feature::v.3' }
2019-03-02 22:35:43 +05:30
let(:user) { create(:user) }
let(:group) { create(:group) }
2019-12-21 20:55:43 +05:30
let(:subgroup) { create(:group, parent: group) }
2019-03-02 22:35:43 +05:30
let!(:group_member) { create(:group_member, group: group, user: user) }
2021-03-11 19:13:27 +05:30
let!(:group_label1) { create(:group_label, title: valid_group_label_title_1, group: group) }
let!(:group_label2) { create(:group_label, title: valid_group_label_title_2, group: group) }
let!(:subgroup_label) { create(:group_label, title: valid_subgroup_label_title_1, group: subgroup) }
2019-03-02 22:35:43 +05:30
describe 'GET :id/labels' do
2021-01-29 00:20:46 +05:30
context 'get current group labels' do
let(:request) { get api("/groups/#{group.id}/labels", user) }
let(:expected_labels) { [group_label1.name, group_label2.name] }
2019-03-02 22:35:43 +05:30
2021-01-29 00:20:46 +05:30
it_behaves_like 'fetches labels'
2019-10-12 21:52:04 +05:30
2021-01-29 00:20:46 +05:30
context 'when search param is provided' do
let(:request) { get api("/groups/#{group.id}/labels?search=lab", user) }
let(:expected_labels) { [group_label1.name] }
2019-10-12 21:52:04 +05:30
2021-01-29 00:20:46 +05:30
it_behaves_like 'fetches labels'
2021-09-30 23:02:18 +05:30
context 'and is subscribed' do
before do
group_label1.subscribe(user)
end
it 'returns true' do
get api("/groups/#{group.id}/labels?search=#{group_label1.name}", user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response[0]['subscribed']).to be true
end
end
context 'and is unsubscribed' do
before do
group_label1.unsubscribe(user)
end
it 'returns false' do
get api("/groups/#{group.id}/labels?search=#{group_label1.name}", user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response[0]['subscribed']).to be false
end
end
2019-10-12 21:52:04 +05:30
end
2019-03-02 22:35:43 +05:30
2021-01-29 00:20:46 +05:30
context 'when the with_counts parameter is set' do
it 'includes counts in the response' do
get api("/groups/#{group.id}/labels", user), params: { with_counts: true }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response).to all(match_schema('public_api/v4/labels/label_with_counts'))
expect(json_response.size).to eq(2)
expect(json_response.map { |r| r['open_issues_count'] }).to contain_exactly(0, 0)
end
end
context 'when include_descendant_groups param is provided' do
let!(:project) { create(:project, group: group) }
let!(:project_label1) { create(:label, title: 'project-label1', project: project, priority: 3) }
let!(:project_label2) { create(:label, title: 'project-bug', project: project) }
let(:request) { get api("/groups/#{group.id}/labels", user), params: { include_descendant_groups: true } }
let(:expected_labels) { [group_label1.name, group_label2.name, subgroup_label.name] }
it_behaves_like 'fetches labels'
context 'when search param is provided' do
let(:request) { get api("/groups/#{group.id}/labels", user), params: { search: 'lab', include_descendant_groups: true } }
let(:expected_labels) { [group_label1.name, subgroup_label.name] }
it_behaves_like 'fetches labels'
end
context 'when only_group_labels param is false' do
let(:request) { get api("/groups/#{group.id}/labels", user), params: { include_descendant_groups: true, only_group_labels: false } }
let(:expected_labels) { [group_label1.name, group_label2.name, subgroup_label.name, project_label1.name, project_label2.name] }
it_behaves_like 'fetches labels'
context 'when search param is provided' do
let(:request) { get api("/groups/#{group.id}/labels", user), params: { search: 'lab', include_descendant_groups: true, only_group_labels: false } }
let(:expected_labels) { [group_label1.name, subgroup_label.name, project_label1.name] }
it_behaves_like 'fetches labels'
end
end
2019-12-21 20:55:43 +05:30
end
end
2021-01-29 00:20:46 +05:30
describe 'with subgroup labels' do
context 'when the include_ancestor_groups parameter is not set' do
let(:request) { get api("/groups/#{subgroup.id}/labels", user) }
let(:expected_labels) { [group_label1.name, group_label2.name, subgroup_label.name] }
it_behaves_like 'fetches labels'
context 'when search param is provided' do
let(:request) { get api("/groups/#{subgroup.id}/labels?search=lab", user) }
let(:expected_labels) { [group_label1.name, subgroup_label.name] }
it_behaves_like 'fetches labels'
end
end
context 'when the include_ancestor_groups parameter is set to false' do
let(:request) { get api("/groups/#{subgroup.id}/labels", user), params: { include_ancestor_groups: false } }
let(:expected_labels) { [subgroup_label.name] }
it_behaves_like 'fetches labels'
context 'when search param is provided' do
let(:request) { get api("/groups/#{subgroup.id}/labels?search=lab", user), params: { include_ancestor_groups: false } }
let(:expected_labels) { [subgroup_label.name] }
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
it_behaves_like 'fetches labels'
end
2019-12-21 20:55:43 +05:30
end
end
end
describe 'GET :id/labels/:label_id' do
it 'returns a single label for the group' do
2021-03-11 19:13:27 +05:30
get api("/groups/#{group.id}/labels/#{valid_group_label_title_1_esc}", user)
2019-12-21 20:55:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
expect(json_response['name']).to eq(group_label1.name)
2022-05-07 20:08:51 +05:30
expect(json_response['color']).to be_color(group_label1.color)
2019-12-21 20:55:43 +05:30
expect(json_response['description']).to eq(group_label1.description)
end
end
2019-03-02 22:35:43 +05:30
describe 'POST /groups/:id/labels' do
it 'returns created label when all params are given' do
post api("/groups/#{group.id}/labels", user),
params: {
2021-03-11 19:13:27 +05:30
name: valid_new_label_title,
2019-03-02 22:35:43 +05:30
color: '#FFAABB',
description: 'test'
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2021-03-11 19:13:27 +05:30
expect(json_response['name']).to eq(valid_new_label_title)
2022-05-07 20:08:51 +05:30
expect(json_response['color']).to be_color('#FFAABB')
2019-03-02 22:35:43 +05:30
expect(json_response['description']).to eq('test')
end
it 'returns created label when only required params are given' do
post api("/groups/#{group.id}/labels", user),
params: {
2021-03-11 19:13:27 +05:30
name: valid_new_label_title,
2019-03-02 22:35:43 +05:30
color: '#FFAABB'
}
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:created)
2021-03-11 19:13:27 +05:30
expect(json_response['name']).to eq(valid_new_label_title)
2022-05-07 20:08:51 +05:30
expect(json_response['color']).to be_color('#FFAABB')
2019-03-02 22:35:43 +05:30
expect(json_response['description']).to be_nil
end
it 'returns a 400 bad request if name not given' do
post api("/groups/#{group.id}/labels", user), params: { color: '#FFAABB' }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2019-03-02 22:35:43 +05:30
end
it 'returns a 400 bad request if color is not given' do
post api("/groups/#{group.id}/labels", user), params: { name: 'Foobar' }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2019-03-02 22:35:43 +05:30
end
it 'returns 409 if label already exists' do
post api("/groups/#{group.id}/labels", user),
params: {
2019-12-21 20:55:43 +05:30
name: group_label1.name,
2019-03-02 22:35:43 +05:30
color: '#FFAABB'
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:conflict)
2019-03-02 22:35:43 +05:30
expect(json_response['message']).to eq('Label already exists')
end
end
2019-12-21 20:55:43 +05:30
describe 'DELETE /groups/:id/labels (deprecated)' do
2019-03-02 22:35:43 +05:30
it 'returns 204 for existing label' do
2019-12-21 20:55:43 +05:30
delete api("/groups/#{group.id}/labels", user), params: { name: group_label1.name }
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2019-03-02 22:35:43 +05:30
end
it 'returns 404 for non existing label' do
2019-12-21 20:55:43 +05:30
delete api("/groups/#{group.id}/labels", user), params: { name: 'not_exists' }
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-03-02 22:35:43 +05:30
expect(json_response['message']).to eq('404 Label Not Found')
end
it 'returns 400 for wrong parameters' do
delete api("/groups/#{group.id}/labels", user)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2019-03-02 22:35:43 +05:30
end
2019-10-12 21:52:04 +05:30
it "does not delete parent's group labels" do
2019-03-02 22:35:43 +05:30
subgroup = create(:group, parent: group)
subgroup_label = create(:group_label, title: 'feature', group: subgroup)
delete api("/groups/#{subgroup.id}/labels", user), params: { name: subgroup_label.name }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2019-03-02 22:35:43 +05:30
expect(subgroup.labels.size).to eq(0)
2019-12-21 20:55:43 +05:30
expect(group.labels).to include(group_label1)
2019-03-02 22:35:43 +05:30
end
it_behaves_like '412 response' do
let(:request) { api("/groups/#{group.id}/labels", user) }
2019-12-21 20:55:43 +05:30
let(:params) { { name: group_label1.name } }
2019-03-02 22:35:43 +05:30
end
end
2019-12-21 20:55:43 +05:30
describe 'DELETE /groups/:id/labels/:label_id' do
it 'returns 204 for existing label' do
2021-03-11 19:13:27 +05:30
delete api("/groups/#{group.id}/labels/#{valid_group_label_title_1_esc}", user)
2019-12-21 20:55:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2019-12-21 20:55:43 +05:30
end
it 'returns 404 for non existing label' do
delete api("/groups/#{group.id}/labels/not_exists", user)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-12-21 20:55:43 +05:30
expect(json_response['message']).to eq('404 Label Not Found')
end
it "does not delete parent's group labels" do
subgroup = create(:group, parent: group)
subgroup_label = create(:group_label, title: 'feature', group: subgroup)
delete api("/groups/#{subgroup.id}/labels/#{subgroup_label.name}", user)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2019-12-21 20:55:43 +05:30
expect(subgroup.labels.size).to eq(0)
expect(group.labels).to include(group_label1)
end
it_behaves_like '412 response' do
2021-03-11 19:13:27 +05:30
let(:request) { api("/groups/#{group.id}/labels/#{valid_group_label_title_1_esc}", user) }
2019-12-21 20:55:43 +05:30
end
end
describe 'PUT /groups/:id/labels (deprecated)' do
2019-03-02 22:35:43 +05:30
it 'returns 200 if name and colors and description are changed' do
put api("/groups/#{group.id}/labels", user),
params: {
2019-12-21 20:55:43 +05:30
name: group_label1.name,
2021-03-11 19:13:27 +05:30
new_name: valid_new_label_title,
2019-03-02 22:35:43 +05:30
color: '#FFFFFF',
description: 'test'
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-03-11 19:13:27 +05:30
expect(json_response['name']).to eq(valid_new_label_title)
2022-05-07 20:08:51 +05:30
expect(json_response['color']).to be_color('#FFFFFF')
2019-03-02 22:35:43 +05:30
expect(json_response['description']).to eq('test')
end
2019-10-12 21:52:04 +05:30
it "does not update parent's group label" do
2019-03-02 22:35:43 +05:30
subgroup = create(:group, parent: group)
subgroup_label = create(:group_label, title: 'feature', group: subgroup)
put api("/groups/#{subgroup.id}/labels", user),
params: {
name: subgroup_label.name,
2021-03-11 19:13:27 +05:30
new_name: valid_new_label_title
2019-03-02 22:35:43 +05:30
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-03-11 19:13:27 +05:30
expect(subgroup.labels[0].name).to eq(valid_new_label_title)
2021-01-29 00:20:46 +05:30
expect(group_label1.name).to eq(group_label1.title)
2019-03-02 22:35:43 +05:30
end
it 'returns 404 if label does not exist' do
put api("/groups/#{group.id}/labels", user),
params: {
2019-12-21 20:55:43 +05:30
name: 'not_exists',
2021-03-11 19:13:27 +05:30
new_name: valid_new_label_title
2019-03-02 22:35:43 +05:30
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-03-02 22:35:43 +05:30
end
it 'returns 400 if no label name given' do
2019-12-21 20:55:43 +05:30
put api("/groups/#{group.id}/labels", user), params: { new_name: group_label1.name }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2019-12-21 20:55:43 +05:30
expect(json_response['error']).to eq('label_id, name are missing, exactly one parameter must be provided')
end
it 'returns 400 if no new parameters given' do
put api("/groups/#{group.id}/labels", user), params: { name: group_label1.name }
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2021-09-04 01:27:46 +05:30
expect(json_response['error']).to eq('new_name, color, description are missing, '\
2019-12-21 20:55:43 +05:30
'at least one parameter must be provided')
end
end
describe 'PUT /groups/:id/labels/:label_id' do
it 'returns 200 if name and colors and description are changed' do
2021-03-11 19:13:27 +05:30
put api("/groups/#{group.id}/labels/#{valid_group_label_title_1_esc}", user),
2019-12-21 20:55:43 +05:30
params: {
2021-03-11 19:13:27 +05:30
new_name: valid_new_label_title,
2019-12-21 20:55:43 +05:30
color: '#FFFFFF',
description: 'test'
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-03-11 19:13:27 +05:30
expect(json_response['name']).to eq(valid_new_label_title)
2022-05-07 20:08:51 +05:30
expect(json_response['color']).to be_color('#FFFFFF')
2019-12-21 20:55:43 +05:30
expect(json_response['description']).to eq('test')
end
it "does not update parent's group label" do
subgroup = create(:group, parent: group)
subgroup_label = create(:group_label, title: 'feature', group: subgroup)
put api("/groups/#{subgroup.id}/labels/#{subgroup_label.name}", user),
params: {
2021-03-11 19:13:27 +05:30
new_name: valid_new_label_title
2019-12-21 20:55:43 +05:30
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-03-11 19:13:27 +05:30
expect(subgroup.labels[0].name).to eq(valid_new_label_title)
2021-01-29 00:20:46 +05:30
expect(group_label1.name).to eq(group_label1.title)
2019-12-21 20:55:43 +05:30
end
it 'returns 404 if label does not exist' do
put api("/groups/#{group.id}/labels/not_exists", user),
params: {
2021-03-11 19:13:27 +05:30
new_name: valid_new_label_title
2019-12-21 20:55:43 +05:30
}
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-03-02 22:35:43 +05:30
end
it 'returns 400 if no new parameters given' do
2021-03-11 19:13:27 +05:30
put api("/groups/#{group.id}/labels/#{valid_group_label_title_1_esc}", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2021-09-04 01:27:46 +05:30
expect(json_response['error']).to eq('new_name, color, description are missing, '\
2019-03-02 22:35:43 +05:30
'at least one parameter must be provided')
end
end
describe 'POST /groups/:id/labels/:label_id/subscribe' do
context 'when label_id is a label title' do
it 'subscribes to the label' do
2021-03-11 19:13:27 +05:30
post api("/groups/#{group.id}/labels/#{valid_group_label_title_1_esc}/subscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2019-12-21 20:55:43 +05:30
expect(json_response['name']).to eq(group_label1.title)
2019-03-02 22:35:43 +05:30
expect(json_response['subscribed']).to be_truthy
end
end
context 'when label_id is a label ID' do
it 'subscribes to the label' do
2019-12-21 20:55:43 +05:30
post api("/groups/#{group.id}/labels/#{group_label1.id}/subscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2019-12-21 20:55:43 +05:30
expect(json_response['name']).to eq(group_label1.title)
2019-03-02 22:35:43 +05:30
expect(json_response['subscribed']).to be_truthy
end
end
context 'when user is already subscribed to label' do
before do
2019-12-21 20:55:43 +05:30
group_label1.subscribe(user)
2019-03-02 22:35:43 +05:30
end
it 'returns 304' do
2019-12-21 20:55:43 +05:30
post api("/groups/#{group.id}/labels/#{group_label1.id}/subscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_modified)
2019-03-02 22:35:43 +05:30
end
end
context 'when label ID is not found' do
it 'returns 404 error' do
2020-04-22 19:07:51 +05:30
post api("/groups/#{group.id}/labels/#{non_existing_record_id}/subscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-03-02 22:35:43 +05:30
end
end
end
describe 'POST /groups/:id/labels/:label_id/unsubscribe' do
before do
2019-12-21 20:55:43 +05:30
group_label1.subscribe(user)
2019-03-02 22:35:43 +05:30
end
context 'when label_id is a label title' do
it 'unsubscribes from the label' do
2021-03-11 19:13:27 +05:30
post api("/groups/#{group.id}/labels/#{valid_group_label_title_1_esc}/unsubscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2019-12-21 20:55:43 +05:30
expect(json_response['name']).to eq(group_label1.title)
2019-03-02 22:35:43 +05:30
expect(json_response['subscribed']).to be_falsey
end
end
context 'when label_id is a label ID' do
it 'unsubscribes from the label' do
2019-12-21 20:55:43 +05:30
post api("/groups/#{group.id}/labels/#{group_label1.id}/unsubscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2019-12-21 20:55:43 +05:30
expect(json_response['name']).to eq(group_label1.title)
2019-03-02 22:35:43 +05:30
expect(json_response['subscribed']).to be_falsey
end
end
context 'when user is already unsubscribed from label' do
before do
2019-12-21 20:55:43 +05:30
group_label1.unsubscribe(user)
2019-03-02 22:35:43 +05:30
end
it 'returns 304' do
2019-12-21 20:55:43 +05:30
post api("/groups/#{group.id}/labels/#{group_label1.id}/unsubscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_modified)
2019-03-02 22:35:43 +05:30
end
end
context 'when label ID is not found' do
it 'returns 404 error' do
2020-04-22 19:07:51 +05:30
post api("/groups/#{group.id}/labels/#{non_existing_record_id}/unsubscribe", user)
2019-03-02 22:35:43 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-03-02 22:35:43 +05:30
end
end
end
end