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

3
describe 'Cycle Analytics', :js do
4 5
  let(:user) { create(:user) }
  let(:guest) { create(:user) }
6
  let(:project) { create(:project, :repository) }
Alfredo Sumaran's avatar
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(user, project, 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

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

17
        sign_in(user)
Felipe Artur's avatar
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])
42
        project.add_maintainer(user)
43

44
        @build = create_cycle(user, project, issue, mr, milestone, pipeline)
45
        deploy_master(user, project)
46

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's avatar
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

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

98
        project.add_maintainer(user)
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
116
      project.add_developer(user)
Felipe Artur's avatar
Felipe Artur committed
117
      project.add_guest(guest)
118 119

      allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
120
      create_cycle(user, project, issue, mr, milestone, pipeline)
121
      deploy_master(user, project)
122

123
      sign_in(guest)
124
      visit project_cycle_analytics_path(project)
125
      wait_for_requests
126 127 128 129 130 131 132 133 134 135 136 137 138
    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

139 140 141 142 143 144 145 146 147 148 149 150
  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

151 152 153 154 155 156 157 158 159 160 161 162 163
  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's avatar
Alfredo Sumaran committed
164 165 166
    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}")
167 168 169 170
  end

  def click_stage(stage_name)
    find('.stage-nav li', text: stage_name).click
171
    wait_for_requests
172 173
  end
end