diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 13:37:47 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 13:37:47 +0000 |
commit | aee0a117a889461ce8ced6fcf73207fe017f1d99 (patch) | |
tree | 891d9ef189227a8445d83f35c1b0fc99573f4380 /spec/requests/projects | |
parent | 8d46af3258650d305f53b819eabf7ab18d22f59e (diff) | |
download | gitlab-ce-aee0a117a889461ce8ced6fcf73207fe017f1d99.tar.gz |
Add latest changes from gitlab-org/gitlab@14-6-stable-eev14.6.0-rc42
Diffstat (limited to 'spec/requests/projects')
5 files changed, 321 insertions, 10 deletions
diff --git a/spec/requests/projects/google_cloud/service_accounts_controller_spec.rb b/spec/requests/projects/google_cloud/service_accounts_controller_spec.rb new file mode 100644 index 00000000000..6b4d1c490e2 --- /dev/null +++ b/spec/requests/projects/google_cloud/service_accounts_controller_spec.rb @@ -0,0 +1,184 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# Mock Types +MockGoogleOAuth2Credentials = Struct.new(:app_id, :app_secret) +MockServiceAccount = Struct.new(:project_id, :unique_id) + +RSpec.describe Projects::GoogleCloud::ServiceAccountsController do + let_it_be(:project) { create(:project, :public) } + + describe 'GET index' do + let_it_be(:url) { "#{project_google_cloud_service_accounts_path(project)}" } + + let(:user_guest) { create(:user) } + let(:user_developer) { create(:user) } + let(:user_maintainer) { create(:user) } + let(:user_creator) { project.creator } + + let(:unauthorized_members) { [user_guest, user_developer] } + let(:authorized_members) { [user_maintainer, user_creator] } + + before do + project.add_guest(user_guest) + project.add_developer(user_developer) + project.add_maintainer(user_maintainer) + end + + context 'when a public request is made' do + it 'returns not found on GET request' do + get url + + expect(response).to have_gitlab_http_status(:not_found) + end + + it 'returns not found on POST request' do + post url + + expect(response).to have_gitlab_http_status(:not_found) + end + end + + context 'when unauthorized members make requests' do + it 'returns not found on GET request' do + unauthorized_members.each do |unauthorized_member| + sign_in(unauthorized_member) + + get url + + expect(response).to have_gitlab_http_status(:not_found) + end + end + + it 'returns not found on POST request' do + unauthorized_members.each do |unauthorized_member| + sign_in(unauthorized_member) + + post url + + expect(response).to have_gitlab_http_status(:not_found) + end + end + end + + context 'when authorized members make requests' do + it 'redirects on GET request' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + get url + + expect(response).to redirect_to(assigns(:authorize_url)) + end + end + + it 'redirects on POST request' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + post url + + expect(response).to redirect_to(assigns(:authorize_url)) + end + end + + context 'and user has successfully completed the google oauth2 flow' do + before do + allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client| + allow(client).to receive(:validate_token).and_return(true) + allow(client).to receive(:list_projects).and_return([{}, {}, {}]) + allow(client).to receive(:create_service_account).and_return(MockServiceAccount.new(123, 456)) + allow(client).to receive(:create_service_account_key).and_return({}) + end + end + + it 'returns success on GET' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + get url + + expect(response).to have_gitlab_http_status(:ok) + end + end + + it 'returns success on POST' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + post url, params: { gcp_project: 'prj1', environment: 'env1' } + + expect(response).to redirect_to(project_google_cloud_index_path(project)) + end + end + end + + context 'but google returns client error' do + before do + allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client| + allow(client).to receive(:validate_token).and_return(true) + allow(client).to receive(:list_projects).and_raise(Google::Apis::ClientError.new('')) + allow(client).to receive(:create_service_account).and_raise(Google::Apis::ClientError.new('')) + allow(client).to receive(:create_service_account_key).and_raise(Google::Apis::ClientError.new('')) + end + end + + it 'renders gcp_error template on GET' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + get url + + expect(response).to render_template(:gcp_error) + end + end + + it 'renders gcp_error template on POST' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + post url, params: { gcp_project: 'prj1', environment: 'env1' } + + expect(response).to render_template(:gcp_error) + end + end + end + + context 'but gitlab instance is not configured for google oauth2' do + before do + unconfigured_google_oauth2 = MockGoogleOAuth2Credentials.new('', '') + allow(Gitlab::Auth::OAuth::Provider).to receive(:config_for) + .with('google_oauth2') + .and_return(unconfigured_google_oauth2) + end + + it 'returns forbidden' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + get url + + expect(response).to have_gitlab_http_status(:forbidden) + end + end + end + + context 'but feature flag is disabled' do + before do + stub_feature_flags(incubation_5mp_google_cloud: false) + end + + it 'returns not found' do + authorized_members.each do |authorized_member| + sign_in(authorized_member) + + get url + + expect(response).to have_gitlab_http_status(:not_found) + end + end + end + end + end +end diff --git a/spec/requests/projects/integrations/shimos_controller_spec.rb b/spec/requests/projects/integrations/shimos_controller_spec.rb new file mode 100644 index 00000000000..7322143f87e --- /dev/null +++ b/spec/requests/projects/integrations/shimos_controller_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Projects::Integrations::ShimosController do + let_it_be(:project) { create(:project) } + let_it_be(:user) { create(:user, developer_projects: [project]) } + let_it_be(:shimo_integration) { create(:shimo_integration, project: project) } + + before do + sign_in(user) + end + + describe 'GET #show' do + context 'when Shimo integration is inactive' do + before do + shimo_integration.update!(active: false) + end + + it 'returns 404 status' do + get project_integrations_shimo_path(project) + + expect(response).to have_gitlab_http_status(:not_found) + end + end + + context 'when Shimo integration is active' do + it 'renders the "show" template' do + get project_integrations_shimo_path(project) + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to render_template(:show) + expect(response.body).to include shimo_integration.external_wiki_url + end + end + end +end diff --git a/spec/requests/projects/merge_requests/context_commit_diffs_spec.rb b/spec/requests/projects/merge_requests/context_commit_diffs_spec.rb new file mode 100644 index 00000000000..434e6f19ff5 --- /dev/null +++ b/spec/requests/projects/merge_requests/context_commit_diffs_spec.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'Merge Requests Context Commit Diffs' do + let_it_be(:sha1) { "33f3729a45c02fc67d00adb1b8bca394b0e761d9" } + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + let_it_be(:merge_request) { create(:merge_request, target_project: project, source_project: project) } + let_it_be(:merge_request_context_commit) { create(:merge_request_context_commit, merge_request: merge_request, sha: sha1) } + + before do + project.add_maintainer(user) + sign_in(user) + end + + describe 'GET diffs_batch' do + let(:headers) { {} } + + shared_examples_for 'serializes diffs with expected arguments' do + it 'serializes paginated merge request diff collection' do + expect_next_instance_of(PaginatedDiffSerializer) do |instance| + expect(instance).to receive(:represent) + .with(an_instance_of(collection), expected_options) + .and_call_original + end + + subject + end + end + + def collection_arguments(pagination_data = {}) + { + environment: nil, + merge_request: merge_request, + commit: nil, + diff_view: :inline, + merge_ref_head_diff: nil, + allow_tree_conflicts: true, + pagination_data: { + total_pages: nil + }.merge(pagination_data) + } + end + + def go(extra_params = {}) + params = { + namespace_id: project.namespace.to_param, + project_id: project, + id: merge_request.iid, + only_context_commits: true, + page: 0, + per_page: 20, + format: 'json' + } + + get diffs_batch_namespace_project_json_merge_request_path(params.merge(extra_params)), headers: headers + end + + context 'with caching', :use_clean_rails_memory_store_caching do + subject { go(page: 0, per_page: 5) } + + context 'when the request has not been cached' do + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::Compare } + let(:expected_options) { collection_arguments } + end + end + + context 'when the request has already been cached' do + before do + go(page: 0, per_page: 5) + end + + it 'does not serialize diffs' do + expect_next_instance_of(PaginatedDiffSerializer) do |instance| + expect(instance).not_to receive(:represent) + end + + subject + end + + context 'with the different user' do + let(:another_user) { create(:user) } + + before do + project.add_maintainer(another_user) + sign_in(another_user) + end + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::Compare } + let(:expected_options) { collection_arguments } + end + end + end + end + end +end diff --git a/spec/requests/projects/merge_requests/diffs_spec.rb b/spec/requests/projects/merge_requests/diffs_spec.rb index 349cbf1b76c..ad50c39c65d 100644 --- a/spec/requests/projects/merge_requests/diffs_spec.rb +++ b/spec/requests/projects/merge_requests/diffs_spec.rb @@ -31,6 +31,7 @@ RSpec.describe 'Merge Requests Diffs' do { environment: nil, merge_request: merge_request, + commit: nil, diff_view: :inline, merge_ref_head_diff: nil, allow_tree_conflicts: true, diff --git a/spec/requests/projects/usage_quotas_spec.rb b/spec/requests/projects/usage_quotas_spec.rb index 114e9bd9f1e..6e449a21804 100644 --- a/spec/requests/projects/usage_quotas_spec.rb +++ b/spec/requests/projects/usage_quotas_spec.rb @@ -23,20 +23,10 @@ RSpec.describe 'Project Usage Quotas' do describe 'GET /:namespace/:project/usage_quotas' do it 'renders usage quotas path' do - mock_storage_app_data = { - project_path: project.full_path, - usage_quotas_help_page_path: help_page_path('user/usage_quotas'), - build_artifacts_help_page_path: help_page_path('ci/pipelines/job_artifacts', anchor: 'when-job-artifacts-are-deleted'), - packages_help_page_path: help_page_path('user/packages/package_registry/index.md', anchor: 'delete-a-package'), - repository_help_page_path: help_page_path('user/project/repository/reducing_the_repo_size_using_git'), - snippets_help_page_path: help_page_path('user/snippets', anchor: 'reduce-snippets-repository-size'), - wiki_help_page_path: help_page_path('administration/wikis/index.md', anchor: 'reduce-wiki-repository-size') - } get project_usage_quotas_path(project) expect(response).to have_gitlab_http_status(:ok) expect(response.body).to include(project_usage_quotas_path(project)) - expect(assigns[:storage_app_data]).to eq(mock_storage_app_data) expect(response.body).to include("Usage of project resources across the <strong>#{project.name}</strong> project") end |