summaryrefslogtreecommitdiff
path: root/spec/controllers/boards/lists_controller_spec.rb
blob: 1e8a8145b3508c328b340f2b56ef72d79ca346ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# frozen_string_literal: true

require 'spec_helper'

describe Boards::ListsController do
  let(:project) { create(:project) }
  let(:board)   { create(:board, project: project) }
  let(:user)    { create(:user) }
  let(:guest)   { create(:user) }

  before do
    project.add_maintainer(user)
    project.add_guest(guest)
  end

  describe 'GET index' do
    it 'returns a successful 200 response' do
      read_board_list user: user, board: board

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

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

      read_board_list user: user, board: board

      expect(response).to match_response_schema('lists')
      expect(json_response.length).to eq 3
    end

    it 'avoids n+1 queries when serializing lists' do
      list_1 = create(:list, board: board)
      list_1.update_preferences_for(user, { collapsed: true })

      control_count = ActiveRecord::QueryRecorder.new { read_board_list user: user, board: board }.count

      list_2 = create(:list, board: board)
      list_2.update_preferences_for(user, { collapsed: true })

      list_3 = create(:list, board: board)
      list_3.update_preferences_for(user, { collapsed: true })

      expect { read_board_list user: user, board: board }.not_to exceed_query_limit(control_count)
    end

    context 'with unauthorized user' do
      let(:unauth_user) { create(:user) }

      it 'returns a forbidden 403 response' do
        read_board_list user: unauth_user, board: board

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

    def read_board_list(user:, board:)
      sign_in(user)

      get :index, params: {
                    namespace_id: project.namespace.to_param,
                    project_id: project,
                    board_id: board.to_param
                  },
                  format: :json
    end
  end

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

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

        expect(response).to have_gitlab_http_status(200)
      end

      it 'returns the created list' do
        create_board_list user: user, board: board, label_id: label.id

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

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

          expect(response).to have_gitlab_http_status(404)
        end
      end

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

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

          expect(response).to have_gitlab_http_status(404)
        end
      end
    end

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

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

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

    def create_board_list(user:, board:, label_id:)
      sign_in(user)

      post :create, params: {
                      namespace_id: project.namespace.to_param,
                      project_id: project,
                      board_id: board.to_param,
                      list: { label_id: label_id }
                    },
                    format: :json
    end
  end

  describe 'PATCH update' do
    let!(:planning)    { create(:list, board: board, position: 0) }
    let!(:development) { create(:list, board: board, position: 1) }

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

        expect(response).to have_gitlab_http_status(200)
      end

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

        expect(planning.reload.position).to eq 1
      end
    end

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

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

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

        expect(response).to have_gitlab_http_status(404)
      end
    end

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

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

    context 'with collapsed preference' do
      it 'saves collapsed preference for user' do
        save_setting user: user, board: board, list: planning, setting: { collapsed: true }

        expect(planning.preferences_for(user).collapsed).to eq(true)
        expect(response).to have_gitlab_http_status(200)
      end

      it 'saves not collapsed preference for user' do
        save_setting user: user, board: board, list: planning, setting: { collapsed: false }

        expect(planning.preferences_for(user).collapsed).to eq(false)
        expect(response).to have_gitlab_http_status(200)
      end
    end

    def move(user:, board:, list:, position:)
      sign_in(user)

      params = { namespace_id: project.namespace.to_param,
                 project_id: project,
                 board_id: board.to_param,
                 id: list.to_param,
                 list: { position: position },
                 format: :json }

      patch :update, params: params, as: :json
    end

    def save_setting(user:, board:, list:, setting: {})
      sign_in(user)

      params = { namespace_id: project.namespace.to_param,
                 project_id: project,
                 board_id: board.to_param,
                 id: list.to_param,
                 list: setting,
                 format: :json }

      patch :update, params: params, as: :json
    end
  end

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

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

        expect(response).to have_gitlab_http_status(200)
      end

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

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

        expect(response).to have_gitlab_http_status(404)
      end
    end

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

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

    def remove_board_list(user:, board:, list:)
      sign_in(user)

      delete :destroy, params: {
                         namespace_id: project.namespace.to_param,
                         project_id: project,
                         board_id: board.to_param,
                         id: list.to_param
                       },
                       format: :json
    end
  end

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

        expect(response).to have_gitlab_http_status(200)
      end

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

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

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

        generate_default_lists user: user, board: board

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

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

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

    def generate_default_lists(user:, board:)
      sign_in(user)

      post :generate, params: {
                        namespace_id: project.namespace.to_param,
                        project_id: project,
                        board_id: board.to_param
                      },
                      format: :json
    end
  end
end