issues_spec.rb 13.1 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
  let!(:project) { create(:project, namespace: user.namespace ) }
J
jubianchi 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  let!(:closed_issue) do
    create :closed_issue,
           author: user,
           assignee: user,
           project: project,
           state: :closed,
           milestone: milestone
  end
  let!(:issue) do
    create :issue,
           author: user,
           assignee: user,
           project: project,
           milestone: milestone
  end
22 23 24
  let!(:label) do
    create(:label, title: 'label', color: '#FFAABB', project: project)
  end
J
jubianchi 已提交
25
  let!(:label_link) { create(:label_link, label: label, target: issue) }
J
jubianchi 已提交
26 27 28 29
  let!(:milestone) { create(:milestone, title: '1.0.0', project: project) }
  let!(:empty_milestone) do
    create(:milestone, title: '2.0.0', project: project)
  end
30

D
Dmitriy Zaporozhets 已提交
31
  before { project.team << [user, :reporter] }
N
Nihad Abbasov 已提交
32 33

  describe "GET /issues" do
34 35 36
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/issues")
37
        expect(response.status).to eq(401)
38
      end
N
Nihad Abbasov 已提交
39 40
    end

41
    context "when authenticated" do
N
Nihad Abbasov 已提交
42
      it "should return an array of issues" do
R
Robert Speicher 已提交
43
        get api("/issues", user)
44 45 46
        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 已提交
47
      end
N
Nihad Abbasov 已提交
48 49 50

      it "should add pagination headers" do
        get api("/issues?per_page=3", user)
51
        expect(response.headers['Link']).to eq(
N
Nihad Abbasov 已提交
52
          '<http://www.example.com/api/v3/issues?page=1&per_page=3>; rel="first", <http://www.example.com/api/v3/issues?page=1&per_page=3>; rel="last"'
53
        )
N
Nihad Abbasov 已提交
54
      end
J
jubianchi 已提交
55 56 57

      it 'should return an array of closed issues' do
        get api('/issues?state=closed', user)
58 59 60 61
        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 已提交
62 63 64 65
      end

      it 'should return an array of opened issues' do
        get api('/issues?state=opened', user)
66 67 68 69
        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 已提交
70 71 72 73
      end

      it 'should return an array of all issues' do
        get api('/issues?state=all', user)
74 75 76 77 78
        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 已提交
79
      end
J
jubianchi 已提交
80 81 82

      it 'should return an array of labeled issues' do
        get api("/issues?labels=#{label.title}", user)
83 84 85 86
        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 已提交
87 88 89 90
      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)
91 92 93 94
        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 已提交
95 96 97 98
      end

      it 'should return an empty array if no issue matches labels' do
        get api('/issues?labels=foo,bar', user)
99 100 101
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
J
jubianchi 已提交
102 103 104 105
      end

      it 'should return an array of labeled issues matching given state' do
        get api("/issues?labels=#{label.title}&state=opened", user)
106 107 108 109 110
        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 已提交
111 112 113 114
      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)
115 116 117
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
J
jubianchi 已提交
118
      end
N
Nihad Abbasov 已提交
119 120 121 122
    end
  end

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

N
Nihad Abbasov 已提交
126
    it "should return project issues" do
J
jubianchi 已提交
127
      get api("#{base_url}/issues", user)
128 129 130
      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 已提交
131
    end
J
jubianchi 已提交
132 133

    it 'should return an array of labeled project issues' do
J
jubianchi 已提交
134
      get api("#{base_url}/issues?labels=#{label.title}", user)
135 136 137 138
      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 已提交
139 140 141
    end

    it 'should return an array of labeled project issues when at least one label matches' do
J
jubianchi 已提交
142
      get api("#{base_url}/issues?labels=#{label.title},foo,bar", user)
143 144 145 146
      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 已提交
147 148 149
    end

    it 'should return an empty array if no project issue matches labels' do
J
jubianchi 已提交
150
      get api("#{base_url}/issues?labels=foo,bar", user)
151 152 153
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
J
jubianchi 已提交
154 155 156 157
    end

    it 'should return an empty array if no issue matches milestone' do
      get api("#{base_url}/issues?milestone=#{empty_milestone.title}", user)
158 159 160
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
J
jubianchi 已提交
161
    end
J
jubianchi 已提交
162 163 164

    it 'should return an empty array if milestone does not exist' do
      get api("#{base_url}/issues?milestone=foo", user)
165 166 167
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
J
jubianchi 已提交
168 169 170 171
    end

    it 'should return an array of issues in given milestone' do
      get api("#{base_url}/issues?milestone=#{title}", user)
172 173 174 175 176
      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 已提交
177 178 179 180 181
    end

    it 'should return an array of issues matching state in milestone' do
      get api("#{base_url}/issues?milestone=#{milestone.title}"\
              '&state=closed', user)
