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

describe Action::Custom, vcr: true do
  let(:key_id) { 'key-1' }
  let(:secret) { "0a3938d9d95d807e94d937af3a4fbbea" }
  let(:base_api_endpoint) { 'http://localhost:3000/api/v4' }
  let(:json_str_valid) do
    {
      'payload' => {
        'api_endpoints' => %w{fake/info_refs fake/push},
        'data' => {
          'username' => 'user1',
          'primary_repo' => 'http://localhost:3001/user1/repo1.git'
        }
      }
    }.to_json
  end
  let(:json_str_missing_api_endpoints) do
    {
      'payload' => {
        'data' => {
          'username' => 'user1',
          'primary_repo' => 'http://localhost:3001/user1/repo1.git'
        }
      }
    }.to_json
  end
  let(:json_str_empty_api_endpoints) do
    {
      'payload' => {
        'api_endpoints' => [],
        'data' => {
          'username' => 'user1',
          'primary_repo' => 'http://localhost:3001/user1/repo1.git'
        }
      }
    }.to_json
  end
  let(:json_str_missing_data) do
    {
      'payload' => {
        'api_endpoints' => %w{fake/info_refs fake/push}
      }
    }.to_json
  end
  let(:json_str_invalid_api_endpoints) do
    {
      'payload' => {
        'api_endpoints' => %w{fake/info_refs_bad fake/push_bad},
        'data' => {
          'username' => 'user1',
          'primary_repo' => 'http://localhost:3001/user1/repo1.git'
        }
      }
    }.to_json
  end
  let(:json_str_empty) { { '' => {} }.to_json }
  let(:json_str_empty_payload) { { 'payload' => {} }.to_json }

  describe '.create_from_json' do
    it 'creates an instance from a JSON string' do
      described_class.create_from_json(json_str_empty)
    end
  end

  context 'instantiated' do
    describe '#execute' do
      context 'with an empy JSON string' do
        subject { described_class.create_from_json(json_str_empty) }

        it 'returns nil' do
          expect { subject.execute(key_id) }.to raise_error(CustomActionStatus::MissingPayloadError)
        end
      end

      context 'with an empty payload' do
        subject { described_class.create_from_json(json_str_empty_payload) }

        it 'returns nil' do
          expect { subject.execute(key_id) }.to raise_error(CustomActionStatus::MissingPayloadError)
        end
      end

      context 'with api_endpoints defined' do
        before do
          subject.stub(:base_api_endpoint).and_return(base_api_endpoint)
          subject.stub(:secret_token).and_return(secret)
          $stdin.stub(:read).and_return('')
        end

        context 'that are valid' do
          subject { described_class.create_from_json(json_str_valid) }

          it 'HTTP posts data to defined api_endpoints' do
            VCR.use_cassette("custom-action-ok") do
              subject.execute(key_id).should be_instance_of(Net::HTTPCreated)
            end
          end
        end

        context 'that are invalid' do
          context 'where api_endpoints key is missing' do
            subject { described_class.create_from_json(json_str_missing_api_endpoints) }

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

          context 'where api_endpoints is empty' do
            subject { described_class.create_from_json(json_str_empty_api_endpoints) }

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

          context 'where data key is missing' do
            subject { described_class.create_from_json(json_str_missing_data) }

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

          context 'where API endpoints are bad' do
            subject { described_class.create_from_json(json_str_invalid_api_endpoints) }

            it 'HTTP posts data to defined api_endpoints' do
              VCR.use_cassette("custom-action-not-ok") do
                subject.execute(key_id).should be_instance_of(Net::HTTPForbidden)
              end
            end
          end
        end
      end
    end
  end
end