BigW Consortium Gitlab

commit_statuses_spec.rb 9.57 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::CommitStatuses, api: true do
4
  include ApiHelpers
5

6
  let!(:project) { create(:project, :repository) }
7
  let(:commit) { project.repository.commit }
8 9 10
  let(:guest) { create_user(:guest) }
  let(:reporter) { create_user(:reporter) }
  let(:developer) { create_user(:developer) }
11 12
  let(:sha) { commit.id }

13 14 15 16
  let(:commit_status) do
    create(:commit_status, status: :pending, pipeline: pipeline)
  end

17
  describe "GET /projects/:id/repository/commits/:sha/statuses" do
18
    let(:get_url) { "/projects/#{project.id}/repository/commits/#{sha}/statuses" }
19

20
    context 'ci commit exists' do
21 22
      let!(:master) { project.pipelines.create(sha: commit.id, ref: 'master') }
      let!(:develop) { project.pipelines.create(sha: commit.id, ref: 'develop') }
23

24 25
      context "reporter user" do
        let(:statuses_id) { json_response.map { |status| status['id'] } }
26

Kamil Trzcinski committed
27
        def create_status(commit, opts = {})
28
          create(:commit_status, { pipeline: commit, ref: commit.ref }.merge(opts))
29 30
        end

Kamil Trzcinski committed
31 32 33 34 35 36
        let!(:status1) { create_status(master, status: 'running') }
        let!(:status2) { create_status(master, name: 'coverage', status: 'pending') }
        let!(:status3) { create_status(develop, status: 'running', allow_failure: true) }
        let!(:status4) { create_status(master, name: 'coverage', status: 'success') }
        let!(:status5) { create_status(develop, name: 'coverage', status: 'success') }
        let!(:status6) { create_status(master, status: 'success') }
37

38 39 40 41
        context 'latest commit statuses' do
          before { get api(get_url, reporter) }

          it 'returns latest commit statuses' do
42
            expect(response).to have_http_status(200)
43

44
            expect(response).to include_pagination_headers
45 46 47 48 49
            expect(json_response).to be_an Array
            expect(statuses_id).to contain_exactly(status3.id, status4.id, status5.id, status6.id)
            json_response.sort_by!{ |status| status['id'] }
            expect(json_response.map{ |status| status['allow_failure'] }).to eq([true, false, false, false])
          end
50 51
        end

52 53 54 55
        context 'all commit statuses' do
          before { get api(get_url, reporter), all: 1 }

          it 'returns all commit statuses' do
56
            expect(response).to have_http_status(200)
57
            expect(response).to include_pagination_headers
58
            expect(json_response).to be_an Array
59 60 61
            expect(statuses_id).to contain_exactly(status1.id, status2.id,
                                                   status3.id, status4.id,
                                                   status5.id, status6.id)
62
          end
63 64
        end

65 66
        context 'latest commit statuses for specific ref' do
          before { get api(get_url, reporter), ref: 'develop' }
67

68
          it 'returns latest commit statuses for specific ref' do
69
            expect(response).to have_http_status(200)
70
            expect(response).to include_pagination_headers
71 72 73
            expect(json_response).to be_an Array
            expect(statuses_id).to contain_exactly(status3.id, status5.id)
          end
74 75
        end

76 77 78 79
        context 'latest commit statues for specific name' do
          before { get api(get_url, reporter), name: 'coverage' }

          it 'return latest commit statuses for specific name' do
80
            expect(response).to have_http_status(200)
81
            expect(response).to include_pagination_headers
82
            expect(json_response).to be_an Array
83
            expect(statuses_id).to contain_exactly(status4.id, status5.id)
84
          end
85
        end
86
      end
87
    end
88

89
    context 'ci commit does not exist' do
90
      before { get api(get_url, reporter) }
91

92 93
      it 'returns empty array' do
        expect(response.status).to eq 200
94
        expect(json_response).to be_an Array
95
        expect(json_response).to be_empty
96 97 98 99
      end
    end

    context "guest user" do
100 101
      before { get api(get_url, guest) }

102
      it "does not return project commits" do
103
        expect(response).to have_http_status(403)
104 105 106 107
      end
    end

    context "unauthorized user" do
108 109
      before { get api(get_url) }

110
      it "does not return project commits" do
111
        expect(response).to have_http_status(401)
112 113 114 115
      end
    end
  end

116
  describe 'POST /projects/:id/statuses/:sha' do
117
    let(:post_url) { "/projects/#{project.id}/statuses/#{sha}" }
118

