BigW Consortium Gitlab

milestones_spec.rb 7 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!(:closed_milestone) { create(:closed_milestone, project: project) }
8
  let!(:milestone) { create(:milestone, project: project) }
Robert Speicher committed
9

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

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

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

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

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

34
    it 'returns an array of closed milestones' do
35
      get api("/projects/#{project.id}/milestones?state=closed", user)
36

37
      expect(response).to have_http_status(200)
38 39 40 41
      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
Robert Speicher committed
42 43
  end

44 45
  describe 'GET /projects/:id/milestones/:milestone_id' do
    it 'should return a project milestone by id' do
46
      get api("/projects/#{project.id}/milestones/#{milestone.id}", user)
47
      expect(response).to have_http_status(200)
48 49
      expect(json_response['title']).to eq(milestone.title)
      expect(json_response['iid']).to eq(milestone.iid)
Robert Speicher committed
50
    end
51

52
    it 'should return a project milestone by iid' do
53 54
      get api("/projects/#{project.id}/milestones?iid=#{closed_milestone.iid}", user)

55
      expect(response.status).to eq 200
56 57 58
      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
59 60
    end

61
    it 'should return 401 error if user not authenticated' do
62
      get api("/projects/#{project.id}/milestones/#{milestone.id}")
63
      expect(response).to have_http_status(401)
64 65
    end

66
    it 'should return a 404 error if milestone id not found' do
67
      get api("/projects/#{project.id}/milestones/1234", user)
68
      expect(response).to have_http_status(404)
69
    end
Robert Speicher committed
70 71
  end

72 73
  describe 'POST /projects/:id/milestones' do
    it 'should create a new project milestone' do
74
      post api("/projects/#{project.id}/milestones", user), title: 'new milestone'
75
      expect(response).to have_http_status(201)
76 77
      expect(json_response['title']).to eq('new milestone')
      expect(json_response['description']).to be_nil
Robert Speicher committed
78
    end
79

80
    it 'should create a new project milestone with description and due date' do
81 82
      post api("/projects/#{project.id}/milestones", user),
        title: 'new milestone', description: 'release', due_date: '2013-03-02'
83
      expect(response).to have_http_status(201)
84 85
      expect(json_response['description']).to eq('release')
      expect(json_response['due_date']).to eq('2013-03-02')
86 87
    end

88
    it 'should return a 400 error if title is missing' do
89
      post api("/projects/#{project.id}/milestones", user)
90
      expect(response).to have_http_status(400)
91
    end
Robert Speicher committed
92 93
  end

94 95
  describe 'PUT /projects/:id/milestones/:milestone_id' do
    it 'should update a project milestone' do
96
      put api("/projects/#{project.id}/milestones/#{milestone.id}", user),
Robert Speicher committed
97
        title: 'updated title'
98
      expect(response).to have_http_status(200)
99
      expect(json_response['title']).to eq('updated title')
Robert Speicher committed
100
    end
101

102
    it 'should return a 404 error if milestone id not found' do
103 104
      put api("/projects/#{project.id}/milestones/1234", user),
        title: 'updated title'
105
      expect(response).to have_http_status(404)
106
    end
Robert Speicher committed
107
  end
108

109 110
  describe 'PUT /projects/:id/milestones/:milestone_id to close milestone' do
    it 'should update a project milestone' do
111 112
      put api("/projects/#{project.id}/milestones/#{milestone.id}", user),
        state_event: 'close'
113
      expect(response).to have_http_status(200)
114

115
      expect(json_response['state']).to eq('closed')
116 117
    end
  end
118

119 120
  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
121
      expect(Event).to receive(:create)
122 123 124 125 126

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

128 129
  describe 'GET /projects/:id/milestones/:milestone_id/issues' do
    before do
130
      milestone.issues << create(:issue, project: project)
131 132
    end
    it 'should return project issues for a particular milestone' do
133
      get api("/projects/#{project.id}/milestones/#{milestone.id}/issues", user)
134
      expect(response).to have_http_status(200)
135 136
      expect(json_response).to be_an Array
      expect(json_response.first['milestone']['title']).to eq(milestone.title)
137 138
    end

139
    it 'should return a 401 error if user not authenticated' do
140
      get api("/projects/#{project.id}/milestones/#{milestone.id}/issues")
141
      expect(response).to have_http_status(401)
142
    end
143

144 145 146 147 148
    describe 'confidential issues' do
      let(:public_project) { create(:project, :public) }
      let(:milestone) { create(:milestone, project: public_project) }
      let(:issue) { create(:issue, project: public_project) }
      let(:confidential_issue) { create(:issue, confidential: true, project: public_project) }
149

150 151 152 153 154 155 156 157
      before do
        public_project.team << [user, :developer]
        milestone.issues << issue << confidential_issue
      end

      it 'returns confidential issues to team members' do
        get api("/projects/#{public_project.id}/milestones/#{milestone.id}/issues", user)

158
        expect(response).to have_http_status(200)
159 160 161 162 163
        expect(json_response).to be_an Array
        expect(json_response.size).to eq(2)
        expect(json_response.map { |issue| issue['id'] }).to include(issue.id, confidential_issue.id)
      end

164 165 166 167 168 169
      it 'does not return confidential issues to team members with guest role' do
        member = create(:user)
        project.team << [member, :guest]

        get api("/projects/#{public_project.id}/milestones/#{milestone.id}/issues", member)

170
        expect(response).to have_http_status(200)
171 172 173 174 175
        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

176 177 178
      it 'does not return confidential issues to regular users' do
        get api("/projects/#{public_project.id}/milestones/#{milestone.id}/issues", create(:user))

179
        expect(response).to have_http_status(200)
180 181 182 183
        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
184 185
    end
  end
Robert Speicher committed
186
end