BigW Consortium Gitlab

help_controller_spec.rb 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9
require 'spec_helper'

describe HelpController do
  let(:user) { create(:user) }

  before do
    sign_in(user)
  end

Fu Xu committed
10
  describe 'GET #index' do
11 12 13 14
    context 'with absolute url' do
      it 'keeps the URL absolute' do
        stub_readme("[API](/api/README.md)")

Fu Xu committed
15
        get :index
16 17

        expect(assigns[:help_index]).to eq '[API](/api/README.md)'
Fu Xu committed
18 19 20
      end
    end

21 22 23 24
    context 'with relative url' do
      it 'prefixes it with /help/' do
        stub_readme("[API](api/README.md)")

Fu Xu committed
25
        get :index
26 27

        expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
Fu Xu committed
28 29 30
      end
    end

31
    context 'when url is an external link' do
32
      it 'does not change it' do
33
        stub_readme("[external](https://some.external.link)")
34

Fu Xu committed
35
        get :index
36

37
        expect(assigns[:help_index]).to eq '[external](https://some.external.link)'
Fu Xu committed
38 39 40 41
      end
    end
  end

42 43 44 45
  describe 'GET #show' do
    context 'for Markdown formats' do
      context 'when requested file exists' do
        before do
Connor Shea committed
46
          get :show, path: 'ssh/README', format: :md
47 48 49 50 51 52 53 54 55 56 57 58 59 60
        end

        it 'assigns to @markdown' do
          expect(assigns[:markdown]).not_to be_empty
        end

        it 'renders HTML' do
          expect(response).to render_template('show.html.haml')
          expect(response.content_type).to eq 'text/html'
        end
      end

      context 'when requested file is missing' do
        it 'renders not found' do
Connor Shea committed
61
          get :show, path: 'foo/bar', format: :md
62 63 64 65 66 67 68 69
          expect(response).to be_not_found
        end
      end
    end

    context 'for image formats' do
      context 'when requested file exists' do
        it 'renders the raw file' do
70
          get :show,
71
              path: 'user/project/img/labels_filter',
72
              format: :png
73 74 75 76 77 78 79 80
          expect(response).to be_success
          expect(response.content_type).to eq 'image/png'
          expect(response.headers['Content-Disposition']).to match(/^inline;/)
        end
      end

      context 'when requested file is missing' do
        it 'renders not found' do
81
          get :show,
Connor Shea committed
82
              path: 'foo/bar',
83
              format: :png
84 85 86 87 88 89 90
          expect(response).to be_not_found
        end
      end
    end

    context 'for other formats' do
      it 'always renders not found' do
91
        get :show,
Connor Shea committed
92
            path: 'ssh/README',
93
            format: :foo
94 95 96 97
        expect(response).to be_not_found
      end
    end
  end
98 99 100 101 102 103 104 105 106

  describe 'GET #ui' do
    context 'for UI Development Kit' do
      it 'renders found' do
        get :ui
        expect(response).to have_http_status(200)
      end
    end
  end
Fu Xu committed
107 108 109 110

  def stub_readme(content)
    allow(File).to receive(:read).and_return(content)
  end
111
end