2018-10-17 14:08:20 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-09-24 23:09:30 -04:00
|
|
|
RSpec.describe QA::Resource::Base do
|
2021-08-25 11:11:14 -04:00
|
|
|
include QA::Support::Helpers::StubEnv
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
let(:resource) { spy('resource') }
|
2022-04-14 02:08:29 -04:00
|
|
|
let(:api_client) { instance_double('Runtime::API::Client') }
|
2018-10-30 22:44:47 -04:00
|
|
|
let(:location) { 'http://location' }
|
2021-12-06 13:14:09 -05:00
|
|
|
let(:log_regex) { %r{==> Built a MyResource with username 'qa' via #{method} in [\d.\-e]+ seconds+} }
|
2018-01-11 06:00:22 -05:00
|
|
|
|
2022-01-27 13:14:37 -05:00
|
|
|
before do
|
|
|
|
allow(QA::Tools::TestResourceDataProcessor).to receive(:collect)
|
|
|
|
allow(QA::Tools::TestResourceDataProcessor).to receive(:write_to_file)
|
|
|
|
end
|
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
shared_context 'with simple resource' do
|
|
|
|
subject do
|
|
|
|
Class.new(QA::Resource::Base) do
|
|
|
|
def self.name
|
|
|
|
'MyResource'
|
|
|
|
end
|
|
|
|
|
|
|
|
attribute :test do
|
|
|
|
'block'
|
|
|
|
end
|
|
|
|
|
2022-09-01 20:11:46 -04:00
|
|
|
attribute :token do
|
|
|
|
'token_value'
|
|
|
|
end
|
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
attribute :username do
|
|
|
|
'qa'
|
|
|
|
end
|
|
|
|
|
|
|
|
attribute :no_block
|
|
|
|
|
|
|
|
def fabricate!(*args)
|
|
|
|
'any'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.current_url
|
|
|
|
'http://stub'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:resource) { subject.new }
|
|
|
|
end
|
|
|
|
|
2021-07-27 11:09:49 -04:00
|
|
|
shared_context 'with fabrication context' do
|
2018-10-17 14:08:20 -04:00
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
def self.name
|
2018-11-02 05:32:28 -04:00
|
|
|
'MyResource'
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
|
|
|
before do
|
2018-10-30 22:44:47 -04:00
|
|
|
allow(subject).to receive(:current_url).and_return(location)
|
2018-11-02 05:32:28 -04:00
|
|
|
allow(subject).to receive(:new).and_return(resource)
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-17 14:08:20 -04:00
|
|
|
shared_examples 'fabrication method' do |fabrication_method_called, actual_fabrication_method = nil|
|
|
|
|
let(:fabrication_method_used) { actual_fabrication_method || fabrication_method_called }
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-11-02 05:32:28 -04:00
|
|
|
it 'yields resource before calling resource method' do
|
|
|
|
expect(resource).to receive(:something!).ordered
|
|
|
|
expect(resource).to receive(fabrication_method_used).ordered.and_return(location)
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2021-07-27 11:09:49 -04:00
|
|
|
subject.public_send(fabrication_method_called, resource: resource, &:something!)
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.fabricate!' do
|
2018-11-02 05:32:28 -04:00
|
|
|
context 'when resource does not support fabrication via the API' do
|
2018-10-17 14:08:20 -04:00
|
|
|
before do
|
2021-07-27 11:09:49 -04:00
|
|
|
allow(described_class).to receive(:fabricate_via_api!).and_raise(NotImplementedError)
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-17 14:08:20 -04:00
|
|
|
it 'calls .fabricate_via_browser_ui!' do
|
|
|
|
expect(described_class).to receive(:fabricate_via_browser_ui!)
|
|
|
|
|
|
|
|
described_class.fabricate!
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
|
|
|
|
2018-11-02 05:32:28 -04:00
|
|
|
context 'when resource supports fabrication via the API' do
|
2018-10-17 14:08:20 -04:00
|
|
|
it 'calls .fabricate_via_browser_ui!' do
|
|
|
|
expect(described_class).to receive(:fabricate_via_api!)
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-17 14:08:20 -04:00
|
|
|
described_class.fabricate!
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.fabricate_via_api!' do
|
2022-02-15 22:18:24 -05:00
|
|
|
context 'when fabricating' do
|
|
|
|
include_context 'with fabrication context'
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
it_behaves_like 'fabrication method', :fabricate_via_api!
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
it 'instantiates the resource, calls resource method returns the resource' do
|
|
|
|
expect(resource).to receive(:fabricate_via_api!).and_return(location)
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
result = subject.fabricate_via_api!(resource: resource, parents: [])
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
expect(result).to eq(resource)
|
|
|
|
end
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
|
|
|
|
2021-07-27 11:09:49 -04:00
|
|
|
context "with debug log level" do
|
2022-02-15 22:18:24 -05:00
|
|
|
include_context 'with simple resource'
|
|
|
|
|
2021-12-06 13:14:09 -05:00
|
|
|
let(:method) { 'api' }
|
|
|
|
|
2021-07-27 11:09:49 -04:00
|
|
|
before do
|
2022-06-02 20:09:04 -04:00
|
|
|
allow(QA::Runtime::Logger).to receive(:info)
|
2022-02-15 22:18:24 -05:00
|
|
|
allow(resource).to receive(:api_support?).and_return(true)
|
|
|
|
allow(resource).to receive(:fabricate_via_api!)
|
2022-04-14 02:08:29 -04:00
|
|
|
allow(resource).to receive(:api_client) { api_client }
|
2021-07-27 11:09:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the resource and build method' do
|
|
|
|
subject.fabricate_via_api!('something', resource: resource, parents: [])
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-06-02 20:09:04 -04:00
|
|
|
expect(QA::Runtime::Logger).to have_received(:info) do |&msg|
|
2021-12-06 13:14:09 -05:00
|
|
|
expect(msg.call).to match_regex(log_regex)
|
2021-07-27 11:09:49 -04:00
|
|
|
end
|
|
|
|
end
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.fabricate_via_browser_ui!' do
|
2022-02-15 22:18:24 -05:00
|
|
|
context 'when fabricating' do
|
|
|
|
include_context 'with fabrication context'
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
it_behaves_like 'fabrication method', :fabricate_via_browser_ui!, :fabricate!
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
it 'instantiates the resource and calls resource method' do
|
|
|
|
subject.fabricate_via_browser_ui!('something', resource: resource, parents: [])
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
expect(resource).to have_received(:fabricate!).with('something')
|
|
|
|
end
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
it 'returns fabrication resource' do
|
|
|
|
result = subject.fabricate_via_browser_ui!('something', resource: resource, parents: [])
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-02-15 22:18:24 -05:00
|
|
|
expect(result).to eq(resource)
|
|
|
|
end
|
2018-10-17 14:08:20 -04:00
|
|
|
end
|
|
|
|
|
2021-07-27 11:09:49 -04:00
|
|
|
context "with debug log level" do
|
2022-02-15 22:18:24 -05:00
|
|
|
include_context 'with simple resource'
|
|
|
|
|
2021-12-06 13:14:09 -05:00
|
|
|
let(:method) { 'browser_ui' }
|
|
|
|
|
2021-07-27 11:09:49 -04:00
|
|
|
before do
|
2022-06-02 20:09:04 -04:00
|
|
|
allow(QA::Runtime::Logger).to receive(:info)
|
2021-07-27 11:09:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the resource and build method' do
|
|
|
|
subject.fabricate_via_browser_ui!('something', resource: resource, parents: [])
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2022-06-02 20:09:04 -04:00
|
|
|
expect(QA::Runtime::Logger).to have_received(:info) do |&msg|
|
2021-12-06 13:14:09 -05:00
|
|
|
expect(msg.call).to match_regex(log_regex)
|
2021-07-27 11:09:49 -04:00
|
|
|
end
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-25 16:29:24 -04:00
|
|
|
describe '.attribute' do
|
2021-07-27 11:09:49 -04:00
|
|
|
include_context 'with simple resource'
|
2018-10-25 16:29:24 -04:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
context 'when the attribute is populated via a block' do
|
|
|
|
it 'returns value from the block' do
|
2018-11-02 05:32:28 -04:00
|
|
|
result = subject.fabricate!(resource: resource)
|
2018-10-25 16:29:24 -04:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
expect(result).to be_a(described_class)
|
2018-10-25 16:29:24 -04:00
|
|
|
expect(result.test).to eq('block')
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
context 'when the attribute is populated via the api' do
|
2018-10-25 16:29:24 -04:00
|
|
|
let(:api_resource) { { no_block: 'api' } }
|
|
|
|
|
|
|
|
before do
|
2021-07-27 11:09:49 -04:00
|
|
|
allow(resource).to receive(:api_resource).and_return(api_resource)
|
2018-10-25 16:29:24 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
it 'returns value from api' do
|
2018-11-02 05:32:28 -04:00
|
|
|
result = subject.fabricate!(resource: resource)
|
2018-10-25 16:29:24 -04:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
expect(result).to be_a(described_class)
|
2018-10-25 16:29:24 -04:00
|
|
|
expect(result.no_block).to eq('api')
|
|
|
|
end
|
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
context 'when the attribute also has a block' do
|
2018-10-25 16:29:24 -04:00
|
|
|
let(:api_resource) { { test: 'api_with_block' } }
|
|
|
|
|
|
|
|
before do
|
2021-08-11 02:10:02 -04:00
|
|
|
allow(QA::Runtime::Logger).to receive(:debug)
|
2018-10-25 16:29:24 -04:00
|
|
|
end
|
|
|
|
|
2021-08-11 02:10:02 -04:00
|
|
|
it 'returns value from api and emits an debug log entry' do
|
2018-11-02 05:32:28 -04:00
|
|
|
result = subject.fabricate!(resource: resource)
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
expect(result).to be_a(described_class)
|
2018-10-25 16:29:24 -04:00
|
|
|
expect(result.test).to eq('api_with_block')
|
|
|
|
expect(QA::Runtime::Logger)
|
2021-08-11 02:10:02 -04:00
|
|
|
.to have_received(:debug).with(/api_with_block/)
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
2022-09-01 20:11:46 -04:00
|
|
|
|
|
|
|
context 'when the attribute is token and has a block' do
|
|
|
|
let(:api_resource) { { token: 'another_token_value' } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(QA::Runtime::Logger).to receive(:debug)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'emits a masked debug log entry' do
|
|
|
|
result = subject.fabricate!(resource: resource)
|
|
|
|
|
|
|
|
expect(result).to be_a(described_class)
|
|
|
|
expect(result.token).to eq('another_token_value')
|
|
|
|
|
|
|
|
expect(QA::Runtime::Logger)
|
|
|
|
.to have_received(:debug).with(/MASKED/)
|
|
|
|
end
|
|
|
|
end
|
2018-10-25 16:29:24 -04:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
context 'when the attribute is populated via direct assignment' do
|
2018-01-11 04:58:56 -05:00
|
|
|
before do
|
2018-11-02 05:32:28 -04:00
|
|
|
resource.test = 'value'
|
2018-10-25 16:29:24 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
it 'returns value from the assignment' do
|
2018-11-02 05:32:28 -04:00
|
|
|
result = subject.fabricate!(resource: resource)
|
2018-10-25 16:29:24 -04:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
expect(result).to be_a(described_class)
|
2018-10-25 16:29:24 -04:00
|
|
|
expect(result.test).to eq('value')
|
|
|
|
end
|
2018-01-11 04:58:56 -05:00
|
|
|
|
2018-10-25 16:29:24 -04:00
|
|
|
context 'when the api also has such response' do
|
|
|
|
before do
|
2018-11-02 05:32:28 -04:00
|
|
|
allow(resource).to receive(:api_resource).and_return({ test: 'api' })
|
2018-10-25 16:29:24 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
it 'returns value from the assignment' do
|
2018-11-02 05:32:28 -04:00
|
|
|
result = subject.fabricate!(resource: resource)
|
2018-10-25 16:29:24 -04:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
expect(result).to be_a(described_class)
|
2018-10-25 16:29:24 -04:00
|
|
|
expect(result.test).to eq('value')
|
|
|
|
end
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
2018-10-25 16:29:24 -04:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-30 22:44:47 -04:00
|
|
|
context 'when the attribute has no value' do
|
2018-10-25 16:29:24 -04:00
|
|
|
it 'raises an error because no values could be found' do
|
2018-11-02 05:32:28 -04:00
|
|
|
result = subject.fabricate!(resource: resource)
|
2018-01-11 04:58:56 -05:00
|
|
|
|
2021-07-27 11:09:49 -04:00
|
|
|
expect { result.no_block }.to raise_error(
|
|
|
|
described_class::NoValueError, "No value was computed for no_block of #{resource.class.name}."
|
|
|
|
)
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
2019-01-15 15:18:17 -05:00
|
|
|
|
|
|
|
context 'when multiple resources have the same attribute name' do
|
|
|
|
let(:base) do
|
|
|
|
Class.new(QA::Resource::Base) do
|
|
|
|
def fabricate!
|
|
|
|
'any'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.current_url
|
|
|
|
'http://stub'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-06 14:09:41 -04:00
|
|
|
|
2019-01-15 15:18:17 -05:00
|
|
|
let(:first_resource) do
|
|
|
|
Class.new(base) do
|
|
|
|
attribute :test do
|
|
|
|
'first block'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-06 14:09:41 -04:00
|
|
|
|
2019-01-15 15:18:17 -05:00
|
|
|
let(:second_resource) do
|
|
|
|
Class.new(base) do
|
|
|
|
attribute :test do
|
|
|
|
'second block'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has unique attribute values' do
|
|
|
|
first_result = first_resource.fabricate!(resource: first_resource.new)
|
|
|
|
second_result = second_resource.fabricate!(resource: second_resource.new)
|
|
|
|
|
|
|
|
expect(first_result.test).to eq 'first block'
|
|
|
|
expect(second_result.test).to eq 'second block'
|
|
|
|
end
|
|
|
|
end
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-25 16:29:24 -04:00
|
|
|
describe '#web_url' do
|
2021-07-27 11:09:49 -04:00
|
|
|
include_context 'with simple resource'
|
2018-10-17 14:08:20 -04:00
|
|
|
|
2018-10-25 16:29:24 -04:00
|
|
|
it 'sets #web_url to #current_url after fabrication' do
|
2018-11-02 05:32:28 -04:00
|
|
|
subject.fabricate!(resource: resource)
|
2018-01-26 14:25:01 -05:00
|
|
|
|
2018-11-02 05:32:28 -04:00
|
|
|
expect(resource.web_url).to eq(subject.current_url)
|
2018-10-25 16:29:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#visit!' do
|
2021-07-27 11:09:49 -04:00
|
|
|
include_context 'with simple resource'
|
2018-10-25 16:29:24 -04:00
|
|
|
|
2022-01-14 10:16:52 -05:00
|
|
|
let(:wait_for_requests_class) { QA::Support::WaitForRequests }
|
|
|
|
|
2018-10-25 16:29:24 -04:00
|
|
|
before do
|
2018-11-02 05:32:28 -04:00
|
|
|
allow(resource).to receive(:visit)
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-10-25 16:29:24 -04:00
|
|
|
it 'calls #visit with the underlying #web_url' do
|
2019-11-08 01:06:24 -05:00
|
|
|
allow(resource).to receive(:current_url).and_return(subject.current_url)
|
2022-01-14 10:16:52 -05:00
|
|
|
expect(wait_for_requests_class).to receive(:wait_for_requests).with({ skip_resp_code_check: false }).twice
|
2019-11-08 01:06:24 -05:00
|
|
|
|
2018-11-02 05:32:28 -04:00
|
|
|
resource.web_url = subject.current_url
|
|
|
|
resource.visit!
|
2018-10-25 16:29:24 -04:00
|
|
|
|
2018-11-02 05:32:28 -04:00
|
|
|
expect(resource).to have_received(:visit).with(subject.current_url)
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
2022-01-14 10:16:52 -05:00
|
|
|
|
|
|
|
it 'calls #visit with the underlying #web_url with skip_resp_code_check specified as true' do
|
|
|
|
allow(resource).to receive(:current_url).and_return(subject.current_url)
|
|
|
|
expect(wait_for_requests_class).to receive(:wait_for_requests).with({ skip_resp_code_check: true }).twice
|
|
|
|
|
|
|
|
resource.web_url = subject.current_url
|
|
|
|
resource.visit!(skip_resp_code_check: true)
|
|
|
|
|
|
|
|
expect(resource).to have_received(:visit).with(subject.current_url)
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
|
|
|
end
|