summaryrefslogtreecommitdiff
path: root/spec/controllers/admin/requests_profiles_controller_spec.rb
blob: 345f7720c25b38510f3f4193bad4e7a086345bf9 (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
# frozen_string_literal: true

require 'spec_helper'

describe Admin::RequestsProfilesController do
  set(:admin) { create(:admin) }

  before do
    sign_in(admin)
  end

  describe '#show' do
    let(:tmpdir) { Dir.mktmpdir('profiler-test') }
    let(:test_file) { File.join(tmpdir, basename) }

    subject do
      get :show, params: { name: basename }
    end

    before do
      stub_const('Gitlab::RequestProfiler::PROFILES_DIR', tmpdir)
      File.write(test_file, sample_data)
    end

    after do
      FileUtils.rm_rf(tmpdir)
    end

    context 'when loading HTML profile' do
      let(:basename) { "profile_#{Time.now.to_i}_execution.html" }

      let(:sample_data) do
        '<html> <body> <h1>Heading</h1> <p>paragraph.</p> </body> </html>'
      end

      it 'renders the data' do
        subject

        expect(response).to have_gitlab_http_status(200)
        expect(response.body).to eq(sample_data)
      end
    end

    context 'when loading TXT profile' do
      let(:basename) { "profile_#{Time.now.to_i}_memory.txt" }

      let(:sample_data) do
        <<~TXT
          Total allocated: 112096396 bytes (1080431 objects)
          Total retained:  10312598 bytes (53567 objects)
        TXT
      end

      it 'renders the data' do
        subject

        expect(response).to have_gitlab_http_status(200)
        expect(response.body).to eq(sample_data)
      end
    end

    context 'when loading PDF profile' do
      let(:basename) { "profile_#{Time.now.to_i}_anything.pdf" }

      let(:sample_data) { 'mocked pdf content' }

      it 'fails to render the data' do
        expect { subject }.to raise_error(ActionController::UrlGenerationError, /No route matches.*unmatched constraints:/)
      end
    end
  end
end