boards_controller_spec.rb 3.2 KB
Newer Older
1 2 3
require 'spec_helper'

describe Projects::BoardsController do
4
  let(:project) { create(:project) }
5 6 7 8 9 10 11
  let(:user)    { create(:user) }

  before do
    project.team << [user, :master]
    sign_in(user)
  end

12 13 14 15 16
  describe 'GET index' do
    it 'creates a new project board when project does not have one' do
      expect { list_boards }.to change(project.boards, :count).by(1)
    end

17 18 19 20 21 22
    it 'sets boards_endpoint instance variable to a boards path' do
      list_boards

      expect(assigns(:boards_endpoint)).to eq project_boards_path(project)
    end

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
    context 'when format is HTML' do
      it 'renders template' do
        list_boards

        expect(response).to render_template :index
        expect(response.content_type).to eq 'text/html'
      end
    end

    context 'when format is JSON' do
      it 'returns a list of project boards' do
        create_list(:board, 2, project: project)

        list_boards format: :json

        parsed_response = JSON.parse(response.body)

        expect(response).to match_response_schema('boards')
        expect(parsed_response.length).to eq 2
      end
    end

    context 'with unauthorized user' do
      before do
        allow(Ability).to receive(:allowed?).with(user, :read_project, project).and_return(true)
        allow(Ability).to receive(:allowed?).with(user, :read_board, project).and_return(false)
      end

      it 'returns a not found 404 response' do
        list_boards

54
        expect(response).to have_gitlab_http_status(404)
55 56 57 58
      end
    end

    def list_boards(format: :html)
59 60
      get :index, namespace_id: project.namespace,
                  project_id: project,
61 62 63 64
                  format: format
    end
  end

65
  describe 'GET show' do
66 67
    let!(:board) { create(:board, project: project) }

L
Luke Bennett 已提交
68 69 70 71 72 73
    it 'sets boards_endpoint instance variable to a boards path' do
      read_board board: board

      expect(assigns(:boards_endpoint)).to eq project_boards_path(project)
    end

74 75 76 77 78 79 80
    context 'when format is HTML' do
      it 'renders template' do
        read_board board: board

        expect(response).to render_template :show
        expect(response.content_type).to eq 'text/html'
      end
81 82
    end

83 84 85
    context 'when format is JSON' do
      it 'returns project board' do
        read_board board: board, format: :json
86

87 88
        expect(response).to match_response_schema('board')
      end
89 90
    end

91 92
    context 'with unauthorized user' do
      before do
H
http://jneen.net/ 已提交
93 94
        allow(Ability).to receive(:allowed?).with(user, :read_project, project).and_return(true)
        allow(Ability).to receive(:allowed?).with(user, :read_board, project).and_return(false)
95
      end
96

97 98 99
      it 'returns a not found 404 response' do
        read_board board: board

100
        expect(response).to have_gitlab_http_status(404)
101 102 103 104 105 106 107 108
      end
    end

    context 'when board does not belong to project' do
      it 'returns a not found 404 response' do
        another_board = create(:board)

        read_board board: another_board
109

110
        expect(response).to have_gitlab_http_status(404)
111
      end
112 113
    end

114
    def read_board(board:, format: :html)
115 116
      get :show, namespace_id: project.namespace,
                 project_id: project,
117
                 id: board.to_param,
118
                 format: format
119 120 121
    end
  end
end