2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-15 08:29:53 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Namespaces do
|
2021-10-22 08:09:12 -04:00
|
|
|
let_it_be(:admin) { create(:admin) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:group1) { create(:group, name: 'group.one') }
|
|
|
|
let_it_be(:group2) { create(:group, :nested) }
|
|
|
|
let_it_be(:project) { create(:project, namespace: group2, name: group2.name, path: group2.path) }
|
2021-11-10 13:12:35 -05:00
|
|
|
let_it_be(:project_namespace) { project.project_namespace }
|
2013-11-15 08:29:53 -05:00
|
|
|
|
|
|
|
describe "GET /namespaces" do
|
|
|
|
context "when unauthenticated" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns authentication error" do
|
2013-11-15 08:29:53 -05:00
|
|
|
get api("/namespaces")
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2013-11-15 08:29:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-25 16:51:37 -04:00
|
|
|
context "when authenticated as admin" do
|
2017-06-27 16:35:35 -04:00
|
|
|
it "returns correct attributes" do
|
|
|
|
get api("/namespaces", admin)
|
|
|
|
|
2017-06-28 16:27:01 -04:00
|
|
|
group_kind_json_response = json_response.find { |resource| resource['kind'] == 'group' }
|
|
|
|
user_kind_json_response = json_response.find { |resource| resource['kind'] == 'user' }
|
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-06-27 16:35:35 -04:00
|
|
|
expect(response).to include_pagination_headers
|
2018-07-02 18:01:31 -04:00
|
|
|
expect(group_kind_json_response.keys).to include('id', 'kind', 'name', 'path', 'full_path',
|
2021-10-22 08:09:12 -04:00
|
|
|
'parent_id', 'members_count_with_descendants')
|
2017-06-28 16:27:01 -04:00
|
|
|
|
2018-07-02 18:01:31 -04:00
|
|
|
expect(user_kind_json_response.keys).to include('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
|
2017-06-27 16:35:35 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "admin: returns an array of all namespaces" do
|
2013-11-15 08:29:53 -05:00
|
|
|
get api("/namespaces", admin)
|
2017-01-24 15:49:10 -05:00
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response).to be_an Array
|
2021-10-22 08:09:12 -04:00
|
|
|
# project namespace is excluded
|
|
|
|
expect(json_response.length).to eq(Namespace.count - 1)
|
2013-11-15 08:29:53 -05:00
|
|
|
end
|
2015-05-25 16:51:37 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "admin: returns an array of matched namespaces" do
|
2017-02-14 08:34:36 -05:00
|
|
|
get api("/namespaces?search=#{group2.name}", admin)
|
2017-01-24 15:49:10 -05:00
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2015-05-25 16:51:37 -04:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.length).to eq(1)
|
2017-02-14 08:34:36 -05:00
|
|
|
expect(json_response.last['path']).to eq(group2.path)
|
|
|
|
expect(json_response.last['full_path']).to eq(group2.full_path)
|
2015-05-25 16:51:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when authenticated as a regular user" do
|
2017-06-28 16:55:50 -04:00
|
|
|
it "returns correct attributes when user can admin group" do
|
2017-06-28 16:27:01 -04:00
|
|
|
group1.add_owner(user)
|
|
|
|
|
2017-06-27 16:35:35 -04:00
|
|
|
get api("/namespaces", user)
|
|
|
|
|
2017-06-28 16:27:01 -04:00
|
|
|
owned_group_response = json_response.find { |resource| resource['id'] == group1.id }
|
|
|
|
|
2018-07-02 18:01:31 -04:00
|
|
|
expect(owned_group_response.keys).to include('id', 'kind', 'name', 'path', 'full_path',
|
2021-10-22 08:09:12 -04:00
|
|
|
'parent_id', 'members_count_with_descendants')
|
2017-06-28 16:27:01 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 16:55:50 -04:00
|
|
|
it "returns correct attributes when user cannot admin group" do
|
2017-06-28 16:27:01 -04:00
|
|
|
group1.add_guest(user)
|
|
|
|
|
|
|
|
get api("/namespaces", user)
|
|
|
|
|
|
|
|
guest_group_response = json_response.find { |resource| resource['id'] == group1.id }
|
|
|
|
|
2018-07-02 18:01:31 -04:00
|
|
|
expect(guest_group_response.keys).to include('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
|
2017-06-27 16:35:35 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "user: returns an array of namespaces" do
|
2015-05-25 16:51:37 -04:00
|
|
|
get api("/namespaces", user)
|
2017-01-24 15:49:10 -05:00
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2015-05-25 16:51:37 -04:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "admin: returns an array of matched namespaces" do
|
2015-05-25 16:51:37 -04:00
|
|
|
get api("/namespaces?search=#{user.username}", user)
|
2017-01-24 15:49:10 -05:00
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2015-05-25 16:51:37 -04:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.length).to eq(1)
|
|
|
|
end
|
2021-07-29 17:10:10 -04:00
|
|
|
|
|
|
|
context 'with owned_only param' do
|
|
|
|
it 'returns only owned groups' do
|
|
|
|
group1.add_developer(user)
|
|
|
|
group2.add_owner(user)
|
|
|
|
|
|
|
|
get api("/namespaces?owned_only=true", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response.map { |resource| resource['id'] }).to match_array([user.namespace_id, group2.id])
|
|
|
|
end
|
|
|
|
end
|
2013-11-15 08:29:53 -05:00
|
|
|
end
|
|
|
|
end
|
2017-11-16 20:43:55 -05:00
|
|
|
|
|
|
|
describe 'GET /namespaces/:id' do
|
|
|
|
let(:owned_group) { group1 }
|
2021-10-22 08:09:12 -04:00
|
|
|
|
|
|
|
let_it_be(:user2) { create(:user) }
|
2017-11-16 20:43:55 -05:00
|
|
|
|
2017-11-23 08:32:16 -05:00
|
|
|
shared_examples 'can access namespace' do
|
|
|
|
it 'returns namespace details' do
|
|
|
|
get api("/namespaces/#{namespace_id}", request_actor)
|
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-11-23 08:32:16 -05:00
|
|
|
|
|
|
|
expect(json_response['id']).to eq(requested_namespace.id)
|
|
|
|
expect(json_response['path']).to eq(requested_namespace.path)
|
|
|
|
expect(json_response['name']).to eq(requested_namespace.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-16 20:43:55 -05:00
|
|
|
shared_examples 'namespace reader' do
|
2017-11-23 08:32:16 -05:00
|
|
|
let(:requested_namespace) { owned_group }
|
|
|
|
|
2017-11-16 20:43:55 -05:00
|
|
|
before do
|
|
|
|
owned_group.add_owner(request_actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace exists' do
|
2017-11-23 08:32:16 -05:00
|
|
|
context 'when requested by ID' do
|
2017-11-23 09:47:15 -05:00
|
|
|
context 'when requesting group' do
|
|
|
|
let(:namespace_id) { owned_group.id }
|
2017-11-16 20:43:55 -05:00
|
|
|
|
2017-11-23 09:47:15 -05:00
|
|
|
it_behaves_like 'can access namespace'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting personal namespace' do
|
|
|
|
let(:namespace_id) { request_actor.namespace.id }
|
|
|
|
let(:requested_namespace) { request_actor.namespace }
|
|
|
|
|
|
|
|
it_behaves_like 'can access namespace'
|
|
|
|
end
|
2021-10-22 08:09:12 -04:00
|
|
|
|
|
|
|
context 'when requesting project_namespace' do
|
|
|
|
let(:namespace_id) { project_namespace.id }
|
|
|
|
|
|
|
|
it 'returns not-found' do
|
|
|
|
get api("/namespaces/#{namespace_id}", request_actor)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
2017-11-23 08:32:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requested by path' do
|
2017-11-23 09:47:15 -05:00
|
|
|
context 'when requesting group' do
|
|
|
|
let(:namespace_id) { owned_group.path }
|
2017-11-16 20:43:55 -05:00
|
|
|
|
2017-11-23 09:47:15 -05:00
|
|
|
it_behaves_like 'can access namespace'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting personal namespace' do
|
|
|
|
let(:namespace_id) { request_actor.namespace.path }
|
|
|
|
let(:requested_namespace) { request_actor.namespace }
|
|
|
|
|
|
|
|
it_behaves_like 'can access namespace'
|
|
|
|
end
|
2021-10-22 08:09:12 -04:00
|
|
|
|
|
|
|
context 'when requesting project_namespace' do
|
|
|
|
let(:namespace_id) { project_namespace.full_path }
|
|
|
|
|
|
|
|
it 'returns not-found' do
|
|
|
|
get api("/namespaces/#{namespace_id}", request_actor)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when namespace doesn't exist" do
|
|
|
|
it 'returns not-found' do
|
2019-02-26 18:18:40 -05:00
|
|
|
get api('/namespaces/0', request_actor)
|
2017-11-16 20:43:55 -05:00
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated' do
|
|
|
|
it 'returns authentication error' do
|
|
|
|
get api("/namespaces/#{group1.id}")
|
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
2021-10-22 08:09:12 -04:00
|
|
|
|
|
|
|
it 'returns authentication error' do
|
|
|
|
get api("/namespaces/#{project_namespace.id}")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as regular user' do
|
|
|
|
let(:request_actor) { user }
|
|
|
|
|
|
|
|
context 'when requested namespace is not owned by user' do
|
2017-11-23 09:47:15 -05:00
|
|
|
context 'when requesting group' do
|
|
|
|
it 'returns not-found' do
|
|
|
|
get api("/namespaces/#{group2.id}", request_actor)
|
2017-11-16 20:43:55 -05:00
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-11-23 09:47:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting personal namespace' do
|
|
|
|
it 'returns not-found' do
|
|
|
|
get api("/namespaces/#{user2.namespace.id}", request_actor)
|
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-11-23 09:47:15 -05:00
|
|
|
end
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requested namespace is owned by user' do
|
|
|
|
it_behaves_like 'namespace reader'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as admin' do
|
|
|
|
let(:request_actor) { admin }
|
|
|
|
|
|
|
|
context 'when requested namespace is not owned by user' do
|
2017-11-23 09:47:15 -05:00
|
|
|
context 'when requesting group' do
|
|
|
|
let(:namespace_id) { group2.id }
|
|
|
|
let(:requested_namespace) { group2 }
|
|
|
|
|
|
|
|
it_behaves_like 'can access namespace'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting personal namespace' do
|
|
|
|
let(:namespace_id) { user2.namespace.id }
|
|
|
|
let(:requested_namespace) { user2.namespace }
|
2017-11-16 20:43:55 -05:00
|
|
|
|
2017-11-23 09:47:15 -05:00
|
|
|
it_behaves_like 'can access namespace'
|
|
|
|
end
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requested namespace is owned by user' do
|
|
|
|
it_behaves_like 'namespace reader'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-04-19 14:09:09 -04:00
|
|
|
|
|
|
|
describe 'GET /namespaces/:namespace/exists' do
|
2021-10-22 08:09:12 -04:00
|
|
|
let_it_be(:namespace1) { create(:group, name: 'Namespace 1', path: 'namespace-1') }
|
|
|
|
let_it_be(:namespace2) { create(:group, name: 'Namespace 2', path: 'namespace-2') }
|
|
|
|
let_it_be(:namespace1sub) { create(:group, name: 'Sub Namespace 1', path: 'sub-namespace-1', parent: namespace1) }
|
|
|
|
let_it_be(:namespace2sub) { create(:group, name: 'Sub Namespace 2', path: 'sub-namespace-2', parent: namespace2) }
|
2021-04-19 14:09:09 -04:00
|
|
|
|
|
|
|
context 'when unauthenticated' do
|
|
|
|
it 'returns authentication error' do
|
|
|
|
get api("/namespaces/#{namespace1.path}/exists")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
2021-10-29 08:14:45 -04:00
|
|
|
|
|
|
|
context 'when requesting project_namespace' do
|
|
|
|
let(:namespace_id) { project_namespace.id }
|
|
|
|
|
|
|
|
it 'returns authentication error' do
|
|
|
|
get api("/namespaces/#{project_namespace.path}/exists"), params: { parent_id: group2.id }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
2021-04-19 14:09:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated' do
|
2022-08-30 17:09:41 -04:00
|
|
|
it_behaves_like 'rate limited endpoint', rate_limit_key: :namespace_exists do
|
|
|
|
let(:current_user) { user }
|
|
|
|
|
|
|
|
def request
|
|
|
|
get api("/namespaces/#{namespace1.path}/exists", current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-19 14:09:09 -04:00
|
|
|
it 'returns JSON indicating the namespace exists and a suggestion' do
|
|
|
|
get api("/namespaces/#{namespace1.path}/exists", user)
|
|
|
|
|
|
|
|
expected_json = { exists: true, suggests: ["#{namespace1.path}1"] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns JSON indicating the namespace does not exist without a suggestion' do
|
|
|
|
get api("/namespaces/non-existing-namespace/exists", user)
|
|
|
|
|
|
|
|
expected_json = { exists: false, suggests: [] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks the existence of a namespace in case-insensitive manner' do
|
|
|
|
get api("/namespaces/#{namespace1.path.upcase}/exists", user)
|
|
|
|
|
|
|
|
expected_json = { exists: true, suggests: ["#{namespace1.path.upcase}1"] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks the existence within the parent namespace only' do
|
|
|
|
get api("/namespaces/#{namespace1sub.path}/exists", user), params: { parent_id: namespace1.id }
|
|
|
|
|
|
|
|
expected_json = { exists: true, suggests: ["#{namespace1sub.path}1"] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores nested namespaces when checking for top-level namespace' do
|
|
|
|
get api("/namespaces/#{namespace1sub.path}/exists", user)
|
|
|
|
|
|
|
|
expected_json = { exists: false, suggests: [] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
|
2022-05-25 08:08:10 -04:00
|
|
|
it 'ignores paths of groups present in other hierarchies when making suggestions' do
|
|
|
|
(1..2).to_a.each do |suffix|
|
|
|
|
create(:group, name: "mygroup#{suffix}", path: "mygroup#{suffix}", parent: namespace2)
|
|
|
|
end
|
|
|
|
|
|
|
|
create(:group, name: 'mygroup', path: 'mygroup', parent: namespace1)
|
|
|
|
|
|
|
|
get api("/namespaces/mygroup/exists", user), params: { parent_id: namespace1.id }
|
|
|
|
|
|
|
|
# if the paths of groups present in hierachies aren't ignored, the suggestion generated would have
|
|
|
|
# been `mygroup3`, just because groups with path `mygroup1` and `mygroup2` exists somewhere else.
|
|
|
|
# But there is no reason for those groups that exists elsewhere to cause a conflict because
|
|
|
|
# their hierarchies differ. Hence, the correct suggestion to be generated would be `mygroup1`
|
|
|
|
expected_json = { exists: true, suggests: ["mygroup1"] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
|
2021-04-19 14:09:09 -04:00
|
|
|
it 'ignores top-level namespaces when checking with parent_id' do
|
|
|
|
get api("/namespaces/#{namespace1.path}/exists", user), params: { parent_id: namespace1.id }
|
|
|
|
|
|
|
|
expected_json = { exists: false, suggests: [] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores namespaces of other parent namespaces when checking with parent_id' do
|
|
|
|
get api("/namespaces/#{namespace2sub.path}/exists", user), params: { parent_id: namespace1.id }
|
|
|
|
|
|
|
|
expected_json = { exists: false, suggests: [] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
2021-10-29 08:14:45 -04:00
|
|
|
|
|
|
|
context 'when requesting project_namespace' do
|
|
|
|
let(:namespace_id) { project_namespace.id }
|
|
|
|
|
|
|
|
it 'returns JSON indicating the namespace does not exist without a suggestion' do
|
|
|
|
get api("/namespaces/#{project_namespace.path}/exists", user), params: { parent_id: group2.id }
|
|
|
|
|
|
|
|
expected_json = { exists: false, suggests: [] }.to_json
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(expected_json)
|
|
|
|
end
|
|
|
|
end
|
2021-04-19 14:09:09 -04:00
|
|
|
end
|
|
|
|
end
|
2013-11-15 08:29:53 -05:00
|
|
|
end
|