BigW Consortium Gitlab

builds_spec.rb 6.88 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers

  let(:user) { create(:user) }
Kamil Trzcinski committed
7
  let(:api_user) { user }
8 9
  let(:user2) { create(:user) }
  let!(:project) { create(:project, creator_id: user.id) }
10 11
  let!(:developer) { create(:project_member, :developer, user: user, project: project) }
  let!(:reporter) { create(:project_member, :reporter, user: user2, project: project) }
12 13
  let(:commit) { create(:ci_commit, project: project)}
  let(:build) { create(:ci_build, commit: commit) }
14 15

  describe 'GET /projects/:id/builds ' do
Kamil Trzcinski committed
16 17 18 19
    let(:query) { '' }

    before { get api("/projects/#{project.id}/builds?#{query}", api_user) }

20 21
    context 'authorized user' do
      it 'should return project builds' do
22 23 24 25
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
      end

Kamil Trzcinski committed
26 27
      context 'filter project with one scope element' do
        let(:query) { 'scope=pending' }
28

Kamil Trzcinski committed
29 30 31 32
        it do
          expect(response.status).to eq(200)
          expect(json_response).to be_an Array
        end
33 34
      end

Kamil Trzcinski committed
35 36
      context 'filter project with array of scope elements' do
        let(:query) { 'scope[0]=pending&scope[1]=running' }
37

Kamil Trzcinski committed
38 39 40 41
        it do
          expect(response.status).to eq(200)
          expect(json_response).to be_an Array
        end
42
      end
43

Kamil Trzcinski committed
44 45
      context 'respond 400 when scope contains invalid state' do
        let(:query) { 'scope[0]=pending&scope[1]=unknown_status' }
46

Kamil Trzcinski committed
47
        it { expect(response.status).to eq(400) }
48
      end
49 50 51
    end

    context 'unauthorized user' do
Kamil Trzcinski committed
52
      let(:api_user) { nil }
53

Kamil Trzcinski committed
54
      it 'should not return project builds' do
55 56 57 58 59
        expect(response.status).to eq(401)
      end
    end
  end

60
  describe 'GET /projects/:id/repository/commits/:sha/builds' do
Kamil Trzcinski committed
61
    before do
Kamil Trzcinski committed
62
      project.ensure_ci_commit(commit.sha, 'master')
Kamil Trzcinski committed
63 64 65
      get api("/projects/#{project.id}/repository/commits/#{commit.sha}/builds", api_user)
    end

66 67 68 69 70 71 72 73
    context 'authorized user' do
      it 'should return project builds for specific commit' do
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
      end
    end

    context 'unauthorized user' do
Kamil Trzcinski committed
74
      let(:api_user) { nil }
75

Kamil Trzcinski committed
76
      it 'should not return project builds' do
77 78 79 80
        expect(response.status).to eq(401)
      end
    end
  end
81

82
  describe 'GET /projects/:id/builds/:build_id' do
Kamil Trzcinski committed
83 84
    before { get api("/projects/#{project.id}/builds/#{build.id}", api_user) }

85 86 87 88 89
    context 'authorized user' do
      it 'should return specific build data' do
        expect(response.status).to eq(200)
        expect(json_response['name']).to eq('test')
      end
90 91 92
    end

    context 'unauthorized user' do
Kamil Trzcinski committed
93
      let(:api_user) { nil }
94

Kamil Trzcinski committed
95
      it 'should not return specific build data' do
96 97 98 99 100
        expect(response.status).to eq(401)
      end
    end
  end

101
  describe 'GET /projects/:id/builds/:build_id/artifacts' do
Kamil Trzcinski committed
102
    before { get api("/projects/#{project.id}/builds/#{build.id}/artifacts", api_user) }
103

Kamil Trzcinski committed
104 105
    context 'build with artifacts' do
      let(:build) { create(:ci_build, :artifacts, commit: commit) }
106

Kamil Trzcinski committed
107 108
      context 'authorized user' do
        let(:download_headers) do
109 110
          { 'Content-Transfer-Encoding' => 'binary',
            'Content-Disposition' => 'attachment; filename=ci_build_artifacts.zip' }
Kamil Trzcinski committed
111
        end
112

Kamil Trzcinski committed
113 114 115 116
        it 'should return specific build artifacts' do
          expect(response.status).to eq(200)
          expect(response.headers).to include(download_headers)
        end
117 118
      end

