BigW Consortium Gitlab

pipeline_schedules_spec.rb 10.1 KB
Newer Older
1 2 3
require 'spec_helper'

describe API::PipelineSchedules do
4 5 6
  set(:developer) { create(:user) }
  set(:user) { create(:user) }
  set(:project) { create(:project) }
7 8 9 10 11 12 13

  before do
    project.add_developer(developer)
  end

  describe 'GET /projects/:id/pipeline_schedules' do
    context 'authenticated user with valid permissions' do
Shinya Maeda committed
14 15
      let(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: developer) }

16
      before do
Shinya Maeda committed
17
        pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
18 19 20 21 22 23 24 25 26
      end

      it 'returns list of pipeline_schedules' do
        get api("/projects/#{project.id}/pipeline_schedules", developer)

        expect(response).to have_http_status(:ok)
        expect(response).to include_pagination_headers
        expect(response).to match_response_schema('pipeline_schedules')
      end
Shinya Maeda committed
27 28 29 30 31 32

      it 'avoids N + 1 queries' do
        control_count = ActiveRecord::QueryRecorder.new do
          get api("/projects/#{project.id}/pipeline_schedules", developer)
        end.count

33
        create_list(:ci_pipeline_schedule, 10, project: project)
Shinya Maeda committed
34
          .each do |pipeline_schedule|
35 36 37 38
          create(:user).tap do |user|
            project.add_developer(user)
            pipeline_schedule.update_attributes(owner: user)
          end
Shinya Maeda committed
39 40
          pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
        end
Shinya Maeda committed
41 42 43 44 45

        expect do
          get api("/projects/#{project.id}/pipeline_schedules", developer)
        end.not_to exceed_query_limit(control_count)
      end
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

      %w[active inactive].each do |target|
        context "when scope is #{target}" do
          before do
            create(:ci_pipeline_schedule, project: project, active: active?(target))
          end

          it 'returns matched pipeline schedules' do
            get api("/projects/#{project.id}/pipeline_schedules", developer), scope: target

            expect(json_response.map{ |r| r['active'] }).to all(eq(active?(target)))
          end
        end

        def active?(str)
          (str == 'active') ? true : false
        end
      end
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    end

    context 'authenticated user with invalid permissions' do
      it 'does not return pipeline_schedules list' do
        get api("/projects/#{project.id}/pipeline_schedules", user)

        expect(response).to have_http_status(:not_found)
      end
    end

    context 'unauthenticated user' do
      it 'does not return pipeline_schedules list' do
        get api("/projects/#{project.id}/pipeline_schedules")

        expect(response).to have_http_status(:unauthorized)
      end
    end
  end

  describe 'GET /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
Shinya Maeda committed
84 85 86 87
    let(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: developer) }

    before do
      pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    end

    context 'authenticated user with valid permissions' do
      it 'returns pipeline_schedule details' do
        get api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer)

        expect(response).to have_http_status(:ok)
        expect(response).to match_response_schema('pipeline_schedule')
      end

      it 'responds with 404 Not Found if requesting non-existing pipeline_schedule' do
        get api("/projects/#{project.id}/pipeline_schedules/-5", developer)

        expect(response).to have_http_status(:not_found)
      end
    end

    context 'authenticated user with invalid permissions' do
      it 'does not return pipeline_schedules list' do
        get api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", user)

        expect(response).to have_http_status(:not_found)
      end
    end

    context 'unauthenticated user' do
      it 'does not return pipeline_schedules list' do
        get api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}")

        expect(response).to have_http_status(:unauthorized)
      end
    end
  end

  describe 'POST /projects/:id/pipeline_schedules' do
Shinya Maeda committed
123
    let(:params) { attributes_for(:ci_pipeline_schedule) }
124 125 126 127 128 129

    context 'authenticated user with valid permissions' do
      context 'with required parameters' do
        it 'creates pipeline_schedule' do
          expect do
            post api("/projects/#{project.id}/pipeline_schedules", developer),
Shinya Maeda committed
130
              params
131
          end.to change { project.pipeline_schedules.count }.by(1)
132 133 134

          expect(response).to have_http_status(:created)
          expect(response).to match_response_schema('pipeline_schedule')
Shinya Maeda committed
135 136 137 138
          expect(json_response['description']).to eq(params[:description])
          expect(json_response['ref']).to eq(params[:ref])
          expect(json_response['cron']).to eq(params[:cron])
          expect(json_response['cron_timezone']).to eq(params[:cron_timezone])
Shinya Maeda committed
139
          expect(json_response['owner']['id']).to eq(developer.id)
