BigW Consortium Gitlab

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

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

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 committed
28
        def create_status(commit, opts = {})
29
          create(:commit_status, { pipeline: commit, ref: commit.ref }.merge(opts))
30 31
        end

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

119
    context 'developer user' do
120 121 122 123
      context 'only required parameters' do
        before { post api(post_url, developer), state: 'success' }

        it 'creates commit status' do
124
          expect(response).to have_http_status(201)
125 126 127 128 129 130 131
          expect(json_response['sha']).to eq(commit.id)
          expect(json_response['status']).to eq('success')
          expect(json_response['name']).to eq('default')
          expect(json_response['ref']).to be_nil
          expect(json_response['target_url']).to be_nil
          expect(json_response['description']).to be_nil
        end
132 133 134 135 136 137 138 139 140
      end

      context 'with all optional parameters' do
        before do
          optional_params = { state: 'success', context: 'coverage',
                              ref: 'develop', target_url: 'url', description: 'test' }

          post api(post_url, developer), optional_params
        end
141

142
        it 'creates commit status' do
143
          expect(response).to have_http_status(201)
144 145 146 147 148 149 150 151 152
          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['target_url']).to eq('url')
          expect(json_response['description']).to eq('test')
        end
      end

153 154 155 156
      context 'invalid status' do
        before { post api(post_url, developer), state: 'invalid' }

        it 'does not create commit status' do
157
          expect(response).to have_http_status(400)
158
        end
159 160 161 162
      end

      context 'request without state' do
        before { post api(post_url, developer) }
163

164
        it 'does not create commit status' do
165
          expect(response).to have_http_status(400)
166
        end
167
      end
168

169
      context 'invalid commit' do
170 171
        let(:sha) { 'invalid_sha' }
        before { post api(post_url, developer), state: 'running' }
172 173

        it 'returns not found error' do
174
          expect(response).to have_http_status(404)
175 176 177 178
        end
      end
    end

179
    context 'reporter user' do
180 181
      before { post api(post_url, reporter) }

182
      it 'does not create commit status' do
183
        expect(response).to have_http_status(403)
184 185 186
      end
    end

187
    context 'guest user' do
188 189
      before { post api(post_url, guest) }

190
      it 'does not create commit status' do
191
        expect(response).to have_http_status(403)
192 193 194 195
      end
    end

    context 'unauthorized user' do
196 197
      before { post api(post_url) }

198
      it 'does not create commit status' do
199
        expect(response).to have_http_status(401)
200 201 202
      end
    end
  end
203

204
  def create_user(access_level_trait)
205
    user = create(:user)
206
    create(:project_member, access_level_trait, user: user, project: project)
207 208
    user
  end
209
end