issues_spec.rb 17.8 KB
Newer Older
N
Nihad Abbasov 已提交
1 2
require 'spec_helper'

J
Jeroen van Baarsen 已提交
3
describe API::API, api: true  do
4
  include ApiHelpers
5
  let(:user) { create(:user) }
6 7 8 9 10
  let(:non_member) { create(:user) }
  let(:author) { create(:author) }
  let(:assignee) { create(:assignee) }
  let(:admin) { create(:admin) }
  let!(:project) { create(:project, :public, namespace: user.namespace ) }
J
jubianchi 已提交
11 12 13 14 15 16 17 18
  let!(:closed_issue) do
    create :closed_issue,
           author: user,
           assignee: user,
           project: project,
           state: :closed,
           milestone: milestone
  end
19 20 21 22 23 24 25
  let!(:confidential_issue) do
    create :issue,
           :confidential,
           project: project,
           author: author,
           assignee: assignee
  end
J
jubianchi 已提交
26 27 28 29 30 31 32
  let!(:issue) do
    create :issue,
           author: user,
           assignee: user,
           project: project,
           milestone: milestone
  end
33 34 35
  let!(:label) do
    create(:label, title: 'label', color: '#FFAABB', project: project)
  end
J
jubianchi 已提交
36
  let!(:label_link) { create(:label_link, label: label, target: issue) }
J
jubianchi 已提交
37 38 39 40
  let!(:milestone) { create(:milestone, title: '1.0.0', project: project) }
  let!(:empty_milestone) do
    create(:milestone, title: '2.0.0', project: project)
  end
41

D
Dmitriy Zaporozhets 已提交
42
  before { project.team << [user, :reporter] }
N
Nihad Abbasov 已提交
43 44

  describe "GET /issues" do
45 46 47
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/issues")
48
        expect(response.status).to eq(401)
49
      end
N
Nihad Abbasov 已提交
50 51
    end

52
    context "when authenticated" do
N
Nihad Abbasov 已提交
53
      it "should return an array of issues" do
R
Robert Speicher 已提交
54
        get api("/issues", user)
55 56 57
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['title']).to eq(issue.title)
N
Nihad Abbasov 已提交
58
      end
N
Nihad Abbasov 已提交
59

60 61
      it "should add pagination headers and keep query params" do
        get api("/issues?state=closed&per_page=3", user)
62
        expect(response.headers['Link']).to eq(
63
          '<http://www.example.com/api/v3/issues?page=1&per_page=3&private_token=%s&state=closed>; rel="first", <http://www.example.com/api/v3/issues?page=1&per_page=3&private_token=%s&state=closed>; rel="last"' % [user.private_token, user.private_token]
64
        )
N
Nihad Abbasov 已提交
65
      end
J
jubianchi 已提交
66 67 68

      it 'should return an array of closed issues' do
        get api('/issues?state=closed', user)
69 70 71 72
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(closed_issue.id)
J
jubianchi 已提交
73 74 75 76
      end

      it 'should return an array of opened issues' do
        get api('/issues?state=opened', user)
77 78 79 80
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(issue.id)
J
jubianchi 已提交
81 82 83 84
      end

      it 'should return an array of all issues' do
        get api('/issues?state=all', user)
85 86 87 88 89
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(2)
        expect(json_response.first['id']).to eq(issue.id)
        expect(json_response.second['id']).to eq(closed_issue.id)
J
jubianchi 已提交
90
      end
J
jubianchi 已提交
91 92 93

      it 'should return an array of labeled issues' do
        get api("/issues?labels=#{label.title}", user)
94 95 96 97
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['labels']).to eq([label.title])
J
jubianchi 已提交
98 99 100 101
      end

      it 'should return an array of labeled issues when at least one label matches' do
        get api("/issues?labels=#{label.title},foo,bar", user)
102 103 104 105
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['labels']).to eq([label.title])
J
jubianchi 已提交
106 107 108 109
      end

      it 'should return an empty array if no issue matches labels' do
        get api('/issues?labels=foo,bar', user)
110 111 112
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
J
jubianchi 已提交
113 114 115 116
      end

      it 'should return an array of labeled issues matching given state' do
        get api("/issues?labels=#{label.title}&state=opened", user)
117 118 119 120 121
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['labels']).to eq([label.title])
        expect(json_response.first['state']).to eq('opened')
