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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

297 lines
10 KiB
Ruby
Raw Permalink Normal View History

2019-03-13 22:55:13 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::AutocompleteSourcesController do
2022-04-04 11:22:00 +05:30
let_it_be(:group, reload: true) { create(:group) }
2023-04-23 21:23:45 +05:30
let_it_be(:private_group) { create(:group, :private) }
2019-12-21 20:55:43 +05:30
let_it_be(:project) { create(:project, namespace: group) }
2022-11-25 23:54:43 +05:30
let_it_be(:public_project) { create(:project, :public, group: group) }
let_it_be(:development) { create(:label, project: project, name: 'Development') }
2023-04-23 21:23:45 +05:30
let_it_be(:private_issue) { create(:labeled_issue, project: project, labels: [development]) }
let_it_be(:private_work_item) { create(:work_item, project: project) }
let_it_be(:issue) { create(:labeled_issue, project: public_project, labels: [development]) }
let_it_be(:work_item) { create(:work_item, project: public_project, id: 1, iid: 100) }
2019-12-21 20:55:43 +05:30
let_it_be(:user) { create(:user) }
2019-07-07 11:18:12 +05:30
2019-10-31 01:37:42 +05:30
def members_by_username(username)
json_response.find { |member| member['username'] == username }
end
2022-11-25 23:54:43 +05:30
describe 'GET commands' do
before do
group.add_owner(user)
end
context 'with a public project' do
shared_examples 'issuable commands' do
it 'returns empty array when no user logged in' do
2023-04-23 21:23:45 +05:30
get :commands, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }
2022-11-25 23:54:43 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq([])
end
it 'raises an error when no target type specified' do
sign_in(user)
expect { get :commands, format: :json, params: { namespace_id: group.path, project_id: project.path } }
.to raise_error(ActionController::ParameterMissing)
end
it 'returns an array of commands' do
sign_in(user)
get :commands, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type, type_id: issuable_iid }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_present
end
end
context 'with an issue' do
let(:issuable_type) { issue.class.name }
let(:issuable_iid) { issue.iid }
it_behaves_like 'issuable commands'
end
2023-04-23 21:23:45 +05:30
context 'with work items' do
let(:issuable_type) { work_item.class.name }
let(:issuable_iid) { work_item.iid }
it_behaves_like 'issuable commands'
end
2022-11-25 23:54:43 +05:30
context 'with merge request' do
let(:merge_request) { create(:merge_request, target_project: public_project, source_project: public_project) }
let(:issuable_type) { merge_request.class.name }
let(:issuable_iid) { merge_request.iid }
it_behaves_like 'issuable commands'
end
end
end
describe 'GET labels' do
2019-07-07 11:18:12 +05:30
before do
group.add_owner(user)
sign_in(user)
end
2023-04-23 21:23:45 +05:30
shared_examples 'label commands' do
it 'raises an error when no target type specified' do
expect { get :labels, format: :json, params: { namespace_id: group.path, project_id: project.path } }
.to raise_error(ActionController::ParameterMissing)
end
it 'returns an array of labels' do
get :labels, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issuable_type }
expect(json_response).to be_a(Array)
expect(json_response.count).to eq(1)
expect(json_response[0]['title']).to eq('Development')
end
end
context 'with issues' do
let(:issuable_type) { issue.class.name }
let(:issuable_iid) { issue.iid }
it_behaves_like 'label commands'
2022-11-25 23:54:43 +05:30
end
2023-04-23 21:23:45 +05:30
context 'with work items' do
let(:issuable_type) { work_item.class.name }
let(:issuable_iid) { work_item.iid }
2022-11-25 23:54:43 +05:30
2023-04-23 21:23:45 +05:30
it_behaves_like 'label commands'
2022-11-25 23:54:43 +05:30
end
end
describe 'GET members' do
2023-04-23 21:23:45 +05:30
let_it_be(:invited_private_member) { create(:user) }
let_it_be(:issue) { create(:labeled_issue, project: public_project, labels: [development], author: user) }
let_it_be(:work_item) { create(:work_item, project: public_project, author: user) }
before_all do
create(:project_group_link, group: private_group, project: public_project)
group.add_owner(user)
private_group.add_developer(invited_private_member)
end
2022-11-25 23:54:43 +05:30
context 'when logged in' do
before do
sign_in(user)
end
it 'returns 400 when no target type specified' do
expect { get :members, format: :json, params: { namespace_id: group.path, project_id: project.path } }
.to raise_error(ActionController::ParameterMissing)
end
2023-04-23 21:23:45 +05:30
shared_examples 'all members are returned' do
it 'returns an array of member object' do
get :members, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }
expect(members_by_username('all').symbolize_keys).to include(
username: 'all',
name: 'All Project and Group Members',
count: 2)
expect(members_by_username(group.full_path).symbolize_keys).to include(
type: group.class.name,
name: group.full_name,
avatar_url: group.avatar_url,
count: 1)
expect(members_by_username(user.username).symbolize_keys).to include(
type: user.class.name,
name: user.name,
avatar_url: user.avatar_url)
expect(members_by_username(invited_private_member.username).symbolize_keys).to include(
type: invited_private_member.class.name,
name: invited_private_member.name,
avatar_url: invited_private_member.avatar_url)
end
end
2022-11-25 23:54:43 +05:30
2023-04-23 21:23:45 +05:30
context 'with issue' do
let(:issuable_type) { issue.class.name }
2022-11-25 23:54:43 +05:30
2023-04-23 21:23:45 +05:30
it_behaves_like 'all members are returned'
end
2022-11-25 23:54:43 +05:30
2023-04-23 21:23:45 +05:30
context 'with work item' do
let(:issuable_type) { work_item.class.name }
it_behaves_like 'all members are returned'
2022-11-25 23:54:43 +05:30
end
end
context 'when anonymous' do
2023-04-23 21:23:45 +05:30
shared_examples 'private project is inaccessible' do
it 'redirects to login page for private project' do
get :members, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issuable_type }
2019-07-07 11:18:12 +05:30
2023-04-23 21:23:45 +05:30
expect(response).to redirect_to new_user_session_path
end
2022-11-25 23:54:43 +05:30
end
2019-10-31 01:37:42 +05:30
2023-04-23 21:23:45 +05:30
shared_examples 'only public members are returned for public project' do
it 'only returns public members' do
get :members, format: :json, params: { namespace_id: group.path, project_id: public_project.path, type: issuable_type }
2019-10-31 01:37:42 +05:30
2023-04-23 21:23:45 +05:30
expect(members_by_username('all').symbolize_keys).to include(
username: 'all',
name: 'All Project and Group Members',
count: 1)
expect(members_by_username(user.username).symbolize_keys).to include(
type: user.class.name,
name: user.name,
avatar_url: user.avatar_url)
end
end
context 'with issue' do
it_behaves_like 'private project is inaccessible' do
let(:issuable_type) { private_issue.class.name }
end
it_behaves_like 'only public members are returned for public project' do
let(:issuable_type) { issue.class.name }
end
end
context 'with work item' do
it_behaves_like 'private project is inaccessible' do
let(:issuable_type) { private_work_item.class.name }
end
it_behaves_like 'only public members are returned for public project' do
let(:issuable_type) { work_item.class.name }
2022-11-25 23:54:43 +05:30
end
end
2019-07-07 11:18:12 +05:30
end
end
2019-03-13 22:55:13 +05:30
describe 'GET milestones' do
let(:group) { create(:group, :public) }
let(:project) { create(:project, :public, namespace: group) }
let!(:project_milestone) { create(:milestone, project: project) }
let!(:group_milestone) { create(:milestone, group: group) }
before do
sign_in(user)
end
it 'lists milestones' do
group.add_owner(user)
get :milestones, format: :json, params: { namespace_id: group.path, project_id: project.path }
milestone_titles = json_response.map { |milestone| milestone["title"] }
expect(milestone_titles).to match_array([project_milestone.title, group_milestone.title])
end
context 'when user cannot read project issues and merge requests' do
it 'renders 404' do
project.project_feature.update!(issues_access_level: ProjectFeature::PRIVATE)
project.project_feature.update!(merge_requests_access_level: ProjectFeature::PRIVATE)
get :milestones, format: :json, params: { namespace_id: group.path, project_id: project.path }
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-03-13 22:55:13 +05:30
end
end
end
2022-04-04 11:22:00 +05:30
describe 'GET contacts' do
let_it_be(:contact_1) { create(:contact, group: group) }
let_it_be(:contact_2) { create(:contact, group: group) }
before do
sign_in(user)
end
context 'when feature flag is enabled' do
context 'when a group has contact relations enabled' do
before do
create(:crm_settings, group: group, enabled: true)
end
context 'when a user can read contacts' do
it 'lists contacts' do
group.add_developer(user)
2023-04-23 21:23:45 +05:30
get :contacts, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issue.class.name }
2022-04-04 11:22:00 +05:30
emails = json_response.map { |contact_data| contact_data["email"] }
expect(emails).to match_array([contact_1.email, contact_2.email])
end
end
context 'when a user can not read contacts' do
it 'renders 404' do
2023-04-23 21:23:45 +05:30
get :contacts, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issue.class.name }
2022-04-04 11:22:00 +05:30
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'when a group has contact relations disabled' do
it 'renders 404' do
group.add_developer(user)
2023-04-23 21:23:45 +05:30
get :contacts, format: :json, params: { namespace_id: group.path, project_id: project.path, type: issue.class.name }
2022-04-04 11:22:00 +05:30
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
2019-03-13 22:55:13 +05:30
end