BigW Consortium Gitlab

environment_serializer_spec.rb 5.87 KB
Newer Older
1 2 3
require 'spec_helper'

describe EnvironmentSerializer do
4 5 6 7
  let(:user) { create(:user) }
  let(:project) { create(:project) }

  let(:json) do
8 9
    described_class
      .new(user: user, project: project)
10
      .represent(resource)
11 12 13
  end

  context 'when there is a single object provided' do
14 15 16 17 18
    before do
      create(:ci_build, :manual, name: 'manual1',
                                 pipeline: deployable.pipeline)
    end

19 20
    let(:deployment) do
      create(:deployment, deployable: deployable,
21 22 23
                          user: user,
                          project: project,
                          sha: project.commit.id)
24 25 26 27
    end

    let(:deployable) { create(:ci_build) }
    let(:resource) { deployment.environment }
28

29
    it 'contains important elements of environment' do
30
      expect(json)
31
        .to include(:name, :external_url, :environment_path, :last_deployment)
32 33 34
    end

    it 'contains relevant information about last deployment' do
35
      last_deployment = json.fetch(:last_deployment)
36 37 38 39

      expect(last_deployment)
        .to include(:ref, :user, :commit, :deployable, :manual_actions)
    end
40 41 42
  end

  context 'when there is a collection of objects provided' do
43
    let(:project) { create(:empty_project) }
44 45
    let(:resource) { create_list(:environment, 2) }

46
    it 'contains important elements of environment' do
47
      expect(json.first)
48
        .to include(:last_deployment, :name, :external_url)
49 50 51
    end

    it 'generates payload for collection' do
52
      expect(json).to be_an_instance_of Array
53 54
    end
  end
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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
  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
      before { create(:environment, name: 'staging') }

      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

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  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
        before { create(:environment) }

        it 'serializes environments' do
          expect(subject.first).to have_key :id
        end
      end

      context 'when multiple environment objects are serialized' do
        before { create_list(:environment, 3) }

        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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

      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
      end
185 186
    end
  end
187
end