debian-mirror-gitlab/qa/spec/resource/base_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

328 lines
9.2 KiB
Ruby
Raw Normal View History

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