BigW Consortium Gitlab

labels_spec.rb 14.6 KB
Newer Older
Dmitriy Zaporozhets committed
1 2
require 'spec_helper'

3
describe API::Labels do
Dmitriy Zaporozhets committed
4
  let(:user) { create(:user) }
5
  let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
Dmitriy Zaporozhets committed
6
  let!(:label1) { create(:label, title: 'label1', project: project) }
7
  let!(:priority_label) { create(:label, title: 'bug', project: project, priority: 3) }
Dmitriy Zaporozhets committed
8 9 10 11 12 13

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

  describe 'GET /projects/:id/labels' do
14 15
    it 'returns all available labels to the project' do
      group = create(:group)
16
      group_label = create(:group_label, title: 'feature', group: group)
17
      project.update(group: group)
18 19
      create(:labeled_issue, project: project, labels: [group_label], author: user)
      create(:labeled_issue, project: project, labels: [label1], author: user, state: :closed)
Francesco Coda Zabetta committed
20 21
      create(:labeled_merge_request, labels: [priority_label], author: user, source_project: project )

Douwe Maan committed
22
      expected_keys = %w(
Douwe Maan committed
23 24 25 26
        id name color description
        open_issues_count closed_issues_count open_merge_requests_count
        subscribed priority
      )
27

Dmitriy Zaporozhets committed
28
      get api("/projects/#{project.id}/labels", user)
29

30
      expect(response).to have_gitlab_http_status(200)
31
      expect(response).to include_pagination_headers
32
      expect(json_response).to be_an Array
33
      expect(json_response.size).to eq(3)
34
      expect(json_response.first.keys).to match_array expected_keys
35
      expect(json_response.map { |l| l['name'] }).to match_array([group_label.name, priority_label.name, label1.name])
36

37 38 39
      label1_response = json_response.find { |l| l['name'] == label1.title }
      group_label_response = json_response.find { |l| l['name'] == group_label.title }
      priority_label_response = json_response.find { |l| l['name'] == priority_label.title }
40 41 42

      expect(label1_response['open_issues_count']).to eq(0)
      expect(label1_response['closed_issues_count']).to eq(1)
Francesco Coda Zabetta committed
43
      expect(label1_response['open_merge_requests_count']).to eq(0)
44 45 46 47 48
      expect(label1_response['name']).to eq(label1.name)
      expect(label1_response['color']).to be_present
      expect(label1_response['description']).to be_nil
      expect(label1_response['priority']).to be_nil
      expect(label1_response['subscribed']).to be_falsey
Francesco Coda Zabetta committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

      expect(group_label_response['open_issues_count']).to eq(1)
      expect(group_label_response['closed_issues_count']).to eq(0)
      expect(group_label_response['open_merge_requests_count']).to eq(0)
      expect(group_label_response['name']).to eq(group_label.name)
      expect(group_label_response['color']).to be_present
      expect(group_label_response['description']).to be_nil
      expect(group_label_response['priority']).to be_nil
      expect(group_label_response['subscribed']).to be_falsey

      expect(priority_label_response['open_issues_count']).to eq(0)
      expect(priority_label_response['closed_issues_count']).to eq(0)
      expect(priority_label_response['open_merge_requests_count']).to eq(1)
      expect(priority_label_response['name']).to eq(priority_label.name)
      expect(priority_label_response['color']).to be_present
      expect(priority_label_response['description']).to be_nil
      expect(priority_label_response['priority']).to eq(3)
      expect(priority_label_response['subscribed']).to be_falsey
Dmitriy Zaporozhets committed
67 68
    end
  end
69 70

  describe 'POST /projects/:id/labels' do
71
    it 'returns created label when all params' do
72 73 74
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAABB',
75 76 77
           description: 'test',
           priority: 2

78
      expect(response).to have_gitlab_http_status(201)
79 80 81
      expect(json_response['name']).to eq('Foo')
      expect(json_response['color']).to eq('#FFAABB')
      expect(json_response['description']).to eq('test')
82
      expect(json_response['priority']).to eq(2)
83 84
    end

85
    it 'returns created label when only required params' do
86
      post api("/projects/#{project.id}/labels", user),
87
           name: 'Foo & Bar',
88
           color: '#FFAABB'
89

90 91
      expect(response.status).to eq(201)
      expect(json_response['name']).to eq('Foo & Bar')
92
      expect(json_response['color']).to eq('#FFAABB')
93
      expect(json_response['description']).to be_nil
94 95 96 97 98 99 100 101 102 103 104 105 106 107
      expect(json_response['priority']).to be_nil
    end

    it 'creates a prioritized label' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo & Bar',
           color: '#FFAABB',
           priority: 3

      expect(response.status).to eq(201)
      expect(json_response['name']).to eq('Foo & Bar')
      expect(json_response['color']).to eq('#FFAABB')
      expect(json_response['description']).to be_nil
      expect(json_response['priority']).to eq(3)
