Skip to content

Commit f6d9914

Browse files
committed
Handle a prov_type that isn't in the list of possibilities
1 parent b87b3d0 commit f6d9914

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

app/models/service_template.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,17 @@ def self.class_from_prov_type(prov_type)
145145
# class name validity before calling constantize.
146146
allowed_class_names = descendants.collect(&:name)
147147

148+
raise ArgumentError, "Invalid prov_type '#{prov_type}'" unless all_catalog_item_types.keys.include?(prov_type)
149+
148150
# If we are given a "generic" provision type then assume that the prov_type
149151
# maps to a valid class otherwise it is invalid
150152
#
151153
# Non-generic provision types however are able to work with the base
152154
# ServiceTemplate class and thus we cannot raise an exception in this case.
153155
if prov_type.starts_with?("generic_")
154-
class_name = request_type.split('generic_').last
155-
raise NameError, _("uninitialized constant") unless allowed_class_names.include?(class_name)
156+
generic_type = prov_type.split('generic_').last
157+
class_name = "ServiceTemplate#{generic_type.camelize}"
158+
raise ArgumentError, "Invalid prov_type '#{prov_type}'" unless allowed_class_names.include?(class_name)
156159
else
157160
class_name = "ServiceTemplate#{prov_type.camelize}"
158161
class_name = "ServiceTemplate" unless allowed_class_names.include?(class_name)

spec/models/service_template_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,34 @@
719719
end
720720
end
721721

722+
describe '.class_from_prov_type' do
723+
it 'returns the correct generic type' do
724+
template_class = ServiceTemplate.class_from_prov_type('generic_ansible_tower')
725+
726+
expect(template_class).to eq(ServiceTemplateAnsibleTower)
727+
end
728+
729+
it 'raises an exception for an invalid generic type' do
730+
expect { ServiceTemplate.class_from_prov_type('generic_invalid') }
731+
.to raise_error(ArgumentError, /Invalid prov_type/)
732+
end
733+
734+
it 'returns the correct non-generic type mapping to ::ServiceTemplate' do
735+
template_class = ServiceTemplate.class_from_prov_type('amazon')
736+
737+
expect(template_class).to eq(ServiceTemplate)
738+
end
739+
740+
it 'returns the correct non-generic type when mapping to a ServiceTemplate subclass' do
741+
expect(ServiceTemplate.class_from_prov_type("awx")).to eq(ServiceTemplateAwx)
742+
end
743+
744+
it 'raises an exception for an invalid non-generic type' do
745+
expect { ServiceTemplate.class_from_prov_type('invalid') }
746+
.to raise_error(ArgumentError, /Invalid prov_type/)
747+
end
748+
end
749+
722750
let(:user) { FactoryBot.create(:user_with_group) }
723751
let(:ra1) { FactoryBot.create(:resource_action, :action => 'Provision') }
724752
let(:ra2) { FactoryBot.create(:resource_action, :action => 'Retirement') }

0 commit comments

Comments
 (0)