Update puppetlabs/stdlib module
[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     end
12
13     context 'when a non-existing file is specified' do
14       let(:filename) do
15         if Puppet::Util::Platform.windows?
16           'C:/tmp/doesnotexist'
17         else
18           '/tmp/doesnotexist'
19         end
20       end
21
22       before(:each) do
23         allow(File).to receive(:exists?).with(filename).and_return(false).once
24         allow(PSON).to receive(:load).never
25       end
26       it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
27       it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
28       it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') }
29     end
30
31     context 'when an existing file is specified' do
32       let(:filename) do
33         if Puppet::Util::Platform.windows?
34           'C:/tmp/doesexist'
35         else
36           '/tmp/doesexist'
37         end
38       end
39       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
40       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
41
42       before(:each) do
43         allow(File).to receive(:exists?).with(filename).and_return(true).once
44         allow(File).to receive(:read).with(filename).and_return(json).once
45         allow(File).to receive(:read).with(filename).and_return(json).once
46         allow(PSON).to receive(:load).with(json).and_return(data).once
47       end
48       it { is_expected.to run.with_params(filename).and_return(data) }
49     end
50
51     context 'when the file could not be parsed' do
52       let(:filename) do
53         if Puppet::Util::Platform.windows?
54           'C:/tmp/doesexist'
55         else
56           '/tmp/doesexist'
57         end
58       end
59       let(:json) { '{"key":"value"}' }
60
61       before(:each) do
62         allow(File).to receive(:exists?).with(filename).and_return(true).once
63         allow(File).to receive(:read).with(filename).and_return(json).once
64         allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
65       end
66       it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
67     end
68
69     context 'when an existing URL is specified' do
70       let(:filename) do
71         'https://example.local/myhash.json'
72       end
73       let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
74       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
75       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
76
77       it {
78         expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
79         expect(PSON).to receive(:load).with(json).and_return(data).once
80         is_expected.to run.with_params(filename).and_return(data)
81       }
82     end
83
84     context 'when an existing URL (with username and password) is specified' do
85       let(:filename) do
86         'https://user1:pass1@example.local/myhash.json'
87       end
88       let(:url_no_auth) { 'https://example.local/myhash.json' }
89       let(:basic_auth) { { :http_basic_authentication => ['user1', 'pass1'] } }
90       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
91       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
92
93       it {
94         expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
95         expect(PSON).to receive(:load).with(json).and_return(data).once
96         is_expected.to run.with_params(filename).and_return(data)
97       }
98     end
99
100     context 'when an existing URL (with username) is specified' do
101       let(:filename) do
102         'https://user1@example.local/myhash.json'
103       end
104       let(:url_no_auth) { 'https://example.local/myhash.json' }
105       let(:basic_auth) { { :http_basic_authentication => ['user1', ''] } }
106       let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
107       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
108
109       it {
110         expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
111         expect(PSON).to receive(:load).with(json).and_return(data).once
112         is_expected.to run.with_params(filename).and_return(data)
113       }
114     end
115
116     context 'when the URL output could not be parsed, with default specified' do
117       let(:filename) do
118         'https://example.local/myhash.json'
119       end
120       let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
121       let(:json) { ',;{"key":"value"}' }
122
123       it {
124         expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
125         expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
126         is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
127       }
128     end
129
130     context 'when the URL does not exist, with default specified' do
131       let(:filename) do
132         'https://example.local/myhash.json'
133       end
134       let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
135
136       it {
137         expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found'
138         is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
139       }
140     end
141   end
142 end