summaryrefslogtreecommitdiff
path: root/spec/serializers/deployment_entity_spec.rb
blob: 76ad2aee5c566f6ef3cac35a61e3cee8946cbfda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'spec_helper'

describe DeploymentEntity do
  let(:user) { developer }
  let(:developer) { create(:user) }
  let(:reporter) { create(:user) }
  let(:project) { create(:project) }
  let(:request) { double('request') }
  let(:deployment) { create(:deployment, deployable: build, project: project) }
  let(:build) { create(:ci_build, :manual, pipeline: pipeline) }
  let(:pipeline) { create(:ci_pipeline, project: project, user: user) }
  let(:entity) { described_class.new(deployment, request: request) }

  subject { entity.as_json }

  before do
    project.add_developer(developer)
    project.add_reporter(reporter)
    allow(request).to receive(:current_user).and_return(user)
    allow(request).to receive(:project).and_return(project)
  end

  it 'exposes internal deployment id' do
    expect(subject).to include(:iid)
  end

  it 'exposes nested information about branch' do
    expect(subject[:ref][:name]).to eq 'master'
  end

  it 'exposes creation date' do
    expect(subject).to include(:created_at)
  end

  context 'when the pipeline has another manual action' do
    let(:other_build) { create(:ci_build, :manual, name: 'another deploy', pipeline: pipeline) }
    let!(:other_deployment) { create(:deployment, deployable: other_build) }

    it 'returns another manual action' do
      expect(subject[:manual_actions].count).to eq(1)
      expect(subject[:manual_actions].first[:name]).to eq('another deploy')
    end

    context 'when user is a reporter' do
      let(:user) { reporter }

      it 'returns another manual action' do
        expect(subject[:manual_actions]).not_to be_present
      end
    end

    context 'when deployment details serialization was disabled' do
      let(:entity) do
        described_class.new(deployment, request: request, deployment_details: false)
      end

      it 'does not serialize manual actions details' do
        expect(subject.with_indifferent_access).not_to include(:manual_actions)
      end
    end
  end

  describe 'scheduled_actions' do
    let(:project) { create(:project, :repository) }
    let(:pipeline) { create(:ci_pipeline, project: project, user: user) }
    let(:build) { create(:ci_build, :success, pipeline: pipeline) }
    let(:deployment) { create(:deployment, deployable: build) }

    context 'when the same pipeline has a scheduled action' do
      let(:other_build) { create(:ci_build, :schedulable, :success, pipeline: pipeline, name: 'other build') }
      let!(:other_deployment) { create(:deployment, deployable: other_build) }

      it 'returns other scheduled actions' do
        expect(subject[:scheduled_actions][0][:name]).to eq 'other build'
      end
    end

    context 'when the same pipeline does not have a scheduled action' do
      it 'does not return other actions' do
        expect(subject[:scheduled_actions]).to be_empty
      end
    end

    context 'when deployment details serialization was disabled' do
      let(:entity) do
        described_class.new(deployment, request: request, deployment_details: false)
      end

      it 'does not serialize scheduled actions details' do
        expect(subject.with_indifferent_access).not_to include(:scheduled_actions)
      end
    end
  end

  context 'when deployment details serialization was disabled' do
    include Gitlab::Routing

    let(:entity) do
      described_class.new(deployment, request: request, deployment_details: false)
    end

    it 'does not serialize deployment details' do
      expect(subject.with_indifferent_access)
        .not_to include(:commit, :manual_actions, :scheduled_actions)
    end

    it 'only exposes deployable name and path' do
      project_job_path(project, deployment.deployable).tap do |path|
        expect(subject.fetch(:deployable))
          .to eq(name: 'test', build_path: path)
      end
    end
  end
end