BigW Consortium Gitlab

cycle_analytics_spec.rb 5.33 KB
Newer Older
1 2
require 'spec_helper'

3
feature 'Cycle Analytics', :js do
4 5
  let(:user) { create(:user) }
  let(:guest) { create(:user) }
6
  let(:project) { create(:project, :repository) }
Alfredo Sumaran committed
7 8
  let(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
  let(:milestone) { create(:milestone, project: project) }
9
  let(:mr) { create_merge_request_closing_issue(issue, commit_message: "References #{issue.to_reference}") }
10
  let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha, head_pipeline_of: mr) }
11 12 13 14

  context 'as an allowed user' do
    context 'when project is new' do
      before  do
Felipe Artur committed
15
        project.add_master(user)
Felipe Artur committed
16

17
        sign_in(user)
Felipe Artur committed
18

19
        visit project_cycle_analytics_path(project)
20
        wait_for_requests
21 22 23 24 25 26
      end

      it 'shows introductory message' do
        expect(page).to have_content('Introducing Cycle Analytics')
      end

27 28 29 30 31 32
      it 'shows pipeline summary' do
        expect(new_issues_counter).to have_content('-')
        expect(commits_counter).to have_content('-')
        expect(deploys_counter).to have_content('-')
      end

33 34 35 36 37 38 39 40
      it 'shows active stage with empty message' do
        expect(page).to have_selector('.stage-nav-item.active', text: 'Issue')
        expect(page).to have_content("We don't have enough data to show this stage.")
      end
    end

    context "when there's cycle analytics data" do
      before do
41
        allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
Felipe Artur committed
42
        project.add_master(user)
43

44 45 46
        create_cycle
        deploy_master

47
        sign_in(user)
48
        visit project_cycle_analytics_path(project)
49 50
      end

51 52 53 54 55 56
      it 'shows pipeline summary' do
        expect(new_issues_counter).to have_content('1')
        expect(commits_counter).to have_content('2')
        expect(deploys_counter).to have_content('1')
      end

57 58 59 60
      it 'shows data on each stage' do
        expect_issue_to_be_present

        click_stage('Plan')
Alfredo Sumaran committed
61
        expect(find('.stage-events')).to have_content(mr.commits.last.title)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

        click_stage('Code')
        expect_merge_request_to_be_present

        click_stage('Test')
        expect_build_to_be_present

        click_stage('Review')
        expect_merge_request_to_be_present

        click_stage('Staging')
        expect_build_to_be_present

        click_stage('Production')
        expect_issue_to_be_present
      end
78 79 80 81 82 83 84 85 86 87 88 89 90 91

      context "when I change the time period observed" do
        before do
          _two_weeks_old_issue = create(:issue, project: project, created_at: 2.weeks.ago)

          click_button('Last 30 days')
          click_link('Last 7 days')
          wait_for_requests
        end

        it 'shows only relevant data' do
          expect(new_issues_counter).to have_content('1')
        end
      end
92
    end
93 94 95 96 97 98

    context "when my preferred language is Spanish" do
      before do
        user.update_attribute(:preferred_language, 'es')

        project.team << [user, :master]
99
        sign_in(user)
100
        visit project_cycle_analytics_path(project)
101
        wait_for_requests
102 103 104 105 106 107 108 109 110 111
      end

      it 'shows the content in Spanish' do
        expect(page).to have_content('Estado del Pipeline')
      end

      it 'resets the language to English' do
        expect(I18n.locale).to eq(:en)
      end
    end
112 113 114 115
  end

  context "as a guest" do
    before do
Felipe Artur committed
116
      project.add_guest(guest)
117 118 119 120 121

      allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
      create_cycle
      deploy_master

122
      sign_in(guest)
123
      visit project_cycle_analytics_path(project)
124
      wait_for_requests
125 126 127 128 129 130 131 132 133 134 135 136 137
    end

    it 'needs permissions to see restricted stages' do
      expect(find('.stage-events')).to have_content(issue.title)

      click_stage('Code')
      expect(find('.stage-events')).to have_content('You need permission.')

      click_stage('Review')
      expect(find('.stage-events')).to have_content('You need permission.')
    end
  end

138 139 140 141 142 143 144 145 146 147 148 149
  def new_issues_counter
    find(:xpath, "//p[contains(text(),'New Issue')]/preceding-sibling::h3")
  end

  def commits_counter
    find(:xpath, "//p[contains(text(),'Commits')]/preceding-sibling::h3")
  end

  def deploys_counter
    find(:xpath, "//p[contains(text(),'Deploy')]/preceding-sibling::h3")
  end

150 151 152 153 154 155 156 157 158 159 160 161 162
  def expect_issue_to_be_present
    expect(find('.stage-events')).to have_content(issue.title)
    expect(find('.stage-events')).to have_content(issue.author.name)
    expect(find('.stage-events')).to have_content("##{issue.iid}")
  end

  def expect_build_to_be_present
    expect(find('.stage-events')).to have_content(@build.ref)
    expect(find('.stage-events')).to have_content(@build.short_sha)
    expect(find('.stage-events')).to have_content("##{@build.id}")
  end

  def expect_merge_request_to_be_present
Alfredo Sumaran committed
163 164 165
    expect(find('.stage-events')).to have_content(mr.title)
    expect(find('.stage-events')).to have_content(mr.author.name)
    expect(find('.stage-events')).to have_content("!#{mr.iid}")
166 167 168 169 170 171 172 173 174
  end

  def create_cycle
    issue.update(milestone: milestone)
    pipeline.run

    @build = create(:ci_build, pipeline: pipeline, status: :success, author: user)

    merge_merge_requests_closing_issue(issue)
Alfredo Sumaran committed
175
    ProcessCommitWorker.new.perform(project.id, user.id, mr.commits.last.to_hash)
176 177 178 179
  end

  def click_stage(stage_name)
    find('.stage-nav li', text: stage_name).click
180
    wait_for_requests
181 182
  end
end