milestones_shared_examples.rb 13.4 KB
Newer Older
1 2
shared_examples_for 'group and project milestones' do |route_definition|
  let(:resource_route) { "#{route}/#{milestone.id}" }
3 4 5
  let(:label_1) { create(:label, title: 'label_1', project: project, priority: 1) }
  let(:label_2) { create(:label, title: 'label_2', project: project, priority: 2) }
  let(:label_3) { create(:label, title: 'label_3', project: project) }
6 7
  let(:merge_request) { create(:merge_request, source_project: project) }
  let(:another_merge_request) { create(:merge_request, :simple, source_project: project) }
Robert Speicher's avatar
Robert Speicher committed
8

9 10 11
  describe "GET #{route_definition}" do
    it 'returns milestones list' do
      get api(route, user)
12

13
      expect(response).to have_http_status(200)
14
      expect(response).to include_pagination_headers
15 16
      expect(json_response).to be_an Array
      expect(json_response.first['title']).to eq(milestone.title)
Robert Speicher's avatar
Robert Speicher committed
17
    end
18

19
    it 'returns a 401 error if user not authenticated' do
20
      get api(route)
21

22
      expect(response).to have_http_status(401)
23
    end
24

25
    it 'returns an array of active milestones' do
26
      get api("#{route}/?state=active", user)
27

28
      expect(response).to have_http_status(200)
29
      expect(response).to include_pagination_headers
30 31 32 33 34
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['id']).to eq(milestone.id)
    end

35
    it 'returns an array of closed milestones' do
36
      get api("#{route}/?state=closed", user)
37

38
      expect(response).to have_http_status(200)
39
      expect(response).to include_pagination_headers
40 41 42 43
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['id']).to eq(closed_milestone.id)
    end
44

45
    it 'returns an array of milestones specified by iids' do
46
      other_milestone = create(:milestone, project: try(:project), group: try(:group))
47

48
      get api(route, user), iids: [closed_milestone.iid, other_milestone.iid]