140 141 142 143 144 145 146 147 148 149
        end
      end

      context 'without required parameters' do
        it 'does not create pipeline_schedule' do
          post api("/projects/#{project.id}/pipeline_schedules", developer)

          expect(response).to have_http_status(:bad_request)
        end
      end
Shinya Maeda committed
150 151 152 153

      context 'when cron has validation error' do
        it 'does not create pipeline_schedule' do
          post api("/projects/#{project.id}/pipeline_schedules", developer),
Shinya Maeda committed
154
            params.merge('cron' => 'invalid-cron')
Shinya Maeda committed
155 156 157 158 159

          expect(response).to have_http_status(:bad_request)
          expect(json_response['message']).to have_key('cron')
        end
      end
160 161 162 163
    end

    context 'authenticated user with invalid permissions' do
      it 'does not create pipeline_schedule' do
Shinya Maeda committed
164
        post api("/projects/#{project.id}/pipeline_schedules", user), params
165 166 167 168 169 170 171

        expect(response).to have_http_status(:not_found)
      end
    end

    context 'unauthenticated user' do
      it 'does not create pipeline_schedule' do
Shinya Maeda committed
172
        post api("/projects/#{project.id}/pipeline_schedules"), params
173 174 175 176 177 178 179 180 181 182 183 184 185 186

        expect(response).to have_http_status(:unauthorized)
      end
    end
  end

  describe 'PUT /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
    let(:pipeline_schedule) do
      create(:ci_pipeline_schedule, project: project, owner: developer)
    end

    context 'authenticated user with valid permissions' do
      it 'updates cron' do
        put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer),
Shinya Maeda committed
187
          cron: '1 2 3 4 *'
188 189 190

        expect(response).to have_http_status(:ok)
        expect(response).to match_response_schema('pipeline_schedule')
Shinya Maeda committed
191
        expect(json_response['cron']).to eq('1 2 3 4 *')
192
      end
Shinya Maeda committed
193 194 195 196 197 198 199 200 201 202

      context 'when cron has validation error' do
        it 'does not update pipeline_schedule' do
          put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer),
            cron: 'invalid-cron'

          expect(response).to have_http_status(:bad_request)
          expect(json_response['message']).to have_key('cron')
        end
      end
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    end

    context 'authenticated user with invalid permissions' do
      it 'does not update pipeline_schedule' do
        put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", user)

        expect(response).to have_http_status(:not_found)
      end
    end

    context 'unauthenticated user' do
      it 'does not update pipeline_schedule' do
        put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}")

        expect(response).to have_http_status(:unauthorized)
      end
    end
  end

  describe 'POST /projects/:id/pipeline_schedules/:pipeline_schedule_id/take_ownership' do
    let(:pipeline_schedule) do
      create(:ci_pipeline_schedule, project: project, owner: developer)
    end

    context 'authenticated user with valid permissions' do
      it 'updates owner' do
Shinya Maeda committed
229
        post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/take_ownership", developer)
230

Shinya Maeda committed
231
        expect(response).to have_http_status(:created)
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        expect(response).to match_response_schema('pipeline_schedule')
      end
    end

    context 'authenticated user with invalid permissions' do
      it 'does not update owner' do
        post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/take_ownership", user)

        expect(response).to have_http_status(:not_found)
      end
    end

    context 'unauthenticated user' do
      it 'does not update owner' do
        post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/take_ownership")

        expect(response).to have_http_status(:unauthorized)
      end
    end
  end

  describe 'DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
    let(:master) { create(:user) }

    let!(:pipeline_schedule) do
      create(:ci_pipeline_schedule, project: project, owner: developer)
    end

    before do
      project.add_master(master)
    end

    context 'authenticated user with valid permissions' do
      it 'deletes pipeline_schedule' do
        expect do
          delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", master)
Shinya Maeda committed
268
        end.to change { project.pipeline_schedules.count }.by(-1)
269

270
        expect(response).to have_http_status(:accepted)
271 272 273 274 275 276 277 278 279 280 281
        expect(response).to match_response_schema('pipeline_schedule')
      end

      it 'responds with 404 Not Found if requesting non-existing pipeline_schedule' do
        delete api("/projects/#{project.id}/pipeline_schedules/-5", master)

        expect(response).to have_http_status(:not_found)
      end
    end

    context 'authenticated user with invalid permissions' do
282 283
      let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: master) }

284 285 286
      it 'does not delete pipeline_schedule' do
        delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer)

287
        expect(response).to have_http_status(:forbidden)
288 289 290 291 292 293 294
      end
    end

    context 'unauthenticated user' do
      it 'does not delete pipeline_schedule' do
        delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}")

295
        expect(response).to have_http_status(:unauthorized)
296 297 298 299
      end
    end
  end
end