J
jubianchi 已提交
122 123 124 125
      end

      it 'should return an empty array if no issue matches labels and state filters' do
        get api("/issues?labels=#{label.title}&state=closed", user)
126 127 128
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
J
jubianchi 已提交
129
      end
N
Nihad Abbasov 已提交
130 131 132 133
    end
  end

  describe "GET /projects/:id/issues" do
J
jubianchi 已提交
134 135 136
    let(:base_url) { "/projects/#{project.id}" }
    let(:title) { milestone.title }

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
    it 'should return project issues without confidential issues for non project members' do
      get api("#{base_url}/issues", non_member)
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(2)
      expect(json_response.first['title']).to eq(issue.title)
    end

    it 'should return project confidential issues for author' do
      get api("#{base_url}/issues", author)
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(3)
      expect(json_response.first['title']).to eq(issue.title)
    end

    it 'should return project confidential issues for assignee' do
      get api("#{base_url}/issues", assignee)
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(3)
      expect(json_response.first['title']).to eq(issue.title)
    end

    it 'should return project issues with confidential issues for project members' do
J
jubianchi 已提交
162
      get api("#{base_url}/issues", user)
163 164
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
165 166 167 168 169 170 171 172 173
      expect(json_response.length).to eq(3)
      expect(json_response.first['title']).to eq(issue.title)
    end

    it 'should return project confidential issues for admin' do
      get api("#{base_url}/issues", admin)
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(3)
174
      expect(json_response.first['title']).to eq(issue.title)
N
Nihad Abbasov 已提交
175
    end
J
jubianchi 已提交
176 177

    it 'should return an array of labeled project issues' do
J
jubianchi 已提交
178
      get api("#{base_url}/issues?labels=#{label.title}", user)
179 180 181 182
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['labels']).to eq([label.title])
J
jubianchi 已提交
183 184 185
    end

    it 'should return an array of labeled project issues when at least one label matches' do
J
jubianchi 已提交
186
      get api("#{base_url}/issues?labels=#{label.title},foo,bar", user)
187 188 189 190
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['labels']).to eq([label.title])
J
jubianchi 已提交
191 192 193
    end

    it 'should return an empty array if no project issue matches labels' do
J
jubianchi 已提交
194
      get api("#{base_url}/issues?labels=foo,bar", user)
195 196 197
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
J
jubianchi 已提交
198 199 200 201
    end

    it 'should return an empty array if no issue matches milestone' do
      get api("#{base_url}/issues?milestone=#{empty_milestone.title}", user)
202 203 204
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
J
jubianchi 已提交
205
    end
J
jubianchi 已提交
206 207 208

    it 'should return an empty array if milestone does not exist' do
      get api("#{base_url}/issues?milestone=foo", user)
209 210 211
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
J
jubianchi 已提交
212 213 214 215
    end

    it 'should return an array of issues in given milestone' do
      get api("#{base_url}/issues?milestone=#{title}", user)
216 217 218 219 220
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(2)
      expect(json_response.first['id']).to eq(issue.id)
      expect(json_response.second['id']).to eq(closed_issue.id)
J
jubianchi 已提交
221 222 223 224 225
    end

    it 'should return an array of issues matching state in milestone' do
      get api("#{base_url}/issues?milestone=#{milestone.title}"\
              '&state=closed', user)
226 227 228 229
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['id']).to eq(closed_issue.id)
J
jubianchi 已提交
230
    end
N
Nihad Abbasov 已提交
231 232 233 234
  end

  describe "GET /projects/:id/issues/:issue_id" do
    it "should return a project issue by id" do
235
      get api("/projects/#{project.id}/issues/#{issue.id}", user)
236 237 238
      expect(response.status).to eq(200)
      expect(json_response['title']).to eq(issue.title)
      expect(json_response['iid']).to eq(issue.iid)
N
Nihad Abbasov 已提交
239
    end
240

241 242
    it 'should return a project issue by iid' do
      get api("/projects/#{project.id}/issues?iid=#{issue.iid}", user)
243 244 245 246
      expect(response.status).to eq 200
      expect(json_response.first['title']).to eq issue.title
      expect(json_response.first['id']).to eq issue.id
      expect(json_response.first['iid']).to eq issue.iid
247 248
    end

249 250
    it "should return 404 if issue id not found" do
      get api("/projects/#{project.id}/issues/54321", user)
251
      expect(response.status).to eq(404)
