Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / loadjson_spec.rb
1 require 'spec_helper'
2
3 describe 'loadjson' do
4   it { is_expected.not_to eq(nil) }
5   it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
6
7   describe 'when calling with valid arguments' do
8     before :each do
9       allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"name": "puppetlabs-stdlib"}')
10       allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}')
11       # Additional modules used by litmus which are identified while running these dues to being in fixtures
12       allow(File).to receive(:read).with(%r{\/(provision|puppet_agent|facts)\/metadata.json}, :encoding => 'utf-8')
13     end
14
15     context 'when a non-existing file is specified' do
16       let(:filename) do
17         if Puppet::Util::Platform.windows?
18           'C:/tmp/doesnotexist'
19         else
20           '/tmp/doesnotexist'
21         end
22       end
23
24       before(:each) do
25         allow(File).to receive(:exists?).with(filename).and_return(false).once
26         allow(PSON).to receive(:load).never
27       end
28       it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
29       it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
30       it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') }
31     end
32
33     context 'when an existing file is specified' do
34       let(:filename) do
35         if Puppet::Util::Platform.windows?
36           'C:/tmp/doesexist'
37         else
38           '/tmp/doesexist'
39         end
40       end
41       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
42       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
43
44       before(:each) do
45         allow(File).to receive(:exists?).with(filename).and_return(true).once
46         allow(File).to receive(:read).with(filename).and_return(json).once
47         allow(File).to receive(:read).with(filename).and_return(json).once
48         allow(PSON).to receive(:load).with(json).and_return(data).once
49       end
50       it { is_expected.to run.with_params(filename).and_return(data) }
51     end
52
53     context 'when the file could not be parsed' do
54       let(:filename) do
55         if Puppet::Util::Platform.windows?
56           'C:/tmp/doesexist'
57         else
58           '/tmp/doesexist'
59         end
60       end
61       let(:json) { '{"key":"value"}' }
62
63       before(:each) do
64         allow(File).to receive(:exists?).with(filename).and_return(true).once
65         allow(File).to receive(:read).with(filename).and_return(json).once
66         allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
67       end
68       it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
69     end
70
71     context 'when an existing URL is specified' do
72       let(:filename) do
73         'https://example.local/myhash.json'
74       end
75       let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
76       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
77       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
78
79       it {
80         expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
81         expect(PSON).to receive(:load).with(json).and_return(data).once
82         is_expected.to run.with_params(filename).and_return(data)
83       }
84     end
85
86     context 'when an existing URL (with username and password) is specified' do
87       let(:filename) do
88         'https://user1:pass1@example.local/myhash.json'
89       end
90       let(:url_no_auth) { 'https://example.local/myhash.json' }
91       let(:basic_auth) { { :http_basic_authentication => ['user1', 'pass1'] } }
92       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
93       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
94
95       it {
96         expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
97         expect(PSON).to receive(:load).with(json).and_return(data).once
98         is_expected.to run.with_params(filename).and_return(data)
99       }
100     end
101
102     context 'when an existing URL (with username) is specified' do
103       let(:filename) do
104         'https://user1@example.local/myhash.json'
105       end
106       let(:url_no_auth) { 'https://example.local/myhash.json' }
107       let(:basic_auth) { { :http_basic_authentication => ['user1', ''] } }
108       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
109       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
110
111       it {
112         expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
113         expect(PSON).to receive(:load).with(json).and_return(data).once
114         is_expected.to run.with_params(filename).and_return(data)
115       }
116     end
117
118     context 'when the URL output could not be parsed, with default specified' do
119       let(:filename) do
120         'https://example.local/myhash.json'
121       end
122       let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
123       let(:json) { ',;{"key":"value"}' }
124
125       it {
126         expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
127         expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
128         is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
129       }
130     end
131
132     context 'when the URL does not exist, with default specified' do
133       let(:filename) do
134         'https://example.local/myhash.json'
135       end
136       let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
137
138       it {
139         expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found'
140         is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
141       }
142     end
143   end
144 end