49 50 51 52 53 54 55 56

      expect(response).to have_http_status(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(2)
      expect(json_response.map{ |m| m['id'] }).to match_array([closed_milestone.id, other_milestone.id])
    end

    it 'does not return any milestone if none found' do
57
      get api(route, user), iids: [Milestone.maximum(:iid).succ]
58 59 60 61 62 63

      expect(response).to have_http_status(200)
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
    end

64 65
    it 'returns a milestone by iids array' do
      get api("#{route}?iids=#{closed_milestone.iid}", user)
66

67
      expect(response.status).to eq 200
68 69
      expect(response).to include_pagination_headers
      expect(json_response.size).to eq(1)
70 71 72
      expect(json_response.size).to eq(1)
      expect(json_response.first['title']).to eq closed_milestone.title
      expect(json_response.first['id']).to eq closed_milestone.id
73 74
    end

75 76
    it 'returns a milestone by searching for title' do
      get api(route, user), search: 'version2'
77 78 79

      expect(response).to have_http_status(200)
      expect(response).to include_pagination_headers
80 81 82 83 84
      expect(json_response.size).to eq(1)
      expect(json_response.first['title']).to eq milestone.title
      expect(json_response.first['id']).to eq milestone.id
    end

85 86
    it 'returns a milestones by searching for description' do
      get api(route, user), search: 'open'
87 88 89 90

      expect(response).to have_http_status(200)
      expect(response).to include_pagination_headers
      expect(json_response.size).to eq(1)
91 92 93
      expect(json_response.first['title']).to eq milestone.title
      expect(json_response.first['id']).to eq milestone.id
    end
94 95
  end

96 97 98 99 100 101 102 103 104 105 106
  describe "GET #{route_definition}/:milestone_id" do
    it 'returns a milestone by id' do
      get api(resource_route, user)

      expect(response).to have_http_status(200)
      expect(json_response['title']).to eq(milestone.title)
      expect(json_response['iid']).to eq(milestone.iid)
    end

    it 'returns a milestone by id' do
      get api(resource_route, user)
107 108 109 110 111

      expect(response).to have_http_status(200)
      expect(json_response['title']).to eq(milestone.title)
      expect(json_response['iid']).to eq(milestone.iid)
    end
112

113
    it 'returns 401 error if user not authenticated' do
114
      get api(resource_route)
115

116
      expect(response).to have_http_status(401)
117 118
    end

119
    it 'returns a 404 error if milestone id not found' do
120
      get api("#{route}/1234", user)
121

122
      expect(response).to have_http_status(404)
123
    end
Robert Speicher's avatar
Robert Speicher committed
124 125
  end

126 127 128
  describe "POST #{route_definition}" do
    it 'creates a new milestone' do
      post api(route, user), title: 'new milestone'
129

130
      expect(response).to have_http_status(201)
131 132
      expect(json_response['title']).to eq('new milestone')
      expect(json_response['description']).to be_nil
Robert Speicher's avatar
Robert Speicher committed
133
    end
134

135 136
    it 'creates a new milestone with description and dates' do
      post api(route, user),
137
        title: 'new milestone', description: 'release', due_date: '2013-03-02', start_date: '2013-02-02'
138

139
      expect(response).to have_http_status(201)
140 141
      expect(json_response['description']).to eq('release')
      expect(json_response['due_date']).to eq('2013-03-02')
142
      expect(json_response['start_date']).to eq('2013-02-02')
143 144
    end

145
    it 'returns a 400 error if title is missing' do
146
      post api(route, user)
147 148 149 150 151

      expect(response).to have_http_status(400)
    end

    it 'returns a 400 error if params are invalid (duplicate title)' do
152
      post api(route, user),
153 154
        title: milestone.title, description: 'release', due_date: '2013-03-02'

155
      expect(response).to have_http_status(400)
156
    end
157

158 159
    it 'creates a new milestone with reserved html characters' do
      post api(route, user), title: 'foo & bar 1.1 -> 2.2'
160 161 162 163 164

      expect(response).to have_http_status(201)
      expect(json_response['title']).to eq('foo & bar 1.1 -> 2.2')
      expect(json_response['description']).to be_nil
    end
Robert Speicher's avatar
Robert Speicher committed
165 166
  end

167 168 169
  describe "PUT #{route_definition}/:milestone_id" do
    it 'updates a milestone' do
      put api(resource_route, user),
Robert Speicher's avatar
Robert Speicher committed
170
        title: 'updated title'
171

172
      expect(response).to have_http_status(200)
173
      expect(json_response['title']).to eq('updated title')
Robert Speicher's avatar
Robert Speicher committed
174
    end
175

Robert Schilling's avatar
Robert Schilling committed
176 177 178
    it 'removes a due date if nil is passed' do
      milestone.update!(due_date: "2016-08-05")

179
      put api(resource_route, user), due_date: nil
Robert Schilling's avatar
Robert Schilling committed
180 181 182 183 184

      expect(response).to have_http_status(200)
      expect(json_response['due_date']).to be_nil
    end

185
    it 'returns a 404 error if milestone id not found' do
186
      put api("#{route}/1234", user),
187
        title: 'updated title'
188

189
      expect(response).to have_http_status(404)
190
    end
Andrew8xx8's avatar
Andrew8xx8 committed
191

192 193
    it 'closes milestone' do
      put api(resource_route, user),
Andrew8xx8's avatar
Andrew8xx8 committed
194
        state_event: 'close'
195
      expect(response).to have_http_status(200)
Andrew8xx8's avatar
Andrew8xx8 committed
196

197
      expect(json_response['state']).to eq('closed')
Andrew8xx8's avatar
Andrew8xx8 committed
198 199
    end
  end
200

201 202
  describe "GET #{route_definition}/:milestone_id/issues" do
    let(:issues_route) { "#{route}/#{milestone.id}/issues" }
203

204
    before do
205
      milestone.issues << create(:issue, project: project)
206
    end
207 208
    it 'returns issues for a particular milestone' do
      get api(issues_route, user)
209

210
      expect(response).to have_http_status(200)
211
      expect(response).to include_pagination_headers
212 213
      expect(json_response).to be_an Array
      expect(json_response.first['milestone']['title']).to eq(milestone.title)
214 215
    end

216
    it 'returns issues sorted by label priority' do
217 218 219 220
      issue_1 = create(:labeled_issue, project: project, milestone: milestone, labels: [label_3])
      issue_2 = create(:labeled_issue, project: project, milestone: milestone, labels: [label_1])
      issue_3 = create(:labeled_issue, project: project, milestone: milestone, labels: [label_2])

221
      get api(issues_route, user)
222 223 224 225 226 227

      expect(json_response.first['id']).to eq(issue_2.id)
      expect(json_response.second['id']).to eq(issue_3.id)
      expect(json_response.third['id']).to eq(issue_1.id)
    end

228
    it 'matches V4 response schema for a list of issues' do
229
      get api(issues_route, user)
230 231 232 233 234

      expect(response).to have_http_status(200)
      expect(response).to match_response_schema('public_api/v4/issues')
    end

235
    it 'returns a 401 error if user not authenticated' do
236
      get api(issues_route)
237

238
      expect(response).to have_http_status(401)
239
    end
240

241
    describe 'confidential issues' do
242 243 244 245 246 247 248 249 250 251 252 253 254 255
      let!(:public_project) { create(:empty_project, :public) }
      let!(:context_group) { try(:group) }
      let!(:milestone) do
        context_group ? create(:milestone, group: context_group) : create(:milestone, project: public_project)
      end
      let!(:issue) { create(:issue, project: public_project) }
      let!(:confidential_issue) { create(:issue, confidential: true, project: public_project) }
      let!(:issues_route) do
        if context_group
          "#{route}/#{milestone.id}/issues"
        else
          "/projects/#{public_project.id}/milestones/#{milestone.id}/issues"
        end
      end
256

257
      before do
258 259 260
        # Add public project to the group in context
        setup_for_group if context_group

261 262 263 264 265
        public_project.team << [user, :developer]
        milestone.issues << issue << confidential_issue
      end

      it 'returns confidential issues to team members' do
266
        get api(issues_route, user)
267

268
        expect(response).to have_http_status(200)
269
        expect(response).to include_pagination_headers
270
        expect(json_response).to be_an Array
271 272
        # 2 for projects, 3 for group(which has another project with an issue)
        expect(json_response.size).to be_between(2, 3)
273 274 275
        expect(json_response.map { |issue| issue['id'] }).to include(issue.id, confidential_issue.id)
      end

276 277
      it 'does not return confidential issues to team members with guest role' do
        member = create(:user)
278
        public_project.team << [member, :guest]
279

280
        get api(issues_route, member)
281

282
        expect(response).to have_http_status(200)
283
        expect(response).to include_pagination_headers
284 285 286 287 288
        expect(json_response).to be_an Array
        expect(json_response.size).to eq(1)
        expect(json_response.map { |issue| issue['id'] }).to include(issue.id)
      end

289
      it 'does not return confidential issues to regular users' do
290
        get api(issues_route, create(:user))
291

292
        expect(response).to have_http_status(200)
293
        expect(response).to include_pagination_headers
294 295 296 297
        expect(json_response).to be_an Array
        expect(json_response.size).to eq(1)
        expect(json_response.map { |issue| issue['id'] }).to include(issue.id)
      end
298

299 300 301 302
      it 'returns issues ordered by label priority' do
        issue.labels << label_2
        confidential_issue.labels << label_1

303
        get api(issues_route, user)
304 305 306 307

        expect(response).to have_http_status(200)
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
308 309
        # 2 for projects, 3 for group(which has another project with an issue)
        expect(json_response.size).to be_between(2, 3)
310 311 312
        expect(json_response.first['id']).to eq(confidential_issue.id)
        expect(json_response.second['id']).to eq(issue.id)
      end
313 314
    end
  end
315

316 317
  describe "GET #{route_definition}/:milestone_id/merge_requests" do
    let(:merge_requests_route) { "#{route}/#{milestone.id}/merge_requests" }
318

319 320 321 322
    before do
      milestone.merge_requests << merge_request
    end

323
    it 'returns merge_requests for a particular milestone' do
324 325
      # eager-load another_merge_request
      another_merge_request
326
      get api(merge_requests_route, user)
327 328 329 330 331 332 333 334

      expect(response).to have_http_status(200)
      expect(json_response).to be_an Array
      expect(json_response.size).to eq(1)
      expect(json_response.first['title']).to eq(merge_request.title)
      expect(json_response.first['milestone']['title']).to eq(milestone.title)
    end

335
    it 'returns merge_requests sorted by label priority' do
336 337 338 339
      merge_request_1 = create(:labeled_merge_request, source_branch: 'branch_1', source_project: project, milestone: milestone, labels: [label_2])
      merge_request_2 = create(:labeled_merge_request, source_branch: 'branch_2', source_project: project, milestone: milestone, labels: [label_1])
      merge_request_3 = create(:labeled_merge_request, source_branch: 'branch_3', source_project: project, milestone: milestone, labels: [label_3])

340
      get api(merge_requests_route, user)
341 342 343 344 345 346

      expect(json_response.first['id']).to eq(merge_request_2.id)
      expect(json_response.second['id']).to eq(merge_request_1.id)
      expect(json_response.third['id']).to eq(merge_request_3.id)
    end

347
    it 'returns a 404 error if milestone id not found' do
348 349 350
      not_found_route = "#{route}/1234/merge_requests"

      get api(not_found_route, user)
351 352 353 354 355 356

      expect(response).to have_http_status(404)
    end

    it 'returns a 404 if the user has no access to the milestone' do
      new_user = create :user
357
      get api(merge_requests_route, new_user)
358 359 360 361 362

      expect(response).to have_http_status(404)
    end

    it 'returns a 401 error if user not authenticated' do
363
      get api(merge_requests_route)
364 365 366

      expect(response).to have_http_status(401)
    end
367 368 369

    it 'returns merge_requests ordered by position asc' do
      milestone.merge_requests << another_merge_request
370 371
      another_merge_request.labels << label_1
      merge_request.labels << label_2
372

373
      get api(merge_requests_route, user)
374 375 376 377 378 379 380 381

      expect(response).to have_http_status(200)
      expect(response).to include_pagination_headers
      expect(json_response).to be_an Array
      expect(json_response.size).to eq(2)
      expect(json_response.first['id']).to eq(another_merge_request.id)
      expect(json_response.second['id']).to eq(merge_request.id)
    end
382
  end
Robert Speicher's avatar
Robert Speicher committed
383
end