diff --git a/app/controllers/kaui/admin_tenants_controller.rb b/app/controllers/kaui/admin_tenants_controller.rb index 3ccc698a..6f12ed8a 100644 --- a/app/controllers/kaui/admin_tenants_controller.rb +++ b/app/controllers/kaui/admin_tenants_controller.rb @@ -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 diff --git a/test/fixtures/missing-name-catalog.xml b/test/fixtures/missing-name-catalog.xml new file mode 100644 index 00000000..38cc6c48 --- /dev/null +++ b/test/fixtures/missing-name-catalog.xml @@ -0,0 +1,72 @@ + + + 2013-02-08T00:00:00+00:00 + IN_ADVANCE + + USD + + + + BASE + + + + + + END_OF_TERM + + + + + START_OF_BUNDLE + + + + + END_OF_TERM + + + + + START_OF_BUNDLE + + + + + ACCOUNT + + + + + DEFAULT + + + + + + Basic + + + UNLIMITED + + + MONTHLY + + + USD + 1000.00 + + + + + + + + + + basic-monthly + + + + diff --git a/test/functional/kaui/admin_tenants_controller_test.rb b/test/functional/kaui/admin_tenants_controller_test.rb index 6aa59ac5..d01f1b10 100644 --- a/test/functional/kaui/admin_tenants_controller_test.rb +++ b/test/functional/kaui/admin_tenants_controller_test.rb @@ -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") }