119
    context 'developer user' do
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
      %w[pending running success failed canceled].each do |status|
        context "for #{status}" do
          context 'uses only required parameters' do
            it 'creates commit status' do
              post api(post_url, developer), state: status

              expect(response).to have_http_status(201)
              expect(json_response['sha']).to eq(commit.id)
              expect(json_response['status']).to eq(status)
              expect(json_response['name']).to eq('default')
              expect(json_response['ref']).not_to be_empty
              expect(json_response['target_url']).to be_nil
              expect(json_response['description']).to be_nil
            end
          end
        end
      end

      context 'transitions status from pending' do
        before do
          post api(post_url, developer), state: 'pending'
        end

        %w[running success failed canceled].each do |status|
          it "to #{status}" do
            expect { post api(post_url, developer), state: status }.not_to change { CommitStatus.count }

            expect(response).to have_http_status(201)
            expect(json_response['status']).to eq(status)
          end
150
        end
151 152 153
      end

      context 'with all optional parameters' do
154 155 156 157 158 159 160 161
        context 'when creating a commit status' do
          it 'creates commit status' do
            post api(post_url, developer), {
              state: 'success',
              context: 'coverage',
              ref: 'develop',
              description: 'test',
              coverage: 80.0,
162 163
              target_url: 'http://gitlab.com/status'
            }
164 165 166 167 168 169 170 171 172 173

            expect(response).to have_http_status(201)
            expect(json_response['sha']).to eq(commit.id)
            expect(json_response['status']).to eq('success')
            expect(json_response['name']).to eq('coverage')
            expect(json_response['ref']).to eq('develop')
            expect(json_response['coverage']).to eq(80.0)
            expect(json_response['description']).to eq('test')
            expect(json_response['target_url']).to eq('http://gitlab.com/status')
          end
174
        end
175

176 177 178 179 180 181 182 183
        context 'when updatig a commit status' do
          before do
            post api(post_url, developer), {
              state: 'running',
              context: 'coverage',
              ref: 'develop',
              description: 'coverage test',
              coverage: 0.0,
184 185
              target_url: 'http://gitlab.com/status'
            }
186 187 188 189 190 191

            post api(post_url, developer), {
              state: 'success',
              name: 'coverage',
              ref: 'develop',
              description: 'new description',
192 193
              coverage: 90.0
            }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
          end

          it 'updates a commit status' do
            expect(response).to have_http_status(201)
            expect(json_response['sha']).to eq(commit.id)
            expect(json_response['status']).to eq('success')
            expect(json_response['name']).to eq('coverage')
            expect(json_response['ref']).to eq('develop')
            expect(json_response['coverage']).to eq(90.0)
            expect(json_response['description']).to eq('new description')
            expect(json_response['target_url']).to eq('http://gitlab.com/status')
          end

          it 'does not create a new commit status' do
            expect(CommitStatus.count).to eq 1
          end
210 211 212
        end
      end

213
      context 'when status is invalid' do
214 215 216
        before { post api(post_url, developer), state: 'invalid' }

        it 'does not create commit status' do
217
          expect(response).to have_http_status(400)
218
        end
219 220
      end

221
      context 'when request without a state made' do
222
        before { post api(post_url, developer) }
223

224
        it 'does not create commit status' do
225
          expect(response).to have_http_status(400)
226
        end
227
      end
228

229
      context 'when commit SHA is invalid' do
230 231
        let(:sha) { 'invalid_sha' }
        before { post api(post_url, developer), state: 'running' }
232 233

        it 'returns not found error' do
234
          expect(response).to have_http_status(404)
235 236
        end
      end
237 238 239 240 241 242 243 244 245 246 247 248 249

      context 'when target URL is an invalid address' do
        before do
          post api(post_url, developer), state: 'pending',
                                         target_url: 'invalid url'
        end

        it 'responds with bad request status and validation errors' do
          expect(response).to have_http_status(400)
          expect(json_response['message']['target_url'])
            .to include 'must be a valid URL'
        end
      end
250 251
    end

252
    context 'reporter user' do
253
      before { post api(post_url, reporter), state: 'running' }
254

255
      it 'does not create commit status' do
256
        expect(response).to have_http_status(403)
257 258 259
      end
    end

260
    context 'guest user' do
261
      before { post api(post_url, guest), state: 'running' }
262

263
      it 'does not create commit status' do
264
        expect(response).to have_http_status(403)
265 266 267 268
      end
    end

    context 'unauthorized user' do
269 270
      before { post api(post_url) }

271
      it 'does not create commit status' do
272
        expect(response).to have_http_status(401)
273 274 275
      end
    end
  end
276

277
  def create_user(access_level_trait)
278
    user = create(:user)
279
    create(:project_member, access_level_trait, user: user, project: project)
280 281
    user
  end
282
end