view_on_env_spec.rb 4.13 KB
Newer Older
1 2
# frozen_string_literal: true

Douwe Maan's avatar
Douwe Maan committed
3 4
require 'spec_helper'

5
describe 'View on environment', :js do
Douwe Maan's avatar
Douwe Maan committed
6 7
  let(:branch_name) { 'feature' }
  let(:file_path) { 'files/ruby/feature.rb' }
Douwe Maan's avatar
Douwe Maan committed
8
  let(:project) { create(:project, :repository) }
Douwe Maan's avatar
Douwe Maan committed
9 10 11
  let(:user) { project.creator }

  before do
12
    stub_feature_flags(diffs_batch_load: false)
13

14
    project.add_maintainer(user)
Douwe Maan's avatar
Douwe Maan committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28
  end

  context 'when the branch has a route map' do
    let(:route_map) do
      <<-MAP.strip_heredoc
      - source: /files/(.*)\\..*/
        public: '\\1'
      MAP
    end

    before do
      Files::CreateService.new(
        project,
        user,
Douwe Maan's avatar
Douwe Maan committed
29
        start_branch: branch_name,
30
        branch_name: branch_name,
31
        commit_message: 'Add .gitlab/route-map.yml',
Douwe Maan's avatar
Douwe Maan committed
32 33 34 35 36 37 38 39
        file_path: '.gitlab/route-map.yml',
        file_content: route_map
      ).execute

      # Update the file so that we still have a commit that will have a file on the environment
      Files::UpdateService.new(
        project,
        user,
Douwe Maan's avatar
Douwe Maan committed
40
        start_branch: branch_name,
41
        branch_name: branch_name,
42
        commit_message: 'Update feature',
Douwe Maan's avatar
Douwe Maan committed
43
        file_path: file_path,
44
        file_content: '# Noop'
Douwe Maan's avatar
Douwe Maan committed
45 46 47 48 49 50
      ).execute
    end

    context 'and an active deployment' do
      let(:sha) { project.commit(branch_name).sha }
      let(:environment) { create(:environment, project: project, name: 'review/feature', external_url: 'http://feature.review.example.com') }
51
      let!(:deployment) { create(:deployment, :success, environment: environment, ref: branch_name, sha: sha) }
Douwe Maan's avatar
Douwe Maan committed
52 53 54 55 56

      context 'when visiting the diff of a merge request for the branch' do
        let(:merge_request) { create(:merge_request, :simple, source_project: project, source_branch: branch_name) }

        before do
57
          sign_in(user)
Douwe Maan's avatar
Douwe Maan committed
58

59
          visit diffs_project_merge_request_path(project, merge_request)
Douwe Maan's avatar
Douwe Maan committed
60

61
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
62 63 64 65
        end

        it 'has a "View on env" button' do
          within '.diffs' do
Felipe Artur's avatar
Felipe Artur committed
66 67
            text = 'View on feature.review.example.com'
            url = 'http://feature.review.example.com/ruby/feature'
68
            expect(page).to have_selector("a[title='#{text}'][href='#{url}']")
Douwe Maan's avatar
Douwe Maan committed
69 70 71 72 73 74
          end
        end
      end

      context 'when visiting a comparison for the branch' do
        before do
75
          sign_in(user)
Douwe Maan's avatar
Douwe Maan committed
76

77
          visit project_compare_path(project, from: 'master', to: branch_name)
Douwe Maan's avatar
Douwe Maan committed
78

79
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
80 81 82 83 84 85 86 87 88
        end

        it 'has a "View on env" button' do
          expect(page).to have_link('View on feature.review.example.com', href: 'http://feature.review.example.com/ruby/feature')
        end
      end

      context 'when visiting a comparison for the commit' do
        before do
89
          sign_in(user)
Douwe Maan's avatar
Douwe Maan committed
90

91
          visit project_compare_path(project, from: 'master', to: sha)
Douwe Maan's avatar
Douwe Maan committed
92

93
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
94 95 96 97 98 99 100 101 102
        end

        it 'has a "View on env" button' do
          expect(page).to have_link('View on feature.review.example.com', href: 'http://feature.review.example.com/ruby/feature')
        end
      end

      context 'when visiting a blob on the branch' do
        before do
103
          sign_in(user)
Douwe Maan's avatar
Douwe Maan committed
104

105
          visit project_blob_path(project, File.join(branch_name, file_path))
Douwe Maan's avatar
Douwe Maan committed
106

107
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
108 109 110 111 112 113 114 115 116
        end

        it 'has a "View on env" button' do
          expect(page).to have_link('View on feature.review.example.com', href: 'http://feature.review.example.com/ruby/feature')
        end
      end

      context 'when visiting a blob on the commit' do
        before do
117
          sign_in(user)
Douwe Maan's avatar
Douwe Maan committed
118

119
          visit project_blob_path(project, File.join(sha, file_path))
Douwe Maan's avatar
Douwe Maan committed
120

121
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
122 123 124 125 126 127 128 129 130
        end

        it 'has a "View on env" button' do
          expect(page).to have_link('View on feature.review.example.com', href: 'http://feature.review.example.com/ruby/feature')
        end
      end

      context 'when visiting the commit' do
        before do
131
          sign_in(user)
Douwe Maan's avatar
Douwe Maan committed
132

133
          visit project_commit_path(project, sha)
Douwe Maan's avatar
Douwe Maan committed
134

135
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
136 137 138 139 140 141 142 143 144
        end

        it 'has a "View on env" button' do
          expect(page).to have_link('View on feature.review.example.com', href: 'http://feature.review.example.com/ruby/feature')
        end
      end
    end
  end
end