summaryrefslogtreecommitdiff
path: root/spec/features/cycle_analytics_spec.rb
blob: de6cb53fdfa68567f7b24082664da15004ce4a52 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Value Stream Analytics', :js do
  let_it_be(:user) { create(:user) }
  let_it_be(:guest) { create(:user) }
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:stage_table_selector) { '[data-testid="vsa-stage-table"]' }
  let_it_be(:metrics_selector) { "[data-testid='vsa-time-metrics']" }

  let(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
  let(:milestone) { create(:milestone, project: project) }
  let(:mr) { create_merge_request_closing_issue(user, project, issue, commit_message: "References #{issue.to_reference}") }
  let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha, head_pipeline_of: mr) }

  context 'as an allowed user' do
    context 'when project is new' do
      before(:all) do
        project.add_maintainer(user)
      end

      before do
        sign_in(user)

        visit project_cycle_analytics_path(project)
        wait_for_requests
      end

      it 'displays metrics' do
        aggregate_failures 'with relevant values' do
          expect(new_issues_counter).to have_content('-')
          expect(commits_counter).to have_content('-')
          expect(deploys_counter).to have_content('-')
          expect(deployment_frequency_counter).to have_content('-')
        end
      end

      it 'shows active stage with empty message' do
        expect(page).to have_selector('.gl-path-active-item-indigo', text: 'Issue')
        expect(page).to have_content("We don't have enough data to show this stage.")
      end
    end

    context "when there's value stream analytics data" do
      before do
        project.add_maintainer(user)

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

        issue.metrics.update!(first_mentioned_in_commit_at: issue.metrics.first_associated_with_milestone_at + 1.hour)
        merge_request = issue.merge_requests_closing_issues.first.merge_request
        merge_request.update!(created_at: issue.metrics.first_associated_with_milestone_at + 1.hour)
        merge_request.metrics.update!(
          latest_build_started_at: 4.hours.ago,
          latest_build_finished_at: 3.hours.ago,
          merged_at: merge_request.created_at + 1.hour,
          first_deployed_to_production_at: merge_request.created_at + 2.hours
        )

        sign_in(user)
        visit project_cycle_analytics_path(project)
      end

      it 'displays metrics' do
        metrics_tiles = page.find(metrics_selector)

        aggregate_failures 'with relevant values' do
          expect(metrics_tiles).to have_content('Commit')
          expect(metrics_tiles).to have_content('Deploy')
          expect(metrics_tiles).to have_content('Deployment Frequency')
          expect(metrics_tiles).to have_content('New Issue')
        end
      end

      it 'shows data on each stage', :sidekiq_might_not_need_inline do
        expect_issue_to_be_present

        click_stage('Plan')
        expect_issue_to_be_present

        click_stage('Code')
        expect_merge_request_to_be_present

        click_stage('Test')
        expect_merge_request_to_be_present

        click_stage('Review')
        expect_merge_request_to_be_present

        click_stage('Staging')
        expect_merge_request_to_be_present
      end

      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_issue_counter).to have_content('1')
        end
      end
    end
  end

  context "as a guest" do
    before do
      project.add_developer(user)
      project.add_guest(guest)

      create_cycle(user, project, issue, mr, milestone, pipeline)
      deploy_master(user, project)

      sign_in(guest)
      visit project_cycle_analytics_path(project)
      wait_for_requests
    end

    it 'does not show the commit stats' do
      expect(page.find(metrics_selector)).not_to have_selector("#commits")
    end

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

      click_stage('Code')
      expect(find(stage_table_selector)).to have_content('You need permission.')

      click_stage('Review')
      expect(find(stage_table_selector)).to have_content('You need permission.')
    end
  end

  def find_metric_tile(sel)
    page.find("#{metrics_selector} #{sel}")
  end

  # When now use proper pluralization for the metric names, which affects the id
  def new_issue_counter
    find_metric_tile("#new-issue")
  end

  def new_issues_counter
    find_metric_tile("#new-issues")
  end

  def commits_counter
    find_metric_tile("#commits")
  end

  def deploys_counter
    find_metric_tile("#deploys")
  end

  def deployment_frequency_counter
    find_metric_tile("#deployment-frequency")
  end

  def expect_issue_to_be_present
    expect(find(stage_table_selector)).to have_content(issue.title)
    expect(find(stage_table_selector)).to have_content(issue.author.name)
    expect(find(stage_table_selector)).to have_content("##{issue.iid}")
  end

  def expect_merge_request_to_be_present
    expect(find(stage_table_selector)).to have_content(mr.title)
    expect(find(stage_table_selector)).to have_content(mr.author.name)
    expect(find(stage_table_selector)).to have_content("!#{mr.iid}")
  end

  def click_stage(stage_name)
    find('.gl-path-nav-list-item', text: stage_name).click
    wait_for_requests
  end
end