Welcome to mirror list, hosted at ThFree Co, Russian Federation.

notes_spec.rb « alert « alert_management « project « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1350cba119bc46413d80e2d6b5c0d0dfbcbda8c8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'getting Alert Management Alert Notes' do
  include GraphqlHelpers

  let_it_be(:project) { create(:project) }
  let_it_be(:current_user) { create(:user) }
  let_it_be(:first_alert) { create(:alert_management_alert, project: project, assignees: [current_user]) }
  let_it_be(:second_alert) { create(:alert_management_alert, project: project) }
  let_it_be(:first_system_note) { create(:note_on_alert, :with_system_note_metadata, noteable: first_alert, project: project) }
  let_it_be(:second_system_note) { create(:note_on_alert, :with_system_note_metadata, noteable: first_alert, project: project) }

  let(:params) { {} }

  let(:fields) do
    <<~QUERY
      nodes {
        iid
        notes {
          nodes {
            id
            body
            systemNoteIconName
          }
        }
      }
    QUERY
  end

  let(:query) do
    graphql_query_for(
      'project',
      { 'fullPath' => project.full_path },
      query_graphql_field('alertManagementAlerts', params, fields)
    )
  end

  let(:alerts_result) { graphql_data.dig('project', 'alertManagementAlerts', 'nodes') }
  let(:notes_result) { alerts_result.map { |alert| [alert['iid'], alert['notes']['nodes']] }.to_h }
  let(:first_notes_result) { notes_result[first_alert.iid.to_s] }
  let(:second_notes_result) { notes_result[second_alert.iid.to_s] }

  before do
    project.add_developer(current_user)
  end

  it 'includes expected data' do
    post_graphql(query, current_user: current_user)

    expect(first_notes_result.first).to include(
      'id' => first_system_note.to_global_id.to_s,
      'systemNoteIconName' => 'git-merge',
      'body' => first_system_note.note
    )
  end

  it 'returns the notes ordered by createdAt with sufficient content' do
    post_graphql(query, current_user: current_user)

    expect(first_notes_result.length).to eq(2)
    expect(first_notes_result.first).to include('id' => first_system_note.to_global_id.to_s)
    expect(first_notes_result.second).to include('id' => second_system_note.to_global_id.to_s)
    expect(second_notes_result).to be_empty
  end

  it 'avoids N+1 queries' do
    base_count = ActiveRecord::QueryRecorder.new do
      post_graphql(query, current_user: current_user)
    end

    # An N+1 would mean a new alert would increase the query count
    create(:alert_management_alert, project: project)

    expect { post_graphql(query, current_user: current_user) }.not_to exceed_query_limit(base_count)
    expect(alerts_result.length).to eq(3)
  end

  context 'for non-system notes' do
    let_it_be(:user_note) { create(:note_on_alert, noteable: second_alert, project: project) }

    it 'includes expected data' do
      post_graphql(query, current_user: current_user)

      expect(second_notes_result.first).to include(
        'id' => user_note.to_global_id.to_s,
        'systemNoteIconName' => nil,
        'body' => user_note.note
      )
    end
  end
end