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

220 lines
7 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2017-08-17 22:00:37 +05:30
describe API::Namespaces do
2014-09-02 18:07:02 +05:30
let(:admin) { create(:admin) }
2015-09-11 14:41:01 +05:30
let(:user) { create(:user) }
2019-02-15 15:39:39 +05:30
let!(:group1) { create(:group, name: 'group.one') }
2017-08-17 22:00:37 +05:30
let!(:group2) { create(:group, :nested) }
2014-09-02 18:07:02 +05:30
describe "GET /namespaces" do
context "when unauthenticated" do
2016-09-13 17:45:13 +05:30
it "returns authentication error" do
2014-09-02 18:07:02 +05:30
get api("/namespaces")
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2014-09-02 18:07:02 +05:30
end
end
2015-09-11 14:41:01 +05:30
context "when authenticated as admin" do
2017-09-10 17:25:29 +05:30
it "returns correct attributes" do
get api("/namespaces", admin)
group_kind_json_response = json_response.find { |resource| resource['kind'] == 'group' }
user_kind_json_response = json_response.find { |resource| resource['kind'] == 'user' }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-09-10 17:25:29 +05:30
expect(response).to include_pagination_headers
2018-11-18 11:00:15 +05:30
expect(group_kind_json_response.keys).to include('id', 'kind', 'name', 'path', 'full_path',
'parent_id', 'members_count_with_descendants')
2017-09-10 17:25:29 +05:30
2018-11-18 11:00:15 +05:30
expect(user_kind_json_response.keys).to include('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
2017-09-10 17:25:29 +05:30
end
2016-09-13 17:45:13 +05:30
it "admin: returns an array of all namespaces" do
2014-09-02 18:07:02 +05:30
get api("/namespaces", admin)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2015-04-26 12:48:37 +05:30
expect(json_response).to be_an Array
expect(json_response.length).to eq(Namespace.count)
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it "admin: returns an array of matched namespaces" do
2017-08-17 22:00:37 +05:30
get api("/namespaces?search=#{group2.name}", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2015-09-11 14:41:01 +05:30
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
2017-08-17 22:00:37 +05:30
expect(json_response.last['path']).to eq(group2.path)
expect(json_response.last['full_path']).to eq(group2.full_path)
2015-09-11 14:41:01 +05:30
end
end
context "when authenticated as a regular user" do
2017-09-10 17:25:29 +05:30
it "returns correct attributes when user can admin group" do
group1.add_owner(user)
get api("/namespaces", user)
owned_group_response = json_response.find { |resource| resource['id'] == group1.id }
2018-11-18 11:00:15 +05:30
expect(owned_group_response.keys).to include('id', 'kind', 'name', 'path', 'full_path',
'parent_id', 'members_count_with_descendants')
2017-09-10 17:25:29 +05:30
end
it "returns correct attributes when user cannot admin group" do
group1.add_guest(user)
get api("/namespaces", user)
guest_group_response = json_response.find { |resource| resource['id'] == group1.id }
2018-11-18 11:00:15 +05:30
expect(guest_group_response.keys).to include('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
2017-09-10 17:25:29 +05:30
end
2016-09-13 17:45:13 +05:30
it "user: returns an array of namespaces" do
2015-09-11 14:41:01 +05:30
get api("/namespaces", user)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2015-09-11 14:41:01 +05:30
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
end
2016-09-13 17:45:13 +05:30
it "admin: returns an array of matched namespaces" do
2015-09-11 14:41:01 +05:30
get api("/namespaces?search=#{user.username}", user)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2015-09-11 14:41:01 +05:30
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
end
2014-09-02 18:07:02 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe 'GET /namespaces/:id' do
let(:owned_group) { group1 }
let(:user2) { create(:user) }
shared_examples 'can access namespace' do
it 'returns namespace details' do
get api("/namespaces/#{namespace_id}", request_actor)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-17 18:26:18 +05:30
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
shared_examples 'namespace reader' do
let(:requested_namespace) { owned_group }
before do
owned_group.add_owner(request_actor)
end
context 'when namespace exists' do
context 'when requested by ID' do
context 'when requesting group' do
let(:namespace_id) { owned_group.id }
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
end
context 'when requested by path' do
context 'when requesting group' do
let(:namespace_id) { owned_group.path }
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
end
end
context "when namespace doesn't exist" do
it 'returns not-found' do
2019-07-07 11:18:12 +05:30
get api('/namespaces/0', request_actor)
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
end
end
context 'when unauthenticated' do
it 'returns authentication error' do
get api("/namespaces/#{group1.id}")
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2018-03-17 18:26:18 +05:30
end
end
context 'when authenticated as regular user' do
let(:request_actor) { user }
context 'when requested namespace is not owned by user' do
context 'when requesting group' do
it 'returns not-found' do
get api("/namespaces/#{group2.id}", request_actor)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
end
context 'when requesting personal namespace' do
it 'returns not-found' do
get api("/namespaces/#{user2.namespace.id}", request_actor)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
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
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 }
it_behaves_like 'can access namespace'
end
end
context 'when requested namespace is owned by user' do
it_behaves_like 'namespace reader'
end
end
end
2014-09-02 18:07:02 +05:30
end