diff options
Diffstat (limited to 'spec/requests/api/namespaces_spec.rb')
-rw-r--r-- | spec/requests/api/namespaces_spec.rb | 74 |
1 files changed, 43 insertions, 31 deletions
diff --git a/spec/requests/api/namespaces_spec.rb b/spec/requests/api/namespaces_spec.rb index 44574caf54a..f268a092034 100644 --- a/spec/requests/api/namespaces_spec.rb +++ b/spec/requests/api/namespaces_spec.rb @@ -2,25 +2,27 @@ require 'spec_helper' -RSpec.describe API::Namespaces, feature_category: :subgroups do +RSpec.describe API::Namespaces, :aggregate_failures, feature_category: :subgroups do 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) } let_it_be(:project_namespace) { project.project_namespace } + let_it_be(:path) { "/namespaces" } describe "GET /namespaces" do context "when unauthenticated" do it "returns authentication error" do - get api("/namespaces") + get api(path) + expect(response).to have_gitlab_http_status(:unauthorized) end end context "when authenticated as admin" do it "returns correct attributes" do - get api("/namespaces", admin) + get api(path, admin, admin_mode: true) group_kind_json_response = json_response.find { |resource| resource['kind'] == 'group' } user_kind_json_response = json_response.find { |resource| resource['kind'] == 'user' } @@ -34,7 +36,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it "admin: returns an array of all namespaces" do - get api("/namespaces", admin) + get api(path, admin, admin_mode: true) expect(response).to have_gitlab_http_status(:ok) expect(response).to include_pagination_headers @@ -44,7 +46,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it "admin: returns an array of matched namespaces" do - get api("/namespaces?search=#{group2.name}", admin) + get api("/namespaces?search=#{group2.name}", admin, admin_mode: true) expect(response).to have_gitlab_http_status(:ok) expect(response).to include_pagination_headers @@ -59,7 +61,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do it "returns correct attributes when user can admin group" do group1.add_owner(user) - get api("/namespaces", user) + get api(path, user) owned_group_response = json_response.find { |resource| resource['id'] == group1.id } @@ -70,7 +72,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do it "returns correct attributes when user cannot admin group" do group1.add_guest(user) - get api("/namespaces", user) + get api(path, user) guest_group_response = json_response.find { |resource| resource['id'] == group1.id } @@ -78,7 +80,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it "user: returns an array of namespaces" do - get api("/namespaces", user) + get api(path, user) expect(response).to have_gitlab_http_status(:ok) expect(response).to include_pagination_headers @@ -115,9 +117,19 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do let_it_be(:user2) { create(:user) } - shared_examples 'can access namespace' do + it_behaves_like 'GET request permissions for admin mode' do + let(:path) { "/namespaces/#{group2.id}" } + let(:failed_status_code) { :not_found } + end + + it_behaves_like 'GET request permissions for admin mode' do + let(:path) { "/namespaces/#{user2.namespace.id}" } + let(:failed_status_code) { :not_found } + end + + shared_examples 'can access namespace' do |admin_mode: false| it 'returns namespace details' do - get api("/namespaces/#{namespace_id}", request_actor) + get api("#{path}/#{namespace_id}", request_actor, admin_mode: admin_mode) expect(response).to have_gitlab_http_status(:ok) @@ -153,7 +165,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do let(:namespace_id) { project_namespace.id } it 'returns not-found' do - get api("/namespaces/#{namespace_id}", request_actor) + get api("#{path}/#{namespace_id}", request_actor) expect(response).to have_gitlab_http_status(:not_found) end @@ -188,7 +200,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do context "when namespace doesn't exist" do it 'returns not-found' do - get api('/namespaces/0', request_actor) + get api("#{path}/0", request_actor) expect(response).to have_gitlab_http_status(:not_found) end @@ -197,13 +209,13 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do context 'when unauthenticated' do it 'returns authentication error' do - get api("/namespaces/#{group1.id}") + get api("#{path}/#{group1.id}") expect(response).to have_gitlab_http_status(:unauthorized) end it 'returns authentication error' do - get api("/namespaces/#{project_namespace.id}") + get api("#{path}/#{project_namespace.id}") expect(response).to have_gitlab_http_status(:unauthorized) end @@ -215,7 +227,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do 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) + get api("#{path}/#{group2.id}", request_actor) expect(response).to have_gitlab_http_status(:not_found) end @@ -223,7 +235,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do context 'when requesting personal namespace' do it 'returns not-found' do - get api("/namespaces/#{user2.namespace.id}", request_actor) + get api("#{path}/#{user2.namespace.id}", request_actor) expect(response).to have_gitlab_http_status(:not_found) end @@ -243,14 +255,14 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do let(:namespace_id) { group2.id } let(:requested_namespace) { group2 } - it_behaves_like 'can access namespace' + it_behaves_like 'can access namespace', admin_mode: true end context 'when requesting personal namespace' do let(:namespace_id) { user2.namespace.id } let(:requested_namespace) { user2.namespace } - it_behaves_like 'can access namespace' + it_behaves_like 'can access namespace', admin_mode: true end end @@ -269,7 +281,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do context 'when unauthenticated' do it 'returns authentication error' do - get api("/namespaces/#{namespace1.path}/exists") + get api("#{path}/#{namespace1.path}/exists") expect(response).to have_gitlab_http_status(:unauthorized) end @@ -278,7 +290,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do let(:namespace_id) { project_namespace.id } it 'returns authentication error' do - get api("/namespaces/#{project_namespace.path}/exists"), params: { parent_id: group2.id } + get api("#{path}/#{project_namespace.path}/exists"), params: { parent_id: group2.id } expect(response).to have_gitlab_http_status(:unauthorized) end @@ -290,12 +302,12 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do let(:current_user) { user } def request - get api("/namespaces/#{namespace1.path}/exists", current_user) + get api("#{path}/#{namespace1.path}/exists", current_user) end end it 'returns JSON indicating the namespace exists and a suggestion' do - get api("/namespaces/#{namespace1.path}/exists", user) + get api("#{path}/#{namespace1.path}/exists", user) expected_json = { exists: true, suggests: ["#{namespace1.path}1"] }.to_json expect(response).to have_gitlab_http_status(:ok) @@ -303,7 +315,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it 'supports dot in namespace path' do - get api("/namespaces/#{namespace_with_dot.path}/exists", user) + get api("#{path}/#{namespace_with_dot.path}/exists", user) expected_json = { exists: true, suggests: ["#{namespace_with_dot.path}1"] }.to_json expect(response).to have_gitlab_http_status(:ok) @@ -311,7 +323,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it 'returns JSON indicating the namespace does not exist without a suggestion' do - get api("/namespaces/non-existing-namespace/exists", user) + get api("#{path}/non-existing-namespace/exists", user) expected_json = { exists: false, suggests: [] }.to_json expect(response).to have_gitlab_http_status(:ok) @@ -319,7 +331,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it 'checks the existence of a namespace in case-insensitive manner' do - get api("/namespaces/#{namespace1.path.upcase}/exists", user) + get api("#{path}/#{namespace1.path.upcase}/exists", user) expected_json = { exists: true, suggests: ["#{namespace1.path.upcase}1"] }.to_json expect(response).to have_gitlab_http_status(:ok) @@ -327,7 +339,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it 'checks the existence within the parent namespace only' do - get api("/namespaces/#{namespace1sub.path}/exists", user), params: { parent_id: namespace1.id } + get api("#{path}/#{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) @@ -335,7 +347,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it 'ignores nested namespaces when checking for top-level namespace' do - get api("/namespaces/#{namespace1sub.path}/exists", user) + get api("#{path}/#{namespace1sub.path}/exists", user) expected_json = { exists: false, suggests: [] }.to_json expect(response).to have_gitlab_http_status(:ok) @@ -349,7 +361,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do create(:group, name: 'mygroup', path: 'mygroup', parent: namespace1) - get api("/namespaces/mygroup/exists", user), params: { parent_id: namespace1.id } + get api("#{path}/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. @@ -361,7 +373,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do end it 'ignores top-level namespaces when checking with parent_id' do - get api("/namespaces/#{namespace1.path}/exists", user), params: { parent_id: namespace1.id } + get api("#{path}/#{namespace1.path}/exists", user), params: { parent_id: namespace1.id } expected_json = { exists: false, suggests: [] }.to_json expect(response).to have_gitlab_http_status(:ok) @@ -369,7 +381,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups do 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 } + get api("#{path}/#{namespace2sub.path}/exists", user), params: { parent_id: namespace1.id } expected_json = { exists: false, suggests: [] }.to_json expect(response).to have_gitlab_http_status(:ok) @@ -380,7 +392,7 @@ RSpec.describe API::Namespaces, feature_category: :subgroups 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 } + get api("#{path}/#{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) |