BigW Consortium Gitlab

lists_controller_spec.rb 7.14 KB
Newer Older
1 2
require 'spec_helper'

3
describe Projects::Boards::ListsController do
4 5
  let(:project) { create(:empty_project) }
  let(:board)   { create(:board, project: project) }
6
  let(:user)    { create(:user) }
7
  let(:guest)   { create(:user) }
8 9 10

  before do
    project.team << [user, :master]
11
    project.team << [guest, :guest]
12
  end
13

14
  describe 'GET index' do
15
    it 'returns a successful 200 response' do
16
      read_board_list user: user, board: board
17 18 19 20 21 22 23 24

      expect(response).to have_http_status(200)
      expect(response.content_type).to eq 'application/json'
    end

    it 'returns a list of board lists' do
      create(:list, board: board)

25
      read_board_list user: user, board: board
26 27 28

      parsed_response = JSON.parse(response.body)

29
      expect(response).to match_response_schema('lists')
30 31 32
      expect(parsed_response.length).to eq 3
    end

33 34
    context 'with unauthorized user' do
      before do
35 36
        allow(Ability).to receive(:allowed?).with(user, :read_project, project).and_return(true)
        allow(Ability).to receive(:allowed?).with(user, :read_list, project).and_return(false)
37
      end
38

39
      it 'returns a forbidden 403 response' do
40
        read_board_list user: user, board: board
41

42 43
        expect(response).to have_http_status(403)
      end
44 45
    end

46
    def read_board_list(user:, board:)
47 48 49 50
      sign_in(user)

      get :index, namespace_id: project.namespace.to_param,
                  project_id: project.to_param,
51
                  board_id: board.to_param,
52 53 54
                  format: :json
    end
  end
55

56
  describe 'POST create' do
57
    context 'with valid params' do
58 59
      let(:label) { create(:label, project: project, name: 'Development') }

60
      it 'returns a successful 200 response' do
61
        create_board_list user: user, board: board, label_id: label.id
62 63 64 65 66

        expect(response).to have_http_status(200)
      end

      it 'returns the created list' do
67
        create_board_list user: user, board: board, label_id: label.id
68 69 70 71 72 73

        expect(response).to match_response_schema('list')
      end
    end

    context 'with invalid params' do
74 75
      context 'when label is nil' do
        it 'returns a not found 404 response' do
76
          create_board_list user: user, board: board, label_id: nil
77 78 79 80

          expect(response).to have_http_status(404)
        end
      end
81

82 83 84
      context 'when label that does not belongs to project' do
        it 'returns a not found 404 response' do
          label = create(:label, name: 'Development')
85

86
          create_board_list user: user, board: board, label_id: label.id
87 88 89

          expect(response).to have_http_status(404)
        end
90 91
      end
    end
92

93
    context 'with unauthorized user' do
94 95
      it 'returns a forbidden 403 response' do
        label = create(:label, project: project, name: 'Development')
96

97
        create_board_list user: guest, board: board, label_id: label.id
98 99 100 101 102

        expect(response).to have_http_status(403)
      end
    end

103
    def create_board_list(user:, board:, label_id:)
104 105
      sign_in(user)

106 107
      post :create, namespace_id: project.namespace.to_param,
                    project_id: project.to_param,
108
                    board_id: board.to_param,
109 110 111
                    list: { label_id: label_id },
                    format: :json
    end
112
  end
113

114
  describe 'PATCH update' do
115 116
    let!(:planning)    { create(:list, board: board, position: 0) }
    let!(:development) { create(:list, board: board, position: 1) }
117 118 119

    context 'with valid position' do
      it 'returns a successful 200 response' do
120
        move user: user, board: board, list: planning, position: 1
121 122 123 124 125

        expect(response).to have_http_status(200)
      end

      it 'moves the list to the desired position' do
126
        move user: user, board: board, list: planning, position: 1
127

128
        expect(planning.reload.position).to eq 1
129 130 131 132
      end
    end

    context 'with invalid position' do
133
      it 'returns an unprocessable entity 422 response' do
134
        move user: user, board: board, list: planning, position: 6
135 136 137 138 139 140 141

        expect(response).to have_http_status(422)
      end
    end

    context 'with invalid list id' do
      it 'returns a not found 404 response' do
142
        move user: user, board: board, list: 999, position: 1
143 144 145 146

        expect(response).to have_http_status(404)
      end
    end
147

148
    context 'with unauthorized user' do
149
      it 'returns a forbidden 403 response' do
150
        move user: guest, board: board, list: planning, position: 6
151 152 153 154 155

        expect(response).to have_http_status(403)
      end
    end

156
    def move(user:, board:, list:, position:)
157 158
      sign_in(user)

159 160
      patch :update, namespace_id: project.namespace.to_param,
                     project_id: project.to_param,
161
                     board_id: board.to_param,
162 163 164 165
                     id: list.to_param,
                     list: { position: position },
                     format: :json
    end
166
  end
167

168
  describe 'DELETE destroy' do
169
    let!(:planning) { create(:list, board: board, position: 0) }
170

171
    context 'with valid list id' do
172
      it 'returns a successful 200 response' do
173
        remove_board_list user: user, board: board, list: planning
174 175 176 177 178

        expect(response).to have_http_status(200)
      end

      it 'removes list from board' do
179
        expect { remove_board_list user: user, board: board, list: planning }.to change(board.lists, :size).by(-1)
180 181 182 183 184
      end
    end

    context 'with invalid list id' do
      it 'returns a not found 404 response' do
185
        remove_board_list user: user, board: board, list: 999
186 187 188 189

        expect(response).to have_http_status(404)
      end
    end
190

191
    context 'with unauthorized user' do
192
      it 'returns a forbidden 403 response' do
193
        remove_board_list user: guest, board: board, list: planning
194 195 196 197 198

        expect(response).to have_http_status(403)
      end
    end

199
    def remove_board_list(user:, board:, list:)
200 201
      sign_in(user)

202 203
      delete :destroy, namespace_id: project.namespace.to_param,
                       project_id: project.to_param,
204
                       board_id: board.to_param,
205 206 207
                       id: list.to_param,
                       format: :json
    end
208
  end
209

210
  describe 'POST generate' do
211 212
    context 'when board lists is empty' do
      it 'returns a successful 200 response' do
213
        generate_default_lists user: user, board: board
214 215 216 217 218

        expect(response).to have_http_status(200)
      end

      it 'returns the defaults lists' do
219
        generate_default_lists user: user, board: board
220

221
        expect(response).to match_response_schema('lists')
222 223 224 225
      end
    end

    context 'when board lists is not empty' do
226
      it 'returns an unprocessable entity 422 response' do
227 228
        create(:list, board: board)

229
        generate_default_lists user: user, board: board
230 231 232 233 234

        expect(response).to have_http_status(422)
      end
    end

235
    context 'with unauthorized user' do
236
      it 'returns a forbidden 403 response' do
237
        generate_default_lists user: guest, board: board
238 239 240 241 242

        expect(response).to have_http_status(403)
      end
    end

243
    def generate_default_lists(user:, board:)
244 245
      sign_in(user)

246 247
      post :generate, namespace_id: project.namespace.to_param,
                      project_id: project.to_param,
248
                      board_id: board.to_param,
249 250 251
                      format: :json
    end
  end
252
end