BigW Consortium Gitlab

labels_spec.rb 12.7 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers

  let(:user) { create(:user) }
  let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
  let!(:label1) { create(:label, title: 'label1', project: project) }
9
  let!(:priority_label) { create(:label, title: 'bug', project: project, priority: 3) }
Dmitriy Zaporozhets committed
10 11 12 13 14 15

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

  describe 'GET /projects/:id/labels' do
16 17
    it 'returns all available labels to the project' do
      group = create(:group)
18
      group_label = create(:group_label, title: 'feature', group: group)
19
      project.update(group: group)
20 21 22 23 24
      expected_keys = [
        'id', 'name', 'color', 'description',
        'open_issues_count', 'closed_issues_count', 'open_merge_requests_count',
        'subscribed', 'priority'
      ]
25

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

28
      expect(response).to have_http_status(200)
29
      expect(json_response).to be_an Array
30
      expect(json_response.size).to eq(3)
31
      expect(json_response.first.keys).to match_array expected_keys
32 33 34 35 36 37 38 39 40
      expect(json_response.map { |l| l['name'] }).to match_array([group_label.name, priority_label.name, label1.name])
      expect(json_response.last['name']).to eq(label1.name)
      expect(json_response.last['color']).to be_present
      expect(json_response.last['description']).to be_nil
      expect(json_response.last['open_issues_count']).to eq(0)
      expect(json_response.last['closed_issues_count']).to eq(0)
      expect(json_response.last['open_merge_requests_count']).to eq(0)
      expect(json_response.last['priority']).to be_nil
      expect(json_response.last['subscribed']).to be_falsey
Dmitriy Zaporozhets committed
41 42
    end
  end
43 44

  describe 'POST /projects/:id/labels' do
45
    it 'returns created label when all params' do
46 47 48
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAABB',
49 50 51
           description: 'test',
           priority: 2

52
      expect(response).to have_http_status(201)
53 54 55
      expect(json_response['name']).to eq('Foo')
      expect(json_response['color']).to eq('#FFAABB')
      expect(json_response['description']).to eq('test')
56
      expect(json_response['priority']).to eq(2)
57 58
    end

59
    it 'returns created label when only required params' do
60
      post api("/projects/#{project.id}/labels", user),
61
           name: 'Foo & Bar',
62
           color: '#FFAABB'
63

64 65
      expect(response.status).to eq(201)
      expect(json_response['name']).to eq('Foo & Bar')
66
      expect(json_response['color']).to eq('#FFAABB')
67
      expect(json_response['description']).to be_nil
68 69 70 71 72 73 74 75 76 77 78 79 80 81
      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)
82 83
    end

84
    it 'returns a 400 bad request if name not given' do
85
      post api("/projects/#{project.id}/labels", user), color: '#FFAABB'
86
      expect(response).to have_http_status(400)
87 88
    end

89
    it 'returns a 400 bad request if color not given' do
90
      post api("/projects/#{project.id}/labels", user), name: 'Foobar'
91
      expect(response).to have_http_status(400)
92 93
    end

94
    it 'returns 400 for invalid color' do
95 96 97
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAA'
98
      expect(response).to have_http_status(400)
99
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
100 101
    end

102
    it 'returns 400 for too long color code' do
103 104 105
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
106
      expect(response).to have_http_status(400)
107
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
108 109
    end

110
    it 'returns 400 for invalid name' do
111
      post api("/projects/#{project.id}/labels", user),
112
           name: ',',
113
           color: '#FFAABB'
114
      expect(response).to have_http_status(400)
115
      expect(json_response['message']['title']).to eq(['is invalid'])
116 117
    end

118 119 120 121 122 123 124 125 126 127 128 129 130
    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'

      expect(response).to have_http_status(409)
      expect(json_response['message']).to eq('Label already exists')
    end

131 132 133 134 135 136 137 138 139
    it 'returns 400 for invalid priority' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF',
           priority: 'foo'

      expect(response).to have_http_status(400)
    end

140
    it 'returns 409 if label already exists in project' do
141 142 143
      post api("/projects/#{project.id}/labels", user),
           name: 'label1',
           color: '#FFAABB'
144
      expect(response).to have_http_status(409)
145
      expect(json_response['message']).to eq('Label already exists')
146 147 148 149
    end
  end

  describe 'DELETE /projects/:id/labels' do
150
    it 'returns 200 for existing label' do
Robert Schilling committed
151
      delete api("/projects/#{project.id}/labels", user), name: 'label1'
152
      expect(response).to have_http_status(200)
153 154
    end

155
    it 'returns 404 for non existing label' do
Robert Schilling committed
156
      delete api("/projects/#{project.id}/labels", user), name: 'label2'
157
      expect(response).to have_http_status(404)
158
      expect(json_response['message']).to eq('404 Label Not Found')
159 160
    end

161
    it 'returns 400 for wrong parameters' do
162
      delete api("/projects/#{project.id}/labels", user)
163
      expect(response).to have_http_status(400)
164 165
    end
  end
