summaryrefslogtreecommitdiff
path: root/spec/features/merge_request/user_sees_deployment_widget_spec.rb
blob: 14fbfc2fd3f68823236ed75293db447855f5f9a4 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

require 'rails_helper'

describe 'Merge request > User sees deployment widget', :js do
  describe 'when merge request has associated environments' do
    let(:user) { create(:user) }
    let(:project) { create(:project, :repository) }
    let(:merge_request) { create(:merge_request, :merged, source_project: project) }
    let(:environment) { create(:environment, project: project) }
    let(:role) { :developer }
    let(:ref) { merge_request.target_branch }
    let(:sha) { project.commit(ref).id }
    let(:pipeline) { create(:ci_pipeline_without_jobs, sha: sha, project: project, ref: ref) }
    let!(:manual) { }

    before do
      merge_request.update!(merge_commit_sha: sha)
      project.add_user(user, role)
      sign_in(user)
    end

    context 'when deployment succeeded' do
      let(:build) { create(:ci_build, :success, pipeline: pipeline) }
      let!(:deployment) { create(:deployment, :succeed, environment: environment, sha: sha, ref: ref, deployable: build) }

      it 'displays that the environment is deployed' do
        visit project_merge_request_path(project, merge_request)
        wait_for_requests

        expect(page).to have_content("Deployed to #{environment.name}")
        expect(find('.js-deploy-time')['data-original-title']).to eq(deployment.created_at.to_time.in_time_zone.to_s(:medium))
      end

      context 'when a user created a new merge request with the same SHA' do
        let(:pipeline2) { create(:ci_pipeline_without_jobs, sha: sha, project: project, ref: 'new-patch-1') }
        let(:build2) { create(:ci_build, :success, pipeline: pipeline2) }
        let(:environment2) { create(:environment, project: project) }
        let!(:deployment2) { create(:deployment, environment: environment2, sha: sha, ref: 'new-patch-1', deployable: build2) }

        it 'displays one environment which is related to the pipeline' do
          visit project_merge_request_path(project, merge_request)
          wait_for_requests

          expect(page).to have_selector('.js-deployment-info', count: 1)
          expect(page).to have_content("#{environment.name}")
          expect(page).not_to have_content("#{environment2.name}")
        end
      end
    end

    context 'when deployment failed' do
      let(:build) { create(:ci_build, :failed, pipeline: pipeline) }
      let!(:deployment) { create(:deployment, :failed, environment: environment, sha: sha, ref: ref, deployable: build) }

      it 'displays that the deployment failed' do
        visit project_merge_request_path(project, merge_request)
        wait_for_requests

        expect(page).to have_content("Failed to deploy to #{environment.name}")
        expect(page).not_to have_css('.js-deploy-time')
      end
    end

    context 'when deployment running' do
      let(:build) { create(:ci_build, :running, pipeline: pipeline) }
      let!(:deployment) { create(:deployment, :running, environment: environment, sha: sha, ref: ref, deployable: build) }

      it 'displays that the running deployment' do
        visit project_merge_request_path(project, merge_request)
        wait_for_requests

        expect(page).to have_content("Deploying to #{environment.name}")
        expect(page).not_to have_css('.js-deploy-time')
      end
    end

    context 'when deployment will happen' do
      let(:build) { create(:ci_build, :created, pipeline: pipeline) }
      let!(:deployment) { create(:deployment, environment: environment, sha: sha, ref: ref, deployable: build) }

      it 'displays that the environment name' do
        visit project_merge_request_path(project, merge_request)
        wait_for_requests

        expect(page).to have_content("Will deploy to #{environment.name}")
        expect(page).not_to have_css('.js-deploy-time')
      end
    end

    context 'when deployment was cancelled' do
      let(:build) { create(:ci_build, :canceled, pipeline: pipeline) }
      let!(:deployment) { create(:deployment, :canceled, environment: environment, sha: sha, ref: ref, deployable: build) }

      it 'displays that the environment name' do
        visit project_merge_request_path(project, merge_request)
        wait_for_requests

        expect(page).to have_content("Failed to deploy to #{environment.name}")
        expect(page).not_to have_css('.js-deploy-time')
      end
    end

    context 'with stop action' do
      let(:build) { create(:ci_build, :success, pipeline: pipeline) }
      let!(:deployment) { create(:deployment, :succeed, environment: environment, sha: sha, ref: ref, deployable: build) }
      let(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'close_app') }

      before do
        deployment.update!(on_stop: manual.name)
        visit project_merge_request_path(project, merge_request)
        wait_for_requests
      end

      it 'does start build when stop button clicked' do
        accept_confirm { find('.js-stop-env').click }

        expect(page).to have_content('close_app')
      end

      context 'for reporter' do
        let(:role) { :reporter }

        it 'does not show stop button' do
          expect(page).not_to have_selector('.js-stop-env')
        end
      end
    end
  end
end