diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /spec/serializers | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) | |
download | gitlab-ce-85dc423f7090da0a52c73eb66faf22ddb20efff9.tar.gz |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'spec/serializers')
25 files changed, 600 insertions, 73 deletions
diff --git a/spec/serializers/analytics_build_entity_spec.rb b/spec/serializers/analytics_build_entity_spec.rb index 20bd017d1cf..09804681f5d 100644 --- a/spec/serializers/analytics_build_entity_spec.rb +++ b/spec/serializers/analytics_build_entity_spec.rb @@ -16,7 +16,7 @@ RSpec.describe AnalyticsBuildEntity do subject { entity.as_json } around do |example| - Timecop.freeze { example.run } + freeze_time { example.run } end it 'contains the URL' do diff --git a/spec/serializers/build_details_entity_spec.rb b/spec/serializers/build_details_entity_spec.rb index 3166c08ff4e..5d29452e91c 100644 --- a/spec/serializers/build_details_entity_spec.rb +++ b/spec/serializers/build_details_entity_spec.rb @@ -188,25 +188,31 @@ RSpec.describe BuildDetailsEntity do context 'when the build has expired artifacts' do let!(:build) { create(:ci_build, :artifacts, artifacts_expire_at: 7.days.ago) } - it 'does not expose any artifact actions path' do - expect(subject[:artifact].keys).not_to include(:download_path, :browse_path, :keep_path) - end + context 'when pipeline is unlocked' do + before do + build.pipeline.unlocked! + end + + it 'artifact locked is false' do + expect(subject.dig(:artifact, :locked)).to eq(false) + end - it 'artifact locked is false' do - expect(subject.dig(:artifact, :locked)).to eq(false) + it 'does not expose any artifact actions path' do + expect(subject[:artifact].keys).not_to include(:download_path, :browse_path, :keep_path) + end end context 'when the pipeline is artifacts_locked' do before do - build.pipeline.update!(locked: :artifacts_locked) + build.pipeline.artifacts_locked! end it 'artifact locked is true' do expect(subject.dig(:artifact, :locked)).to eq(true) end - it 'exposes download and browse artifact actions path' do - expect(subject[:artifact].keys).to include(:download_path, :browse_path) + it 'exposes download, browse and keep artifact actions path' do + expect(subject[:artifact].keys).to include(:download_path, :browse_path, :keep_path) end end end diff --git a/spec/serializers/ci/lint/job_entity_spec.rb b/spec/serializers/ci/lint/job_entity_spec.rb new file mode 100644 index 00000000000..2ef86cfd004 --- /dev/null +++ b/spec/serializers/ci/lint/job_entity_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::Lint::JobEntity, :aggregate_failures do + describe '#represent' do + let(:job) do + { + name: 'rspec', + stage: 'test', + before_script: ['bundle install', 'bundle exec rake db:create'], + script: ["rake spec"], + after_script: ["rake spec"], + tag_list: %w[ruby postgres], + environment: { name: 'hello', url: 'world' }, + when: 'on_success', + allow_failure: false, + except: { refs: ["branches"] }, + only: { refs: ["branches"] }, + variables: { hello: 'world' } + } + end + + subject(:serialized_job_result) { described_class.new(job).as_json } + + it 'exposes job data' do + expect(serialized_job_result.keys).to contain_exactly( + :name, + :stage, + :before_script, + :script, + :after_script, + :tag_list, + :environment, + :when, + :allow_failure, + :only, + :except + ) + expect(serialized_job_result.keys).not_to include(:variables) + end + end +end diff --git a/spec/serializers/ci/lint/result_entity_spec.rb b/spec/serializers/ci/lint/result_entity_spec.rb new file mode 100644 index 00000000000..7b5465a3649 --- /dev/null +++ b/spec/serializers/ci/lint/result_entity_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::Lint::ResultEntity do + describe '#represent' do + let(:yaml_content) { YAML.dump({ rspec: { script: 'test', tags: 'mysql' } }) } + let(:result) { Gitlab::Ci::YamlProcessor.new(yaml_content).execute } + + subject(:serialized_linting_result) { described_class.new(result).as_json } + + it 'serializes with lint result entity' do + expect(serialized_linting_result.keys).to include(:valid, :errors, :jobs, :warnings) + end + end +end diff --git a/spec/serializers/ci/lint/result_serializer_spec.rb b/spec/serializers/ci/lint/result_serializer_spec.rb new file mode 100644 index 00000000000..7aa95a574bf --- /dev/null +++ b/spec/serializers/ci/lint/result_serializer_spec.rb @@ -0,0 +1,261 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::Lint::ResultSerializer, :aggregate_failures do + let_it_be(:project) { create(:project, :repository) } + let(:result) do + Gitlab::Ci::Lint + .new(project: project, current_user: project.owner) + .validate(yaml_content, dry_run: false) + end + + let(:first_job) { linting_result[:jobs].first } + let(:serialized_linting_result) { linting_result.to_json } + + subject(:linting_result) { described_class.new.represent(result) } + + shared_examples 'matches schema' do + it { expect(serialized_linting_result).to match_schema('entities/lint_result_entity') } + end + + context 'when config is invalid' do + let(:yaml_content) { YAML.dump({ rspec: { script: 'test', tags: 'mysql' } }) } + + it_behaves_like 'matches schema' + + it 'returns expected validity' do + expect(linting_result[:valid]).to eq(false) + expect(linting_result[:errors]).to eq(['jobs:rspec:tags config should be an array of strings']) + expect(linting_result[:warnings]).to eq([]) + end + + it 'returns job data' do + expect(linting_result[:jobs]).to eq([]) + end + end + + context 'when config is valid' do + let(:yaml_content) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) } + + it_behaves_like 'matches schema' + + it 'returns expected validity' do + expect(linting_result[:valid]).to eq(true) + expect(linting_result[:errors]).to eq([]) + expect(linting_result[:warnings]).to eq([]) + end + + it 'returns job data' do + expect(first_job[:name]).to eq('rspec') + expect(first_job[:stage]).to eq('test') + expect(first_job[:before_script]).to eq(['bundle install', 'bundle exec rake db:create']) + expect(first_job[:script]).to eq(['rake spec']) + expect(first_job[:after_script]).to eq([]) + expect(first_job[:tag_list]).to eq(%w[ruby postgres]) + expect(first_job[:environment]).to eq(nil) + expect(first_job[:when]).to eq('on_success') + expect(first_job[:allow_failure]).to eq(false) + expect(first_job[:only]).to eq(refs: ['branches']) + expect(first_job[:except]).to eq(nil) + end + + context 'when dry run is enabled' do + let(:result) do + Gitlab::Ci::Lint + .new(project: project, current_user: project.owner) + .validate(yaml_content, dry_run: true) + end + + it_behaves_like 'matches schema' + + it 'returns expected validity' do + expect(linting_result[:valid]).to eq(true) + expect(linting_result[:errors]).to eq([]) + expect(linting_result[:warnings]).to eq([]) + end + + it 'returns job data' do + expect(first_job[:name]).to eq('rspec') + expect(first_job[:stage]).to eq('test') + expect(first_job[:before_script]).to eq(['bundle install', 'bundle exec rake db:create']) + expect(first_job[:script]).to eq(['rake spec']) + expect(first_job[:after_script]).to eq([]) + expect(first_job[:tag_list]).to eq(%w[ruby postgres]) + expect(first_job[:environment]).to eq(nil) + expect(first_job[:when]).to eq('on_success') + expect(first_job[:allow_failure]).to eq(false) + expect(first_job[:only]).to eq(nil) + expect(first_job[:except]).to eq(nil) + end + end + + context 'when only is not nil in the yaml' do + context 'when only: is hash' do + let(:yaml_content) do + <<~YAML + build: + stage: build + script: echo + only: + refs: + - branches + YAML + end + + it_behaves_like 'matches schema' + + it 'renders only:refs as hash' do + expect(first_job[:only]).to eq(refs: ['branches']) + end + end + + context 'when only is an array of strings in the yaml' do + let(:yaml_content) do + <<~YAML + build: + stage: build + script: echo + only: + - pushes + YAML + end + + it_behaves_like 'matches schema' + + it 'renders only: list as hash' do + expect(first_job[:only]).to eq(refs: ['pushes']) + end + end + end + + context 'when except is not nil in the yaml' do + context 'when except: is hash' do + let(:yaml_content) do + <<~YAML + build: + stage: build + script: echo + except: + refs: + - branches + YAML + end + + it_behaves_like 'matches schema' + + it 'renders except as hash' do + expect(first_job[:except]).to eq(refs: ['branches']) + end + end + + context 'when except is an array of strings in the yaml' do + let(:yaml_content) do + <<~YAML + build: + stage: build + script: echo + except: + - pushes + YAML + end + + it_behaves_like 'matches schema' + + it 'renders only: list as hash' do + expect(first_job[:except]).to eq(refs: ['pushes']) + end + end + + context 'with minimal job configuration' do + let(:yaml_content) do + <<~YAML + build: + stage: build + script: echo + YAML + end + + it_behaves_like 'matches schema' + + it 'renders the job with defaults' do + expect(first_job[:name]).to eq('build') + expect(first_job[:stage]).to eq('build') + expect(first_job[:before_script]).to eq([]) + expect(first_job[:script]).to eq(['echo']) + expect(first_job[:after_script]).to eq([]) + expect(first_job[:tag_list]).to eq([]) + expect(first_job[:environment]).to eq(nil) + expect(first_job[:when]).to eq('on_success') + expect(first_job[:allow_failure]).to eq(false) + expect(first_job[:only]).to eq(refs: %w[branches tags]) + expect(first_job[:except]).to eq(nil) + end + end + + context 'with environment defined' do + context 'when formatted as a hash in yaml' do + let(:yaml_content) do + <<~YAML + build: + stage: build + script: echo + environment: + name: production + url: https://example.com + YAML + end + + it_behaves_like 'matches schema' + + it 'renders the environment as a string' do + expect(first_job[:environment]).to eq('production') + end + end + + context 'when formatted as a string in yaml' do + let(:yaml_content) do + <<~YAML + build: + stage: build + script: echo + environment: production + YAML + end + + it_behaves_like 'matches schema' + + it 'renders the environment as a string' do + expect(first_job[:environment]).to eq('production') + end + end + end + + context 'when script values are formatted as arrays in the yaml' do + let(:yaml_content) do + <<~YAML + build: + stage: build + before_script: + - echo + - cat '~/.zshrc' + script: + - echo + - cat '~/.zshrc' + after_script: + - echo + - cat '~/.zshrc' + YAML + end + + it_behaves_like 'matches schema' + + it 'renders the scripts as arrays' do + expect(first_job[:before_script]).to eq(['echo', "cat '~/.zshrc'"]) + expect(first_job[:script]).to eq(['echo', "cat '~/.zshrc'"]) + expect(first_job[:after_script]).to eq(['echo', "cat '~/.zshrc'"]) + end + end + end + end +end diff --git a/spec/serializers/cluster_entity_spec.rb b/spec/serializers/cluster_entity_spec.rb index 223d37b6acd..10c6bc0e42a 100644 --- a/spec/serializers/cluster_entity_spec.rb +++ b/spec/serializers/cluster_entity_spec.rb @@ -78,5 +78,26 @@ RSpec.describe ClusterEntity do expect(subject[:gitlab_managed_apps_logs_path]).to eq(log_explorer_path) end end + + context 'enable_advanced_logs_querying' do + let(:cluster) { create(:cluster, :project) } + let(:user) { create(:user) } + + subject { described_class.new(cluster, request: request).as_json } + + context 'elastic stack is not installed on cluster' do + it 'returns false' do + expect(subject[:enable_advanced_logs_querying]).to be false + end + end + + context 'elastic stack is installed on cluster' do + it 'returns true' do + create(:clusters_applications_elastic_stack, :installed, cluster: cluster) + + expect(subject[:enable_advanced_logs_querying]).to be true + end + end + end end end diff --git a/spec/serializers/cluster_serializer_spec.rb b/spec/serializers/cluster_serializer_spec.rb index f34409c3cfb..04999975276 100644 --- a/spec/serializers/cluster_serializer_spec.rb +++ b/spec/serializers/cluster_serializer_spec.rb @@ -14,6 +14,7 @@ RSpec.describe ClusterSerializer do :enabled, :environment_scope, :gitlab_managed_apps_logs_path, + :enable_advanced_logs_querying, :kubernetes_errors, :name, :nodes, diff --git a/spec/serializers/diff_file_base_entity_spec.rb b/spec/serializers/diff_file_base_entity_spec.rb index bf69a50a072..94c39e11790 100644 --- a/spec/serializers/diff_file_base_entity_spec.rb +++ b/spec/serializers/diff_file_base_entity_spec.rb @@ -7,21 +7,50 @@ RSpec.describe DiffFileBaseEntity do let(:repository) { project.repository } let(:entity) { described_class.new(diff_file, options).as_json } - context 'diff for a changed submodule' do - let(:commit_sha_with_changed_submodule) do - "cfe32cf61b73a0d5e9f13e774abde7ff789b1660" - end - - let(:commit) { project.commit(commit_sha_with_changed_submodule) } + context 'submodule information for a' do + let(:commit_sha) { "" } + let(:commit) { project.commit(commit_sha) } let(:options) { { request: {}, submodule_links: Gitlab::SubmoduleLinks.new(repository) } } let(:diff_file) { commit.diffs.diff_files.to_a.last } - it do - expect(entity[:submodule]).to eq(true) - expect(entity[:submodule_link]).to eq("https://github.com/randx/six") - expect(entity[:submodule_tree_url]).to eq( - "https://github.com/randx/six/tree/409f37c4f05865e4fb208c771485f211a22c4c2d" - ) + context 'newly added submodule' do + let(:commit_sha) { "cfe32cf61b73a0d5e9f13e774abde7ff789b1660" } + + it 'says it is a submodule and contains links' do + expect(entity[:submodule]).to eq(true) + expect(entity[:submodule_link]).to eq("https://github.com/randx/six") + expect(entity[:submodule_tree_url]).to eq( + "https://github.com/randx/six/tree/409f37c4f05865e4fb208c771485f211a22c4c2d" + ) + end + + it 'has no compare url because the submodule was newly added' do + expect(entity[:submodule_compare]).to eq(nil) + end + end + + context 'changed submodule' do + # Test repo does not contain a commit that changes a submodule, so we have create such a commit here + let(:commit_sha) { repository.update_submodule(project.members[0].user, "six", "c6bc3aa2ee76cadaf0cbd325067c55450997632e", message: "Go one commit back", branch: "master") } + + it 'contains a link to compare the changes' do + expect(entity[:submodule_compare]).to eq( + { + url: "https://github.com/randx/six/compare/409f37c4f05865e4fb208c771485f211a22c4c2d...c6bc3aa2ee76cadaf0cbd325067c55450997632e", + old_sha: "409f37c4f05865e4fb208c771485f211a22c4c2d", + new_sha: "c6bc3aa2ee76cadaf0cbd325067c55450997632e" + } + ) + end + end + + context 'normal file (no submodule)' do + let(:commit_sha) { '570e7b2abdd848b95f2f578043fc23bd6f6fd24d' } + + it 'sets submodule to false' do + expect(entity[:submodule]).to eq(false) + expect(entity[:submodule_compare]).to eq(nil) + end end end diff --git a/spec/serializers/environment_entity_spec.rb b/spec/serializers/environment_entity_spec.rb index c969638614e..c90f771335e 100644 --- a/spec/serializers/environment_entity_spec.rb +++ b/spec/serializers/environment_entity_spec.rb @@ -82,26 +82,6 @@ RSpec.describe EnvironmentEntity do end end - context 'with alert' do - let!(:environment) { create(:environment, project: project) } - let!(:prometheus_alert) { create(:prometheus_alert, project: project, environment: environment) } - let!(:alert) { create(:alert_management_alert, :triggered, :prometheus, project: project, environment: environment, prometheus_alert: prometheus_alert) } - - it 'exposes active alert flag' do - project.add_maintainer(user) - - expect(subject[:has_opened_alert]).to eq(true) - end - - context 'when user does not have permission to read alert' do - it 'does not expose active alert flag' do - project.add_reporter(user) - - expect(subject[:has_opened_alert]).to be_nil - end - end - end - context 'pod_logs' do context 'with reporter access' do before do diff --git a/spec/serializers/environment_status_entity_spec.rb b/spec/serializers/environment_status_entity_spec.rb index a940c4b465e..77ef06f90c2 100644 --- a/spec/serializers/environment_status_entity_spec.rb +++ b/spec/serializers/environment_status_entity_spec.rb @@ -17,7 +17,7 @@ RSpec.describe EnvironmentStatusEntity do subject { entity.as_json } before do - deployment.update(sha: merge_request.diff_head_sha) + deployment.update!(sha: merge_request.diff_head_sha) allow(request).to receive(:current_user).and_return(user) end diff --git a/spec/serializers/fork_namespace_entity_spec.rb b/spec/serializers/fork_namespace_entity_spec.rb index 7ce6b77da44..7740ed77540 100644 --- a/spec/serializers/fork_namespace_entity_spec.rb +++ b/spec/serializers/fork_namespace_entity_spec.rb @@ -8,13 +8,17 @@ RSpec.describe ForkNamespaceEntity do let_it_be(:user) { create(:user) } let_it_be(:project) { create(:project) } + let_it_be(:namespace) { create(:group, :with_avatar, description: 'test') } + let(:memberships) do + user.members.index_by(&:source_id) + end - let(:namespace) { create(:group, :with_avatar, description: 'test') } - let(:entity) { described_class.new(namespace, current_user: user, project: project) } + let(:entity) { described_class.new(namespace, current_user: user, project: project, memberships: memberships) } subject(:json) { entity.as_json } before do + namespace.add_developer(user) project.add_maintainer(user) end @@ -52,7 +56,6 @@ RSpec.describe ForkNamespaceEntity do end it 'exposes human readable permission level' do - namespace.add_developer(user) expect(json[:permission]).to eql 'Developer' end diff --git a/spec/serializers/group_group_link_entity_spec.rb b/spec/serializers/group_group_link_entity_spec.rb new file mode 100644 index 00000000000..8384563e3e6 --- /dev/null +++ b/spec/serializers/group_group_link_entity_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe GroupGroupLinkEntity do + include_context 'group_group_link' + + subject(:json) { described_class.new(group_group_link).to_json } + + it 'matches json schema' do + expect(json).to match_schema('entities/group_group_link') + end +end diff --git a/spec/serializers/group_group_link_serializer_spec.rb b/spec/serializers/group_group_link_serializer_spec.rb new file mode 100644 index 00000000000..0d977ea0a9a --- /dev/null +++ b/spec/serializers/group_group_link_serializer_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe GroupGroupLinkSerializer do + include_context 'group_group_link' + + subject(:json) { described_class.new.represent(shared_group.shared_with_group_links).to_json } + + it 'matches json schema' do + expect(json).to match_schema('group_group_links') + end +end diff --git a/spec/serializers/issue_entity_spec.rb b/spec/serializers/issue_entity_spec.rb index 5c5ac184778..82ea26fae40 100644 --- a/spec/serializers/issue_entity_spec.rb +++ b/spec/serializers/issue_entity_spec.rb @@ -112,7 +112,7 @@ RSpec.describe IssueEntity do context 'when project is archived' do before do - project.update(archived: true) + project.update!(archived: true) end it 'returns archived true' do diff --git a/spec/serializers/issue_serializer_spec.rb b/spec/serializers/issue_serializer_spec.rb index a51297d6d80..491e2f0835b 100644 --- a/spec/serializers/issue_serializer_spec.rb +++ b/spec/serializers/issue_serializer_spec.rb @@ -3,8 +3,9 @@ require 'spec_helper' RSpec.describe IssueSerializer do - let(:resource) { create(:issue) } - let(:user) { create(:user) } + let_it_be(:resource) { create(:issue) } + let_it_be(:user) { create(:user) } + let(:json_entity) do described_class.new(current_user: user) .represent(resource, serializer: serializer) diff --git a/spec/serializers/job_entity_spec.rb b/spec/serializers/job_entity_spec.rb index 02262be9511..1cbf1914c0c 100644 --- a/spec/serializers/job_entity_spec.rb +++ b/spec/serializers/job_entity_spec.rb @@ -45,7 +45,7 @@ RSpec.describe JobEntity do context 'when job is retryable' do before do - job.update(status: :failed) + job.update!(status: :failed) end it 'contains cancel path' do @@ -55,7 +55,7 @@ RSpec.describe JobEntity do context 'when job is cancelable' do before do - job.update(status: :running) + job.update!(status: :running) end it 'contains cancel path' do @@ -218,4 +218,16 @@ RSpec.describe JobEntity do expect(subject).not_to include('recoverable') end end + + context 'when job is a bridge' do + let(:job) { create(:ci_bridge) } + + it 'does not include build path' do + expect(subject).not_to include(:build_path) + end + + it 'does not include cancel path' do + expect(subject).not_to include(:cancel_path) + end + end end diff --git a/spec/serializers/linked_project_issue_entity_spec.rb b/spec/serializers/linked_project_issue_entity_spec.rb new file mode 100644 index 00000000000..864b5c45599 --- /dev/null +++ b/spec/serializers/linked_project_issue_entity_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe LinkedProjectIssueEntity do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:issue_link) { create(:issue_link) } + + let(:request) { double('request') } + let(:related_issue) { issue_link.source.related_issues(user).first } + let(:entity) { described_class.new(related_issue, request: request, current_user: user) } + + before do + allow(request).to receive(:current_user).and_return(user) + allow(request).to receive(:issuable).and_return(issue_link.source) + issue_link.target.project.add_developer(user) + end + + describe 'issue_link_type' do + it { expect(entity.as_json).to include(link_type: 'relates_to') } + end +end diff --git a/spec/serializers/merge_request_basic_entity_spec.rb b/spec/serializers/merge_request_basic_entity_spec.rb index 1cddd87e917..4a8bcd72d9c 100644 --- a/spec/serializers/merge_request_basic_entity_spec.rb +++ b/spec/serializers/merge_request_basic_entity_spec.rb @@ -3,7 +3,8 @@ require 'spec_helper' RSpec.describe MergeRequestBasicEntity do - let(:resource) { build(:merge_request) } + let(:resource) { build(:merge_request, params) } + let(:params) { {} } subject do described_class.new(resource).as_json @@ -14,4 +15,28 @@ RSpec.describe MergeRequestBasicEntity do expect(subject[:merge_status]).to eq 'checking' end + + describe '#reviewers' do + let(:params) { { reviewers: [reviewer] } } + let(:reviewer) { build(:user) } + + context 'when merge_request_reviewers feature is disabled' do + it 'does not contain assignees attributes' do + stub_feature_flags(merge_request_reviewers: false) + + expect(subject[:reviewers]).to be_nil + end + end + + context 'when merge_request_reviewers feature is enabled' do + it 'contains reviewers attributes' do + stub_feature_flags(merge_request_reviewers: true) + + expect(subject[:reviewers].count).to be 1 + expect(subject[:reviewers].first.keys).to include( + :id, :name, :username, :state, :avatar_url, :web_url + ) + 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 e5f88e31025..161940dd01a 100644 --- a/spec/serializers/merge_request_poll_widget_entity_spec.rb +++ b/spec/serializers/merge_request_poll_widget_entity_spec.rb @@ -265,7 +265,7 @@ RSpec.describe MergeRequestPollWidgetEntity do context 'when is not up to date' do it 'returns nil' do - pipeline.update(sha: "not up to date") + pipeline.update!(sha: "not up to date") expect(subject[:pipeline]).to eq(nil) end @@ -285,4 +285,20 @@ RSpec.describe MergeRequestPollWidgetEntity do end end end + + describe '#builds_with_coverage' do + it 'serializes the builds with coverage' do + allow(resource).to receive(:head_pipeline_builds_with_coverage).and_return([ + double(name: 'rspec', coverage: 91.5), + double(name: 'jest', coverage: 94.1) + ]) + + result = subject[:builds_with_coverage] + + expect(result).to eq([ + { name: 'rspec', coverage: 91.5 }, + { name: 'jest', coverage: 94.1 } + ]) + end + end end diff --git a/spec/serializers/merge_request_sidebar_extras_entity_spec.rb b/spec/serializers/merge_request_sidebar_extras_entity_spec.rb new file mode 100644 index 00000000000..74e9956c8a0 --- /dev/null +++ b/spec/serializers/merge_request_sidebar_extras_entity_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe MergeRequestSidebarExtrasEntity do + let_it_be(:assignee) { build(:user) } + let_it_be(:reviewer) { build(:user) } + let_it_be(:user) { build(:user) } + let_it_be(:project) { create :project, :repository } + + let(:params) do + { + source_project: project, + target_project: project, + assignees: [assignee], + reviewers: [reviewer] + } + end + + let(:resource) do + build(:merge_request, params) + end + + let(:request) { double('request', current_user: user, project: project) } + + let(:entity) { described_class.new(resource, request: request).as_json } + + describe '#assignees' do + it 'contains assignees attributes' do + expect(entity[:assignees].count).to be 1 + expect(entity[:assignees].first.keys).to include( + :id, :name, :username, :state, :avatar_url, :web_url, :can_merge + ) + end + end + + describe '#reviewers' do + context 'when merge_request_reviewers feature is disabled' do + it 'does not contain reviewers attributes' do + stub_feature_flags(merge_request_reviewers: false) + + expect(entity[:reviewers]).to be_nil + end + end + + context 'when merge_request_reviewers feature is enabled' do + it 'contains reviewers attributes' do + stub_feature_flags(merge_request_reviewers: true) + + expect(entity[:reviewers].count).to be 1 + expect(entity[:reviewers].first.keys).to include( + :id, :name, :username, :state, :avatar_url, :web_url, :can_merge + ) + end + end + end +end diff --git a/spec/serializers/merge_request_widget_entity_spec.rb b/spec/serializers/merge_request_widget_entity_spec.rb index 1704208d8b9..1432c4499ae 100644 --- a/spec/serializers/merge_request_widget_entity_spec.rb +++ b/spec/serializers/merge_request_widget_entity_spec.rb @@ -136,9 +136,22 @@ RSpec.describe MergeRequestWidgetEntity do let(:role) { :developer } it 'has add ci config path' do - expected_path = "/#{resource.project.full_path}/-/new/#{resource.source_branch}?commit_message=Add+.gitlab-ci.yml&file_name=.gitlab-ci.yml&suggest_gitlab_ci_yml=true" + expected_path = "/#{resource.project.full_path}/-/new/#{resource.source_branch}" - expect(subject[:merge_request_add_ci_config_path]).to eq(expected_path) + expect(subject[:merge_request_add_ci_config_path]).to include(expected_path) + end + + it 'has expected params' do + expected_params = { + commit_message: 'Add .gitlab-ci.yml', + file_name: '.gitlab-ci.yml', + suggest_gitlab_ci_yml: 'true', + mr_path: "/#{resource.project.full_path}/-/merge_requests/#{resource.iid}" + }.with_indifferent_access + + uri = Addressable::URI.parse(subject[:merge_request_add_ci_config_path]) + + expect(uri.query_values).to match(expected_params) end context 'when auto devops is enabled' do @@ -197,7 +210,7 @@ RSpec.describe MergeRequestWidgetEntity do context 'when build feature is disabled' do before do - project.project_feature.update(builds_access_level: ProjectFeature::DISABLED) + project.project_feature.update!(builds_access_level: ProjectFeature::DISABLED) end it 'has no path' do @@ -333,7 +346,7 @@ RSpec.describe MergeRequestWidgetEntity do it 'returns a blank rebase_path' do allow(merge_request).to receive(:should_be_rebased?).and_return(true) - forked_project.destroy + forked_project.destroy! merge_request.reload entity = described_class.new(merge_request, request: request).as_json diff --git a/spec/serializers/pipeline_details_entity_spec.rb b/spec/serializers/pipeline_details_entity_spec.rb index 35ce7c7175c..1357836cb89 100644 --- a/spec/serializers/pipeline_details_entity_spec.rb +++ b/spec/serializers/pipeline_details_entity_spec.rb @@ -6,6 +6,10 @@ RSpec.describe PipelineDetailsEntity do let_it_be(:user) { create(:user) } let(:request) { double('request') } + let(:entity) do + described_class.represent(pipeline, request: request) + end + it 'inherrits from PipelineEntity' do expect(described_class).to be < PipelineEntity end @@ -16,10 +20,6 @@ RSpec.describe PipelineDetailsEntity do allow(request).to receive(:current_user).and_return(user) end - let(:entity) do - described_class.represent(pipeline, request: request) - end - describe '#as_json' do subject { entity.as_json } diff --git a/spec/serializers/pipeline_entity_spec.rb b/spec/serializers/pipeline_entity_spec.rb index e00f05a8fe8..d7cd13edec8 100644 --- a/spec/serializers/pipeline_entity_spec.rb +++ b/spec/serializers/pipeline_entity_spec.rb @@ -260,13 +260,5 @@ RSpec.describe PipelineEntity do end end end - - context 'when pipeline has build report results' do - let(:pipeline) { create(:ci_pipeline, :with_report_results, project: project, user: user) } - - it 'exposes tests total count' do - expect(subject[:tests_total_count]).to eq(2) - end - end end end diff --git a/spec/serializers/pipeline_serializer_spec.rb b/spec/serializers/pipeline_serializer_spec.rb index dfe51e9006f..b42a4f6ad3f 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? ? 46 : 43 + expected_queries = Gitlab.ee? ? 43 : 40 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 bd2a1b0fb98..32e9562f4c1 100644 --- a/spec/serializers/test_case_entity_spec.rb +++ b/spec/serializers/test_case_entity_spec.rb @@ -5,6 +5,8 @@ require 'spec_helper' RSpec.describe TestCaseEntity do include TestReportsHelper + let_it_be(:job) { create(:ci_build) } + let(:entity) { described_class.new(test_case) } describe '#as_json' do @@ -38,7 +40,7 @@ RSpec.describe TestCaseEntity do end context 'when attachment is present' do - let(:test_case) { build(:test_case, :failed_with_attachment) } + let(:test_case) { build(:test_case, :failed_with_attachment, job: job) } it 'returns the attachment_url' do expect(subject).to include(:attachment_url) @@ -46,7 +48,7 @@ RSpec.describe TestCaseEntity do end context 'when attachment is not present' do - let(:test_case) { build(:test_case) } + let(:test_case) { build(:test_case, job: job) } it 'returns a nil attachment_url' do expect(subject[:attachment_url]).to be_nil @@ -60,7 +62,7 @@ RSpec.describe TestCaseEntity do end context 'when attachment is present' do - let(:test_case) { build(:test_case, :failed_with_attachment) } + let(:test_case) { build(:test_case, :failed_with_attachment, job: job) } it 'returns no attachment_url' do expect(subject).not_to include(:attachment_url) @@ -68,7 +70,7 @@ RSpec.describe TestCaseEntity do end context 'when attachment is not present' do - let(:test_case) { build(:test_case) } + let(:test_case) { build(:test_case, job: job) } it 'returns no attachment_url' do expect(subject).not_to include(:attachment_url) |