-
Notifications
You must be signed in to change notification settings - Fork 45
/
products_controller_spec.rb
374 lines (315 loc) · 12.7 KB
/
products_controller_spec.rb
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
require 'rails_helper'
RSpec.describe Api::Connect::V3::Systems::ProductsController do
include_context 'auth header', :system, :login, :password
include_context 'version header', 3
let(:url) { connect_systems_products_url }
let(:headers) { auth_header.merge(version_header) }
let(:system) { FactoryGirl.create(:system) }
let(:product) { FactoryGirl.create(:product, :with_mirrored_repositories, :with_mirrored_extensions) }
let(:payload) do
{
identifier: product.identifier,
version: product.version,
arch: product.arch
}
end
describe '#activate' do
it_behaves_like 'products controller action' do
let(:verb) { 'post' }
end
it_behaves_like 'product must have mirrored repositories' do
let(:verb) { 'post' }
end
context 'when product has unmet dependencies' do
subject { response }
let(:base_product) { FactoryGirl.create(:product, :with_not_mirrored_repositories, :with_mirrored_extensions) }
let(:product) { base_product.extensions[0] }
let(:error_json) do
{
type: 'error',
error: "Unmet product dependencies, activate one of these products first: #{base_product.friendly_name}",
localized_error: "Unmet product dependencies, activate one of these products first: #{base_product.friendly_name}"
}.to_json
end
before { post url, headers: headers, params: payload }
its(:code) { is_expected.to eq('422') }
its(:body) { is_expected.to eq(error_json) }
end
context 'when product has repos' do
subject { response }
let(:serialized_json) do
V3::ServiceSerializer.new(
product.service,
base_url: URI::HTTP.build({ scheme: response.request.scheme, host: response.request.host }).to_s
).to_json
end
before { post url, headers: headers, params: payload }
its(:code) { is_expected.to eq('201') }
its(:body) { is_expected.to eq(serialized_json) }
describe 'JSON response' do
subject(:json_data) { JSON.parse(response.body, symbolize_names: true) }
it { is_expected.to include :id, :name, :product, :url, :obsoleted_service_name }
it 'has extensions' do
expect(json_data[:product][:extensions]).not_to be_empty
end
end
end
describe 'activations' do
subject(:request) { post url, headers: headers, params: payload }
context 'when the product was already activated on this system' do
before { Activation.create system: system, service: product.service }
specify { expect { request }.not_to change { system.activations.reload } }
end
context 'when the product was not already activated on this system' do
specify { expect { request }.to change { system.activations.count }.from(0).to(1) }
end
end
end
describe '#show' do
let(:activation) { FactoryGirl.create(:activation, :with_mirrored_product) }
it_behaves_like 'products controller action' do
let(:verb) { 'get' }
end
it_behaves_like 'product must have mirrored repositories' do
let(:verb) { 'get' }
end
context 'when product is not activated' do
subject { response }
before { get url, headers: headers, params: payload }
its(:code) { is_expected.to eq('422') }
describe 'JSON response' do
subject { JSON.parse(response.body, symbolize_names: true) }
its([:error]) { is_expected.to match(/The requested product '.*' is not activated on this system/) }
end
end
context 'when product is activated' do
subject { response }
let(:system) { activation.system }
let(:payload) do
{
identifier: activation.product.identifier,
version: activation.product.version,
arch: activation.product.arch
}
end
let(:serialized_json) do
V3::ProductSerializer.new(
activation.service.product,
base_url: URI::HTTP.build({ scheme: response.request.scheme, host: response.request.host }).to_s
).to_json
end
before { get url, headers: headers, params: payload }
its(:code) { is_expected.to eq('200') }
its(:body) { is_expected.to eq(serialized_json) }
end
context 'with eula_url' do
subject { response }
let(:system) { activation.system }
let(:payload) do
{
identifier: activation.product.identifier,
version: activation.product.version,
arch: activation.product.arch
}
end
let(:serialized_json) do
V3::ProductSerializer.new(
activation.product,
base_url: URI::HTTP.build({ scheme: response.request.scheme, host: response.request.host }).to_s
).to_json
end
before do
activation.product.eula_url = 'http://example.com/dummy/eula.txt'
activation.product.save!
get url, headers: headers, params: payload
end
its(:code) { is_expected.to eq('200') }
its(:body) { is_expected.to eq(serialized_json) }
it 'has correct eula_url' do
product = JSON.parse(serialized_json, symbolize_names: true)
expect(product[:eula_url]).to eq('http://www.example.com/repo/dummy/eula.txt')
replacement_url = URI::HTTP.build({ scheme: request.scheme, host: request.host, path: RMT::DEFAULT_MIRROR_URL_PREFIX }).to_s
expect(product[:eula_url]).to eq(
RMT::Misc.replace_uri_parts(activation.product.eula_url, replacement_url)
)
end
end
end
describe '#upgrade' do
let(:request) { put url, headers: headers, params: payload }
let(:new_product) { FactoryGirl.create(:product, :with_mirrored_repositories) }
let(:payload) do
{
identifier: new_product.identifier,
version: new_product.version,
arch: new_product.arch
}
end
let(:serialized_json) do
V3::ServiceSerializer.new(
new_product.service,
base_url: URI::HTTP.build({ scheme: response.request.scheme, host: response.request.host }).to_s
).to_json
end
it_behaves_like 'products controller action' do
let(:verb) { 'put' }
end
before do
request
system.reload
end
context 'new product' do
subject { response }
specify { expect(system.activations.count).to eq(1) }
its(:code) { is_expected.to eq('201') }
its(:body) { is_expected.to eq(serialized_json) }
it 'activates new product' do
expect(system.activations.first.reload.service_id).to equal(new_product.service.id)
end
end
context 'with activated previous product' do
let!(:old_product) { FactoryGirl.create(:product, :with_mirrored_repositories, :activated, system: system) }
let(:new_product) { FactoryGirl.create(:product, :with_mirrored_repositories, predecessors: [old_product]) }
subject { response }
specify { expect(system.activations.count).to eq(1) }
its(:code) { is_expected.to eq('201') }
its(:body) { is_expected.to eq(serialized_json) }
it 'deactivates old product and activates new product' do
expect(system.activations.first.reload.service_id).to equal(new_product.service.id)
end
end
end
describe 'online/offline migrations' do
shared_examples 'migration return values' do
before { post url, headers: headers, params: payload }
subject { response }
context 'without installed_products' do
let(:payload) { {} }
let(:error_response) do
{
type: 'error',
error: "Required parameters are missing or empty: #{required_params}",
localized_error: "Required parameters are missing or empty: #{required_params}"
}.to_json
end
its(:code) { is_expected.to eq('422') }
its(:body) { is_expected.to eq(error_response) }
end
context 'with no base product in installed_products' do
let(:payload) do
{
'installed_products': [ { 'identifier': 'non_existent_product', 'version': '42', 'arch': 'x86_64', 'release_type': nil } ],
'target_base_product': { 'identifier': 'SLES', 'version': '15', 'arch': 'x86_64' }
}
end
let(:error_response) do
{ type: 'error', error: 'No base product found.', localized_error: 'No base product found.' }.to_json
end
its(:code) { is_expected.to eq('422') }
its(:body) { is_expected.to eq(error_response) }
end
context 'with not activated product in installed_products' do
let(:product) { FactoryGirl.create(:product, :with_mirrored_repositories, product_type: 'base') }
let(:payload) do
{
'installed_products': [
{ 'identifier': product.identifier, 'version': product.version, 'arch': product.arch, 'release_type': product.release_type }
],
'target_base_product': {
'identifier': 'SLES', 'version': '15', 'arch': 'x86_64'
}
}
end
let(:error_response) do
{
type: 'error',
error: "The requested products '#{product.friendly_name}' are not activated on the system.",
localized_error: "The requested products '#{product.friendly_name}' are not activated on the system."
}.to_json
end
its(:code) { is_expected.to eq('422') }
its(:body) { is_expected.to eq(error_response) }
end
context 'with multiple base products in installed_products' do
let(:first_product) { FactoryGirl.create(:product, :with_mirrored_repositories, :activated, system: system, product_type: 'base') }
let(:second_product) { FactoryGirl.create(:product, :with_mirrored_repositories, :activated, system: system, product_type: 'base') }
let(:payload) do
{
'installed_products': [
{
'identifier': first_product.identifier,
'version': first_product.version,
'arch': first_product.arch,
'release_type': first_product.release_type
},
{
'identifier': second_product.identifier,
'version': second_product.version,
'arch': second_product.arch,
'release_type': second_product.release_type
}
],
'target_base_product': { 'identifier': 'SLES', 'version': '15', 'arch': 'x86_64' }
}
end
let(:error_response) do
{
type: 'error',
error: "Multiple base products found: '#{first_product.friendly_name}, #{second_product.friendly_name}'.",
localized_error: "Multiple base products found: '#{first_product.friendly_name}, #{second_product.friendly_name}'."
}.to_json
end
its(:code) { is_expected.to eq('422') }
its(:body) { is_expected.to eq(error_response) }
end
context 'with a proper set of products in installed_products' do
let(:first_product) { FactoryGirl.create(:product, :with_mirrored_repositories, :activated, system: system, product_type: 'base') }
let(:second_product) do
FactoryGirl.create(
:product,
:with_mirrored_repositories,
product_type: 'base',
predecessors: [first_product],
migration_kind: migration_kind
)
end
let(:payload) do
product = second_product.predecessors.first # For initializing everything in the correct order
{
'installed_products': [ {
'identifier': product.identifier,
'version': product.version,
'arch': product.arch,
'release_type': product.release_type
} ],
'target_base_product': {
'identifier': second_product.identifier,
'version': second_product.version,
'arch': second_product.arch,
'release_type': second_product.release_type
}
}
end
let(:expected_response) do
[[::V3::UpgradePathItemSerializer.new(second_product)]].to_json
end
its(:code) { is_expected.to eq('200') }
its(:body) do
is_expected.to eq(expected_response)
end
end
end
describe '#migrations' do
let(:url) { connect_systems_products_migrations_url }
let(:required_params) { 'installed_products' }
let(:migration_kind) { :online }
include_examples 'migration return values'
end
describe '#offline_migrations' do
let(:url) { connect_systems_products_offline_migrations_url }
let(:required_params) { 'installed_products, target_base_product' }
let(:migration_kind) { :offline }
include_examples 'migration return values'
end
end
end