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

F
Felipe Artur 已提交
3
describe Boards::ListsController do
4
  let(:project) { create(:project) }
5
  let(:board)   { create(:board, project: project) }
6
  let(:user)    { create(:user) }
7
  let(:guest)   { create(:user) }
8 9

  before do
10
    project.add_maintainer(user)
11
    project.add_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
      expect(response).to have_gitlab_http_status(200)
19 20 21 22 23 24
      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')
P
Phil Hughes 已提交
30
      expect(parsed_response.length).to eq 3
31 32
    end

33 34
    context 'with unauthorized user' do
      before do
H
http://jneen.net/ 已提交
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
        expect(response).to have_gitlab_http_status(403)
43
      end
44 45
    end

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

B
blackst0ne 已提交
49 50 51 52 53
      get :index, params: {
                    namespace_id: project.namespace.to_param,
                    project_id: project,
                    board_id: board.to_param
                  },
54 55 56
                  format: :json
    end
  end
57

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

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

65
        expect(response).to have_gitlab_http_status(200)
66 67 68
      end

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

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

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

80
          expect(response).to have_gitlab_http_status(404)
81 82
        end
      end
83

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

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

90
          expect(response).to have_gitlab_http_status(404)
91
        end
92 93
      end
    end
94

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

99
        create_board_list user: guest, board: board, label_id: label.id
100

101
        expect(response).to have_gitlab_http_status(403)
102 103 104
      end
    end

105
    def create_board_list(user:, board:, label_id:)
106 107
      sign_in(user)

B
blackst0ne 已提交
108 109 110 111 112 113
      post :create, params: {
                      namespace_id: project.namespace.to_param,
                      project_id: project,
                      board_id: board.to_param,
                      list: { label_id: label_id }
                    },
114 115
                    format: :json
    end
116
  end
117

118
  describe 'PATCH update' do
119 120
    let!(:planning)    { create(:list, board: board, position: 0) }
    let!(:development) { create(:list, board: board, position: 1) }
121 122 123

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

126
        expect(response).to have_gitlab_http_status(200)
127 128 129
      end

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

132
        expect(planning.reload.position).to eq 1
133 134 135 136
      end
    end

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

140
        expect(response).to have_gitlab_http_status(422)
141 142 143 144 145
      end
    end

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

148
        expect(response).to have_gitlab_http_status(404)
149 150
      end
    end
151

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

156
        expect(response).to have_gitlab_http_status(403)
157 158 159
      end
    end

160
    def move(user:, board:, list:, position:)
161 162
      sign_in(user)

163 164 165 166 167 168 169
      params = { namespace_id: project.namespace.to_param,
                 project_id: project,
                 board_id: board.to_param,
                 id: list.to_param,
                 list: { position: position },
                 format: :json }

J
Jasper Maes 已提交
170
      patch :update, params: params, as: :json
171
    end
172
  end
173

174
  describe 'DELETE destroy' do
175
    let!(:planning) { create(:list, board: board, position: 0) }
176

177
    context 'with valid list id' do
178
      it 'returns a successful 200 response' do
179
        remove_board_list user: user, board: board, list: planning
180

181
        expect(response).to have_gitlab_http_status(200)
182 183 184
      end

      it 'removes list from board' do
185
        expect { remove_board_list user: user, board: board, list: planning }.to change(board.lists, :size).by(-1)
186 187 188 189 190
      end
    end

    context 'with invalid list id' do
      it 'returns a not found 404 response' do
191
        remove_board_list user: user, board: board, list: 999
192

193
        expect(response).to have_gitlab_http_status(404)
194 195
      end
    end
196

197
    context 'with unauthorized user' do
198
      it 'returns a forbidden 403 response' do
199
        remove_board_list user: guest, board: board, list: planning
200

201
        expect(response).to have_gitlab_http_status(403)
202 203 204
      end
    end

205
    def remove_board_list(user:, board:, list:)
206 207
      sign_in(user)

B
blackst0ne 已提交
208 209 210 211 212 213
      delete :destroy, params: {
                         namespace_id: project.namespace.to_param,
                         project_id: project,
                         board_id: board.to_param,
                         id: list.to_param
                       },
214 215
                       format: :json
    end
216
  end
217

218
  describe 'POST generate' do
219 220
    context 'when board lists is empty' do
      it 'returns a successful 200 response' do
221
        generate_default_lists user: user, board: board
222

223
        expect(response).to have_gitlab_http_status(200)
224 225 226
      end

      it 'returns the defaults lists' do
227
        generate_default_lists user: user, board: board
228

229
        expect(response).to match_response_schema('lists')
230 231 232 233
      end
    end

    context 'when board lists is not empty' do
234
      it 'returns an unprocessable entity 422 response' do
235 236
        create(:list, board: board)

237
        generate_default_lists user: user, board: board
238

239
        expect(response).to have_gitlab_http_status(422)
240 241 242
      end
    end

243
    context 'with unauthorized user' do
244
      it 'returns a forbidden 403 response' do
245
        generate_default_lists user: guest, board: board
246

247
        expect(response).to have_gitlab_http_status(403)
248 249 250
      end
    end

D
Douglas Barbosa Alexandre 已提交
251
    def generate_default_lists(user:, board:)
252 253
      sign_in(user)

B
blackst0ne 已提交
254 255 256 257 258
      post :generate, params: {
                        namespace_id: project.namespace.to_param,
                        project_id: project,
                        board_id: board.to_param
                      },
259 260 261
                      format: :json
    end
  end
262
end