summaryrefslogtreecommitdiff
path: root/spec/action/custom_spec.rb
blob: 2ef7977bf8987c8419a3d463c2d16a0ec3c1647b (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
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
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
require_relative '../spec_helper'
require_relative '../../lib/action/custom'

describe Action::Custom do
  let(:repo_name) { 'gitlab-ci.git' }
  let(:gl_id) { 'key-1' }
  let(:secret) { "0a3938d9d95d807e94d937af3a4fbbea" }
  let(:base_url) { 'http://localhost:3000' }

  subject { described_class.new(gl_id, payload) }

  describe '#execute' do
    context 'with an empty payload' do
      let(:payload) { {} }

      it 'raises a MissingPayloadError exception' do
        expect { subject.execute }.to raise_error(Action::Custom::MissingPayloadError)
      end
    end

    context 'with api_endpoints defined' do
      before do
        allow(subject).to receive(:base_url).and_return(base_url)
        allow(subject).to receive(:secret_token).and_return(secret)
        allow($stdin).to receive(:read).and_return('')
      end

      context 'that are valid' do
        let(:payload) do
          {
            'action' => 'geo_proxy_to_primary',
            'data' => {
              'api_endpoints' => %w{/api/v4/fake/info_refs /api/v4/fake/push},
              'primary_repo' => 'http://localhost:3001/user1/repo1.git'
            }
          }
        end

        context 'and responds correctly' do
          it 'prints a Base64 encoded result to $stdout' do
            VCR.use_cassette("custom-action-ok") do
              expect($stdout).to receive(:print).with('info_refs-result').ordered
              expect($stdout).to receive(:print).with('push-result').ordered
              subject.execute
            end
          end

          context 'with results printed to $stdout' do
            before do
              allow($stdout).to receive(:print).with('info_refs-result')
              allow($stdout).to receive(:print).with('push-result')
            end

            it 'returns an instance of Net::HTTPCreated' do
              VCR.use_cassette("custom-action-ok") do
                expect(subject.execute).to be_instance_of(Net::HTTPCreated)
              end
            end

            context 'and with an information message provided' do
              before do
                payload['data']['info_message'] = 'Important message here.'
              end

              it 'prints said informational message to $stderr' do
                VCR.use_cassette("custom-action-ok-with-message") do
                  expect { subject.execute }.to output(/Important message here./).to_stderr
                end
              end
            end
          end
        end

        context 'but responds incorrectly' do
          it 'raises an UnsuccessfulError exception' do
            VCR.use_cassette("custom-action-ok-not-json") do
              expect do
                subject.execute
              end.to raise_error(Action::Custom::UnsuccessfulError, 'Response was not valid JSON')
            end
          end
        end
      end

      context 'that are invalid' do
        context 'where api_endpoints gl_id is missing' do
          let(:payload) do
            {
              'action' => 'geo_proxy_to_primary',
              'data' => {
                'primary_repo' => 'http://localhost:3001/user1/repo1.git'
              }
            }
          end

          it 'raises a MissingAPIEndpointsError exception' do
            expect { subject.execute }.to raise_error(Action::Custom::MissingAPIEndpointsError)
          end
        end

        context 'where api_endpoints are empty' do
          let(:payload) do
            {
              'action' => 'geo_proxy_to_primary',
              'data' => {
                'api_endpoints' => [],
                'primary_repo' => 'http://localhost:3001/user1/repo1.git'
              }
            }
          end

          it 'raises a MissingAPIEndpointsError exception' do
            expect { subject.execute }.to raise_error(Action::Custom::MissingAPIEndpointsError)
          end
        end

        context 'where data gl_id is missing' do
          let(:payload) { { 'api_endpoints' => %w{/api/v4/fake/info_refs /api/v4/fake/push} } }

          it 'raises a MissingDataError exception' do
            expect { subject.execute }.to raise_error(Action::Custom::MissingDataError)
          end
        end

        context 'where API endpoints are bad' do
          let(:payload) do
            {
              'action' => 'geo_proxy_to_primary',
              'data' => {
                'api_endpoints' => %w{/api/v4/fake/info_refs_bad /api/v4/fake/push_bad},
                'primary_repo' => 'http://localhost:3001/user1/repo1.git'
              }
            }
          end

          context 'and response is JSON' do
            it 'raises an UnsuccessfulError exception' do
              VCR.use_cassette("custom-action-not-ok-json") do
                expect do
                  subject.execute
                end.to raise_error(Action::Custom::UnsuccessfulError, '> GitLab: You cannot perform write operations on a read-only instance (403)')
              end
            end
          end

          context 'and response is not JSON' do
            it 'raises an UnsuccessfulError exception' do
              VCR.use_cassette("custom-action-not-ok-not-json") do
                expect do
                  subject.execute
                end.to raise_error(Action::Custom::UnsuccessfulError, '> GitLab: No message (403)')
              end
            end
          end
        end
      end
    end
  end
end