Robert Schilling committed
166 167

  describe 'PUT /projects/:id/labels' do
168
    it 'returns 200 if name and colors and description are changed' do
Robert Schilling committed
169 170 171
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label',
172 173
          color: '#FFFFFF',
          description: 'test'
174
      expect(response).to have_http_status(200)
175 176
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq('#FFFFFF')
177
      expect(json_response['description']).to eq('test')
Robert Schilling committed
178 179
    end

180
    it 'returns 200 if name is changed' do
Robert Schilling committed
181 182 183
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label'
184
      expect(response).to have_http_status(200)
185 186
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq(label1.color)
Robert Schilling committed
187 188
    end

189
    it 'returns 200 if colors is changed' do
Robert Schilling committed
190 191 192
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FFFFFF'
193
      expect(response).to have_http_status(200)
194 195
      expect(json_response['name']).to eq(label1.name)
      expect(json_response['color']).to eq('#FFFFFF')
Robert Schilling committed
196 197
    end

198
    it 'returns 200 if description is changed' do
199
      put api("/projects/#{project.id}/labels", user),
200
          name: 'bug',
201
          description: 'test'
202

203
      expect(response).to have_http_status(200)
204
      expect(json_response['name']).to eq(priority_label.name)
205
      expect(json_response['description']).to eq('test')
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
      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
237 238
    end

239
    it 'returns 404 if label does not exist' do
Robert Schilling committed
240 241 242
      put api("/projects/#{project.id}/labels", user),
          name: 'label2',
          new_name: 'label3'
243
      expect(response).to have_http_status(404)
Robert Schilling committed
244 245
    end

246
    it 'returns 400 if no label name given' do
Robert Schilling committed
247
      put api("/projects/#{project.id}/labels", user), new_name: 'label2'
248
      expect(response).to have_http_status(400)
249
      expect(json_response['error']).to eq('name is missing')
Robert Schilling committed
250 251
    end

252
    it 'returns 400 if no new parameters given' do
Robert Schilling committed
253
      put api("/projects/#{project.id}/labels", user), name: 'label1'
254
      expect(response).to have_http_status(400)
255
      expect(json_response['error']).to eq('new_name, color, description, priority are missing, '\
256
                                           'at least one parameter must be provided')
Robert Schilling committed
257 258
    end

259
    it 'returns 400 for invalid name' do
Robert Schilling committed
260 261
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
262
          new_name: ',',
Robert Schilling committed
263
          color: '#FFFFFF'
264
      expect(response).to have_http_status(400)
265
      expect(json_response['message']['title']).to eq(['is invalid'])
Robert Schilling committed
266 267
    end

268
    it 'returns 400 when color code is too short' do
Robert Schilling committed
269 270 271
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FF'
272
      expect(response).to have_http_status(400)
273
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
Robert Schilling committed
274
    end
275

276
    it 'returns 400 for too long color code' do
277 278 279
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
280
      expect(response).to have_http_status(400)
281
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
282
    end
283 284 285 286 287 288 289 290

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

      expect(response).to have_http_status(400)
    end
Robert Schilling committed
291
  end
292 293 294

  describe "POST /projects/:id/labels/:label_id/subscription" do
    context "when label_id is a label title" do
295
      it "subscribes to the label" do
296 297
        post api("/projects/#{project.id}/labels/#{label1.title}/subscription", user)

298
        expect(response).to have_http_status(201)
299 300 301 302 303 304
        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
305
      it "subscribes to the label" do
306 307
        post api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

308
        expect(response).to have_http_status(201)
309 310 311 312 313 314 315 316
        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
      before { label1.subscribe(user) }

317
      it "returns 304" do
318 319
        post api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

320
        expect(response).to have_http_status(304)
321 322 323 324
      end
    end

    context "when label ID is not found" do
325
      it "returns 404 error" do
326 327
        post api("/projects/#{project.id}/labels/1234/subscription", user)

328
        expect(response).to have_http_status(404)
329 330 331 332 333 334 335 336
      end
    end
  end

  describe "DELETE /projects/:id/labels/:label_id/subscription" do
    before { label1.subscribe(user) }

    context "when label_id is a label title" do
337
      it "unsubscribes from the label" do
338 339
        delete api("/projects/#{project.id}/labels/#{label1.title}/subscription", user)

340
        expect(response).to have_http_status(200)
341 342 343 344 345 346
        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
347
      it "unsubscribes from the label" do
348 349
        delete api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

350
        expect(response).to have_http_status(200)
351 352 353 354 355 356 357 358
        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
      before { label1.unsubscribe(user) }

359
      it "returns 304" do
360 361
        delete api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

362
        expect(response).to have_http_status(304)
363 364 365 366
      end
    end

    context "when label ID is not found" do
367
      it "returns 404 error" do
368 369
        delete api("/projects/#{project.id}/labels/1234/subscription", user)

370
        expect(response).to have_http_status(404)
371 372 373
      end
    end
  end
Dmitriy Zaporozhets committed
374
end