summaryrefslogtreecommitdiff
path: root/spec/services/projects/update_pages_configuration_service_spec.rb
blob: 9f7ebd40df64ec63ef06b8512f6108c53cea0c3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::UpdatePagesConfigurationService do
  let(:service) { described_class.new(project) }

  describe "#execute" do
    subject { service.execute }

    context 'when pages are deployed' do
      let_it_be(:project) do
        create(:project).tap(&:mark_pages_as_deployed)
      end

      let(:file) { Tempfile.new('pages-test') }

      before do
        allow(service).to receive(:pages_config_file).and_return(file.path)
      end

      after do
        file.close
        file.unlink
      end

      context 'when configuration changes' do
        it 'updates the config and reloads the daemon' do
          allow(service).to receive(:update_file).and_call_original

          expect(service).to receive(:update_file).with(file.path, an_instance_of(String))
            .and_call_original
          expect(service).to receive(:reload_daemon).and_call_original

          expect(subject).to include(status: :success)
        end
      end

      context 'when configuration does not change' do
        before do
          # we set the configuration
          service.execute
        end

        it 'does not update the .update file' do
          expect(service).not_to receive(:reload_daemon)

          expect(subject).to include(status: :success)
        end
      end

      context 'when an error occurs' do
        it 'returns an error object' do
          e = StandardError.new("Failure")
          allow(service).to receive(:reload_daemon).and_raise(e)

          expect(subject).to eq(status: :error, message: "Failure", exception: e)
        end
      end
    end

    context 'when pages are not deployed' do
      let_it_be(:project) do
        create(:project).tap(&:mark_pages_as_not_deployed)
      end

      it 'returns successfully' do
        expect(subject).to eq(status: :success)
      end

      it 'does not update the config' do
        expect(service).not_to receive(:update_file)

        subject
      end
    end
  end
end