diff options
Diffstat (limited to 'spec/serializers')
15 files changed, 526 insertions, 46 deletions
diff --git a/spec/serializers/accessibility_reports_comparer_entity_spec.rb b/spec/serializers/accessibility_reports_comparer_entity_spec.rb index c576dfa4dd1..dade2387ea9 100644 --- a/spec/serializers/accessibility_reports_comparer_entity_spec.rb +++ b/spec/serializers/accessibility_reports_comparer_entity_spec.rb @@ -51,8 +51,8 @@ RSpec.describe AccessibilityReportsComparerEntity do expect(subject[:status]).to eq(Gitlab::Ci::Reports::AccessibilityReportsComparer::STATUS_FAILED) expect(subject[:resolved_errors].first).to include(:code, :type, :type_code, :message, :context, :selector, :runner, :runner_extras) expect(subject[:new_errors].first).to include(:code, :type, :type_code, :message, :context, :selector, :runner, :runner_extras) - expect(subject[:existing_errors].first).to include(:code, :type, :type_code, :message, :context, :selector, :runner, :runner_extras) - expect(subject[:summary]).to include(total: 2, resolved: 1, errored: 1) + expect(subject[:existing_errors]).to be_empty + expect(subject[:summary]).to include(total: 1, resolved: 1, errored: 1) end end diff --git a/spec/serializers/admin/user_entity_spec.rb b/spec/serializers/admin/user_entity_spec.rb new file mode 100644 index 00000000000..7db49af09c3 --- /dev/null +++ b/spec/serializers/admin/user_entity_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Admin::UserEntity do + let_it_be(:user) { build_stubbed(:user) } + let(:request) { double('request') } + + let(:entity) do + described_class.new(user, request: request) + end + + describe '#as_json' do + subject { entity.as_json&.keys } + + it 'exposes correct attributes' do + is_expected.to contain_exactly( + :id, + :name, + :created_at, + :email, + :username, + :last_activity_on, + :avatar_url, + :badges, + :projects_count, + :actions + ) + end + end +end diff --git a/spec/serializers/admin/user_serializer_spec.rb b/spec/serializers/admin/user_serializer_spec.rb new file mode 100644 index 00000000000..719a90384c6 --- /dev/null +++ b/spec/serializers/admin/user_serializer_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Admin::UserSerializer do + let(:resource) { build(:user) } + + subject { described_class.new.represent(resource).keys } + + context 'when there is a single object provided' do + it 'contains important elements for the admin user table' do + is_expected.to contain_exactly( + :id, + :name, + :created_at, + :email, + :username, + :last_activity_on, + :avatar_url, + :badges, + :projects_count, + :actions + ) + end + end +end diff --git a/spec/serializers/board_simple_entity_spec.rb b/spec/serializers/board_simple_entity_spec.rb new file mode 100644 index 00000000000..c5ab9833adf --- /dev/null +++ b/spec/serializers/board_simple_entity_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BoardSimpleEntity do + let_it_be(:project) { create(:project) } + let_it_be(:board) { create(:board, project: project) } + + subject { described_class.new(board).as_json } + + describe '#name' do + it 'has `name` attribute' do + is_expected.to include(:name) + end + end +end diff --git a/spec/serializers/codequality_degradation_entity_spec.rb b/spec/serializers/codequality_degradation_entity_spec.rb new file mode 100644 index 00000000000..fc969195e35 --- /dev/null +++ b/spec/serializers/codequality_degradation_entity_spec.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe CodequalityDegradationEntity do + let(:entity) { described_class.new(codequality_degradation) } + + describe '#as_json' do + subject { entity.as_json } + + context 'when codequality contains an error' do + context 'when line is included in location' do + let(:codequality_degradation) do + { + "categories": [ + "Complexity" + ], + "check_name": "argument_count", + "content": { + "body": "" + }, + "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.", + "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547", + "location": { + "path": "foo.rb", + "lines": { + "begin": 10, + "end": 10 + } + }, + "other_locations": [], + "remediation_points": 900000, + "severity": "major", + "type": "issue", + "engine_name": "structure" + }.with_indifferent_access + end + + it 'contains correct codequality degradation details', :aggregate_failures do + expect(subject[:description]).to eq("Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.") + expect(subject[:severity]).to eq("major") + expect(subject[:file_path]).to eq("foo.rb") + expect(subject[:line]).to eq(10) + end + end + + context 'when line is included in positions' do + let(:codequality_degradation) do + { + "type": "Issue", + "check_name": "Rubocop/Metrics/ParameterLists", + "description": "Avoid parameter lists longer than 5 parameters. [12/5]", + "categories": [ + "Complexity" + ], + "remediation_points": 550000, + "location": { + "path": "foo.rb", + "positions": { + "begin": { + "column": 24, + "line": 14 + }, + "end": { + "column": 49, + "line": 14 + } + } + }, + "content": { + "body": "This cop checks for methods with too many parameters.\nThe maximum number of parameters is configurable.\nKeyword arguments can optionally be excluded from the total count." + }, + "engine_name": "rubocop", + "fingerprint": "ab5f8b935886b942d621399f5a2ca16e", + "severity": "minor" + }.with_indifferent_access + end + + it 'contains correct codequality degradation details', :aggregate_failures do + expect(subject[:description]).to eq("Avoid parameter lists longer than 5 parameters. [12/5]") + expect(subject[:severity]).to eq("minor") + expect(subject[:file_path]).to eq("foo.rb") + expect(subject[:line]).to eq(14) + end + end + end + end +end diff --git a/spec/serializers/codequality_reports_comparer_entity_spec.rb b/spec/serializers/codequality_reports_comparer_entity_spec.rb new file mode 100644 index 00000000000..7a79c30cc3f --- /dev/null +++ b/spec/serializers/codequality_reports_comparer_entity_spec.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe CodequalityReportsComparerEntity do + let(:entity) { described_class.new(comparer) } + let(:comparer) { Gitlab::Ci::Reports::CodequalityReportsComparer.new(base_report, head_report) } + let(:base_report) { Gitlab::Ci::Reports::CodequalityReports.new } + let(:head_report) { Gitlab::Ci::Reports::CodequalityReports.new } + let(:degradation_1) do + { + "categories": [ + "Complexity" + ], + "check_name": "argument_count", + "content": { + "body": "" + }, + "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.", + "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547", + "location": { + "path": "foo.rb", + "lines": { + "begin": 10, + "end": 10 + } + }, + "other_locations": [], + "remediation_points": 900000, + "severity": "major", + "type": "issue", + "engine_name": "structure" + }.with_indifferent_access + end + + let(:degradation_2) do + { + "type": "Issue", + "check_name": "Rubocop/Metrics/ParameterLists", + "description": "Avoid parameter lists longer than 5 parameters. [12/5]", + "categories": [ + "Complexity" + ], + "remediation_points": 550000, + "location": { + "path": "foo.rb", + "positions": { + "begin": { + "column": 14, + "line": 10 + }, + "end": { + "column": 39, + "line": 10 + } + } + }, + "content": { + "body": "This cop checks for methods with too many parameters.\nThe maximum number of parameters is configurable.\nKeyword arguments can optionally be excluded from the total count." + }, + "engine_name": "rubocop", + "fingerprint": "ab5f8b935886b942d621399f5a2ca16e", + "severity": "minor" + }.with_indifferent_access + end + + describe '#as_json' do + subject { entity.as_json } + + context 'when base and head report have errors' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'contains correct compared codequality report details', :aggregate_failures do + expect(subject[:status]).to eq(Gitlab::Ci::Reports::CodequalityReportsComparer::STATUS_FAILED) + expect(subject[:resolved_errors].first).to include(:description, :severity, :file_path, :line) + expect(subject[:new_errors].first).to include(:description, :severity, :file_path, :line) + expect(subject[:existing_errors]).to be_empty + expect(subject[:summary]).to include(total: 1, resolved: 1, errored: 1) + end + end + end +end diff --git a/spec/serializers/codequality_reports_comparer_serializer_spec.rb b/spec/serializers/codequality_reports_comparer_serializer_spec.rb new file mode 100644 index 00000000000..3c47bfb6adc --- /dev/null +++ b/spec/serializers/codequality_reports_comparer_serializer_spec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe CodequalityReportsComparerSerializer do + let(:project) { double(:project) } + let(:serializer) { described_class.new(project: project).represent(comparer) } + let(:comparer) { Gitlab::Ci::Reports::CodequalityReportsComparer.new(base_report, head_report) } + let(:base_report) { Gitlab::Ci::Reports::CodequalityReports.new } + let(:head_report) { Gitlab::Ci::Reports::CodequalityReports.new } + let(:degradation_1) do + { + "categories": [ + "Complexity" + ], + "check_name": "argument_count", + "content": { + "body": "" + }, + "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.", + "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547", + "location": { + "path": "foo.rb", + "lines": { + "begin": 10, + "end": 10 + } + }, + "other_locations": [], + "remediation_points": 900000, + "severity": "major", + "type": "issue", + "engine_name": "structure" + }.with_indifferent_access + end + + let(:degradation_2) do + { + "type": "Issue", + "check_name": "Rubocop/Metrics/ParameterLists", + "description": "Avoid parameter lists longer than 5 parameters. [12/5]", + "categories": [ + "Complexity" + ], + "remediation_points": 550000, + "location": { + "path": "foo.rb", + "positions": { + "begin": { + "column": 14, + "line": 10 + }, + "end": { + "column": 39, + "line": 10 + } + } + }, + "content": { + "body": "This cop checks for methods with too many parameters.\nThe maximum number of parameters is configurable.\nKeyword arguments can optionally be excluded from the total count." + }, + "engine_name": "rubocop", + "fingerprint": "ab5f8b935886b942d621399f5a2ca16e", + "severity": "minor" + }.with_indifferent_access + end + + describe '#to_json' do + subject { serializer.as_json } + + context 'when base report has error and head has a different error' do + before do + base_report.add_degradation(degradation_1) + head_report.add_degradation(degradation_2) + end + + it 'matches the schema' do + expect(subject).to match_schema('entities/codequality_reports_comparer') + end + end + + context 'when base report has no error and head has errors' do + before do + head_report.add_degradation(degradation_1) + end + + it 'matches the schema' do + expect(subject).to match_schema('entities/codequality_reports_comparer') + end + end + end +end diff --git a/spec/serializers/environment_entity_spec.rb b/spec/serializers/environment_entity_spec.rb index f5d6706a844..5b83507b4ec 100644 --- a/spec/serializers/environment_entity_spec.rb +++ b/spec/serializers/environment_entity_spec.rb @@ -7,15 +7,20 @@ RSpec.describe EnvironmentEntity do let(:request) { double('request') } let(:entity) do - described_class.new(environment, request: spy('request')) + described_class.new(environment, request: request) end let_it_be(:user) { create(:user) } - let_it_be(:project) { create(:project) } - let_it_be(:environment) { create(:environment, project: project) } + let_it_be(:project) { create(:project, :repository) } + let_it_be(:environment, refind: true) { create(:environment, project: project) } + + before_all do + project.add_developer(user) + end before do - allow(entity).to receive(:current_user).and_return(user) + allow(request).to receive(:current_user).and_return(user) + allow(request).to receive(:project).and_return(project) end subject { entity.as_json } @@ -32,6 +37,51 @@ RSpec.describe EnvironmentEntity do expect(subject).to include(:folder_path) end + context 'when there is a successful deployment' do + let!(:pipeline) { create(:ci_pipeline, :success, project: project) } + let!(:deployable) { create(:ci_build, :success, project: project, pipeline: pipeline) } + let!(:deployment) { create(:deployment, :success, project: project, environment: environment, deployable: deployable) } + + it 'exposes it as the latest deployment' do + expect(subject[:last_deployment][:sha]).to eq(deployment.sha) + end + + it 'does not expose it as an upcoming deployment' do + expect(subject[:upcoming_deployment]).to be_nil + end + + context 'when the deployment pipeline has the other manual job' do + let!(:manual_job) { create(:ci_build, :manual, name: 'stop-review', project: project, pipeline: pipeline) } + + it 'exposes the manual job in the latest deployment' do + expect(subject[:last_deployment][:manual_actions].first[:name]) + .to eq(manual_job.name) + end + end + end + + context 'when there is a running deployment' do + let!(:pipeline) { create(:ci_pipeline, :running, project: project) } + let!(:deployable) { create(:ci_build, :running, project: project, pipeline: pipeline) } + let!(:deployment) { create(:deployment, :running, project: project, environment: environment, deployable: deployable) } + + it 'does not expose it as the latest deployment' do + expect(subject[:last_deployment]).to be_nil + end + + it 'exposes it as an upcoming deployment' do + expect(subject[:upcoming_deployment][:sha]).to eq(deployment.sha) + end + + context 'when the deployment pipeline has the other manual job' do + let!(:manual_job) { create(:ci_build, :manual, name: 'stop-review', project: project, pipeline: pipeline) } + + it 'does not expose the manual job in the latest deployment' do + expect(subject[:upcoming_deployment][:manual_actions]).to be_nil + end + end + end + context 'metrics disabled' do before do allow(environment).to receive(:has_metrics?).and_return(false) diff --git a/spec/serializers/import/bulk_import_entity_spec.rb b/spec/serializers/import/bulk_import_entity_spec.rb index f35684bef20..3dfc659daf7 100644 --- a/spec/serializers/import/bulk_import_entity_spec.rb +++ b/spec/serializers/import/bulk_import_entity_spec.rb @@ -7,14 +7,15 @@ RSpec.describe Import::BulkImportEntity do { 'id' => 1, 'full_name' => 'test', - 'full_path' => 'full/path/test', + 'full_path' => 'full/path/tes', + 'web_url' => 'http://web.url/path', 'foo' => 'bar' } end subject { described_class.represent(importable_data).as_json } - %w[id full_name full_path].each do |attribute| + %w[id full_name full_path web_url].each do |attribute| it "exposes #{attribute}" do expect(subject[attribute.to_sym]).to eq(importable_data[attribute]) end diff --git a/spec/serializers/merge_request_current_user_entity_spec.rb b/spec/serializers/merge_request_current_user_entity_spec.rb new file mode 100644 index 00000000000..d4cbfe726c1 --- /dev/null +++ b/spec/serializers/merge_request_current_user_entity_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe MergeRequestCurrentUserEntity do + let(:user) { create(:user) } + let(:project) { create(:project, :repository) } + let(:request) { EntityRequest.new(project: project, current_user: user) } + + let(:entity) do + described_class.new(user, request: request) + end + + context 'as json' do + subject { entity.as_json } + + it 'exposes needed attributes' do + expect(subject).to include(:can_fork, :can_create_merge_request, :fork_path) + end + end +end diff --git a/spec/serializers/merge_request_user_entity_spec.rb b/spec/serializers/merge_request_user_entity_spec.rb index 8d6f066481e..a2ad8e72845 100644 --- a/spec/serializers/merge_request_user_entity_spec.rb +++ b/spec/serializers/merge_request_user_entity_spec.rb @@ -15,7 +15,7 @@ RSpec.describe MergeRequestUserEntity do subject { entity.as_json } it 'exposes needed attributes' do - expect(subject).to include(:can_fork, :can_create_merge_request, :fork_path) + expect(subject).to include(:id, :name, :username, :state, :avatar_url, :web_url, :can_merge) end end end diff --git a/spec/serializers/merge_request_widget_entity_spec.rb b/spec/serializers/merge_request_widget_entity_spec.rb index 3f7d5542ae8..9f734c08ef4 100644 --- a/spec/serializers/merge_request_widget_entity_spec.rb +++ b/spec/serializers/merge_request_widget_entity_spec.rb @@ -285,54 +285,34 @@ RSpec.describe MergeRequestWidgetEntity do end describe 'user callouts' do - context 'when suggest pipeline feature is enabled' do - subject { described_class.new(resource, request: request, experiment_enabled: :suggest_pipeline).as_json } + subject { described_class.new(resource, request: request).as_json } - it 'provides a valid path value for user callout path' do - expect(subject[:user_callouts_path]).to eq '/-/user_callouts' - end - - it 'provides a valid value for suggest pipeline feature id' do - expect(subject[:suggest_pipeline_feature_id]).to eq described_class::SUGGEST_PIPELINE - end - - it 'provides a valid value for if it is dismissed' do - expect(subject[:is_dismissed_suggest_pipeline]).to be(false) - end - - context 'when the suggest pipeline has been dismissed' do - before do - create(:user_callout, user: user, feature_name: described_class::SUGGEST_PIPELINE) - end - - it 'indicates suggest pipeline has been dismissed' do - expect(subject[:is_dismissed_suggest_pipeline]).to be(true) - end - end + it 'provides a valid path value for user callout path' do + expect(subject[:user_callouts_path]).to eq '/-/user_callouts' + end - context 'when user is not logged in' do - let(:request) { double('request', current_user: nil, project: project) } + it 'provides a valid value for suggest pipeline feature id' do + expect(subject[:suggest_pipeline_feature_id]).to eq described_class::SUGGEST_PIPELINE + end - it 'returns a blank is dismissed value' do - expect(subject[:is_dismissed_suggest_pipeline]).to be_nil - end - end + it 'provides a valid value for if it is dismissed' do + expect(subject[:is_dismissed_suggest_pipeline]).to be(false) end - context 'when suggest pipeline feature is not enabled' do + context 'when the suggest pipeline has been dismissed' do before do - stub_feature_flags(suggest_pipeline: false) + create(:user_callout, user: user, feature_name: described_class::SUGGEST_PIPELINE) end - it 'provides no valid value for user callout path' do - expect(subject[:user_callouts_path]).to be_nil + it 'indicates suggest pipeline has been dismissed' do + expect(subject[:is_dismissed_suggest_pipeline]).to be(true) end + end - it 'provides no valid value for suggest pipeline feature id' do - expect(subject[:suggest_pipeline_feature_id]).to be_nil - end + context 'when user is not logged in' do + let(:request) { double('request', current_user: nil, project: project) } - it 'provides no valid value for if it is dismissed' do + it 'returns a blank is dismissed value' do expect(subject[:is_dismissed_suggest_pipeline]).to be_nil end end @@ -371,4 +351,18 @@ RSpec.describe MergeRequestWidgetEntity do it 'has security_reports_docs_path' do expect(subject[:security_reports_docs_path]).not_to be_nil end + + describe 'has source_project_default_url' do + it 'returns the default url to the source project' do + expect(subject[:source_project_default_url]).to eq project.http_url_to_repo + end + + context 'when source project is nil' do + it 'returns nil' do + allow(resource).to receive(:source_project).and_return(nil) + + expect(subject[:source_project_default_url]).to be_nil + end + end + end end diff --git a/spec/serializers/paginated_diff_entity_spec.rb b/spec/serializers/paginated_diff_entity_spec.rb index 551b392c9e9..7090ce1f08d 100644 --- a/spec/serializers/paginated_diff_entity_spec.rb +++ b/spec/serializers/paginated_diff_entity_spec.rb @@ -19,6 +19,10 @@ RSpec.describe PaginatedDiffEntity do subject { entity.as_json } + before do + stub_feature_flags(diffs_gradual_load: false) + end + it 'exposes diff_files' do expect(subject[:diff_files]).to be_present end diff --git a/spec/serializers/rollout_status_entity_spec.rb b/spec/serializers/rollout_status_entity_spec.rb new file mode 100644 index 00000000000..7ad4b259bcd --- /dev/null +++ b/spec/serializers/rollout_status_entity_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe RolloutStatusEntity do + include KubernetesHelpers + + let(:rollout_status) { kube_deployment_rollout_status } + + let(:entity) do + described_class.new(rollout_status, request: double) + end + + subject { entity.as_json } + + it "exposes status" do + is_expected.to include(:status) + end + + it 'exposes has_legacy_app_label' do + is_expected.to include(:has_legacy_app_label) + end + + context 'when kube deployment is valid' do + it "exposes deployment data" do + is_expected.to include(:instances, :completion, :is_completed) + end + + it 'does not expose canary ingress if it does not exist' do + is_expected.not_to include(:canary_ingress) + end + + context 'when canary ingress exists' do + let(:rollout_status) { kube_deployment_rollout_status(ingresses: [kube_ingress(track: :canary)]) } + + it 'expose canary ingress' do + is_expected.to include(:canary_ingress) + end + end + end + + context 'when kube deployment is empty' do + let(:rollout_status) { empty_deployment_rollout_status } + + it "exposes status" do + is_expected.to include(:status) + end + + it "does not expose deployment data" do + is_expected.not_to include(:instances, :completion, :is_completed, :canary_ingress) + end + end +end diff --git a/spec/serializers/rollout_statuses/ingress_entity_spec.rb b/spec/serializers/rollout_statuses/ingress_entity_spec.rb new file mode 100644 index 00000000000..b87b9e5c6c4 --- /dev/null +++ b/spec/serializers/rollout_statuses/ingress_entity_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe RolloutStatuses::IngressEntity do + include KubernetesHelpers + + let(:canary_ingress) { kube_ingress(track: :canary) } + + let(:entity) do + described_class.new(canary_ingress, request: double) + end + + subject { entity.as_json } + + it 'exposes canary weight' do + is_expected.to include(:canary_weight) + end +end |