182 183 184 185
      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 已提交
186
    end
N
Nihad Abbasov 已提交
187 188 189 190
  end

  describe "GET /projects/:id/issues/:issue_id" do
    it "should return a project issue by id" do
191
      get api("/projects/#{project.id}/issues/#{issue.id}", user)
192 193 194
      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 已提交
195
    end
196

197 198
    it 'should return a project issue by iid' do
      get api("/projects/#{project.id}/issues?iid=#{issue.iid}", user)
199 200 201 202
      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
203 204
    end

205 206
    it "should return 404 if issue id not found" do
      get api("/projects/#{project.id}/issues/54321", user)
207
      expect(response.status).to eq(404)
208
    end
N
Nihad Abbasov 已提交
209 210 211 212
  end

  describe "POST /projects/:id/issues" do
    it "should create a new project issue" do
213
      post api("/projects/#{project.id}/issues", user),
214
        title: 'new issue', labels: 'label, label2'
215 216 217 218
      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 已提交
219
    end
220 221 222

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

226
    it 'should return 400 on invalid label names' do
227 228 229
      post api("/projects/#{project.id}/issues", user),
           title: 'new issue',
           labels: 'label, ?'
230 231
      expect(response.status).to eq(400)
      expect(json_response['message']['labels']['?']['title']).to eq(['is invalid'])
232
    end
J
jubianchi 已提交
233 234 235 236

    it 'should return 400 if title is too long' do
      post api("/projects/#{project.id}/issues", user),
           title: 'g' * 256
237 238
      expect(response.status).to eq(400)
      expect(json_response['message']['title']).to eq([
J
jubianchi 已提交
239
        'is too long (maximum is 255 characters)'
240
      ])
J
jubianchi 已提交
241
    end
N
Nihad Abbasov 已提交
242 243
  end

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  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

    it "should create a new project issue" do
      post api("/projects/#{project.id}/issues", user),
           title: 'new issue', labels: 'label, label2'
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq({ "error" => "Spam detected" })
      spam_logs = SpamLog.all
      expect(spam_logs.count).to eq(1)
      expect(spam_logs[0].title).to eq('new issue')
      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 已提交
266
  describe "PUT /projects/:id/issues/:issue_id to update only title" do
N
Nihad Abbasov 已提交
267
    it "should update a project issue" do
268
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
A
Andrew8xx8 已提交
269
        title: 'updated title'
270
      expect(response.status).to eq(200)
A
Andrew8xx8 已提交
271

272
      expect(json_response['title']).to eq('updated title')
A
Andrew8xx8 已提交
273
    end
274 275 276 277

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

281
    it 'should return 400 on invalid label names' do
282 283 284
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          title: 'updated title',
          labels: 'label, ?'
285 286
      expect(response.status).to eq(400)
      expect(json_response['message']['labels']['?']['title']).to eq(['is invalid'])
287 288 289 290 291 292 293 294 295 296
    end
  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'
297 298
      expect(response.status).to eq(200)
      expect(json_response['labels']).to eq([label.title])
299 300 301 302 303
    end

    it 'should remove all labels' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: ''
304 305
      expect(response.status).to eq(200)
      expect(json_response['labels']).to eq([])
306 307 308 309 310
    end

    it 'should update labels' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: 'foo,bar'
311 312 313
      expect(response.status).to eq(200)
      expect(json_response['labels']).to include 'foo'
      expect(json_response['labels']).to include 'bar'
314 315 316 317 318
    end

    it 'should return 400 on invalid label names' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: 'label, ?'
319 320
      expect(response.status).to eq(400)
      expect(json_response['message']['labels']['?']['title']).to eq(['is invalid'])
321 322 323 324 325
    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'
326 327 328 329 330
      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'
331
    end
J
jubianchi 已提交
332 333 334 335

    it 'should return 400 if title is too long' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          title: 'g' * 256
336 337
      expect(response.status).to eq(400)
      expect(json_response['message']['title']).to eq([
J
jubianchi 已提交
338
        'is too long (maximum is 255 characters)'
339
      ])
J
jubianchi 已提交
340
    end
A
Andrew8xx8 已提交
341 342 343 344 345 346
  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"
347
      expect(response.status).to eq(200)
A
Andrew8xx8 已提交
348

349 350
      expect(json_response['labels']).to include 'label2'
      expect(json_response['state']).to eq "closed"
N
Nihad Abbasov 已提交
351 352 353 354 355
    end
  end

  describe "DELETE /projects/:id/issues/:issue_id" do
    it "should delete a project issue" do
356
      delete api("/projects/#{project.id}/issues/#{issue.id}", user)
357
      expect(response.status).to eq(405)
N
Nihad Abbasov 已提交
358 359 360
    end
  end
end