BigW Consortium Gitlab

cycle_analytics_events_spec.rb 4.66 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'cycle analytics events', api: true do
4
  let(:user) { create(:user) }
5
  let(:project) { create(:project, :repository, public_builds: false) }
6
  let(:issue) {  create(:issue, project: project, created_at: 2.days.ago) }
7 8 9 10 11

  describe 'GET /:namespace/:project/cycle_analytics/events/issues' do
    before do
      project.team << [user, :developer]

12 13
      allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])

14 15 16 17 18 19
      3.times do |count|
        Timecop.freeze(Time.now + count.days) do
          create_cycle
        end
      end

20 21
      deploy_master

22 23 24
      login_as(user)
    end

25
    it 'lists the issue events' do
26 27
      get namespace_project_cycle_analytics_issue_path(project.namespace, project, format: :json)

28
      first_issue_iid = project.issues.sort(:created_desc).pluck(:iid).first.to_s
29

30
      expect(json_response['events']).not_to be_empty
31
      expect(json_response['events'].first['iid']).to eq(first_issue_iid)
32 33
    end

34
    it 'lists the plan events' do
35 36
      get namespace_project_cycle_analytics_plan_path(project.namespace, project, format: :json)

37
      first_mr_short_sha = project.merge_requests.sort(:created_asc).first.commits.first.short_id
38

39 40
      expect(json_response['events']).not_to be_empty
      expect(json_response['events'].first['short_sha']).to eq(first_mr_short_sha)
41 42
    end

43
    it 'lists the code events' do
44 45
      get namespace_project_cycle_analytics_code_path(project.namespace, project, format: :json)

46
      expect(json_response['events']).not_to be_empty
47

48
      first_mr_iid = project.merge_requests.sort(:created_desc).pluck(:iid).first.to_s
49

50
      expect(json_response['events'].first['iid']).to eq(first_mr_iid)
51 52
    end

53
    it 'lists the test events' do
54 55
      get namespace_project_cycle_analytics_test_path(project.namespace, project, format: :json)

56 57
      expect(json_response['events']).not_to be_empty
      expect(json_response['events'].first['date']).not_to be_empty
58 59
    end

60
    it 'lists the review events' do
61 62
      get namespace_project_cycle_analytics_review_path(project.namespace, project, format: :json)

63
      first_mr_iid = project.merge_requests.sort(:created_desc).pluck(:iid).first.to_s
64

65
      expect(json_response['events']).not_to be_empty
66
      expect(json_response['events'].first['iid']).to eq(first_mr_iid)
67 68
    end

69
    it 'lists the staging events' do
70 71
      get namespace_project_cycle_analytics_staging_path(project.namespace, project, format: :json)

72 73
      expect(json_response['events']).not_to be_empty
      expect(json_response['events'].first['date']).not_to be_empty
74 75
    end

76
    it 'lists the production events' do
77
      get namespace_project_cycle_analytics_production_path(project.namespace, project, format: :json)
78

79
      first_issue_iid = project.issues.sort(:created_desc).pluck(:iid).first.to_s
80

81
      expect(json_response['events']).not_to be_empty
82 83 84 85 86
      expect(json_response['events'].first['iid']).to eq(first_issue_iid)
    end

    context 'specific branch' do
      it 'lists the test events' do
87
        branch = project.merge_requests.first.source_branch
88 89 90 91 92 93

        get namespace_project_cycle_analytics_test_path(project.namespace, project, format: :json, branch: branch)

        expect(json_response['events']).not_to be_empty
        expect(json_response['events'].first['date']).not_to be_empty
      end
94
    end
95 96 97

    context 'with private project and builds' do
      before do
98
        project.members.first.update(access_level: Gitlab::Access::GUEST)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
      end

      it 'does not list the test events' do
        get namespace_project_cycle_analytics_test_path(project.namespace, project, format: :json)

        expect(response).to have_http_status(:not_found)
      end

      it 'does not list the staging events' do
        get namespace_project_cycle_analytics_staging_path(project.namespace, project, format: :json)

        expect(response).to have_http_status(:not_found)
      end

      it 'lists the issue events' do
        get namespace_project_cycle_analytics_issue_path(project.namespace, project, format: :json)

        expect(response).to have_http_status(:ok)
      end
    end
119 120
  end

121 122 123
  def create_cycle
    milestone = create(:milestone, project: project)
    issue.update(milestone: milestone)
124 125 126 127
    mr = create_merge_request_closing_issue(issue)

    pipeline = create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha)
    pipeline.run
128

129 130 131
    create(:ci_build, pipeline: pipeline, status: :success, author: user)
    create(:ci_build, pipeline: pipeline, status: :success, author: user)

132
    merge_merge_requests_closing_issue(issue)
133

134
    ProcessCommitWorker.new.perform(project.id, user.id, mr.commits.last.to_hash)
135
  end
136
end