diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-20 12:09:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-20 12:09:13 +0300 |
commit | 1ac794623a8be5dee111716a44dd04ff708f3541 (patch) | |
tree | 6c18f9fbe0bd9978bd3e8d9b083d3a0ca180686e /spec | |
parent | 5247fe0bef72fa922841a79d5dbefb47d95112fa (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/projects/forks_controller_spec.rb | 11 | ||||
-rw-r--r-- | spec/finders/fork_targets_finder_spec.rb | 35 | ||||
-rw-r--r-- | spec/frontend/create_cluster/gke_cluster/stores/getters_spec.js (renamed from spec/javascripts/create_cluster/gke_cluster/stores/getters_spec.js) | 0 | ||||
-rw-r--r-- | spec/models/namespace_spec.rb | 18 | ||||
-rw-r--r-- | spec/requests/api/projects_spec.rb | 67 | ||||
-rw-r--r-- | spec/services/projects/fork_service_spec.rb | 55 |
6 files changed, 150 insertions, 36 deletions
diff --git a/spec/controllers/projects/forks_controller_spec.rb b/spec/controllers/projects/forks_controller_spec.rb index e6a68459a84..e362790cd3c 100644 --- a/spec/controllers/projects/forks_controller_spec.rb +++ b/spec/controllers/projects/forks_controller_spec.rb @@ -209,6 +209,17 @@ describe Projects::ForksController do expect(response).to redirect_to(namespace_project_import_path(user.namespace, project)) end + context 'when target namespace is not valid for forking' do + let(:params) { super().merge(namespace_key: another_group.id) } + let(:another_group) { create :group } + + it 'responds with :not_found' do + subject + + expect(response).to have_gitlab_http_status(:not_found) + end + end + context 'continue params' do let(:params) do { diff --git a/spec/finders/fork_targets_finder_spec.rb b/spec/finders/fork_targets_finder_spec.rb new file mode 100644 index 00000000000..e7110c33071 --- /dev/null +++ b/spec/finders/fork_targets_finder_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ForkTargetsFinder do + subject(:finder) { described_class.new(project, user) } + + let(:project) { create(:project, namespace: create(:group)) } + let(:user) { create(:user) } + let!(:maintained_group) do + create(:group).tap { |g| g.add_maintainer(user) } + end + let!(:owned_group) do + create(:group).tap { |g| g.add_owner(user) } + end + let!(:developer_group) do + create(:group).tap { |g| g.add_developer(user) } + end + let!(:reporter_group) do + create(:group).tap { |g| g.add_reporter(user) } + end + let!(:guest_group) do + create(:group).tap { |g| g.add_guest(user) } + end + + before do + project.namespace.add_owner(user) + end + + describe '#execute' do + it 'returns all user manageable namespaces except project namespace' do + expect(finder.execute).to match_array([user.namespace, maintained_group, owned_group]) + end + end +end diff --git a/spec/javascripts/create_cluster/gke_cluster/stores/getters_spec.js b/spec/frontend/create_cluster/gke_cluster/stores/getters_spec.js index 39106c3f6ca..39106c3f6ca 100644 --- a/spec/javascripts/create_cluster/gke_cluster/stores/getters_spec.js +++ b/spec/frontend/create_cluster/gke_cluster/stores/getters_spec.js diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index 740385bbd54..54e54366c5a 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -983,6 +983,24 @@ describe Namespace do expect(virtual_domain.lookup_paths).not_to be_empty end end + + it 'preloads project_feature and route' do + project2 = create(:project, namespace: namespace) + project3 = create(:project, namespace: namespace) + + project.mark_pages_as_deployed + project2.mark_pages_as_deployed + project3.mark_pages_as_deployed + + virtual_domain = namespace.pages_virtual_domain + + queries = ActiveRecord::QueryRecorder.new { virtual_domain.lookup_paths } + + # 1 to load projects + # 1 to preload project features + # 1 to load routes + expect(queries.count).to eq(3) + end end end diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 97b61b44856..c78a1d4ec62 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -2698,20 +2698,14 @@ describe API::Projects do create(:project, :repository, creator: user, namespace: user.namespace) end - let(:group) { create(:group) } - let(:group2) do - group = create(:group, name: 'group2_name') - group.add_maintainer(user2) - group - end - - let(:group3) do - group = create(:group, name: 'group3_name', parent: group2) - group.add_owner(user2) - group - end + let(:group) { create(:group, :public) } + let(:group2) { create(:group, name: 'group2_name') } + let(:group3) { create(:group, name: 'group3_name', parent: group2) } before do + group.add_guest(user2) + group2.add_maintainer(user2) + group3.add_owner(user2) project.add_reporter(user2) project2.add_reporter(user2) end @@ -2720,7 +2714,7 @@ describe API::Projects do it 'forks if user has sufficient access to project' do post api("/projects/#{project.id}/fork", user2) - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['name']).to eq(project.name) expect(json_response['path']).to eq(project.path) expect(json_response['owner']['id']).to eq(user2.id) @@ -2733,7 +2727,7 @@ describe API::Projects do it 'forks if user is admin' do post api("/projects/#{project.id}/fork", admin) - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['name']).to eq(project.name) expect(json_response['path']).to eq(project.path) expect(json_response['owner']['id']).to eq(admin.id) @@ -2747,14 +2741,17 @@ describe API::Projects do new_user = create(:user) post api("/projects/#{project.id}/fork", new_user) - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(:not_found) expect(json_response['message']).to eq('404 Project Not Found') end it 'fails if forked project exists in the user namespace' do - post api("/projects/#{project.id}/fork", user) + new_project = create(:project, name: project.name, path: project.path) + new_project.add_reporter(user) + + post api("/projects/#{new_project.id}/fork", user) - expect(response).to have_gitlab_http_status(409) + expect(response).to have_gitlab_http_status(:conflict) expect(json_response['message']['name']).to eq(['has already been taken']) expect(json_response['message']['path']).to eq(['has already been taken']) end @@ -2762,48 +2759,48 @@ describe API::Projects do it 'fails if project to fork from does not exist' do post api('/projects/424242/fork', user) - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(:not_found) expect(json_response['message']).to eq('404 Project Not Found') end it 'forks with explicit own user namespace id' do post api("/projects/#{project.id}/fork", user2), params: { namespace: user2.namespace.id } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['owner']['id']).to eq(user2.id) end it 'forks with explicit own user name as namespace' do post api("/projects/#{project.id}/fork", user2), params: { namespace: user2.username } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['owner']['id']).to eq(user2.id) end it 'forks to another user when admin' do post api("/projects/#{project.id}/fork", admin), params: { namespace: user2.username } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['owner']['id']).to eq(user2.id) end it 'fails if trying to fork to another user when not admin' do post api("/projects/#{project.id}/fork", user2), params: { namespace: admin.namespace.id } - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(:not_found) end it 'fails if trying to fork to non-existent namespace' do post api("/projects/#{project.id}/fork", user2), params: { namespace: 42424242 } - expect(response).to have_gitlab_http_status(404) - expect(json_response['message']).to eq('404 Target Namespace Not Found') + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['message']).to eq('404 Namespace Not Found') end it 'forks to owned group' do post api("/projects/#{project.id}/fork", user2), params: { namespace: group2.name } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['namespace']['name']).to eq(group2.name) end @@ -2811,7 +2808,7 @@ describe API::Projects do full_path = "#{group2.path}/#{group3.path}" post api("/projects/#{project.id}/fork", user2), params: { namespace: full_path } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['namespace']['name']).to eq(group3.name) expect(json_response['namespace']['full_path']).to eq(full_path) end @@ -2819,20 +2816,21 @@ describe API::Projects do it 'fails to fork to not owned group' do post api("/projects/#{project.id}/fork", user2), params: { namespace: group.name } - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['message']).to eq("404 Target Namespace Not Found") end it 'forks to not owned group when admin' do post api("/projects/#{project.id}/fork", admin), params: { namespace: group.name } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['namespace']['name']).to eq(group.name) end it 'accepts a path for the target project' do post api("/projects/#{project.id}/fork", user2), params: { path: 'foobar' } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['name']).to eq(project.name) expect(json_response['path']).to eq('foobar') expect(json_response['owner']['id']).to eq(user2.id) @@ -2846,14 +2844,14 @@ describe API::Projects do post api("/projects/#{project.id}/fork", user2), params: { path: 'foobar' } post api("/projects/#{project2.id}/fork", user2), params: { path: 'foobar' } - expect(response).to have_gitlab_http_status(409) + expect(response).to have_gitlab_http_status(:conflict) expect(json_response['message']['path']).to eq(['has already been taken']) end it 'accepts a name for the target project' do post api("/projects/#{project.id}/fork", user2), params: { name: 'My Random Project' } - expect(response).to have_gitlab_http_status(201) + expect(response).to have_gitlab_http_status(:created) expect(json_response['name']).to eq('My Random Project') expect(json_response['path']).to eq(project.path) expect(json_response['owner']['id']).to eq(user2.id) @@ -2867,7 +2865,7 @@ describe API::Projects do post api("/projects/#{project.id}/fork", user2), params: { name: 'My Random Project' } post api("/projects/#{project2.id}/fork", user2), params: { name: 'My Random Project' } - expect(response).to have_gitlab_http_status(409) + expect(response).to have_gitlab_http_status(:conflict) expect(json_response['message']['name']).to eq(['has already been taken']) end end @@ -2876,7 +2874,7 @@ describe API::Projects do it 'returns authentication error' do post api("/projects/#{project.id}/fork") - expect(response).to have_gitlab_http_status(401) + expect(response).to have_gitlab_http_status(:unauthorized) expect(json_response['message']).to eq('401 Unauthorized') end end @@ -2890,8 +2888,7 @@ describe API::Projects do it 'denies project to be forked' do post api("/projects/#{project.id}/fork", admin) - expect(response).to have_gitlab_http_status(409) - expect(json_response['message']['forked_from_project_id']).to eq(['is forbidden']) + expect(response).to have_gitlab_http_status(:not_found) end end end diff --git a/spec/services/projects/fork_service_spec.rb b/spec/services/projects/fork_service_spec.rb index e14f1abf018..fa6d42369c8 100644 --- a/spec/services/projects/fork_service_spec.rb +++ b/spec/services/projects/fork_service_spec.rb @@ -275,6 +275,7 @@ describe Projects::ForkService do context 'fork project for group when user not owner' do it 'group developer fails to fork project into the group' do to_project = fork_project(@project, @developer, @opts) + expect(to_project.errors[:namespace]).to eq(['is not valid']) end end @@ -336,7 +337,9 @@ describe Projects::ForkService do context 'when linking fork to an existing project' do let(:fork_from_project) { create(:project, :public) } let(:fork_to_project) { create(:project, :public) } - let(:user) { create(:user) } + let(:user) do + create(:user).tap { |u| fork_to_project.add_maintainer(u) } + end subject { described_class.new(fork_from_project, user) } @@ -387,4 +390,54 @@ describe Projects::ForkService do end end end + + describe '#valid_fork_targets' do + let(:finder_mock) { instance_double('ForkTargetsFinder', execute: ['finder_return_value']) } + let(:current_user) { instance_double('User') } + let(:project) { instance_double('Project') } + + before do + allow(ForkTargetsFinder).to receive(:new).with(project, current_user).and_return(finder_mock) + end + + it 'returns whatever finder returns' do + expect(described_class.new(project, current_user).valid_fork_targets).to eq ['finder_return_value'] + end + end + + describe '#valid_fork_target?' do + subject { described_class.new(project, user, params).valid_fork_target? } + + let(:project) { Project.new } + let(:params) { {} } + + context 'when current user is an admin' do + let(:user) { build(:user, :admin) } + + it { is_expected.to be_truthy } + end + + context 'when current_user is not an admin' do + let(:user) { create(:user) } + + let(:finder_mock) { instance_double('ForkTargetsFinder', execute: [user.namespace]) } + let(:project) { create(:project) } + + before do + allow(ForkTargetsFinder).to receive(:new).with(project, user).and_return(finder_mock) + end + + context 'when target namespace is in valid fork targets' do + let(:params) { { namespace: user.namespace } } + + it { is_expected.to be_truthy } + end + + context 'when target namespace is not in valid fork targets' do + let(:params) { { namespace: create(:group) } } + + it { is_expected.to be_falsey } + end + end + end end |