From c6b8e08e38e516d3da13b84ae635deb23c5992a4 Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 9 Nov 2021 14:57:33 +0000 Subject: [PATCH] Add subscription level logic to back-end validation --- lib/custom_wizard/custom_field.rb | 8 ++-- lib/custom_wizard/subscription.rb | 46 +++++++++++++++++++ spec/components/custom_wizard/action_spec.rb | 2 +- spec/components/custom_wizard/builder_spec.rb | 12 ++--- .../custom_wizard/custom_field_spec.rb | 2 +- spec/components/custom_wizard/mapper_spec.rb | 2 +- .../custom_wizard/template_validator_spec.rb | 2 +- .../custom_field_extensions_spec.rb | 2 +- spec/plugin_helper.rb | 4 +- .../custom_field_extensions_spec.rb | 3 +- .../custom_wizard/steps_controller_spec.rb | 2 +- .../wizard_step_serializer_spec.rb | 2 +- 12 files changed, 66 insertions(+), 21 deletions(-) diff --git a/lib/custom_wizard/custom_field.rb b/lib/custom_wizard/custom_field.rb index eb93e292..99dcfbbb 100644 --- a/lib/custom_wizard/custom_field.rb +++ b/lib/custom_wizard/custom_field.rb @@ -17,9 +17,8 @@ class ::CustomWizard::CustomField category: ["basic_category"], post: ["post"] } - SUBSCRIPTION_CLASSES ||= ['category', 'group'] + TYPES ||= ["string", "boolean", "integer", "json"] - SUBSCRIPTION_TYPES ||= ["json"] LIST_CACHE_KEY ||= 'custom_field_list' def self.serializers @@ -84,8 +83,7 @@ class ::CustomWizard::CustomField add_error(I18n.t("#{i18n_key}.unsupported_class", class: value)) next end - - if attr == 'klass' && SUBSCRIPTION_CLASSES.include?(value) && !@subscription.subscribed? + if attr == 'klass' && @subscription.requires_additional_subscription("custom_fields", "klass").include?(value) add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value)) end @@ -100,7 +98,7 @@ class ::CustomWizard::CustomField add_error(I18n.t("#{i18n_key}.unsupported_type", type: value)) end - if attr == 'type' && SUBSCRIPTION_TYPES.include?(value) && !@subscription.subscribed? + if attr == 'type' && @subscription.requires_additional_subscription("custom_fields", "type").include?(value) add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value)) end diff --git a/lib/custom_wizard/subscription.rb b/lib/custom_wizard/subscription.rb index 6c535602..7d94eb89 100644 --- a/lib/custom_wizard/subscription.rb +++ b/lib/custom_wizard/subscription.rb @@ -6,6 +6,23 @@ class CustomWizard::Subscription attr_accessor :authentication, :subscription + SUBSCRIPTION_LEVELS = { + standard: { + actions: ["send_message", "add_to_group", "watch_categories"], + custom_fields: { + klass: [], + type: ["json"], + }, + }, + business: { + actions: ["create_category", "create_group", "send_to_api"], + custom_fields: { + klass: ["group", "category"], + type: [], + }, + }, + } + def initialize @authentication = CustomWizard::Subscription::Authentication.new(get_authentication) @subscription = CustomWizard::Subscription::Subscription.new(get_subscription) @@ -39,6 +56,31 @@ class CustomWizard::Subscription "discourse-subscription-server:user_subscription" end + def requires_additional_subscription(kategory, sub_kategory) + case kategory + when "actions" + case self.type + when "business" + return [] + when "standard" + return SUBSCRIPTION_LEVELS[:business][kategory.to_sym] + else + return SUBSCRIPTION_LEVELS[:standard][kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym] + end + when "custom_fields" + case self.type + when "business" + return [] + when "standard" + return SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym]; + else + return SUBSCRIPTION_LEVELS[:standard][kategory.to_sym][sub_kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym] + end + else + return [] + end + end + def update if @authentication.active? response = Excon.get( @@ -127,6 +169,10 @@ class CustomWizard::Subscription self.new.type end + def self.requires_additional_subscription(kategory, sub_kategory) + self.new.requires_additional_subscription(kategory, sub_kategory) + end + def self.authorized? self.new.authorized? end diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index 0c677300..284f63d3 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -179,7 +179,7 @@ describe CustomWizard::Action do context "subscription actions" do before do - enable_subscription + enable_subscription("standard") end it '#send_message' do diff --git a/spec/components/custom_wizard/builder_spec.rb b/spec/components/custom_wizard/builder_spec.rb index 28880409..f4a6d2fc 100644 --- a/spec/components/custom_wizard/builder_spec.rb +++ b/spec/components/custom_wizard/builder_spec.rb @@ -176,7 +176,7 @@ describe CustomWizard::Builder do context "restart is enabled" do before do - enable_subscription + enable_subscription("standard") @template[:restart_on_revisit] = true CustomWizard::Template.save(@template.as_json) end @@ -205,7 +205,7 @@ describe CustomWizard::Builder do context 'with required data' do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:required_data] = required_data_json['required_data'] @template[:steps][0][:required_data_message] = required_data_json['required_data_message'] CustomWizard::Template.save(@template.as_json) @@ -241,7 +241,7 @@ describe CustomWizard::Builder do context "with permitted params" do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:permitted_params] = permitted_param_json['permitted_params'] CustomWizard::Template.save(@template.as_json) end @@ -256,7 +256,7 @@ describe CustomWizard::Builder do context "with condition" do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:condition] = user_condition_json['condition'] CustomWizard::Template.save(@template.as_json) end @@ -295,7 +295,7 @@ describe CustomWizard::Builder do context "with condition" do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:fields][0][:condition] = user_condition_json['condition'] CustomWizard::Template.save(@template.as_json) end @@ -327,7 +327,7 @@ describe CustomWizard::Builder do context 'save submissions disabled' do before do - enable_subscription + enable_subscription("standard") @template[:save_submissions] = false CustomWizard::Template.save(@template.as_json) @wizard = CustomWizard::Builder.new(@template[:id], user).build diff --git a/spec/components/custom_wizard/custom_field_spec.rb b/spec/components/custom_wizard/custom_field_spec.rb index a30ec02b..fd0fa4fc 100644 --- a/spec/components/custom_wizard/custom_field_spec.rb +++ b/spec/components/custom_wizard/custom_field_spec.rb @@ -217,7 +217,7 @@ describe CustomWizard::CustomField do context "with a subscription" do before do - enable_subscription + enable_subscription("business") end it "saves subscription field types" do diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index 1ac945d0..71c1e7de 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -359,7 +359,7 @@ describe CustomWizard::Mapper do context "with a subscription" do before do - enable_subscription + enable_subscription("standard") end it "treats replaced values as string literals" do diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index e8b8af28..14214e4d 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -75,7 +75,7 @@ describe CustomWizard::TemplateValidator do context "with subscription" do before do - enable_subscription + enable_subscription("standard") end it "validates wizard attributes" do diff --git a/spec/extensions/custom_field_extensions_spec.rb b/spec/extensions/custom_field_extensions_spec.rb index 7bba27a0..3660644b 100644 --- a/spec/extensions/custom_field_extensions_spec.rb +++ b/spec/extensions/custom_field_extensions_spec.rb @@ -74,7 +74,7 @@ describe "custom field extensions" do context "subscription custom fields" do before do - enable_subscription + enable_subscription("business") subscription_custom_field_json['custom_fields'].each do |field_json| custom_field = CustomWizard::CustomField.new(nil, field_json) diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb index e72fe355..232a2411 100644 --- a/spec/plugin_helper.rb +++ b/spec/plugin_helper.rb @@ -29,8 +29,10 @@ def authenticate_subscription CustomWizard::Subscription::Authentication.any_instance.stubs(:active?).returns(true) end -def enable_subscription +def enable_subscription(type) + # CustomWizard::Subscription.new CustomWizard::Subscription.any_instance.stubs(:subscribed?).returns(true) + CustomWizard::Subscription.any_instance.stubs(:type).returns(type) end def disable_subscription diff --git a/spec/requests/custom_wizard/custom_field_extensions_spec.rb b/spec/requests/custom_wizard/custom_field_extensions_spec.rb index 775e3ee0..19a8417a 100644 --- a/spec/requests/custom_wizard/custom_field_extensions_spec.rb +++ b/spec/requests/custom_wizard/custom_field_extensions_spec.rb @@ -40,8 +40,7 @@ describe "custom field extensions" do context "with a subscription" do before do - enable_subscription - + enable_subscription("business") subscription_custom_field_json['custom_fields'].each do |field_json| custom_field = CustomWizard::CustomField.new(nil, field_json) custom_field.save diff --git a/spec/requests/custom_wizard/steps_controller_spec.rb b/spec/requests/custom_wizard/steps_controller_spec.rb index 8396135c..b55de419 100644 --- a/spec/requests/custom_wizard/steps_controller_spec.rb +++ b/spec/requests/custom_wizard/steps_controller_spec.rb @@ -121,7 +121,7 @@ describe CustomWizard::StepsController do context "subscription" do before do - enable_subscription + enable_subscription("standard") end it "raises an error when user cant see the step due to conditions" do diff --git a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb index 0a9f7e89..f0128765 100644 --- a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb +++ b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb @@ -34,7 +34,7 @@ describe CustomWizard::StepSerializer do context 'with required data' do before do - enable_subscription + enable_subscription("standard") wizard_template['steps'][0]['required_data'] = required_data_json['required_data'] wizard_template['steps'][0]['required_data_message'] = required_data_json['required_data_message'] CustomWizard::Template.save(wizard_template)