108 109
    end

110
    it 'returns a 400 bad request if name not given' do
111
      post api("/projects/#{project.id}/labels", user), color: '#FFAABB'
112
      expect(response).to have_gitlab_http_status(400)
113 114
    end

115
    it 'returns a 400 bad request if color not given' do
116
      post api("/projects/#{project.id}/labels", user), name: 'Foobar'
117
      expect(response).to have_gitlab_http_status(400)
118 119
    end

120
    it 'returns 400 for invalid color' do
121 122 123
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAA'
124
      expect(response).to have_gitlab_http_status(400)
125
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
126 127
    end

128
    it 'returns 400 for too long color code' do
129 130 131
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
132
      expect(response).to have_gitlab_http_status(400)
133
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
134 135
    end

136
    it 'returns 400 for invalid name' do
137
      post api("/projects/#{project.id}/labels", user),
138
           name: ',',
139
           color: '#FFAABB'
140
      expect(response).to have_gitlab_http_status(400)
141
      expect(json_response['message']['title']).to eq(['is invalid'])
142 143
    end

144 145 146 147 148 149 150 151 152
    it 'returns 409 if label already exists in group' do
      group = create(:group)
      group_label = create(:group_label, group: group)
      project.update(group: group)

      post api("/projects/#{project.id}/labels", user),
           name: group_label.name,
           color: '#FFAABB'

153
      expect(response).to have_gitlab_http_status(409)
154 155 156
      expect(json_response['message']).to eq('Label already exists')
    end

157 158 159 160 161 162
    it 'returns 400 for invalid priority' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF',
           priority: 'foo'

163
      expect(response).to have_gitlab_http_status(400)
164 165
    end

166
    it 'returns 409 if label already exists in project' do
167 168 169
      post api("/projects/#{project.id}/labels", user),
           name: 'label1',
           color: '#FFAABB'
170
      expect(response).to have_gitlab_http_status(409)
171
      expect(json_response['message']).to eq('Label already exists')
172 173 174 175
    end
  end

  describe 'DELETE /projects/:id/labels' do
176
    it 'returns 204 for existing label' do
Robert Schilling committed
177
      delete api("/projects/#{project.id}/labels", user), name: 'label1'
178

179
      expect(response).to have_gitlab_http_status(204)
180 181
    end

182
    it 'returns 404 for non existing label' do
Robert Schilling committed
183
      delete api("/projects/#{project.id}/labels", user), name: 'label2'
184
      expect(response).to have_gitlab_http_status(404)
185
      expect(json_response['message']).to eq('404 Label Not Found')
186 187
    end

188
    it 'returns 400 for wrong parameters' do
189
      delete api("/projects/#{project.id}/labels", user)
190
      expect(response).to have_gitlab_http_status(400)
191
    end
192 193 194 195 196

    it_behaves_like '412 response' do
      let(:request) { api("/projects/#{project.id}/labels", user) }
      let(:params) { { name: 'label1' } }
    end
197
  end
Robert Schilling committed
198 199

  describe 'PUT /projects/:id/labels' do
200
    it 'returns 200 if name and colors and description are changed' do
Robert Schilling committed
201 202 203
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label',
204 205
          color: '#FFFFFF',
          description: 'test'
206
      expect(response).to have_gitlab_http_status(200)
207 208
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq('#FFFFFF')
209
      expect(json_response['description']).to eq('test')
Robert Schilling committed
210 211
    end

212
    it 'returns 200 if name is changed' do
Robert Schilling committed
213 214 215
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label'
216
      expect(response).to have_gitlab_http_status(200)
217 218
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq(label1.color)
Robert Schilling committed
219 220
    end

221
    it 'returns 200 if colors is changed' do
Robert Schilling committed
222 223 224
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FFFFFF'
225
      expect(response).to have_gitlab_http_status(200)
226 227
      expect(json_response['name']).to eq(label1.name)
      expect(json_response['color']).to eq('#FFFFFF')
Robert Schilling committed
228 229
    end

230
    it 'returns 200 if description is changed' do
231
      put api("/projects/#{project.id}/labels", user),
232
          name: 'bug',
233
          description: 'test'
234

235
      expect(response).to have_gitlab_http_status(200)
236
      expect(json_response['name']).to eq(priority_label.name)
237
      expect(json_response['description']).to eq('test')
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
      expect(json_response['priority']).to eq(3)
    end

    it 'returns 200 if priority is changed' do
      put api("/projects/#{project.id}/labels", user),
           name: 'bug',
           priority: 10

      expect(response.status).to eq(200)
      expect(json_response['name']).to eq(priority_label.name)
      expect(json_response['priority']).to eq(10)
    end

    it 'returns 200 if a priority is added' do
      put api("/projects/#{project.id}/labels", user),
           name: 'label1',
           priority: 3

      expect(response.status).to eq(200)
      expect(json_response['name']).to eq(label1.name)
      expect(json_response['priority']).to eq(3)
    end

    it 'returns 200 if the priority is removed' do
      put api("/projects/#{project.id}/labels", user),
          name: priority_label.name,
          priority: nil

      expect(response.status).to eq(200)
      expect(json_response['name']).to eq(priority_label.name)
      expect(json_response['priority']).to be_nil
