|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +require 'spec_helper' |
| 19 | +require 'webmock/rspec' |
| 20 | + |
| 21 | +describe 'Elasticsearch: Validation' do |
| 22 | + let(:host) { 'http://localhost:9200' } |
| 23 | + let(:verify_request_stub) do |
| 24 | + stub_request(:get, host) |
| 25 | + .to_return(status: status, body: body, headers: headers) |
| 26 | + end |
| 27 | + let(:count_request_stub) do |
| 28 | + stub_request(:post, "#{host}/_count") |
| 29 | + .to_return(status: 200, body: nil, headers: {}) |
| 30 | + end |
| 31 | + let(:status) { 200 } |
| 32 | + let(:body) { {}.to_json } |
| 33 | + let(:client) { Elasticsearch::Client.new } |
| 34 | + let(:headers) do |
| 35 | + { 'content-type' => 'application/json' } |
| 36 | + end |
| 37 | + |
| 38 | + def error_requests_and_expectations(message = Elasticsearch::NOT_ELASTICSEARCH_WARNING) |
| 39 | + expect { client.count }.to raise_error Elasticsearch::UnsupportedProductError, message |
| 40 | + assert_requested :get, host |
| 41 | + assert_not_requested :post, "#{host}/_count" |
| 42 | + expect { client.cluster.health }.to raise_error Elasticsearch::UnsupportedProductError, message |
| 43 | + expect(client.instance_variable_get('@verified')).to be false |
| 44 | + expect { client.cluster.health }.to raise_error Elasticsearch::UnsupportedProductError, message |
| 45 | + end |
| 46 | + |
| 47 | + def valid_requests_and_expectations |
| 48 | + expect(client.instance_variable_get('@verified')).to be false |
| 49 | + assert_not_requested :get, host |
| 50 | + |
| 51 | + client.count |
| 52 | + expect(client.instance_variable_get('@verified')) |
| 53 | + assert_requested :get, host |
| 54 | + assert_requested :post, "#{host}/_count" |
| 55 | + end |
| 56 | + |
| 57 | + context 'When Elasticsearch replies with status 401' do |
| 58 | + let(:status) { 401 } |
| 59 | + let(:body) { {}.to_json } |
| 60 | + |
| 61 | + it 'Verifies the request but shows a warning' do |
| 62 | + stderr = $stderr |
| 63 | + fake_stderr = StringIO.new |
| 64 | + $stderr = fake_stderr |
| 65 | + |
| 66 | + verify_request_stub |
| 67 | + count_request_stub |
| 68 | + |
| 69 | + valid_requests_and_expectations |
| 70 | + |
| 71 | + fake_stderr.rewind |
| 72 | + expect(fake_stderr.string).to eq("#{Elasticsearch::SECURITY_PRIVILEGES_VALIDATION_WARNING}\n") |
| 73 | + ensure |
| 74 | + $stderr = stderr |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + context 'When Elasticsearch replies with status 403' do |
| 79 | + let(:status) { 403 } |
| 80 | + let(:body) { {}.to_json } |
| 81 | + |
| 82 | + it 'Verifies the request but shows a warning' do |
| 83 | + stderr = $stderr |
| 84 | + fake_stderr = StringIO.new |
| 85 | + $stderr = fake_stderr |
| 86 | + |
| 87 | + verify_request_stub |
| 88 | + count_request_stub |
| 89 | + |
| 90 | + valid_requests_and_expectations |
| 91 | + |
| 92 | + fake_stderr.rewind |
| 93 | + expect(fake_stderr.string).to eq("#{Elasticsearch::SECURITY_PRIVILEGES_VALIDATION_WARNING}\n") |
| 94 | + ensure |
| 95 | + $stderr = stderr |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context 'When the Elasticsearch version is >= 8.0.0' do |
| 100 | + context 'With a valid Elasticsearch response' do |
| 101 | + let(:body) { { 'version' => { 'number' => '8.0.0' } }.to_json } |
| 102 | + let(:headers) do |
| 103 | + { |
| 104 | + 'X-Elastic-Product' => 'Elasticsearch', |
| 105 | + 'content-type' => 'json' |
| 106 | + } |
| 107 | + end |
| 108 | + |
| 109 | + it 'Makes requests and passes validation' do |
| 110 | + verify_request_stub |
| 111 | + count_request_stub |
| 112 | + |
| 113 | + valid_requests_and_expectations |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context 'When the header is not present' do |
| 118 | + it 'Fails validation' do |
| 119 | + verify_request_stub |
| 120 | + |
| 121 | + expect(client.instance_variable_get('@verified')).to be false |
| 122 | + assert_not_requested :get, host |
| 123 | + |
| 124 | + error_requests_and_expectations |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + context 'When the Elasticsearch version is >= 8.1.0' do |
| 130 | + context 'With a valid Elasticsearch response' do |
| 131 | + let(:body) { { 'version' => { 'number' => '8.1.0' } }.to_json } |
| 132 | + let(:headers) do |
| 133 | + { |
| 134 | + 'X-Elastic-Product' => 'Elasticsearch', |
| 135 | + 'content-type' => 'json' |
| 136 | + } |
| 137 | + end |
| 138 | + |
| 139 | + it 'Makes requests and passes validation' do |
| 140 | + verify_request_stub |
| 141 | + count_request_stub |
| 142 | + |
| 143 | + valid_requests_and_expectations |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + context 'When the header is not present' do |
| 148 | + it 'Fails validation' do |
| 149 | + verify_request_stub |
| 150 | + |
| 151 | + expect(client.instance_variable_get('@verified')).to be false |
| 152 | + assert_not_requested :get, host |
| 153 | + |
| 154 | + error_requests_and_expectations |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + |
| 160 | + context 'When the Elasticsearch version is 8.0.0.pre' do |
| 161 | + context 'With a valid Elasticsearch response' do |
| 162 | + let(:body) { { 'version' => { 'number' => '8.0.0.pre' } }.to_json } |
| 163 | + let(:headers) do |
| 164 | + { |
| 165 | + 'X-Elastic-Product' => 'Elasticsearch', |
| 166 | + 'content-type' => 'json' |
| 167 | + } |
| 168 | + end |
| 169 | + |
| 170 | + it 'Makes requests and passes validation' do |
| 171 | + verify_request_stub |
| 172 | + count_request_stub |
| 173 | + |
| 174 | + valid_requests_and_expectations |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + context 'When the header is not present' do |
| 179 | + it 'Fails validation' do |
| 180 | + verify_request_stub |
| 181 | + |
| 182 | + expect(client.instance_variable_get('@verified')).to be false |
| 183 | + assert_not_requested :get, host |
| 184 | + |
| 185 | + error_requests_and_expectations |
| 186 | + end |
| 187 | + end |
| 188 | + end |
| 189 | + |
| 190 | + context 'When the version is 8.0.0-SNAPSHOT' do |
| 191 | + let(:body) { { 'version' => { 'number' => '8.0.0-SNAPSHOT' } }.to_json } |
| 192 | + |
| 193 | + context 'When the header is not present' do |
| 194 | + it 'Fails validation' do |
| 195 | + verify_request_stub |
| 196 | + count_request_stub |
| 197 | + |
| 198 | + error_requests_and_expectations |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + context 'With a valid Elasticsearch response' do |
| 203 | + let(:headers) do |
| 204 | + { |
| 205 | + 'X-Elastic-Product' => 'Elasticsearch', |
| 206 | + 'content-type' => 'json' |
| 207 | + } |
| 208 | + end |
| 209 | + |
| 210 | + it 'Makes requests and passes validation' do |
| 211 | + verify_request_stub |
| 212 | + count_request_stub |
| 213 | + |
| 214 | + valid_requests_and_expectations |
| 215 | + end |
| 216 | + end |
| 217 | + end |
| 218 | + |
| 219 | + context 'When Elasticsearch version is < 8.0.0' do |
| 220 | + let(:body) { { 'version' => { 'number' => '7.16.0' } }.to_json } |
| 221 | + |
| 222 | + it 'Raises an exception and client doesnae work' do |
| 223 | + verify_request_stub |
| 224 | + error_requests_and_expectations |
| 225 | + end |
| 226 | + end |
| 227 | + |
| 228 | + context 'When there is no version data' do |
| 229 | + let(:body) { {}.to_json } |
| 230 | + it 'Raises an exception and client doesnae work' do |
| 231 | + verify_request_stub |
| 232 | + error_requests_and_expectations |
| 233 | + end |
| 234 | + end |
| 235 | + |
| 236 | + context 'When doing a yaml content-type request' do |
| 237 | + let(:client) do |
| 238 | + Elasticsearch::Client.new(transport_options: {headers: { accept: 'application/yaml', content_type: 'application/yaml' }}) |
| 239 | + end |
| 240 | + |
| 241 | + let(:headers) { { 'content-type' => 'application/yaml', 'X-Elastic-Product' => 'Elasticsearch' } } |
| 242 | + let(:body) { "---\nversion:\n number: \"8.0.0-SNAPSHOT\"\n" } |
| 243 | + |
| 244 | + it 'validates' do |
| 245 | + verify_request_stub |
| 246 | + count_request_stub |
| 247 | + valid_requests_and_expectations |
| 248 | + end |
| 249 | + end |
| 250 | +end |
0 commit comments