diff options
Diffstat (limited to 'spec/serializers')
17 files changed, 316 insertions, 81 deletions
diff --git a/spec/serializers/ci/trigger_entity_spec.rb b/spec/serializers/ci/trigger_entity_spec.rb new file mode 100644 index 00000000000..b2f3337d166 --- /dev/null +++ b/spec/serializers/ci/trigger_entity_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::TriggerEntity do + let(:project) { create(:project) } + let(:trigger) { create(:ci_trigger, project: project, token: '237f3604900a4cd71ed06ef13e57b96d') } + let(:user) { create(:user) } + let(:entity) { described_class.new(trigger, current_user: user, project: project) } + + describe '#as_json' do + let(:as_json) { entity.as_json } + let(:project_trigger_path) { "/#{project.full_path}/-/triggers/#{trigger.id}" } + + it 'contains required fields' do + expect(as_json).to include( + :description, :owner, :last_used, :token, :has_token_exposed, :can_access_project + ) + end + + it 'contains user fields' do + expect(as_json[:owner].to_json).to match_schema('entities/user') + end + + context 'when current user can manage triggers' do + before do + project.add_maintainer(user) + end + + it 'returns short_token as token' do + expect(as_json[:token]).to eq(trigger.short_token) + end + + it 'contains project_trigger_path' do + expect(as_json[:project_trigger_path]).to eq(project_trigger_path) + end + + it 'does not contain edit_project_trigger_path' do + expect(as_json).not_to include(:edit_project_trigger_path) + end + + it 'returns has_token_exposed' do + expect(as_json[:has_token_exposed]).to eq(false) + end + end + + context 'when current user is the owner of the trigger' do + before do + project.add_maintainer(user) + trigger.update!(owner: user) + end + + it 'returns token as token' do + expect(as_json[:token]).to eq(trigger.token) + end + + it 'contains project_trigger_path' do + expect(as_json[:project_trigger_path]).to eq(project_trigger_path) + end + + it 'contains edit_project_trigger_path' do + expect(as_json[:edit_project_trigger_path]).to eq("#{project_trigger_path}/edit") + end + + it 'returns has_token_exposed' do + expect(as_json[:has_token_exposed]).to eq(true) + end + end + end +end diff --git a/spec/serializers/ci/trigger_serializer_spec.rb b/spec/serializers/ci/trigger_serializer_spec.rb new file mode 100644 index 00000000000..a669a8c3ed0 --- /dev/null +++ b/spec/serializers/ci/trigger_serializer_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::TriggerSerializer do + describe '#represent' do + let(:represent) { described_class.new.represent(trigger) } + + let(:trigger) { build_stubbed(:ci_trigger) } + + it 'matches schema' do + expect(represent.to_json).to match_schema('entities/trigger') + end + end +end diff --git a/spec/serializers/cluster_serializer_spec.rb b/spec/serializers/cluster_serializer_spec.rb index 04999975276..e65e97b6ae0 100644 --- a/spec/serializers/cluster_serializer_spec.rb +++ b/spec/serializers/cluster_serializer_spec.rb @@ -13,6 +13,7 @@ RSpec.describe ClusterSerializer do :cluster_type, :enabled, :environment_scope, + :id, :gitlab_managed_apps_logs_path, :enable_advanced_logs_querying, :kubernetes_errors, diff --git a/spec/serializers/diff_file_base_entity_spec.rb b/spec/serializers/diff_file_base_entity_spec.rb index 94c39e11790..99dbaff4b7e 100644 --- a/spec/serializers/diff_file_base_entity_spec.rb +++ b/spec/serializers/diff_file_base_entity_spec.rb @@ -3,10 +3,24 @@ require 'spec_helper' RSpec.describe DiffFileBaseEntity do - let(:project) { create(:project, :repository) } + include ProjectForksHelper + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + let(:repository) { project.repository } let(:entity) { described_class.new(diff_file, options).as_json } + shared_examples 'nil if removed source branch' do |key| + before do + allow(merge_request).to receive(:source_branch_exists?).and_return(false) + end + + specify do + expect(entity[key]).to eq(nil) + end + end + context 'submodule information for a' do let(:commit_sha) { "" } let(:commit) { project.commit(commit_sha) } @@ -67,7 +81,7 @@ RSpec.describe DiffFileBaseEntity do context 'edit_path' do let(:diff_file) { merge_request.diffs.diff_files.to_a.last } - let(:options) { { request: EntityRequest.new(current_user: create(:user)), merge_request: merge_request } } + let(:options) { { request: EntityRequest.new(current_user: user), merge_request: merge_request } } let(:params) { {} } shared_examples 'a diff file edit path to the source branch' do @@ -81,16 +95,7 @@ RSpec.describe DiffFileBaseEntity do let(:params) { { from_merge_request_iid: merge_request.iid } } it_behaves_like 'a diff file edit path to the source branch' - - context 'removed source branch' do - before do - allow(merge_request).to receive(:source_branch_exists?).and_return(false) - end - - it do - expect(entity[:edit_path]).to eq(nil) - end - end + it_behaves_like 'nil if removed source branch', :edit_path end context 'closed' do @@ -118,4 +123,30 @@ RSpec.describe DiffFileBaseEntity do end end end + + context 'ide_edit_path' do + let(:source_project) { project } + let(:merge_request) { create(:merge_request, iid: 123, target_project: target_project, source_project: source_project) } + let(:diff_file) { merge_request.diffs.diff_files.to_a.last } + let(:options) { { request: EntityRequest.new(current_user: user), merge_request: merge_request } } + let(:expected_merge_request_path) { "/-/ide/project/#{source_project.full_path}/merge_requests/#{merge_request.iid}" } + + context 'when source_project and target_project are the same' do + let(:target_project) { source_project } + + it_behaves_like 'nil if removed source branch', :ide_edit_path + + it 'returns the merge_request ide route' do + expect(entity[:ide_edit_path]).to eq expected_merge_request_path + end + end + + context 'when source_project and target_project are different' do + let(:target_project) { fork_project(source_project, source_project.owner, repository: true) } + + it 'returns the merge_request ide route with the target_project as param' do + expect(entity[:ide_edit_path]).to eq("#{expected_merge_request_path}?target_project=#{ERB::Util.url_encode(target_project.full_path)}") + end + end + end end diff --git a/spec/serializers/diffs_entity_spec.rb b/spec/serializers/diffs_entity_spec.rb index 7c59e4aed83..5928a1c24b3 100644 --- a/spec/serializers/diffs_entity_spec.rb +++ b/spec/serializers/diffs_entity_spec.rb @@ -68,15 +68,5 @@ RSpec.describe DiffsEntity do end end end - - context 'when code_navigation feature flag is disabled' do - it 'does not include code navigation properties' do - stub_feature_flags(code_navigation: false) - - expect(Gitlab::CodeNavigationPath).not_to receive(:new) - - expect(subject).not_to include(:definition_path_prefix) - end - end end end diff --git a/spec/serializers/feature_flag_entity_spec.rb b/spec/serializers/feature_flag_entity_spec.rb new file mode 100644 index 00000000000..21ecfe59c31 --- /dev/null +++ b/spec/serializers/feature_flag_entity_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe FeatureFlagEntity do + let(:feature_flag) { create(:operations_feature_flag, project: project) } + let(:project) { create(:project) } + let(:request) { double('request', current_user: user) } + let(:user) { create(:user) } + let(:entity) { described_class.new(feature_flag, request: request) } + + before do + project.add_developer(user) + end + + subject { entity.as_json } + + it 'has feature flag attributes' do + expect(subject).to include(:id, :active, :created_at, :updated_at, + :description, :name, :edit_path, :destroy_path) + end +end diff --git a/spec/serializers/feature_flag_serializer_spec.rb b/spec/serializers/feature_flag_serializer_spec.rb new file mode 100644 index 00000000000..fab8ca93b1b --- /dev/null +++ b/spec/serializers/feature_flag_serializer_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe FeatureFlagSerializer do + let(:serializer) { described_class.new(project: project, current_user: user) } + let(:user) { create(:user) } + let(:project) { create(:project) } + let(:feature_flags) { create_list(:operations_feature_flag, 3) } + + before do + project.add_developer(user) + end + + describe '#represent' do + subject { serializer.represent(feature_flags) } + + it 'includes feature flag attributes' do + is_expected.to all(include(:id, :active, :created_at, :updated_at, + :description, :name)) + end + end +end diff --git a/spec/serializers/feature_flag_summary_entity_spec.rb b/spec/serializers/feature_flag_summary_entity_spec.rb new file mode 100644 index 00000000000..385a9deb2d7 --- /dev/null +++ b/spec/serializers/feature_flag_summary_entity_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe FeatureFlagSummaryEntity do + let!(:feature_flag) { create(:operations_feature_flag, project: project) } + let(:project) { create(:project) } + let(:request) { double('request', current_user: user) } + let(:user) { create(:user) } + let(:entity) { described_class.new(project, request: request) } + + before do + project.add_developer(user) + end + + subject { entity.as_json } + + it 'has summary information' do + expect(subject).to include(:count) + end +end diff --git a/spec/serializers/feature_flag_summary_serializer_spec.rb b/spec/serializers/feature_flag_summary_serializer_spec.rb new file mode 100644 index 00000000000..79cef6765f7 --- /dev/null +++ b/spec/serializers/feature_flag_summary_serializer_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe FeatureFlagSummarySerializer do + let(:serializer) { described_class.new(project: project, current_user: user) } + let(:user) { create(:user) } + let(:project) { create(:project) } + let!(:feature_flags) { create(:operations_feature_flag, project: project) } + + before do + project.add_developer(user) + end + + describe '#represent' do + subject { serializer.represent(project) } + + it 'has summary information' do + expect(subject).to include(:count) + end + end +end diff --git a/spec/serializers/feature_flags_client_serializer_spec.rb b/spec/serializers/feature_flags_client_serializer_spec.rb new file mode 100644 index 00000000000..3746142a3f1 --- /dev/null +++ b/spec/serializers/feature_flags_client_serializer_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe FeatureFlagsClientSerializer do + let(:project) { create(:project) } + let(:feature_flags_client) { project.create_operations_feature_flags_client! } + let(:serializer) { described_class.new } + + describe '#represent_token' do + subject { serializer.represent_token(feature_flags_client).to_json } + + it 'includes feature flags client token' do + expect(subject).to match_schema('feature_flags_client_token') + end + end +end diff --git a/spec/serializers/group_group_link_entity_spec.rb b/spec/serializers/group_group_link_entity_spec.rb index 8384563e3e6..9affe4af381 100644 --- a/spec/serializers/group_group_link_entity_spec.rb +++ b/spec/serializers/group_group_link_entity_spec.rb @@ -5,9 +5,27 @@ require 'spec_helper' RSpec.describe GroupGroupLinkEntity do include_context 'group_group_link' - subject(:json) { described_class.new(group_group_link).to_json } + let_it_be(:current_user) { create(:user) } + let(:entity) { described_class.new(group_group_link) } + + before do + allow(entity).to receive(:current_user).and_return(current_user) + end it 'matches json schema' do - expect(json).to match_schema('entities/group_group_link') + expect(entity.to_json).to match_schema('entities/group_group_link') + end + + context 'a user with :admin_group_member permissions' do + before do + allow(entity).to receive(:can?).with(current_user, :admin_group_member, shared_group).and_return(true) + end + + it 'sets `can_update` and `can_remove` to `true`' do + json = entity.as_json + + expect(json[:can_update]).to be true + expect(json[:can_remove]).to be true + end end end diff --git a/spec/serializers/merge_request_poll_cached_widget_entity_spec.rb b/spec/serializers/merge_request_poll_cached_widget_entity_spec.rb index 51564de6041..031dc729a79 100644 --- a/spec/serializers/merge_request_poll_cached_widget_entity_spec.rb +++ b/spec/serializers/merge_request_poll_cached_widget_entity_spec.rb @@ -3,12 +3,11 @@ require 'spec_helper' RSpec.describe MergeRequestPollCachedWidgetEntity do - include ProjectForksHelper using RSpec::Parameterized::TableSyntax - let(:project) { create :project, :repository } - let(:resource) { create(:merge_request, source_project: project, target_project: project) } - let(:user) { create(:user) } + let_it_be(:project, refind: true) { create :project, :repository } + let_it_be(:resource, refind: true) { create(:merge_request, source_project: project, target_project: project) } + let_it_be(:user) { create(:user) } let(:request) { double('request', current_user: user, project: project) } @@ -174,8 +173,6 @@ RSpec.describe MergeRequestPollCachedWidgetEntity do end context 'when auto merge is not enabled' do - let(:resource) { create(:merge_request) } - it 'returns auto merge related information' do expect(subject[:auto_merge_enabled]).to be_falsy end @@ -215,15 +212,55 @@ RSpec.describe MergeRequestPollCachedWidgetEntity do expect(subject[:commits_without_merge_commits].size).to eq(12) end end + end - context 'when merge request is not mergeable' do - before do - allow(resource).to receive(:mergeable?).and_return(false) + describe 'pipeline' do + let_it_be(:pipeline) { create(:ci_empty_pipeline, project: project, ref: resource.source_branch, sha: resource.source_branch_sha, head_pipeline_of: resource) } + + before do + allow_any_instance_of(MergeRequestPresenter).to receive(:can?).and_call_original + allow_any_instance_of(MergeRequestPresenter).to receive(:can?).with(user, :read_pipeline, anything).and_return(can_access) + end + + context 'when user has access to pipelines' do + let(:can_access) { true } + + context 'when is up to date' do + let(:req) { double('request', current_user: user, project: project) } + + it 'returns pipeline' do + pipeline_payload = + MergeRequests::PipelineEntity + .represent(pipeline, request: req) + .as_json + + expect(subject[:pipeline]).to eq(pipeline_payload) + end + + context 'when merge_request_cached_pipeline_serializer is disabled' do + it 'does not return pipeline' do + stub_feature_flags(merge_request_cached_pipeline_serializer: false) + + expect(subject[:pipeline]).to be_nil + end + end + end + + context 'when user does not have access to pipelines' do + let(:can_access) { false } + let(:req) { double('request', current_user: user, project: project) } + + it 'does not have pipeline' do + expect(subject[:pipeline]).to eq(nil) + end end - it 'does not have default_squash_commit_message and commits_without_merge_commits' do - expect(subject[:default_squash_commit_message]).to eq(nil) - expect(subject[:commits_without_merge_commits]).to eq(nil) + context 'when is not up to date' do + it 'returns nil' do + pipeline.update!(sha: "not up to date") + + expect(subject[:pipeline]).to eq(nil) + end end end end diff --git a/spec/serializers/merge_request_poll_widget_entity_spec.rb b/spec/serializers/merge_request_poll_widget_entity_spec.rb index 161940dd01a..1e5a8915da0 100644 --- a/spec/serializers/merge_request_poll_widget_entity_spec.rb +++ b/spec/serializers/merge_request_poll_widget_entity_spec.rb @@ -44,20 +44,6 @@ RSpec.describe MergeRequestPollWidgetEntity do expect(subject[:merge_pipeline]).to eq(pipeline_payload) end - context 'when merge_request_short_pipeline_serializer is disabled' do - it 'returns detailed info about pipeline' do - stub_feature_flags(merge_request_short_pipeline_serializer: false) - - pipeline.reload - pipeline_payload = - PipelineDetailsEntity - .represent(pipeline, request: request) - .as_json - - expect(subject[:merge_pipeline]).to eq(pipeline_payload) - end - end - context 'when user cannot read pipelines on target project' do before do project.add_guest(user) @@ -236,21 +222,16 @@ RSpec.describe MergeRequestPollWidgetEntity do context 'when is up to date' do let(:req) { double('request', current_user: user, project: project) } - it 'returns pipeline' do - pipeline_payload = - MergeRequests::PipelineEntity - .represent(pipeline, request: req) - .as_json - - expect(subject[:pipeline]).to eq(pipeline_payload) + it 'does not return pipeline' do + expect(subject[:pipeline]).to be_nil end - context 'when merge_request_short_pipeline_serializer is disabled' do + context 'when merge_request_cached_pipeline_serializer is disabled' do it 'returns detailed info about pipeline' do - stub_feature_flags(merge_request_short_pipeline_serializer: false) + stub_feature_flags(merge_request_cached_pipeline_serializer: false) pipeline_payload = - PipelineDetailsEntity + MergeRequests::PipelineEntity .represent(pipeline, request: req) .as_json @@ -276,10 +257,6 @@ RSpec.describe MergeRequestPollWidgetEntity do let(:result) { false } let(:req) { double('request', current_user: user, project: project) } - it 'does not have pipeline' do - expect(subject[:pipeline]).to eq(nil) - end - it 'does not return ci_status' do expect(subject[:ci_status]).to eq(nil) end diff --git a/spec/serializers/merge_request_widget_entity_spec.rb b/spec/serializers/merge_request_widget_entity_spec.rb index 1432c4499ae..a1017d05bc3 100644 --- a/spec/serializers/merge_request_widget_entity_spec.rb +++ b/spec/serializers/merge_request_widget_entity_spec.rb @@ -272,7 +272,7 @@ RSpec.describe MergeRequestWidgetEntity do describe 'user callouts' do context 'when suggest pipeline feature is enabled' do before do - stub_feature_flags(suggest_pipeline: true) + stub_experiment(suggest_pipeline: true) end it 'provides a valid path value for user callout path' do @@ -308,7 +308,7 @@ RSpec.describe MergeRequestWidgetEntity do context 'when suggest pipeline feature is not enabled' do before do - stub_feature_flags(suggest_pipeline: false) + stub_experiment(suggest_pipeline: false) end it 'provides no valid value for user callout path' do diff --git a/spec/serializers/paginated_diff_entity_spec.rb b/spec/serializers/paginated_diff_entity_spec.rb index a2c58baed55..821ed34d3ec 100644 --- a/spec/serializers/paginated_diff_entity_spec.rb +++ b/spec/serializers/paginated_diff_entity_spec.rb @@ -31,14 +31,4 @@ RSpec.describe PaginatedDiffEntity do total_pages: 7 ) end - - context 'when code_navigation feature flag is disabled' do - it 'does not execute Gitlab::CodeNavigationPath' do - stub_feature_flags(code_navigation: false) - - expect(Gitlab::CodeNavigationPath).not_to receive(:new) - - subject - end - end end diff --git a/spec/serializers/pipeline_serializer_spec.rb b/spec/serializers/pipeline_serializer_spec.rb index b42a4f6ad3f..e0f6ab68034 100644 --- a/spec/serializers/pipeline_serializer_spec.rb +++ b/spec/serializers/pipeline_serializer_spec.rb @@ -155,7 +155,7 @@ RSpec.describe PipelineSerializer do it 'verifies number of queries', :request_store do recorded = ActiveRecord::QueryRecorder.new { subject } - expected_queries = Gitlab.ee? ? 43 : 40 + expected_queries = Gitlab.ee? ? 39 : 36 expect(recorded.count).to be_within(2).of(expected_queries) expect(recorded.cached_count).to eq(0) @@ -176,7 +176,7 @@ RSpec.describe PipelineSerializer do # pipeline. With the same ref this check is cached but if refs are # different then there is an extra query per ref # https://gitlab.com/gitlab-org/gitlab-foss/issues/46368 - expected_queries = Gitlab.ee? ? 49 : 46 + expected_queries = Gitlab.ee? ? 42 : 39 expect(recorded.count).to be_within(2).of(expected_queries) expect(recorded.cached_count).to eq(0) @@ -199,11 +199,10 @@ RSpec.describe PipelineSerializer do it 'verifies number of queries', :request_store do recorded = ActiveRecord::QueryRecorder.new { subject } - # 99 queries by default + 2 related to preloading - # :source_pipeline and :source_job # Existing numbers are high and require performance optimization + # Ongoing issue: # https://gitlab.com/gitlab-org/gitlab/-/issues/225156 - expected_queries = Gitlab.ee? ? 95 : 86 + expected_queries = Gitlab.ee? ? 85 : 76 expect(recorded.count).to be_within(2).of(expected_queries) expect(recorded.cached_count).to eq(0) diff --git a/spec/serializers/test_case_entity_spec.rb b/spec/serializers/test_case_entity_spec.rb index 32e9562f4c1..45e63e3feec 100644 --- a/spec/serializers/test_case_entity_spec.rb +++ b/spec/serializers/test_case_entity_spec.rb @@ -19,6 +19,7 @@ RSpec.describe TestCaseEntity do expect(subject[:status]).to eq('success') expect(subject[:name]).to eq('Test#sum when a is 1 and b is 3 returns summary') expect(subject[:classname]).to eq('spec.test_spec') + expect(subject[:file]).to eq('./spec/test_spec.rb') expect(subject[:execution_time]).to eq(1.11) end end @@ -30,6 +31,7 @@ RSpec.describe TestCaseEntity do expect(subject[:status]).to eq('failed') expect(subject[:name]).to eq('Test#sum when a is 1 and b is 3 returns summary') expect(subject[:classname]).to eq('spec.test_spec') + expect(subject[:file]).to eq('./spec/test_spec.rb') expect(subject[:execution_time]).to eq(2.22) end end |