lints_controller_spec.rb 2.9 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

M
Mayra Cabrera 已提交
3 4 5
require 'spec_helper'

describe Projects::Ci::LintsController do
6 7
  include StubRequests

M
Mayra Cabrera 已提交
8 9 10 11 12 13 14 15 16 17 18 19
  let(:project) { create(:project, :repository) }
  let(:user) { create(:user) }

  before do
    sign_in(user)
  end

  describe 'GET #show' do
    context 'with enough privileges' do
      before do
        project.add_developer(user)

B
blackst0ne 已提交
20
        get :show, params: { namespace_id: project.namespace, project_id: project }
M
Mayra Cabrera 已提交
21 22
      end

23
      it 'is success' do
M
Mayra Cabrera 已提交
24 25 26
        expect(response).to be_success
      end

27
      it 'renders show page' do
M
Mayra Cabrera 已提交
28 29 30
        expect(response).to render_template :show
      end

31
      it 'retrieves project' do
M
Mayra Cabrera 已提交
32 33 34 35 36 37 38 39
        expect(assigns(:project)).to eq(project)
      end
    end

    context 'without enough privileges' do
      before do
        project.add_guest(user)

B
blackst0ne 已提交
40
        get :show, params: { namespace_id: project.namespace, project_id: project }
M
Mayra Cabrera 已提交
41 42
      end

43
      it 'responds with 404' do
M
Mayra Cabrera 已提交
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
        expect(response).to have_gitlab_http_status(404)
      end
    end
  end

  describe 'POST #create' do
    let(:remote_file_path) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }

    let(:remote_file_content) do
      <<~HEREDOC
      before_script:
        - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
        - ruby -v
        - which ruby
        - bundle install --jobs $(nproc)  "${FLAGS[@]}"
      HEREDOC
    end

    let(:content) do
      <<~HEREDOC
      include:
        - #{remote_file_path}

      rubocop:
        script:
          - bundle exec rubocop
      HEREDOC
    end

    context 'with a valid gitlab-ci.yml' do
      before do
75
        stub_full_request(remote_file_path).to_return(body: remote_file_content)
M
Mayra Cabrera 已提交
76 77
        project.add_developer(user)

B
blackst0ne 已提交
78
        post :create, params: { namespace_id: project.namespace, project_id: project, content: content }
M
Mayra Cabrera 已提交
79 80
      end

81
      it 'is success' do
M
Mayra Cabrera 已提交
82 83 84 85 86 87 88
        expect(response).to be_success
      end

      it 'render show page' do
        expect(response).to render_template :show
      end

89
      it 'retrieves project' do
M
Mayra Cabrera 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        expect(assigns(:project)).to eq(project)
      end
    end

    context 'with an invalid gitlab-ci.yml' do
      let(:content) do
        <<~HEREDOC
        rubocop:
          scriptt:
            - bundle exec rubocop
        HEREDOC
      end

      before do
        project.add_developer(user)

B
blackst0ne 已提交
106
        post :create, params: { namespace_id: project.namespace, project_id: project, content: content }
M
Mayra Cabrera 已提交
107 108
      end

109
      it 'assigns errors' do
110
        expect(assigns[:error]).to eq('root config contains unknown keys: rubocop')
M
Mayra Cabrera 已提交
111 112 113 114 115 116 117
      end
    end

    context 'without enough privileges' do
      before do
        project.add_guest(user)

B
blackst0ne 已提交
118
        post :create, params: { namespace_id: project.namespace, project_id: project, content: content }
M
Mayra Cabrera 已提交
119 120
      end

121
      it 'responds with 404' do
M
Mayra Cabrera 已提交
122 123 124 125 126
        expect(response).to have_gitlab_http_status(404)
      end
    end
  end
end