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

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
6
  let!(:project) { create(:project) }
7
  let(:commit) { project.repository.commit }
8
  let(:commit_status) { create(:commit_status, pipeline: pipeline) }
9 10 11
  let(:guest) { create_user(:guest) }
  let(:reporter) { create_user(:reporter) }
  let(:developer) { create_user(:developer) }
12 13
  let(:sha) { commit.id }

14
  describe "GET /projects/:id/repository/commits/:sha/statuses" do
15
    let(:get_url) { "/projects/#{project.id}/repository/commits/#{sha}/statuses" }
16

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

21
      it_behaves_like 'a paginated resources' do
22
        let(:request) { get api(get_url, reporter) }
23 24
      end

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

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
32 33 34 35 36 37
        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') }
38

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

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

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

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 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

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
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 154
      end

      context 'with all optional parameters' do
        before do
155 156 157 158 159
          optional_params = { state: 'success',
                              context: 'coverage',
                              ref: 'develop',
                              description: 'test',
                              target_url: 'http://gitlab.com/status' }
160 161 162

          post api(post_url, developer), optional_params
        end
163

164
        it 'creates commit status' do
165
          expect(response).to have_http_status(201)
166 167 168 169 170
          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['description']).to eq('test')
171
          expect(json_response['target_url']).to eq('http://gitlab.com/status')
172 173 174
        end
      end

175
      context 'when status is invalid' do
176 177 178
        before { post api(post_url, developer), state: 'invalid' }

        it 'does not create commit status' do
179
          expect(response).to have_http_status(400)
180
        end
181 182
      end

183
      context 'when request without a state made' do
184
        before { post api(post_url, developer) }
185

186
        it 'does not create commit status' do
187
          expect(response).to have_http_status(400)
188
        end
189
      end
190

191
      context 'when commit SHA is invalid' do
192 193
        let(:sha) { 'invalid_sha' }
        before { post api(post_url, developer), state: 'running' }
194 195

        it 'returns not found error' do
196
          expect(response).to have_http_status(404)
197 198
        end
      end
199 200 201 202 203 204 205 206 207 208 209 210 211

      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
212 213
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
214
    context 'reporter user' do
215
      before { post api(post_url, reporter), state: 'running' }
216

217
      it 'does not create commit status' do
218
        expect(response).to have_http_status(403)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
219 220 221
      end
    end

222
    context 'guest user' do
223
      before { post api(post_url, guest), state: 'running' }
224

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

    context 'unauthorized user' do
231 232
      before { post api(post_url) }

233
      it 'does not create commit status' do
234
        expect(response).to have_http_status(401)
235 236 237
      end
    end
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
238

239
  def create_user(access_level_trait)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
240
    user = create(:user)
241
    create(:project_member, access_level_trait, user: user, project: project)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
242 243
    user
  end
244
end