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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# frozen_string_literal: true
RSpec.describe QA::Resource::Base do
include QA::Support::Helpers::StubEnv
let(:resource) { spy('resource') }
let(:api_client) { instance_double('Runtime::API::Client') }
let(:location) { 'http://location' }
let(:log_regex) { %r{==> Built a MyResource with username 'qa' via #{method} in [\d.\-e]+ seconds+} }
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 :token do
'token_value'
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
shared_context 'with fabrication context' do
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)
subject.public_send(fabrication_method_called, resource: resource, &:something!)
end
end
describe '.fabricate!' do
context 'when resource does not support fabrication via the API' do
before do
allow(described_class).to receive(:fabricate_via_api!).and_raise(NotImplementedError)
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_api!!' do
expect(described_class).to receive(:fabricate_via_api!)
described_class.fabricate!
end
end
context 'when FIPS mode is enabled' do
before do
stub_env('FIPS', '1')
end
it 'calls .fabricate_via_browser_ui!' do
expect(described_class).to receive(:fabricate_via_browser_ui!)
described_class.fabricate!
end
end
end
describe '.fabricate_via_api_unless_fips!' do
context 'when FIPS mode is not enabled' do
it 'calls .fabricate_via_api!!' do
expect(described_class).to receive(:fabricate_via_api!)
described_class.fabricate_via_api_unless_fips!
end
end
context 'when FIPS mode is enabled' do
before do
stub_env('FIPS', '1')
end
it 'calls .fabricate_via_browser_ui!' do
expect(described_class).to receive(:fabricate_via_browser_ui!)
described_class.fabricate_via_api_unless_fips!
end
end
end
describe '.fabricate_via_api!' do
context 'when fabricating' do
include_context 'with fabrication context'
it_behaves_like 'fabrication method', :fabricate_via_api!
it 'instantiates the resource, calls resource method returns the resource' do
expect(resource).to receive(:fabricate_via_api!).and_return(location)
result = subject.fabricate_via_api!(resource: resource, parents: [])
expect(result).to eq(resource)
end
end
context "with debug log level" do
include_context 'with simple resource'
let(:method) { 'api' }
before do
allow(QA::Runtime::Logger).to receive(:info)
allow(resource).to receive(:api_support?).and_return(true)
allow(resource).to receive(:fabricate_via_api!)
allow(resource).to receive(:api_client) { api_client }
end
it 'logs the resource and build method' do
subject.fabricate_via_api!('something', resource: resource, parents: [])
expect(QA::Runtime::Logger).to have_received(:info) do |&msg|
expect(msg.call).to match_regex(log_regex)
end
end
end
end
describe '.fabricate_via_browser_ui!' do
context 'when fabricating' do
include_context 'with fabrication context'
it_behaves_like 'fabrication method', :fabricate_via_browser_ui!, :fabricate!
it 'instantiates the resource and calls resource method' do
subject.fabricate_via_browser_ui!('something', resource: resource, parents: [])
expect(resource).to have_received(:fabricate!).with('something')
end
it 'returns fabrication resource' do
result = subject.fabricate_via_browser_ui!('something', resource: resource, parents: [])
expect(result).to eq(resource)
end
end
context "with debug log level" do
include_context 'with simple resource'
let(:method) { 'browser_ui' }
before do
allow(QA::Runtime::Logger).to receive(:info)
end
it 'logs the resource and build method' do
subject.fabricate_via_browser_ui!('something', resource: resource, parents: [])
expect(QA::Runtime::Logger).to have_received(:info) do |&msg|
expect(msg.call).to match_regex(log_regex)
end
end
end
end
describe '.attribute' do
include_context 'with simple resource'
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
allow(resource).to receive(:api_resource).and_return(api_resource)
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
allow(QA::Runtime::Logger).to receive(:debug)
end
it 'returns value from api and emits an debug log entry' do
result = subject.fabricate!(resource: resource)
expect(result).to be_a(described_class)
expect(result.test).to eq('api_with_block')
expect(QA::Runtime::Logger)
.to have_received(:debug).with(/api_with_block/)
end
end
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
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)
expect { result.no_block }.to raise_error(
described_class::NoValueError, "No value was computed for no_block of #{resource.class.name}."
)
end
end
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
let(:first_resource) do
Class.new(base) do
attribute :test do
'first block'
end
end
end
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
end
describe '#web_url' do
include_context 'with simple resource'
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
include_context 'with simple resource'
let(:wait_for_requests_class) { QA::Support::WaitForRequests }
before do
allow(resource).to receive(:visit)
end
it 'calls #visit with the underlying #web_url' 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: false }).twice
resource.web_url = subject.current_url
resource.visit!
expect(resource).to have_received(:visit).with(subject.current_url)
end
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
end
end
|