269 270
    end

271
    it 'returns 404 if label does not exist' do
Robert Schilling committed
272 273 274
      put api("/projects/#{project.id}/labels", user),
          name: 'label2',
          new_name: 'label3'
275
      expect(response).to have_gitlab_http_status(404)
Robert Schilling committed
276 277
    end

278
    it 'returns 400 if no label name given' do
Robert Schilling committed
279
      put api("/projects/#{project.id}/labels", user), new_name: 'label2'
280
      expect(response).to have_gitlab_http_status(400)
281
      expect(json_response['error']).to eq('name is missing')
Robert Schilling committed
282 283
    end

284
    it 'returns 400 if no new parameters given' do
Robert Schilling committed
285
      put api("/projects/#{project.id}/labels", user), name: 'label1'
286
      expect(response).to have_gitlab_http_status(400)
287
      expect(json_response['error']).to eq('new_name, color, description, priority are missing, '\
288
                                           'at least one parameter must be provided')
Robert Schilling committed
289 290
    end

291
    it 'returns 400 for invalid name' do
Robert Schilling committed
292 293
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
294
          new_name: ',',
Robert Schilling committed
295
          color: '#FFFFFF'
296
      expect(response).to have_gitlab_http_status(400)
297
      expect(json_response['message']['title']).to eq(['is invalid'])
Robert Schilling committed
298 299
    end

300
    it 'returns 400 when color code is too short' do
Robert Schilling committed
301 302 303
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FF'
304
      expect(response).to have_gitlab_http_status(400)
305
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
Robert Schilling committed
306
    end
307

308
    it 'returns 400 for too long color code' do
309 310 311
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
312
      expect(response).to have_gitlab_http_status(400)
313
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
314
    end
315 316 317 318 319 320

    it 'returns 400 for invalid priority' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           priority: 'foo'

321
      expect(response).to have_gitlab_http_status(400)
322
    end
Robert Schilling committed
323
  end
324

325
  describe "POST /projects/:id/labels/:label_id/subscribe" do
326
    context "when label_id is a label title" do
327
      it "subscribes to the label" do
328
        post api("/projects/#{project.id}/labels/#{label1.title}/subscribe", user)
329

330
        expect(response).to have_gitlab_http_status(201)
331 332 333 334 335 336
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_truthy
      end
    end

    context "when label_id is a label ID" do
337
      it "subscribes to the label" do
338
        post api("/projects/#{project.id}/labels/#{label1.id}/subscribe", user)
339

340
        expect(response).to have_gitlab_http_status(201)
341 342 343 344 345 346
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_truthy
      end
    end

    context "when user is already subscribed to label" do
347 348 349
      before do
        label1.subscribe(user, project)
      end
350

351
      it "returns 304" do
352
        post api("/projects/#{project.id}/labels/#{label1.id}/subscribe", user)
353

354
        expect(response).to have_gitlab_http_status(304)
355 356 357 358
      end
    end

    context "when label ID is not found" do
359
      it "returns 404 error" do
360
        post api("/projects/#{project.id}/labels/1234/subscribe", user)
361

362
        expect(response).to have_gitlab_http_status(404)
363 364 365 366
      end
    end
  end

367
  describe "POST /projects/:id/labels/:label_id/unsubscribe" do
368 369 370
    before do
      label1.subscribe(user, project)
    end
371 372

    context "when label_id is a label title" do
373
      it "unsubscribes from the label" do
374
        post api("/projects/#{project.id}/labels/#{label1.title}/unsubscribe", user)
375

376
        expect(response).to have_gitlab_http_status(201)
377 378 379 380 381 382
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_falsey
      end
    end

    context "when label_id is a label ID" do
383
      it "unsubscribes from the label" do
384
        post api("/projects/#{project.id}/labels/#{label1.id}/unsubscribe", user)
385

386
        expect(response).to have_gitlab_http_status(201)
387 388 389 390 391 392
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_falsey
      end
    end

    context "when user is already unsubscribed from label" do
393 394 395
      before do
        label1.unsubscribe(user, project)
      end
396

397
      it "returns 304" do
398
        post api("/projects/#{project.id}/labels/#{label1.id}/unsubscribe", user)
399

400
        expect(response).to have_gitlab_http_status(304)
401 402 403 404
      end
    end

    context "when label ID is not found" do
405
      it "returns 404 error" do
406
        post api("/projects/#{project.id}/labels/1234/unsubscribe", user)
407

408
        expect(response).to have_gitlab_http_status(404)
409 410 411
      end
    end
  end
Dmitriy Zaporozhets committed
412
end