252
    end
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

    context 'confidential issues' do
      it "should return 404 for non project members" do
        get api("/projects/#{project.id}/issues/#{confidential_issue.id}", non_member)
        expect(response.status).to eq(404)
      end

      it "should return confidential issue for project members" do
        get api("/projects/#{project.id}/issues/#{confidential_issue.id}", user)
        expect(response.status).to eq(200)
        expect(json_response['title']).to eq(confidential_issue.title)
        expect(json_response['iid']).to eq(confidential_issue.iid)
      end

      it "should return confidential issue for author" do
        get api("/projects/#{project.id}/issues/#{confidential_issue.id}", author)
        expect(response.status).to eq(200)
        expect(json_response['title']).to eq(confidential_issue.title)
        expect(json_response['iid']).to eq(confidential_issue.iid)
      end

      it "should return confidential issue for assignee" do
        get api("/projects/#{project.id}/issues/#{confidential_issue.id}", assignee)
        expect(response.status).to eq(200)
        expect(json_response['title']).to eq(confidential_issue.title)
        expect(json_response['iid']).to eq(confidential_issue.iid)
      end

      it "should return confidential issue for admin" do
        get api("/projects/#{project.id}/issues/#{confidential_issue.id}", admin)
        expect(response.status).to eq(200)
        expect(json_response['title']).to eq(confidential_issue.title)
        expect(json_response['iid']).to eq(confidential_issue.iid)
      end
    end
N
Nihad Abbasov 已提交
288 289 290 291
  end

  describe "POST /projects/:id/issues" do
    it "should create a new project issue" do
292
      post api("/projects/#{project.id}/issues", user),
293
        title: 'new issue', labels: 'label, label2'
294 295 296 297
      expect(response.status).to eq(201)
      expect(json_response['title']).to eq('new issue')
      expect(json_response['description']).to be_nil
      expect(json_response['labels']).to eq(['label', 'label2'])
N
Nihad Abbasov 已提交
298
    end
299 300 301

    it "should return a 400 bad request if title not given" do
      post api("/projects/#{project.id}/issues", user), labels: 'label, label2'
302
      expect(response.status).to eq(400)
303
    end
304

305
    it 'should return 400 on invalid label names' do
306 307 308
      post api("/projects/#{project.id}/issues", user),
           title: 'new issue',
           labels: 'label, ?'
309 310
      expect(response.status).to eq(400)
      expect(json_response['message']['labels']['?']['title']).to eq(['is invalid'])
311
    end
J
jubianchi 已提交
312 313 314 315

    it 'should return 400 if title is too long' do
      post api("/projects/#{project.id}/issues", user),
           title: 'g' * 256
316 317
      expect(response.status).to eq(400)
      expect(json_response['message']['title']).to eq([
J
jubianchi 已提交
318
        'is too long (maximum is 255 characters)'
319
      ])
J
jubianchi 已提交
320
    end
N
Nihad Abbasov 已提交
321 322
  end

323 324 325 326 327 328 329 330
  describe 'POST /projects/:id/issues with spam filtering' do
    before do
      Grape::Endpoint.before_each do |endpoint|
        allow(endpoint).to receive(:check_for_spam?).and_return(true)
        allow(endpoint).to receive(:is_spam?).and_return(true)
      end
    end

D
Douglas Barbosa Alexandre 已提交
331 332 333 334 335 336 337
    let(:params) do
      {
        title: 'new issue',
        description: 'content here',
        labels: 'label, label2'
      }
    end
338

D
Douglas Barbosa Alexandre 已提交
339 340
    it "should not create a new project issue" do
      expect { post api("/projects/#{project.id}/issues", user), params }.not_to change(Issue, :count)
341 342
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq({ "error" => "Spam detected" })
343

344 345 346
      spam_logs = SpamLog.all
      expect(spam_logs.count).to eq(1)
      expect(spam_logs[0].title).to eq('new issue')
347
      expect(spam_logs[0].description).to eq('content here')
348 349 350 351 352 353
      expect(spam_logs[0].user).to eq(user)
      expect(spam_logs[0].noteable_type).to eq('Issue')
      expect(spam_logs[0].project_id).to eq(project.id)
    end
  end

A
Andrew8xx8 已提交
354
  describe "PUT /projects/:id/issues/:issue_id to update only title" do
N
Nihad Abbasov 已提交
355
    it "should update a project issue" do
