Skip to content

Commit

Permalink
Merge pull request #396 from tungleduyxyz/issue_347_validate_catalog
Browse files Browse the repository at this point in the history
Issue 347 Add validate catalog feature
  • Loading branch information
pierre authored May 13, 2024
2 parents b60d7ce + b37d85c commit 7de5226
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/controllers/kaui/admin_tenants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,23 @@ def upload_catalog
uploaded_catalog = params.require(:catalog)
catalog_xml = uploaded_catalog.read

Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options)

redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_uploaded_successfully')
validate_response = Kaui::Catalog.validate_catalog(catalog_xml, options[:username], nil, comment, options)
catalog_validation_errors = begin
JSON.parse(validate_response.response.body)['catalogValidationErrors']
rescue StandardError
nil
end
if catalog_validation_errors.blank?
Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options)
redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_uploaded_successfully')
else
errors = ''
catalog_validation_errors.each do |validation_error|
errors += (validation_error['errorDescription'])
end
flash[:error] = errors
redirect_to admin_tenant_new_catalog_path(id: current_tenant.id)
end
end

def new_catalog
Expand Down
72 changes: 72 additions & 0 deletions test/fixtures/missing-name-catalog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="CatalogSchema.xsd ">
<effectiveDate>2013-02-08T00:00:00+00:00</effectiveDate>
<recurringBillingMode>IN_ADVANCE</recurringBillingMode>
<currencies>
<currency>USD</currency>
</currencies>
<products>
<product name="Basic">
<category>BASE</category>
</product>
</products>
<rules>
<changePolicy>
<changePolicyCase>
<policy>END_OF_TERM</policy>
</changePolicyCase>
</changePolicy>
<changeAlignment>
<changeAlignmentCase>
<alignment>START_OF_BUNDLE</alignment>
</changeAlignmentCase>
</changeAlignment>
<cancelPolicy>
<cancelPolicyCase>
<policy>END_OF_TERM</policy>
</cancelPolicyCase>
</cancelPolicy>
<createAlignment>
<createAlignmentCase>
<alignment>START_OF_BUNDLE</alignment>
</createAlignmentCase>
</createAlignment>
<billingAlignment>
<billingAlignmentCase>
<alignment>ACCOUNT</alignment>
</billingAlignmentCase>
</billingAlignment>
<priceList>
<priceListCase>
<toPriceList>DEFAULT</toPriceList>
</priceListCase>
</priceList>
</rules>
<plans>
<plan name="basic-monthly">
<product>Basic</product>
<finalPhase type="EVERGREEN">
<duration>
<unit>UNLIMITED</unit>
</duration>
<recurring>
<billingPeriod>MONTHLY</billingPeriod>
<recurringPrice>
<price>
<currency>USD</currency>
<value>1000.00</value>
</price>
</recurringPrice>
</recurring>
</finalPhase>
</plan>
</plans>
<priceLists>
<defaultPriceList name="DEFAULT">
<plans>
<plan>basic-monthly</plan>
</plans>
</defaultPriceList>
</priceLists>
</catalog>
8 changes: 8 additions & 0 deletions test/functional/kaui/admin_tenants_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class AdminTenantsControllerTest < Kaui::FunctionalTestHelper
assert_response 200
end

test 'should validate catalog' do
tenant = create_kaui_tenant
post :upload_catalog, params: { id: tenant.id, catalog: fixture_file_upload("#{FIXTURES_PATH}/missing-name-catalog.xml") }

assert_redirected_to admin_tenant_new_catalog_path(id: tenant.id)
assert_includes flash[:error], "One of '{catalogName}' is expected."
end

test 'should upload catalog' do
tenant = create_kaui_tenant
post :upload_catalog, params: { id: tenant.id, catalog: fixture_file_upload("#{FIXTURES_PATH}/catalog-v1.xml") }
Expand Down

0 comments on commit 7de5226

Please sign in to comment.