diff options
Diffstat (limited to 'spec/serializers')
-rw-r--r-- | spec/serializers/build_action_entity_spec.rb | 12 | ||||
-rw-r--r-- | spec/serializers/build_artifact_entity_spec.rb | 22 | ||||
-rw-r--r-- | spec/serializers/build_details_entity_spec.rb | 67 | ||||
-rw-r--r-- | spec/serializers/build_entity_spec.rb | 9 | ||||
-rw-r--r-- | spec/serializers/merge_request_entity_spec.rb | 2 | ||||
-rw-r--r-- | spec/serializers/pipeline_details_entity_spec.rb | 120 | ||||
-rw-r--r-- | spec/serializers/pipeline_entity_spec.rb | 52 | ||||
-rw-r--r-- | spec/serializers/pipeline_serializer_spec.rb | 2 | ||||
-rw-r--r-- | spec/serializers/runner_entity_spec.rb | 23 | ||||
-rw-r--r-- | spec/serializers/user_entity_spec.rb | 6 |
10 files changed, 251 insertions, 64 deletions
diff --git a/spec/serializers/build_action_entity_spec.rb b/spec/serializers/build_action_entity_spec.rb index 059deba5416..15720d86583 100644 --- a/spec/serializers/build_action_entity_spec.rb +++ b/spec/serializers/build_action_entity_spec.rb @@ -1,26 +1,26 @@ require 'spec_helper' describe BuildActionEntity do - let(:build) { create(:ci_build, name: 'test_build') } + let(:job) { create(:ci_build, name: 'test_job') } let(:request) { double('request') } let(:entity) do - described_class.new(build, request: spy('request')) + described_class.new(job, request: spy('request')) end describe '#as_json' do subject { entity.as_json } - it 'contains original build name' do - expect(subject[:name]).to eq 'test_build' + it 'contains original job name' do + expect(subject[:name]).to eq 'test_job' end it 'contains path to the action play' do - expect(subject[:path]).to include "builds/#{build.id}/play" + expect(subject[:path]).to include "jobs/#{job.id}/play" end it 'contains whether it is playable' do - expect(subject[:playable]).to eq build.playable? + expect(subject[:playable]).to eq job.playable? end end end diff --git a/spec/serializers/build_artifact_entity_spec.rb b/spec/serializers/build_artifact_entity_spec.rb index 2fc60aa9de6..ad0d3d3839e 100644 --- a/spec/serializers/build_artifact_entity_spec.rb +++ b/spec/serializers/build_artifact_entity_spec.rb @@ -1,22 +1,32 @@ require 'spec_helper' describe BuildArtifactEntity do - let(:build) { create(:ci_build, name: 'test:build') } + let(:job) { create(:ci_build, name: 'test:job', artifacts_expire_at: 1.hour.from_now) } let(:entity) do - described_class.new(build, request: double) + described_class.new(job, request: double) end describe '#as_json' do subject { entity.as_json } - it 'contains build name' do - expect(subject[:name]).to eq 'test:build' + it 'contains job name' do + expect(subject[:name]).to eq 'test:job' end - it 'contains path to the artifacts' do + it 'exposes information about expiration of artifacts' do + expect(subject).to include(:expired, :expire_at) + end + + it 'contains paths to the artifacts' do expect(subject[:path]) - .to include "builds/#{build.id}/artifacts/download" + .to include "jobs/#{job.id}/artifacts/download" + + expect(subject[:keep_path]) + .to include "jobs/#{job.id}/artifacts/keep" + + expect(subject[:browse_path]) + .to include "jobs/#{job.id}/artifacts/browse" end end end diff --git a/spec/serializers/build_details_entity_spec.rb b/spec/serializers/build_details_entity_spec.rb new file mode 100644 index 00000000000..e2511e8968c --- /dev/null +++ b/spec/serializers/build_details_entity_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe BuildDetailsEntity do + set(:user) { create(:admin) } + + it 'inherits from BuildEntity' do + expect(described_class).to be < BuildEntity + end + + describe '#as_json' do + let(:project) { create(:project, :repository) } + let!(:build) { create(:ci_build, :failed, project: project) } + let(:request) { double('request') } + let(:entity) { described_class.new(build, request: request, current_user: user, project: project) } + subject { entity.as_json } + + before do + allow(request).to receive(:current_user).and_return(user) + end + + context 'when the user has access to issues and merge requests' do + let!(:merge_request) do + create(:merge_request, source_project: project, source_branch: build.ref) + end + + before do + allow(build).to receive(:merge_request).and_return(merge_request) + end + + it 'contains the needed key value pairs' do + expect(subject).to include(:coverage, :erased_at, :duration) + expect(subject).to include(:artifacts, :runner, :pipeline) + expect(subject).to include(:raw_path, :merge_request) + expect(subject).to include(:new_issue_path) + end + + it 'exposes details of the merge request' do + expect(subject[:merge_request]).to include(:iid, :path) + end + + context 'when the build has been erased' do + let!(:build) { create(:ci_build, :erasable, project: project) } + + it 'exposes the user whom erased the build' do + expect(subject).to include(:erase_path) + end + end + + context 'when the build has been erased' do + let!(:build) { create(:ci_build, erased_at: Time.now, project: project, erased_by: user) } + + it 'exposes the user whom erased the build' do + expect(subject).to include(:erased_by) + end + end + end + + context 'when the user can only read the build' do + let(:user) { create(:user) } + + it "won't display the paths to issues and merge requests" do + expect(subject['new_issue_path']).to be_nil + expect(subject['merge_request_path']).to be_nil + end + end + end +end diff --git a/spec/serializers/build_entity_spec.rb b/spec/serializers/build_entity_spec.rb index b5eb84ae43b..46d43a80ef7 100644 --- a/spec/serializers/build_entity_spec.rb +++ b/spec/serializers/build_entity_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' describe BuildEntity do let(:user) { create(:user) } - let(:build) { create(:ci_build) } + let(:build) { create(:ci_build, :failed) } + let(:project) { build.project } let(:request) { double('request') } before do @@ -17,6 +18,7 @@ describe BuildEntity do it 'contains paths to build page and retry action' do expect(subject).to include(:build_path, :retry_path) + expect(subject[:retry_path]).not_to be_nil end it 'does not contain sensitive information' do @@ -52,7 +54,10 @@ describe BuildEntity do context 'when user is allowed to trigger action' do before do - build.project.add_master(user) + project.add_developer(user) + + create(:protected_branch, :developers_can_merge, + name: 'master', project: project) end it 'contains path to play action' do diff --git a/spec/serializers/merge_request_entity_spec.rb b/spec/serializers/merge_request_entity_spec.rb index b75c73e78c2..d38433c2365 100644 --- a/spec/serializers/merge_request_entity_spec.rb +++ b/spec/serializers/merge_request_entity_spec.rb @@ -26,7 +26,7 @@ describe MergeRequestEntity do pipeline = build_stubbed(:ci_pipeline) allow(resource).to receive(:head_pipeline).and_return(pipeline) - pipeline_payload = PipelineEntity + pipeline_payload = PipelineDetailsEntity .represent(pipeline, request: req) .as_json diff --git a/spec/serializers/pipeline_details_entity_spec.rb b/spec/serializers/pipeline_details_entity_spec.rb new file mode 100644 index 00000000000..03cc5ae9b63 --- /dev/null +++ b/spec/serializers/pipeline_details_entity_spec.rb @@ -0,0 +1,120 @@ +require 'spec_helper' + +describe PipelineDetailsEntity do + set(:user) { create(:user) } + let(:request) { double('request') } + + it 'inherrits from PipelineEntity' do + expect(described_class).to be < PipelineEntity + end + + before 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 } + + context 'when pipeline is empty' do + let(:pipeline) { create(:ci_empty_pipeline) } + + it 'contains details' do + expect(subject).to include :details + expect(subject[:details]) + .to include :duration, :finished_at + expect(subject[:details]) + .to include :stages, :artifacts, :manual_actions + expect(subject[:details][:status]).to include :icon, :favicon, :text, :label + end + + it 'contains flags' do + expect(subject).to include :flags + expect(subject[:flags]) + .to include :latest, :stuck, + :yaml_errors, :retryable, :cancelable + end + end + + context 'when pipeline is retryable' do + let(:project) { create(:empty_project) } + + let(:pipeline) do + create(:ci_pipeline, status: :success, project: project) + end + + before do + create(:ci_build, :failed, pipeline: pipeline) + end + + context 'user has ability to retry pipeline' do + before { project.team << [user, :developer] } + + it 'retryable flag is true' do + expect(subject[:flags][:retryable]).to eq true + end + end + + context 'user does not have ability to retry pipeline' do + it 'retryable flag is false' do + expect(subject[:flags][:retryable]).to eq false + end + end + end + + context 'when pipeline is cancelable' do + let(:project) { create(:empty_project) } + + let(:pipeline) do + create(:ci_pipeline, status: :running, project: project) + end + + before do + create(:ci_build, :pending, pipeline: pipeline) + end + + context 'user has ability to cancel pipeline' do + before { project.add_developer(user) } + + it 'cancelable flag is true' do + expect(subject[:flags][:cancelable]).to eq true + end + end + + context 'user does not have ability to cancel pipeline' do + it 'cancelable flag is false' do + expect(subject[:flags][:cancelable]).to eq false + end + end + end + + context 'when pipeline has YAML errors' do + let(:pipeline) do + create(:ci_pipeline, config: { rspec: { invalid: :value } }) + end + + it 'contains information about error' do + expect(subject[:yaml_errors]).to be_present + end + + it 'contains flag that indicates there are errors' do + expect(subject[:flags][:yaml_errors]).to be true + end + end + + context 'when pipeline does not have YAML errors' do + let(:pipeline) { create(:ci_empty_pipeline) } + + it 'does not contain field that normally holds an error' do + expect(subject).not_to have_key(:yaml_errors) + end + + it 'contains flag that indicates there are no errors' do + expect(subject[:flags][:yaml_errors]).to be false + end + end + end +end diff --git a/spec/serializers/pipeline_entity_spec.rb b/spec/serializers/pipeline_entity_spec.rb index d2482ac434b..a059c2cc736 100644 --- a/spec/serializers/pipeline_entity_spec.rb +++ b/spec/serializers/pipeline_entity_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe PipelineEntity do - let(:user) { create(:user) } + set(:user) { create(:user) } let(:request) { double('request') } before do @@ -19,7 +19,7 @@ describe PipelineEntity do let(:pipeline) { create(:ci_empty_pipeline) } it 'contains required fields' do - expect(subject).to include :id, :user, :path, :coverage + expect(subject).to include :id, :user, :path, :coverage, :source expect(subject).to include :ref, :commit expect(subject).to include :updated_at, :created_at end @@ -28,15 +28,13 @@ describe PipelineEntity do expect(subject).to include :details expect(subject[:details]) .to include :duration, :finished_at - expect(subject[:details]) - .to include :stages, :artifacts, :manual_actions expect(subject[:details][:status]).to include :icon, :favicon, :text, :label end it 'contains flags' do expect(subject).to include :flags expect(subject[:flags]) - .to include :latest, :triggered, :stuck, + .to include :latest, :stuck, :yaml_errors, :retryable, :cancelable end end @@ -55,20 +53,12 @@ describe PipelineEntity do context 'user has ability to retry pipeline' do before { project.team << [user, :developer] } - it 'retryable flag is true' do - expect(subject[:flags][:retryable]).to eq true - end - it 'contains retry path' do expect(subject[:retry_path]).to be_present end end context 'user does not have ability to retry pipeline' do - it 'retryable flag is false' do - expect(subject[:flags][:retryable]).to eq false - end - it 'does not contain retry path' do expect(subject).not_to have_key(:retry_path) end @@ -87,11 +77,7 @@ describe PipelineEntity do end context 'user has ability to cancel pipeline' do - before { project.team << [user, :developer] } - - it 'cancelable flag is true' do - expect(subject[:flags][:cancelable]).to eq true - end + before { project.add_developer(user) } it 'contains cancel path' do expect(subject[:cancel_path]).to be_present @@ -99,42 +85,12 @@ describe PipelineEntity do end context 'user does not have ability to cancel pipeline' do - it 'cancelable flag is false' do - expect(subject[:flags][:cancelable]).to eq false - end - it 'does not contain cancel path' do expect(subject).not_to have_key(:cancel_path) end end end - context 'when pipeline has YAML errors' do - let(:pipeline) do - create(:ci_pipeline, config: { rspec: { invalid: :value } }) - end - - it 'contains flag that indicates there are errors' do - expect(subject[:flags][:yaml_errors]).to be true - end - - it 'contains information about error' do - expect(subject[:yaml_errors]).to be_present - end - end - - context 'when pipeline does not have YAML errors' do - let(:pipeline) { create(:ci_empty_pipeline) } - - it 'contains flag that indicates there are no errors' do - expect(subject[:flags][:yaml_errors]).to be false - end - - it 'does not contain field that normally holds an error' do - expect(subject).not_to have_key(:yaml_errors) - end - end - context 'when pipeline ref is empty' do let(:pipeline) { create(:ci_empty_pipeline) } diff --git a/spec/serializers/pipeline_serializer_spec.rb b/spec/serializers/pipeline_serializer_spec.rb index f2426db6d81..088f24eb180 100644 --- a/spec/serializers/pipeline_serializer_spec.rb +++ b/spec/serializers/pipeline_serializer_spec.rb @@ -113,7 +113,7 @@ describe PipelineSerializer do it "verifies number of queries" do recorded = ActiveRecord::QueryRecorder.new { subject } - expect(recorded.count).to be_within(1).of(58) + expect(recorded.count).to be_within(1).of(60) expect(recorded.cached_count).to eq(0) end diff --git a/spec/serializers/runner_entity_spec.rb b/spec/serializers/runner_entity_spec.rb new file mode 100644 index 00000000000..4f25a8dcfa0 --- /dev/null +++ b/spec/serializers/runner_entity_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe RunnerEntity do + let(:runner) { create(:ci_runner, :specific) } + let(:entity) { described_class.new(runner, request: request, current_user: user) } + let(:request) { double('request') } + let(:project) { create(:empty_project) } + let(:user) { create(:admin) } + + before do + allow(request).to receive(:current_user).and_return(user) + allow(request).to receive(:project).and_return(project) + end + + describe '#as_json' do + subject { entity.as_json } + + it 'contains required fields' do + expect(subject).to include(:id, :description) + expect(subject).to include(:edit_path) + end + end +end diff --git a/spec/serializers/user_entity_spec.rb b/spec/serializers/user_entity_spec.rb index c5d11cbcf5e..cd778e49107 100644 --- a/spec/serializers/user_entity_spec.rb +++ b/spec/serializers/user_entity_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe UserEntity do + include Gitlab::Routing + let(:entity) { described_class.new(user) } let(:user) { create(:user) } subject { entity.as_json } @@ -20,4 +22,8 @@ describe UserEntity do it 'does not expose 2FA OTPs' do expect(subject).not_to include(/otp/) end + + it 'exposes user path' do + expect(subject[:path]).to eq user_path(user) + end end |