pages_controller_spec.rb 2.72 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe Projects::PagesController do
  let(:user) { create(:user) }
5
  let(:project) { create(:project, :public) }
6 7 8 9 10 11 12 13 14 15 16

  let(:request_params) do
    {
      namespace_id: project.namespace,
      project_id: project
    }
  end

  before do
    allow(Gitlab.config.pages).to receive(:enabled).and_return(true)
    sign_in(user)
17
    project.add_maintainer(user)
18 19 20 21
  end

  describe 'GET show' do
    it 'returns 200 status' do
blackst0ne's avatar
blackst0ne committed
22
      get :show, params: request_params
23

24
      expect(response).to have_gitlab_http_status(200)
25
    end
26 27 28 29 30

    context 'when the project is in a subgroup' do
      let(:group) { create(:group, :nested) }
      let(:project) { create(:project, namespace: group) }

31
      it 'returns a 200 status code' do
blackst0ne's avatar
blackst0ne committed
32
        get :show, params: request_params
33

34
        expect(response).to have_gitlab_http_status(200)
35 36
      end
    end
37 38 39 40
  end

  describe 'DELETE destroy' do
    it 'returns 302 status' do
blackst0ne's avatar
blackst0ne committed
41
      delete :destroy, params: request_params
42

43
      expect(response).to have_gitlab_http_status(302)
44
    end
45 46 47 48 49 50 51 52 53 54 55 56

    context 'when user is developer' do
      before do
        project.add_developer(user)
      end

      it 'returns 404 status' do
        delete :destroy, params: request_params

        expect(response).to have_gitlab_http_status(404)
      end
    end
57 58 59 60 61 62 63 64 65
  end

  context 'pages disabled' do
    before do
      allow(Gitlab.config.pages).to receive(:enabled).and_return(false)
    end

    describe 'GET show' do
      it 'returns 404 status' do
blackst0ne's avatar
blackst0ne committed
66
        get :show, params: request_params
67

68
        expect(response).to have_gitlab_http_status(404)
69 70 71 72 73
      end
    end

    describe 'DELETE destroy' do
      it 'returns 404 status' do
blackst0ne's avatar
blackst0ne committed
74
        delete :destroy, params: request_params
75

76
        expect(response).to have_gitlab_http_status(404)
77 78 79
      end
    end
  end
Rob Watson's avatar
Rob Watson committed
80 81 82 83 84 85

  describe 'PATCH update' do
    let(:request_params) do
      {
        namespace_id: project.namespace,
        project_id: project,
86
        project: { pages_https_only: 'false' }
Rob Watson's avatar
Rob Watson committed
87 88 89 90 91 92 93 94 95 96
      }
    end

    let(:update_service) { double(execute: { status: :success }) }

    before do
      allow(Projects::UpdateService).to receive(:new) { update_service }
    end

    it 'returns 302 status' do
blackst0ne's avatar
blackst0ne committed
97
      patch :update, params: request_params
Rob Watson's avatar
Rob Watson committed
98 99 100 101 102

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

    it 'redirects back to the pages settings' do
blackst0ne's avatar
blackst0ne committed
103
      patch :update, params: request_params
Rob Watson's avatar
Rob Watson committed
104 105 106 107 108 109 110

      expect(response).to redirect_to(project_pages_path(project))
    end

    it 'calls the update service' do
      expect(Projects::UpdateService)
        .to receive(:new)
111
        .with(project, user, ActionController::Parameters.new(request_params[:project]).permit!)
Rob Watson's avatar
Rob Watson committed
112 113
        .and_return(update_service)

blackst0ne's avatar
blackst0ne committed
114
      patch :update, params: request_params
Rob Watson's avatar
Rob Watson committed
115 116
    end
  end
117
end