2013-11-15 08:29:53 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-21 16:32:02 -04:00
|
|
|
describe API::Namespaces do
|
2013-11-15 08:29:53 -05:00
|
|
|
let(:admin) { create(:admin) }
|
2015-05-25 16:51:37 -04:00
|
|
|
let(:user) { create(:user) }
|
2013-11-15 08:29:53 -05:00
|
|
|
let!(:group1) { create(:group) }
|
2017-02-14 08:34:36 -05:00
|
|
|
let!(:group2) { create(:group, :nested) }
|
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")
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
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' }
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-06-27 16:35:35 -04:00
|
|
|
expect(response).to include_pagination_headers
|
2017-06-28 16:27:01 -04:00
|
|
|
expect(group_kind_json_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path',
|
|
|
|
'parent_id', 'members_count_with_descendants')
|
|
|
|
|
|
|
|
expect(user_kind_json_response.keys).to contain_exactly('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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
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
|
|
|
|
expect(json_response.length).to eq(Namespace.count)
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
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 }
|
|
|
|
|
|
|
|
expect(owned_group_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path',
|
|
|
|
'parent_id', 'members_count_with_descendants')
|
|
|
|
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 }
|
|
|
|
|
|
|
|
expect(guest_group_response.keys).to contain_exactly('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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
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
|
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 }
|
2017-11-23 09:47:15 -05:00
|
|
|
let(: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)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
|
|
|
|
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
|
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
|
2017-11-16 20:43:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when namespace doesn't exist" do
|
|
|
|
it 'returns not-found' do
|
|
|
|
get api('/namespaces/9999', request_actor)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated' do
|
|
|
|
it 'returns authentication error' do
|
|
|
|
get api("/namespaces/#{group1.id}")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(401)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2017-11-23 09:47:15 -05:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting personal namespace' do
|
|
|
|
it 'returns not-found' do
|
|
|
|
get api("/namespaces/#{user2.namespace.id}", request_actor)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
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
|
2013-11-15 08:29:53 -05:00
|
|
|
end
|