Kamil Trzcinski committed
119 120
      context 'unauthorized user' do
        let(:api_user) { nil }
121

Kamil Trzcinski committed
122 123 124
        it 'should not return specific build artifacts' do
          expect(response.status).to eq(401)
        end
125 126
      end
    end
Kamil Trzcinski committed
127 128 129 130

    it 'should not return build artifacts if not uploaded' do
      expect(response.status).to eq(404)
    end
131 132
  end

133
  describe 'GET /projects/:id/builds/:build_id/trace' do
Kamil Trzcinski committed
134 135 136 137
    let(:build) { create(:ci_build, :trace, commit: commit) }
    
    before { get api("/projects/#{project.id}/builds/#{build.id}/trace", api_user) }

138
    context 'authorized user' do
139 140
      it 'should return specific build trace' do
        expect(response.status).to eq(200)
Kamil Trzcinski committed
141
        expect(response.body).to eq(build.trace)
142 143 144 145
      end
    end

    context 'unauthorized user' do
Kamil Trzcinski committed
146
      let(:api_user) { nil }
147

Kamil Trzcinski committed
148
      it 'should not return specific build trace' do
149 150 151 152
        expect(response.status).to eq(401)
      end
    end
  end
153

154
  describe 'POST /projects/:id/builds/:build_id/cancel' do
Kamil Trzcinski committed
155 156
    before { post api("/projects/#{project.id}/builds/#{build.id}/cancel", api_user) }

157
    context 'authorized user' do
158
      context 'user with :update_build persmission' do
159 160 161 162 163 164
        it 'should cancel running or pending build' do
          expect(response.status).to eq(201)
          expect(project.builds.first.status).to eq('canceled')
        end
      end

165
      context 'user without :update_build permission' do
Kamil Trzcinski committed
166
        let(:api_user) { user2 }
167

Kamil Trzcinski committed
168
        it 'should not cancel build' do
169 170 171 172 173 174
          expect(response.status).to eq(403)
        end
      end
    end

    context 'unauthorized user' do
Kamil Trzcinski committed
175
      let(:api_user) { nil }
176

Kamil Trzcinski committed
177
      it 'should not cancel build' do
178 179 180 181 182
        expect(response.status).to eq(401)
      end
    end
  end

183
  describe 'POST /projects/:id/builds/:build_id/retry' do
Kamil Trzcinski committed
184 185 186 187
    let(:build) { create(:ci_build, :canceled, commit: commit) }

    before { post api("/projects/#{project.id}/builds/#{build.id}/retry", api_user) }

188
    context 'authorized user' do
Kamil Trzcinski committed
189
      context 'user with :update_build permission' do
190 191 192 193 194 195 196
        it 'should retry non-running build' do
          expect(response.status).to eq(201)
          expect(project.builds.first.status).to eq('canceled')
          expect(json_response['status']).to eq('pending')
        end
      end

197
      context 'user without :update_build permission' do
Kamil Trzcinski committed
198
        let(:api_user) { user2 }
199

Kamil Trzcinski committed
200
        it 'should not retry build' do
201 202 203 204 205 206
          expect(response.status).to eq(403)
        end
      end
    end

    context 'unauthorized user' do
Kamil Trzcinski committed
207
      let(:api_user) { nil }
208

Kamil Trzcinski committed
209
      it 'should not retry build' do
210 211 212 213
        expect(response.status).to eq(401)
      end
    end
  end
214

215
  describe 'POST /projects/:id/builds/:build_id/erase' do
216
    before do
217
      post api("/projects/#{project.id}/builds/#{build.id}/erase", user)
218 219
    end

220
    context 'build is erasable' do
221
      let(:build) { create(:ci_build, :trace, :artifacts, :success, project: project, commit: commit) }
222 223

      it 'should erase build content' do
224
        expect(response.status).to eq 201
225 226 227 228
        expect(build.trace).to be_empty
        expect(build.artifacts_file.exists?).to be_falsy
        expect(build.artifacts_metadata.exists?).to be_falsy
      end
229 230 231 232 233

      it 'should update build' do
        expect(build.reload.erased_at).to be_truthy
        expect(build.reload.erased_by).to eq user
      end
234 235
    end

236
    context 'build is not erasable' do
237
      let(:build) { create(:ci_build, :trace, project: project, commit: commit) }
238 239 240 241 242 243

      it 'should respond with forbidden' do
        expect(response.status).to eq 403
      end
    end
  end
244
end