diff --git a/config/locales/en.yml b/config/locales/en.yml index 2559d545..79f05fe5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -63,6 +63,11 @@ en: multiple_channel_integrations: Specifying multiple channel integrations in requirements.json is not supported. too_many_oauth_parameters: "Too many parameters with type 'oauth': one permitted" + too_many_bundle_oauth: "Too many parameters with type 'bundle_oauth': one permitted" + too_many_bundle_integration_key: "Too many parameters with type 'bundle_integration_key': one permitted" + missing_bundle_oauth: "Parameter of type 'bundle_integration_key' exist without 'bundle_oauth'" + missing_bundle_integration_key: "Parameter of type 'bundle_oauth' exist without 'bundle_integration_key'" + invalid_use_of_oauth: "Parameter 'oauth' can't exist with 'bundle_integration_key' or 'bundle_oauth'" invalid_cr_schema_keys: one: 'Custom resources schema contains an invalid key: %{invalid_keys}' other: 'Custom resources schema contains invalid keys: %{invalid_keys}' diff --git a/lib/zendesk_apps_support/manifest/parameter.rb b/lib/zendesk_apps_support/manifest/parameter.rb index c9343046..4c427ee0 100644 --- a/lib/zendesk_apps_support/manifest/parameter.rb +++ b/lib/zendesk_apps_support/manifest/parameter.rb @@ -3,7 +3,7 @@ module ZendeskAppsSupport class Manifest class Parameter - TYPES = %w[text password checkbox url number multiline hidden oauth].freeze + TYPES = %w[text password checkbox url number multiline hidden oauth bundle_oauth bundle_integration_key].freeze ATTRIBUTES = %i[name type required secure default].freeze attr_reader(*ATTRIBUTES) def default? diff --git a/lib/zendesk_apps_support/validations/manifest.rb b/lib/zendesk_apps_support/validations/manifest.rb index df6976f4..a8d98cfc 100644 --- a/lib/zendesk_apps_support/validations/manifest.rb +++ b/lib/zendesk_apps_support/validations/manifest.rb @@ -42,11 +42,7 @@ def collate_manifest_errors(package) if manifest.marketing_only? errors.push(*marketing_only_errors(manifest)) else - errors << parameters_error(manifest) - errors << invalid_hidden_parameter_error(manifest) - errors << invalid_type_error(manifest) - errors << too_many_oauth_parameters(manifest) - errors << name_as_parameter_name_error(manifest) + errors.push(*non_marketing_only_errors(manifest)) end if manifest.requirements_only? || manifest.marketing_only? @@ -73,6 +69,43 @@ def marketing_only_errors(manifest) end end + def non_marketing_only_errors(manifest) + [].tap do |errors| + errors << parameters_error(manifest) + errors << invalid_hidden_parameter_error(manifest) + errors << invalid_type_error(manifest) + errors << too_many_oauth_parameters(manifest) + errors << name_as_parameter_name_error(manifest) + errors << invalid_use_of_bundle(manifest) + errors << cant_use_oauth_and_bundle(manifest) + end + end + + def invalid_use_of_bundle(manifest) + errors = [] + bundle_oauth_params = manifest.parameters.select { |x| x.type == 'bundle_oauth' } + bundle_integration_key_params = manifest.parameters.select { |x| x.type == 'bundle_integration_key' } + + errors << ValidationError.new(:too_many_bundle_oauth) if bundle_oauth_params.count > 1 + errors << ValidationError.new(:too_many_bundle_integration_key) if bundle_integration_key_params.count > 1 + + if bundle_oauth_params.count != bundle_integration_key_params.count + errors << ValidationError.new(:missing_bundle_oauth) if bundle_oauth_params.count.zero? + errors << ValidationError.new(:missing_bundle_integration_key) if bundle_integration_key_params.count.zero? + end + errors + end + + def cant_use_oauth_and_bundle(manifest) + oauth = manifest.parameters.select { |x| x.type == 'oauth' } + bundle_oauth = manifest.parameters.select { |x| x.type == 'bundle_oauth' } + bundle_integration_key = manifest.parameters.select { |x| x.type == 'bundle_integration_key' } + + if !oauth.count.zero? && (!bundle_oauth.count.zero? || !bundle_integration_key.count.zero?) + return ValidationError.new(:invalid_use_of_oauth) + end + end + def type_checks(manifest) errors = [] errors << boolean_error(manifest) diff --git a/spec/validations/manifest_spec.rb b/spec/validations/manifest_spec.rb index de096f38..a6733d3e 100644 --- a/spec/validations/manifest_spec.rb +++ b/spec/validations/manifest_spec.rb @@ -617,5 +617,93 @@ def create_package(parameter_hash) package = create_package(parameter_hash) expect(package).to have_error "Too many parameters with type 'oauth': one permitted" end + + context 'bundle' do + it 'should have only one bundle_oauth in parameter list' do + parameter_hash = { + 'parameters' => + [ + { + 'name' => 'bundle_token', + 'type' => 'bundle_oauth' + }, + { + 'name' => 'bundle_token_2', + 'type' => 'bundle_oauth' + } + ] + } + package = create_package(parameter_hash) + expect(package).to have_error "Too many parameters with type 'bundle_oauth': one permitted" + end + + it 'should have only one bundle_integration_key in parameter list' do + parameter_hash = { + 'parameters' => + [ + { + 'name' => 'bundle_integration_key', + 'type' => 'bundle_integration_key' + }, + { + 'name' => 'bundle_integration_key_2', + 'type' => 'bundle_integration_key' + } + ] + } + package = create_package(parameter_hash) + expect(package).to have_error "Too many parameters with type 'bundle_integration_key': one permitted" + end + + it 'should have a bundle_oauth if a bundle_integration_key exist' do + parameter_hash = { + 'parameters' => + [ + { + 'name' => 'bundle_integration_key', + 'type' => 'bundle_integration_key' + } + ] + } + package = create_package(parameter_hash) + expect(package).to have_error "Parameter of type 'bundle_integration_key' exist without 'bundle_oauth'" + end + + it 'should have a bundle_oauth if a bundle_oauth exist' do + parameter_hash = { + 'parameters' => + [ + { + 'name' => 'bundle_token', + 'type' => 'bundle_oauth' + } + ] + } + package = create_package(parameter_hash) + expect(package).to have_error "Parameter of type 'bundle_oauth' exist without 'bundle_integration_key'" + end + end + + it 'should not be exist if oauth exist' do + parameter_hash = { + 'parameters' => + [ + { + 'name' => 'bundle_token', + 'type' => 'bundle_oauth' + }, + { + 'name' => 'bundle_integration_key', + 'type' => 'bundle_integration_key' + }, + { + 'name' => 'valid parameter', + 'type' => 'oauth' + } + ] + } + package = create_package(parameter_hash) + expect(package).to have_error "Parameter 'oauth' can't exist with 'bundle_integration_key' or 'bundle_oauth'" + end end end