2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-28 08:32:07 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe EnvironmentSerializer do
|
2021-04-15 14:09:01 -04:00
|
|
|
include CreateEnvironmentsHelpers
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project, reload: true) { create(:project, :repository) }
|
2017-01-27 07:02:59 -05:00
|
|
|
|
|
|
|
let(:json) do
|
2016-11-03 07:54:10 -04:00
|
|
|
described_class
|
2017-05-09 00:15:34 -04:00
|
|
|
.new(current_user: user, project: project)
|
2016-11-02 09:36:21 -04:00
|
|
|
.represent(resource)
|
2016-10-28 08:32:07 -04:00
|
|
|
end
|
|
|
|
|
2021-04-15 14:09:01 -04:00
|
|
|
before_all do
|
2019-02-08 07:53:35 -05:00
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2021-04-15 14:09:01 -04:00
|
|
|
it_behaves_like 'avoid N+1 on environments serialization'
|
2016-11-03 09:01:35 -04:00
|
|
|
|
2021-04-15 14:09:01 -04:00
|
|
|
context 'when there is a collection of objects provided' do
|
|
|
|
let(:resource) { project.environments }
|
2016-11-03 09:01:35 -04:00
|
|
|
|
2021-04-15 14:09:01 -04:00
|
|
|
before_all do
|
|
|
|
create_list(:environment, 2, project: project)
|
2016-11-03 09:01:35 -04:00
|
|
|
end
|
2016-10-28 08:32:07 -04:00
|
|
|
|
2016-11-03 09:01:35 -04:00
|
|
|
it 'contains important elements of environment' do
|
2016-11-04 05:16:30 -04:00
|
|
|
expect(json.first)
|
2016-11-03 09:01:35 -04:00
|
|
|
.to include(:last_deployment, :name, :external_url)
|
2016-10-28 08:32:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates payload for collection' do
|
2016-11-04 05:16:30 -04:00
|
|
|
expect(json).to be_an_instance_of Array
|
2016-10-28 08:32:07 -04:00
|
|
|
end
|
|
|
|
end
|
2017-02-03 05:41:02 -05:00
|
|
|
|
2017-02-03 06:03:57 -05:00
|
|
|
context 'when representing environments within folders' do
|
|
|
|
let(:serializer) do
|
2018-07-10 04:11:04 -04:00
|
|
|
described_class
|
|
|
|
.new(current_user: user, project: project)
|
|
|
|
.within_folders
|
2017-02-03 06:03:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:resource) { Environment.all }
|
|
|
|
|
|
|
|
subject { serializer.represent(resource) }
|
|
|
|
|
|
|
|
context 'when there is a single environment' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2022-04-08 08:08:48 -04:00
|
|
|
create(:environment, project: project, name: 'staging')
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-02-03 06:03:57 -05:00
|
|
|
|
|
|
|
it 'represents one standalone environment' do
|
|
|
|
expect(subject.count).to eq 1
|
|
|
|
expect(subject.first[:name]).to eq 'staging'
|
|
|
|
expect(subject.first[:size]).to eq 1
|
|
|
|
expect(subject.first[:latest][:name]).to eq 'staging'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are multiple environments in folder' do
|
|
|
|
before do
|
2022-04-08 08:08:48 -04:00
|
|
|
create(:environment, project: project, name: 'staging/my-review-1')
|
|
|
|
create(:environment, project: project, name: 'staging/my-review-2')
|
2017-02-03 06:03:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'represents one item that is a folder' do
|
|
|
|
expect(subject.count).to eq 1
|
|
|
|
expect(subject.first[:name]).to eq 'staging'
|
|
|
|
expect(subject.first[:size]).to eq 2
|
|
|
|
expect(subject.first[:latest][:name]).to eq 'staging/my-review-2'
|
|
|
|
expect(subject.first[:latest][:environment_type]).to eq 'staging'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are multiple folders and standalone environments' do
|
|
|
|
before do
|
2022-04-08 08:08:48 -04:00
|
|
|
create(:environment, project: project, name: 'staging/my-review-1')
|
|
|
|
create(:environment, project: project, name: 'staging/my-review-2')
|
|
|
|
create(:environment, project: project, name: 'production/my-review-3')
|
|
|
|
create(:environment, project: project, name: 'testing')
|
2017-02-03 06:03:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'represents multiple items grouped within folders' do
|
|
|
|
expect(subject.count).to eq 3
|
|
|
|
|
|
|
|
expect(subject.first[:name]).to eq 'production'
|
|
|
|
expect(subject.first[:size]).to eq 1
|
|
|
|
expect(subject.first[:latest][:name]).to eq 'production/my-review-3'
|
|
|
|
expect(subject.first[:latest][:environment_type]).to eq 'production'
|
|
|
|
expect(subject.second[:name]).to eq 'staging'
|
|
|
|
expect(subject.second[:size]).to eq 2
|
|
|
|
expect(subject.second[:latest][:name]).to eq 'staging/my-review-2'
|
|
|
|
expect(subject.second[:latest][:environment_type]).to eq 'staging'
|
|
|
|
expect(subject.third[:name]).to eq 'testing'
|
|
|
|
expect(subject.third[:size]).to eq 1
|
|
|
|
expect(subject.third[:latest][:name]).to eq 'testing'
|
|
|
|
expect(subject.third[:latest][:environment_type]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-03 05:41:02 -05:00
|
|
|
context 'when used with pagination' do
|
2019-02-14 19:16:58 -05:00
|
|
|
let(:request) { double(url: "#{Gitlab.config.gitlab.url}:8080/api/v4/projects?#{query.to_query}", query_parameters: query) }
|
2017-02-03 05:41:02 -05:00
|
|
|
let(:response) { spy('response') }
|
|
|
|
let(:resource) { Environment.all }
|
2019-02-14 19:16:58 -05:00
|
|
|
let(:query) { { page: 1, per_page: 2 } }
|
2017-02-03 05:41:02 -05:00
|
|
|
|
|
|
|
let(:serializer) do
|
2018-07-10 04:11:04 -04:00
|
|
|
described_class
|
|
|
|
.new(current_user: user, project: project)
|
2017-02-03 05:41:02 -05:00
|
|
|
.with_pagination(request, response)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { serializer.represent(resource) }
|
|
|
|
|
|
|
|
it 'creates a paginated serializer' do
|
|
|
|
expect(serializer).to be_paginated
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when resource is paginatable relation' do
|
|
|
|
context 'when there is a single environment object in relation' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2022-04-08 08:08:48 -04:00
|
|
|
create(:environment, project: project)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-02-03 05:41:02 -05:00
|
|
|
|
|
|
|
it 'serializes environments' do
|
|
|
|
expect(subject.first).to have_key :id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple environment objects are serialized' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2022-04-08 08:08:48 -04:00
|
|
|
create_list(:environment, 3, project: project)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-02-03 05:41:02 -05:00
|
|
|
|
|
|
|
it 'serializes appropriate number of objects' do
|
|
|
|
expect(subject.count).to be 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends relevant headers' do
|
|
|
|
expect(response).to receive(:[]=).with('X-Total', '3')
|
|
|
|
expect(response).to receive(:[]=).with('X-Total-Pages', '2')
|
|
|
|
expect(response).to receive(:[]=).with('X-Per-Page', '2')
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2017-02-03 06:03:57 -05:00
|
|
|
|
|
|
|
context 'when grouping environments within folders' do
|
|
|
|
let(:serializer) do
|
2018-07-10 04:11:04 -04:00
|
|
|
described_class
|
|
|
|
.new(current_user: user, project: project)
|
2017-02-03 06:03:57 -05:00
|
|
|
.with_pagination(request, response)
|
|
|
|
.within_folders
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2022-04-08 08:08:48 -04:00
|
|
|
create(:environment, project: project, name: 'staging/review-1')
|
|
|
|
create(:environment, project: project, name: 'staging/review-2')
|
|
|
|
create(:environment, project: project, name: 'production/deploy-3')
|
|
|
|
create(:environment, project: project, name: 'testing')
|
2017-02-03 06:03:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'paginates grouped items including ordering' do
|
|
|
|
expect(subject.count).to eq 2
|
|
|
|
expect(subject.first[:name]).to eq 'production'
|
|
|
|
expect(subject.second[:name]).to eq 'staging'
|
|
|
|
end
|
2017-02-10 03:36:52 -05:00
|
|
|
|
|
|
|
it 'appends correct total page count header' do
|
|
|
|
expect(subject).not_to be_empty
|
|
|
|
expect(response).to have_received(:[]=).with('X-Total', '3')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends correct page count headers' do
|
|
|
|
expect(subject).not_to be_empty
|
|
|
|
expect(response).to have_received(:[]=).with('X-Total-Pages', '2')
|
|
|
|
expect(response).to have_received(:[]=).with('X-Per-Page', '2')
|
|
|
|
end
|
2017-02-03 06:03:57 -05:00
|
|
|
end
|
2017-02-03 05:41:02 -05:00
|
|
|
end
|
|
|
|
end
|
2021-04-15 14:09:01 -04:00
|
|
|
|
2022-01-12 04:15:13 -05:00
|
|
|
context 'batching loading' do
|
|
|
|
let(:resource) { Environment.all }
|
|
|
|
|
|
|
|
before do
|
2022-04-08 08:08:48 -04:00
|
|
|
create(:environment, project: project, name: 'staging/review-1')
|
2022-01-12 04:15:13 -05:00
|
|
|
create_environment_with_associations(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the custom preloader service' do
|
|
|
|
expect_next_instance_of(Preloaders::Environments::DeploymentPreloader) do |preloader|
|
|
|
|
expect(preloader).to receive(:execute_with_union).with(:last_deployment, hash_including(:deployable)).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect_next_instance_of(Preloaders::Environments::DeploymentPreloader) do |preloader|
|
|
|
|
expect(preloader).to receive(:execute_with_union).with(:upcoming_deployment, hash_including(:deployable)).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
json
|
|
|
|
end
|
2022-03-07 04:13:09 -05:00
|
|
|
|
|
|
|
# Validates possible bug that can arise when order_by is not honoured in the preloader.
|
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/353966#note_861381504
|
|
|
|
it 'fetches the last and upcoming deployment correctly' do
|
|
|
|
last_deployment = nil
|
|
|
|
upcoming_deployment = nil
|
|
|
|
create(:environment, project: project).tap do |environment|
|
|
|
|
create(:deployment, :success, environment: environment, project: project)
|
2022-05-30 11:08:03 -04:00
|
|
|
|
|
|
|
create(:ci_build, :success, project: project).tap do |build|
|
|
|
|
last_deployment = create(:deployment, :success, environment: environment, project: project, deployable: build)
|
|
|
|
end
|
2022-03-07 04:13:09 -05:00
|
|
|
|
|
|
|
create(:deployment, :running, environment: environment, project: project)
|
|
|
|
upcoming_deployment = create(:deployment, :running, environment: environment, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
response_json = json
|
|
|
|
|
|
|
|
expect(response_json.last[:last_deployment][:id]).to eq(last_deployment.id)
|
|
|
|
expect(response_json.last[:upcoming_deployment][:id]).to eq(upcoming_deployment.id)
|
|
|
|
end
|
2022-01-12 04:15:13 -05:00
|
|
|
end
|
|
|
|
|
2021-04-15 14:09:01 -04:00
|
|
|
def create_environment_with_associations(project)
|
|
|
|
create(:environment, project: project).tap do |environment|
|
2022-05-30 11:08:03 -04:00
|
|
|
create(:ci_pipeline, project: project).tap do |pipeline|
|
|
|
|
create(:ci_build, :manual, project: project, pipeline: pipeline, name: 'stop-action',
|
|
|
|
environment: environment.name)
|
|
|
|
|
|
|
|
create(:ci_build, :scheduled, project: project, pipeline: pipeline,
|
|
|
|
environment: environment.name).tap do |scheduled_build|
|
|
|
|
create(:deployment, :running, environment: environment, project: project,
|
|
|
|
deployable: scheduled_build)
|
|
|
|
end
|
|
|
|
|
|
|
|
create(:ci_build, :success, :manual, project: project, pipeline: pipeline,
|
|
|
|
environment: environment.name).tap do |manual_build|
|
|
|
|
create(:deployment, :success, environment: environment, project: project,
|
|
|
|
deployable: manual_build, on_stop: 'stop-action')
|
|
|
|
end
|
|
|
|
end
|
2021-04-15 14:09:01 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-28 08:32:07 -04:00
|
|
|
end
|