summaryrefslogtreecommitdiff
path: root/spec/requests/api/issues_spec.rb
blob: e8eebda95b40943b24fcd94e6821081561b128f3 (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
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers
  let(:user) { create(:user) }
  let!(:project) { create(:project, namespace: user.namespace ) }
  let!(:closed_issue) { create(:closed_issue, author: user, assignee: user, project: project, state: :closed) }
  let!(:issue) { create(:issue, author: user, assignee: user, project: project) }
  let!(:label) do
    create(:label, title: 'label', color: '#FFAABB', project: project)
  end
  let!(:label_link) { create(:label_link, label: label, target: issue) }

  before { project.team << [user, :reporter] }

  describe "GET /issues" do
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/issues")
        response.status.should == 401
      end
    end

    context "when authenticated" do
      it "should return an array of issues" do
        get api("/issues", user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.first['title'].should == issue.title
      end

      it "should add pagination headers" do
        get api("/issues?per_page=3", user)
        response.headers['Link'].should ==
          '<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"'
      end

      it 'should return an array of closed issues' do
        get api('/issues?state=closed', user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 1
        json_response.first['id'].should == closed_issue.id
      end

      it 'should return an array of opened issues' do
        get api('/issues?state=opened', user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 1
        json_response.first['id'].should == issue.id
      end

      it 'should return an array of all issues' do
        get api('/issues?state=all', user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 2
        json_response.first['id'].should == issue.id
        json_response.second['id'].should == closed_issue.id
      end

      it 'should return an array of labeled issues' do
        get api("/issues?labels=#{label.title}", user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 1
        json_response.first['labels'].should == [label.title]
      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)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 1
        json_response.first['labels'].should == [label.title]
      end

      it 'should return an empty array if no issue matches labels' do
        get api('/issues?labels=foo,bar', user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 0
      end

      it 'should return an array of labeled issues matching given state' do
        get api("/issues?labels=#{label.title}&state=opened", user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 1
        json_response.first['labels'].should == [label.title]
        json_response.first['state'].should == 'opened'
      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)
        response.status.should == 200
        json_response.should be_an Array
        json_response.length.should == 0
      end
    end
  end

  describe "GET /projects/:id/issues" do
    it "should return project issues" do
      get api("/projects/#{project.id}/issues", user)
      response.status.should == 200
      json_response.should be_an Array
      json_response.first['title'].should == issue.title
    end

    it 'should return an array of labeled project issues' do
      get api("/projects/#{project.id}/issues?labels=#{label.title}", user)
      response.status.should == 200
      json_response.should be_an Array
      json_response.length.should == 1
      json_response.first['labels'].should == [label.title]
    end

    it 'should return an array of labeled project issues when at least one label matches' do
      get api("/projects/#{project.id}/issues?labels=#{label.title},foo,bar", user)
      response.status.should == 200
      json_response.should be_an Array
      json_response.length.should == 1
      json_response.first['labels'].should == [label.title]
    end

    it 'should return an empty array if no project issue matches labels' do
      get api("/projects/#{project.id}/issues?labels=foo,bar", user)
      response.status.should == 200
      json_response.should be_an Array
      json_response.length.should == 0
    end
  end

  describe "GET /projects/:id/issues/:issue_id" do
    it "should return a project issue by id" do
      get api("/projects/#{project.id}/issues/#{issue.id}", user)
      response.status.should == 200
      json_response['title'].should == issue.title
      json_response['iid'].should == issue.iid
    end

    it "should return 404 if issue id not found" do
      get api("/projects/#{project.id}/issues/54321", user)
      response.status.should == 404
    end
  end

  describe "POST /projects/:id/issues" do
    it "should create a new project issue" do
      post api("/projects/#{project.id}/issues", user),
        title: 'new issue', labels: 'label, label2'
      response.status.should == 201
      json_response['title'].should == 'new issue'
      json_response['description'].should be_nil
      json_response['labels'].should == ['label', 'label2']
    end

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

    it 'should return 400 on invalid label names' do
      post api("/projects/#{project.id}/issues", user),
           title: 'new issue',
           labels: 'label, ?'
      response.status.should == 400
      json_response['message']['labels']['?']['title'].should == ['is invalid']
    end
  end

  describe "PUT /projects/:id/issues/:issue_id to update only title" do
    it "should update a project issue" do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
        title: 'updated title'
      response.status.should == 200

      json_response['title'].should == 'updated title'
    end

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

    it 'should return 400 on invalid label names' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          title: 'updated title',
          labels: 'label, ?'
      response.status.should == 400
      json_response['message']['labels']['?']['title'].should == ['is invalid']
    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'
      response.status.should == 200
      json_response['labels'].should == [label.title]
    end

    it 'should remove all labels' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: ''
      response.status.should == 200
      json_response['labels'].should == []
    end

    it 'should update labels' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: 'foo,bar'
      response.status.should == 200
      json_response['labels'].should include 'foo'
      json_response['labels'].should include 'bar'
    end

    it 'should return 400 on invalid label names' do
      put api("/projects/#{project.id}/issues/#{issue.id}", user),
          labels: 'label, ?'
      response.status.should == 400
      json_response['message']['labels']['?']['title'].should == ['is invalid']
    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'
      response.status.should == 200
      json_response['labels'].should include 'label:foo'
      json_response['labels'].should include 'label-bar'
      json_response['labels'].should include 'label_bar'
      json_response['labels'].should include 'label/bar'
    end
  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"
      response.status.should == 200

      json_response['labels'].should include 'label2'
      json_response['state'].should eq "closed"
    end
  end

  describe "DELETE /projects/:id/issues/:issue_id" do
    it "should delete a project issue" do
      delete api("/projects/#{project.id}/issues/#{issue.id}", user)
      response.status.should == 405
    end
  end
end