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
|
# frozen_string_literal: true
require 'json'
require 'spec_helper_rspec'
require 'webmock/rspec'
shared_examples 'REST API' do |resource_type, create_uri, singleton = false|
unless singleton
describe 'instances' do
context "with no #{resource_type}s" do
it 'returns an empty list' do
stub_request(:get, "http://localhost:9200/_#{resource_type}").
with(headers: { 'Accept' => 'application/json' }).
to_return(
status: 200,
body: '{}'
)
expect(described_class.instances).to eq([])
end
end
end
end
describe "#{resource_type}s" do
if singleton
let(:json) { json1 }
let(:instance) { [example1] }
else
let(:json) do
if json1["#{resource_type}s"].is_a? Array
json1.update(json2) { |key, v1, v2| v1 + v2 if key == "#{resource_type}s" }
else
json1.merge(json2)
end
end
let(:instance) { [example1, example2] }
end
it "returns #{resource_type}s" do
stub_request(:get, "http://localhost:9200/_#{resource_type}").
with(headers: { 'Accept' => 'application/json' }).
to_return(
status: 200,
body: JSON.dump(json)
)
expect(described_class.instances.map do |provider|
provider.instance_variable_get(:@property_hash)
end).to match_array(instance)
end
end
describe 'basic authentication' do
it 'authenticates' do
stub_request(:get, "http://localhost:9200/_#{resource_type}").
with(
basic_auth: %w[elastic password],
headers: { 'Accept' => 'application/json' }
).
to_return(
status: 200,
body: JSON.dump(json1)
)
expect(described_class.api_objects(
'http', 'localhost', '9200', 10, 'elastic', 'password', validate_tls: true
).map do |provider|
described_class.new(
provider
).instance_variable_get(:@property_hash)
end).to contain_exactly(example1)
end
end
describe 'https' do
it 'uses ssl' do
stub_request(:get, "https://localhost:9200/_#{resource_type}").
with(headers: { 'Accept' => 'application/json' }).
to_return(
status: 200,
body: JSON.dump(json1)
)
expect(described_class.api_objects(
'https', 'localhost', '9200', 10, validate_tls: true
).map do |provider|
described_class.new(
provider
).instance_variable_get(:@property_hash)
end).to contain_exactly(example1)
end
end
unless singleton
describe 'flush' do
it "creates #{resource_type}s" do
stub_request(:put, "http://localhost:9200/#{create_uri}").
with(
headers: {
'Accept' => 'application/json',
'Content-Type' => 'application/json'
},
body: bare_resource
)
stub_request(:get, "http://localhost:9200/_#{resource_type}").
with(headers: { 'Accept' => 'application/json' }).
to_return(status: 200, body: '{}')
provider.flush
end
end
end
end
|