2016-10-28 12:32:07 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe EnvironmentSerializer do
|
2017-01-27 12:02:59 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2017-01-27 12:02:59 +00:00
|
|
|
|
|
|
|
let(:json) do
|
2016-11-03 11:54:10 +00:00
|
|
|
described_class
|
2017-05-09 04:15:34 +00:00
|
|
|
.new(current_user: user, project: project)
|
2016-11-02 13:36:21 +00:00
|
|
|
.represent(resource)
|
2016-10-28 12:32:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a single object provided' do
|
2017-03-27 19:44:13 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:deployable) { create(:ci_build) }
|
2016-11-03 11:54:10 +00:00
|
|
|
let(:deployment) do
|
|
|
|
create(:deployment, deployable: deployable,
|
2016-11-03 14:13:57 +00:00
|
|
|
user: user,
|
|
|
|
project: project,
|
|
|
|
sha: project.commit.id)
|
2016-11-03 11:54:10 +00:00
|
|
|
end
|
|
|
|
let(:resource) { deployment.environment }
|
2016-10-28 12:32:07 +00:00
|
|
|
|
2017-03-27 19:44:13 +00:00
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, name: 'manual1', pipeline: deployable.pipeline)
|
|
|
|
end
|
|
|
|
|
2016-11-03 13:01:35 +00:00
|
|
|
it 'contains important elements of environment' do
|
2016-11-04 09:16:30 +00:00
|
|
|
expect(json)
|
2016-11-17 15:08:05 +00:00
|
|
|
.to include(:name, :external_url, :environment_path, :last_deployment)
|
2016-11-03 13:01:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains relevant information about last deployment' do
|
2016-11-04 09:16:30 +00:00
|
|
|
last_deployment = json.fetch(:last_deployment)
|
2016-11-03 13:01:35 +00:00
|
|
|
|
|
|
|
expect(last_deployment)
|
|
|
|
.to include(:ref, :user, :commit, :deployable, :manual_actions)
|
|
|
|
end
|
2016-10-28 12:32:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a collection of objects provided' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2016-10-28 12:32:07 +00:00
|
|
|
let(:resource) { create_list(:environment, 2) }
|
|
|
|
|
2016-11-03 13:01:35 +00:00
|
|
|
it 'contains important elements of environment' do
|
2016-11-04 09:16:30 +00:00
|
|
|
expect(json.first)
|
2016-11-03 13:01:35 +00:00
|
|
|
.to include(:last_deployment, :name, :external_url)
|
2016-10-28 12:32:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates payload for collection' do
|
2016-11-04 09:16:30 +00:00
|
|
|
expect(json).to be_an_instance_of Array
|
2016-10-28 12:32:07 +00:00
|
|
|
end
|
|
|
|
end
|
2017-02-03 10:41:02 +00:00
|
|
|
|
2017-02-03 11:03:57 +00:00
|
|
|
context 'when representing environments within folders' do
|
|
|
|
let(:serializer) do
|
|
|
|
described_class.new(project: project).within_folders
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:resource) { Environment.all }
|
|
|
|
|
|
|
|
subject { serializer.represent(resource) }
|
|
|
|
|
|
|
|
context 'when there is a single environment' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
create(:environment, name: 'staging')
|
|
|
|
end
|
2017-02-03 11:03:57 +00: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
|
|
|
|
create(:environment, name: 'staging/my-review-1')
|
|
|
|
create(:environment, name: 'staging/my-review-2')
|
|
|
|
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
|
|
|
|
create(:environment, name: 'staging/my-review-1')
|
|
|
|
create(:environment, name: 'staging/my-review-2')
|
|
|
|
create(:environment, name: 'production/my-review-3')
|
|
|
|
create(:environment, name: 'testing')
|
|
|
|
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 10:41:02 +00:00
|
|
|
context 'when used with pagination' do
|
|
|
|
let(:request) { spy('request') }
|
|
|
|
let(:response) { spy('response') }
|
|
|
|
let(:resource) { Environment.all }
|
|
|
|
let(:pagination) { { page: 1, per_page: 2 } }
|
|
|
|
|
|
|
|
let(:serializer) do
|
|
|
|
described_class.new(project: project)
|
|
|
|
.with_pagination(request, response)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(request).to receive(:query_parameters)
|
|
|
|
.and_return(pagination)
|
|
|
|
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 18:18:56 +00:00
|
|
|
before do
|
|
|
|
create(:environment)
|
|
|
|
end
|
2017-02-03 10:41:02 +00: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 18:18:56 +00:00
|
|
|
before do
|
|
|
|
create_list(:environment, 3)
|
|
|
|
end
|
2017-02-03 10:41:02 +00: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 11:03:57 +00:00
|
|
|
|
|
|
|
context 'when grouping environments within folders' do
|
|
|
|
let(:serializer) do
|
|
|
|
described_class.new(project: project)
|
|
|
|
.with_pagination(request, response)
|
|
|
|
.within_folders
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:environment, name: 'staging/review-1')
|
|
|
|
create(:environment, name: 'staging/review-2')
|
|
|
|
create(:environment, name: 'production/deploy-3')
|
|
|
|
create(:environment, name: 'testing')
|
|
|
|
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 08:36:52 +00: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 11:03:57 +00:00
|
|
|
end
|
2017-02-03 10:41:02 +00:00
|
|
|
end
|
|
|
|
end
|
2016-10-28 12:32:07 +00:00
|
|
|
end
|