356
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
A
Andrew8xx8 已提交
357
        title: 'updated title'
358
      expect(response.status).to eq(200)
A
Andrew8xx8 已提交
359

360
      expect(json_response['title']).to eq('updated title')
A
Andrew8xx8 已提交
361
    end
362 363 364 365

    it "should return 404 error if issue id not found" do
      put api("/projects/#{project.id}/issues/44444", user),
        title: 'updated title'
366
      expect(response.status).to eq(404)
367
    end
368

369
    it 'should return 400 on invalid label names' do
370 371 372
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          title: 'updated title',
          labels: 'label, ?'
373 374
      expect(response.status).to eq(400)
      expect(json_response['message']['labels']['?']['title']).to eq(['is invalid'])
375
    end
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

    context 'confidential issues' do
      it "should return 403 for non project members" do
        put api("/projects/#{project.id}/issues/#{confidential_issue.id}", non_member),
          title: 'updated title'
        expect(response.status).to eq(403)
      end

      it "should update a confidential issue for project members" do
        put api("/projects/#{project.id}/issues/#{confidential_issue.id}", user),
          title: 'updated title'
        expect(response.status).to eq(200)
        expect(json_response['title']).to eq('updated title')
      end

      it "should update a confidential issue for author" do
        put api("/projects/#{project.id}/issues/#{confidential_issue.id}", author),
          title: 'updated title'
        expect(response.status).to eq(200)
        expect(json_response['title']).to eq('updated title')
      end

      it "should update a confidential issue for admin" do
        put api("/projects/#{project.id}/issues/#{confidential_issue.id}", admin),
          title: 'updated title'
        expect(response.status).to eq(200)
        expect(json_response['title']).to eq('updated title')
      end
    end
405 406 407 408 409 410 411 412 413
  end

  describe 'PUT /projects/:id/issues/:issue_id to update labels' do
    let!(:label) { create(:label, title: 'dummy', project: project) }
    let!(:label_link) { create(:label_link, label: label, target: issue) }

    it 'should not update labels if not present' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          title: 'updated title'
414 415
      expect(response.status).to eq(200)
      expect(json_response['labels']).to eq([label.title])
416 417 418 419 420
    end

    it 'should remove all labels' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: ''
421 422
      expect(response.status).to eq(200)
      expect(json_response['labels']).to eq([])
423 424 425 426 427
    end

    it 'should update labels' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: 'foo,bar'
428 429 430
      expect(response.status).to eq(200)
      expect(json_response['labels']).to include 'foo'
      expect(json_response['labels']).to include 'bar'
431 432 433 434 435
    end

    it 'should return 400 on invalid label names' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: 'label, ?'
436 437
      expect(response.status).to eq(400)
      expect(json_response['message']['labels']['?']['title']).to eq(['is invalid'])
438 439 440 441 442
    end

    it 'should allow special label names' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: 'label:foo, label-bar,label_bar,label/bar'
443 444 445 446 447
      expect(response.status).to eq(200)
      expect(json_response['labels']).to include 'label:foo'
      expect(json_response['labels']).to include 'label-bar'
      expect(json_response['labels']).to include 'label_bar'
      expect(json_response['labels']).to include 'label/bar'
448
    end
J
jubianchi 已提交
449 450 451 452

    it 'should return 400 if title is too long' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          title: 'g' * 256
453 454
      expect(response.status).to eq(400)
      expect(json_response['message']['title']).to eq([
J
jubianchi 已提交
455
        'is too long (maximum is 255 characters)'
456
      ])
J
jubianchi 已提交
457
    end
A
Andrew8xx8 已提交
458 459 460 461 462 463
  end

  describe "PUT /projects/:id/issues/:issue_id to update state and label" do
    it "should update a project issue" do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
        labels: 'label2', state_event: "close"
464
      expect(response.status).to eq(200)
A
Andrew8xx8 已提交
465

466 467
      expect(json_response['labels']).to include 'label2'
      expect(json_response['state']).to eq "closed"
N
Nihad Abbasov 已提交
468 469 470 471 472
    end
  end

  describe "DELETE /projects/:id/issues/:issue_id" do
    it "should delete a project issue" do
473
      delete api("/projects/#{project.id}/issues/#{issue.id}", user)
474
      expect(response.status).to eq(405)
N
Nihad Abbasov 已提交
475 476 477
    end
  end
end