BigW Consortium Gitlab

milestones_spec.rb 4.42 KB
Newer Older
Robert Speicher committed
1 2
require 'spec_helper'

3
describe API::API, api: true  do
4
  include ApiHelpers
5
  let(:user) { create(:user) }
6
  let!(:project) { create(:project, namespace: user.namespace ) }
7
  let!(:milestone) { create(:milestone, project: project) }
Robert Speicher committed
8

Dmitriy Zaporozhets committed
9
  before { project.team << [user, :developer] }
Robert Speicher committed
10

11 12
  describe 'GET /projects/:id/milestones' do
    it 'should return project milestones' do
13
      get api("/projects/#{project.id}/milestones", user)
14 15 16
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.first['title']).to eq(milestone.title)
Robert Speicher committed
17
    end
18

19
    it 'should return a 401 error if user not authenticated' do
20
      get api("/projects/#{project.id}/milestones")
21
      expect(response.status).to eq(401)
22
    end
Robert Speicher committed
23 24
  end

25 26
  describe 'GET /projects/:id/milestones/:milestone_id' do
    it 'should return a project milestone by id' do
27
      get api("/projects/#{project.id}/milestones/#{milestone.id}", user)
28 29 30
      expect(response.status).to eq(200)
      expect(json_response['title']).to eq(milestone.title)
      expect(json_response['iid']).to eq(milestone.iid)
Robert Speicher committed
31
    end
32

33 34
    it 'should return a project milestone by iid' do
      get api("/projects/#{project.id}/milestones?iid=#{milestone.iid}", user)
35 36 37
      expect(response.status).to eq 200
      expect(json_response.first['title']).to eq milestone.title
      expect(json_response.first['id']).to eq milestone.id
38 39
    end

40
    it 'should return 401 error if user not authenticated' do
41
      get api("/projects/#{project.id}/milestones/#{milestone.id}")
42
      expect(response.status).to eq(401)
43 44
    end

45
    it 'should return a 404 error if milestone id not found' do
46
      get api("/projects/#{project.id}/milestones/1234", user)
47
      expect(response.status).to eq(404)
48
    end
Robert Speicher committed
49 50
  end

51 52
  describe 'POST /projects/:id/milestones' do
    it 'should create a new project milestone' do
53
      post api("/projects/#{project.id}/milestones", user), title: 'new milestone'
54 55 56
      expect(response.status).to eq(201)
      expect(json_response['title']).to eq('new milestone')
      expect(json_response['description']).to be_nil
Robert Speicher committed
57
    end
58

59
    it 'should create a new project milestone with description and due date' do
60 61
      post api("/projects/#{project.id}/milestones", user),
        title: 'new milestone', description: 'release', due_date: '2013-03-02'
62 63 64
      expect(response.status).to eq(201)
      expect(json_response['description']).to eq('release')
      expect(json_response['due_date']).to eq('2013-03-02')
65 66
    end

67
    it 'should return a 400 error if title is missing' do
68
      post api("/projects/#{project.id}/milestones", user)
69
      expect(response.status).to eq(400)
70
    end
Robert Speicher committed
71 72
  end

73 74
  describe 'PUT /projects/:id/milestones/:milestone_id' do
    it 'should update a project milestone' do
75
      put api("/projects/#{project.id}/milestones/#{milestone.id}", user),
Robert Speicher committed
76
        title: 'updated title'
77 78
      expect(response.status).to eq(200)
      expect(json_response['title']).to eq('updated title')
Robert Speicher committed
79
    end
80

81
    it 'should return a 404 error if milestone id not found' do
82 83
      put api("/projects/#{project.id}/milestones/1234", user),
        title: 'updated title'
84
      expect(response.status).to eq(404)
85
    end
Robert Speicher committed
86
  end
87

88 89
  describe 'PUT /projects/:id/milestones/:milestone_id to close milestone' do
    it 'should update a project milestone' do
90 91
      put api("/projects/#{project.id}/milestones/#{milestone.id}", user),
        state_event: 'close'
92
      expect(response.status).to eq(200)
93

94
      expect(json_response['state']).to eq('closed')
95 96
    end
  end
97

98 99
  describe 'PUT /projects/:id/milestones/:milestone_id to test observer on close' do
    it 'should create an activity event when an milestone is closed' do
100
      expect(Event).to receive(:create)
101 102 103 104 105

      put api("/projects/#{project.id}/milestones/#{milestone.id}", user),
          state_event: 'close'
    end
  end
106

107 108 109 110 111
  describe 'GET /projects/:id/milestones/:milestone_id/issues' do
    before do
      milestone.issues << create(:issue)
    end
    it 'should return project issues for a particular milestone' do
112
      get api("/projects/#{project.id}/milestones/#{milestone.id}/issues", user)
113 114 115
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.first['milestone']['title']).to eq(milestone.title)
116 117
    end

118
    it 'should return a 401 error if user not authenticated' do
119
      get api("/projects/#{project.id}/milestones/#{milestone.id}/issues")
120
      expect(response.status).to eq(401)
121 122
    end
  end
Robert Speicher committed
123
end