summaryrefslogtreecommitdiff
path: root/spec/requests/api/usage_data_non_sql_metrics_spec.rb
blob: 225af57a2675f662ee9c35370f4a8528f00e91c9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::UsageDataNonSqlMetrics do
  include UsageDataHelpers

  let_it_be(:admin) { create(:user, admin: true) }
  let_it_be(:user) { create(:user) }

  before do
    stub_usage_data_connections
  end

  describe 'GET /usage_data/non_sql_metrics' do
    let(:endpoint) { '/usage_data/non_sql_metrics' }

    context 'with authentication' do
      before do
        stub_feature_flags(usage_data_non_sql_metrics: true)
      end

      it 'returns non sql metrics if user is admin' do
        get api(endpoint, admin)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['counts']).to be_a(Hash)
      end

      it 'returns forbidden if user is not admin' do
        get api(endpoint, user)

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end

    context 'without authentication' do
      before do
        stub_feature_flags(usage_data_non_sql_metrics: true)
      end

      it 'returns unauthorized' do
        get api(endpoint)

        expect(response).to have_gitlab_http_status(:unauthorized)
      end
    end

    context 'when feature_flag is disabled' do
      before do
        stub_feature_flags(usage_data_non_sql_metrics: false)
      end

      it 'returns not_found for admin' do
        get api(endpoint, admin)

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

      it 'returns forbidden for non-admin' do
        get api(endpoint, user)

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end
  end
end