File: base_spec.rb

package info (click to toggle)
ruby-puppet-forge 5.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,196 kB
  • sloc: ruby: 2,397; makefile: 3
file content (144 lines) | stat: -rw-r--r-- 4,891 bytes parent folder | download
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
require 'spec_helper'

describe PuppetForge::V3::Base do
  context 'connection management' do
    before(:each) do
      PuppetForge::Connection.authorization = nil
      PuppetForge::Connection.proxy = nil
      described_class.conn = nil
    end

    after(:each) do
      PuppetForge::Connection.authorization = nil
      PuppetForge::Connection.proxy = nil
      described_class.conn = nil
    end

    describe 'setting authorization value after a connection is created' do
      it 'should reset connection' do
        old_conn = described_class.conn

        PuppetForge::Connection.authorization = 'post-init auth value'
        new_conn = described_class.conn

        expect(new_conn).to_not eq(old_conn)
        expect(new_conn.headers).to include(:authorization => 'post-init auth value')
      end
    end

    describe 'setting proxy value after a connection is created' do
      it 'should reset connection' do
        old_conn = described_class.conn

        PuppetForge::Connection.proxy = 'http://proxy.example.com:8888'
        new_conn = described_class.conn

        expect(new_conn).to_not eq(old_conn)
        expect(new_conn.proxy).to_not be_nil
        expect(new_conn.proxy.uri.to_s).to eq('http://proxy.example.com:8888')
      end
    end
  end

  describe '::new_collection' do
    it 'should handle responses with no results' do
      response_data = { data: {}, errors: "Something bad happened!" }

      expect(PuppetForge::V3::Base.new_collection(response_data)).to eq([])
    end

    it 'should handle responses with no pagination info' do
      response_data = { data: {}, errors: "Something bad happened!" }

      collection = PuppetForge::V3::Base.new_collection(response_data)

      expect(collection.limit).to eq(20)
      expect(collection.offset).to eq(0)
      expect(collection.total).to eq(0)
    end
  end

  describe 'the host url setting' do
    context 'without a path prefix' do
      before(:each) do
        PuppetForge::V3::Base.lru_cache.clear # We test the cache later, so clear it now
        @orig_host = PuppetForge.host
        PuppetForge.host = 'https://api.example.com'

        # Trigger connection reset
        PuppetForge::V3::Base.conn = PuppetForge::Connection.default_connection
      end

      after(:each) do
        PuppetForge.host = @orig_host

        # Trigger connection reset
        PuppetForge::V3::Base.conn = PuppetForge::Connection.default_connection
      end

      it 'should work' do
        stub_api_for(PuppetForge::V3::Base) do |stubs|
          stub_fixture(stubs, :get, '/v3/bases/puppet')
        end

        base = PuppetForge::V3::Base.find 'puppet'
        expect(base.username).to eq('foo')
      end

      it 'caches responses' do
        stub_api_for(PuppetForge::V3::Base, lru_cache: true) do |stubs|
          stub_fixture(stubs, :get, '/v3/bases/puppet')
        end
        allow(PuppetForge::V3::Base.lru_cache).to receive(:put).and_call_original
        allow(PuppetForge::V3::Base.lru_cache).to receive(:get).and_call_original

        PuppetForge::V3::Base.find 'puppet'
        PuppetForge::V3::Base.find 'puppet'
        PuppetForge::V3::Base.find 'puppet'
        expect(PuppetForge::V3::Base.lru_cache).to have_received(:put).once
        expect(PuppetForge::V3::Base.lru_cache).to have_received(:get).exactly(3).times
      end
    end

    context 'with a path prefix' do
      before(:each) do
        PuppetForge::V3::Base.lru_cache.clear # We test the cache later, so clear it now
        @orig_host = PuppetForge.host
        PuppetForge.host = 'https://api.example.com/uri/prefix'

        # Trigger connection reset
        PuppetForge::V3::Base.conn = PuppetForge::Connection.default_connection
      end

      after(:each) do
        PuppetForge.host = @orig_host

        # Trigger connection reset
        PuppetForge::V3::Base.conn = PuppetForge::Connection.default_connection
      end

      it 'should work' do
        stub_api_for(PuppetForge::V3::Base, PuppetForge.host) do |stubs|
          stub_fixture(stubs, :get, '/uri/prefix/v3/bases/puppet')
        end

        base = PuppetForge::V3::Base.find 'puppet'
        expect(base.username).to eq('bar')
      end

      it 'caches responses' do
        stub_api_for(PuppetForge::V3::Base, PuppetForge.host, lru_cache: true) do |stubs|
          stub_fixture(stubs, :get, '/uri/prefix/v3/bases/puppet')
        end
        allow(PuppetForge::V3::Base.lru_cache).to receive(:put).and_call_original
        allow(PuppetForge::V3::Base.lru_cache).to receive(:get).and_call_original

        PuppetForge::V3::Base.find 'puppet'
        PuppetForge::V3::Base.find 'puppet'
        PuppetForge::V3::Base.find 'puppet'
        expect(PuppetForge::V3::Base.lru_cache).to have_received(:put).once
        expect(PuppetForge::V3::Base.lru_cache).to have_received(:get).exactly(3).times
      end
    end
  end
end