summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb
blob: 30d91346df3edebf349cce08d93abf517a2cc9f0 (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
# frozen_string_literal: true

shared_examples_for 'misconfigured dashboard service response' do |status_code, message = nil|
  it 'returns an appropriate message and status code', :aggregate_failures do
    result = service_call

    expect(result.keys).to contain_exactly(:message, :http_status, :status)
    expect(result[:status]).to eq(:error)
    expect(result[:http_status]).to eq(status_code)
    expect(result[:message]).to eq(message) if message
  end
end

shared_examples_for 'valid dashboard service response for schema' do
  it 'returns a json representation of the dashboard' do
    result = service_call

    expect(result.keys).to contain_exactly(:dashboard, :status)
    expect(result[:status]).to eq(:success)

    expect(JSON::Validator.fully_validate(dashboard_schema, result[:dashboard])).to be_empty
  end
end

shared_examples_for 'valid dashboard service response' do
  let(:dashboard_schema) { JSON.parse(fixture_file('lib/gitlab/metrics/dashboard/schemas/dashboard.json')) }

  it_behaves_like 'valid dashboard service response for schema'
end

shared_examples_for 'caches the unprocessed dashboard for subsequent calls' do
  it do
    expect(YAML).to receive(:safe_load).once.and_call_original

    described_class.new(*service_params).get_dashboard
    described_class.new(*service_params).get_dashboard
  end
end

shared_examples_for 'valid embedded dashboard service response' do
  let(:dashboard_schema) { JSON.parse(fixture_file('lib/gitlab/metrics/dashboard/schemas/embedded_dashboard.json')) }

  it_behaves_like 'valid dashboard service response for schema'
end

shared_examples_for 'raises error for users with insufficient permissions' do
  context 'when the user does not have sufficient access' do
    let(:user) { build(:user) }

    it_behaves_like 'misconfigured dashboard service response', :unauthorized
  end
end