From ac3f03064d1e92bac35e66e6394ee44feb7621da Mon Sep 17 00:00:00 2001 From: Thea Choem <29684683+theachoem@users.noreply.github.com> Date: Wed, 3 Jan 2024 23:11:34 +0700 Subject: [PATCH] close #953 upgrade to spree 4.6 --- Gemfile.lock | 60 ++++++++++++------- .../option_type_decorator.rb | 1 - .../subscribers/handle_request_decorator.rb | 7 ++- .../webhooks/subscribers/make_request.rb | 5 +- .../spree_permitted_attributes.rb | 5 +- .../factories/option_type_factory .rb | 7 ++- .../factories/option_value_factory.rb | 7 ++- spec/models/spree/webhooks/subscriber_spec.rb | 14 +++++ .../v2/storefront/product_serializer_spec.rb | 1 + .../v2/storefront/taxon_serializer_spec.rb | 7 ++- .../v2/storefront/user_serializer_spec.rb | 3 +- .../subscribers/handle_request_spec.rb | 26 ++++++++ .../webhooks/subscribers/make_request_spec.rb | 13 ++++ spree_cm_commissioner.gemspec | 4 +- 14 files changed, 124 insertions(+), 36 deletions(-) create mode 100644 spec/services/spree/webhooks/subscribers/handle_request_spec.rb create mode 100644 spec/services/spree_cm_commissioner/webhooks/subscribers/make_request_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 1b5fdec9b..39626c46f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -53,10 +53,10 @@ PATH rqrcode (~> 2.0) searchkick (~> 5.1) simple_calendar (~> 2.4) - spree (>= 4.5.0) - spree_api_v1 (>= 4.5.0) - spree_auth_devise (>= 4.5.0) - spree_backend (>= 4.5.0) + spree (>= 4.6.0) + spree_api_v1 (~> 4.5.0) + spree_auth_devise (>= 4.6.0) + spree_backend (>= 4.6.0) spree_extension spree_multi_vendor (>= 2.4.1) telegram-bot @@ -349,8 +349,11 @@ GEM flatpickr (4.6.13.0) font-awesome-sass (6.4.2) sassc (~> 2.0) - friendly_id (5.5.0) + friendly_id (5.4.2) activerecord (>= 4.0.0) + friendly_id-mobility (1.0.4) + friendly_id (>= 5.0.0, < 5.5) + mobility (>= 1.0.1, < 2.0) gapic-common (0.20.0) faraday (>= 1.9, < 3.a) faraday-retry (>= 1.0, < 3.a) @@ -495,6 +498,12 @@ GEM mini_mime (1.1.5) mini_portile2 (2.8.4) minitest (5.20.0) + mobility (1.2.9) + i18n (>= 0.6.10, < 2) + request_store (~> 1.0) + mobility-ransack (1.2.2) + mobility (>= 1.0.1, < 2.0) + ransack (>= 1.8.0, < 5.0) monetize (1.12.0) money (~> 6.12) money (6.16.0) @@ -606,6 +615,8 @@ GEM redis-client (0.17.0) connection_pool regexp_parser (2.8.2) + request_store (1.5.1) + rack (>= 1.4) responders (3.1.1) actionpack (>= 5.2) railties (>= 5.2) @@ -703,15 +714,15 @@ GEM simplecov_json_formatter (0.1.4) simpleidn (0.2.1) unf (~> 0.1.4) - spree (4.5.3) - spree_api (= 4.5.3) - spree_cli (= 4.5.3) - spree_core (= 4.5.3) - spree_api (4.5.3) + spree (4.6.3) + spree_api (= 4.6.3) + spree_cli (= 4.6.3) + spree_core (= 4.6.3) + spree_api (4.6.3) bcrypt (~> 3.1) doorkeeper (~> 5.3) jsonapi-serializer (~> 2.1) - spree_core (= 4.5.3) + spree_core (= 4.6.3) spree_api_v1 (4.5.0) rabl (~> 0.14, >= 0.14.2) responders @@ -722,7 +733,7 @@ GEM devise-encryptable (= 0.2.0) spree_core (>= 4.5.0) spree_extension - spree_backend (4.5.1) + spree_backend (4.6.1) babel-transpiler (~> 0.7) bootstrap (~> 4.0) flag-icons-rails (~> 3.4) @@ -736,35 +747,38 @@ GEM responders sass-rails (>= 5) select2-rails (~> 4.0.6) - spree (>= 4.4.0) + spree (>= 4.6.0) sprockets (~> 4.0) tinymce-rails (~> 5.0) - spree_cli (4.5.3) + spree_cli (4.6.3) thor (~> 1.0) - spree_core (4.5.3) - actionpack (>= 6.1) - actionview (>= 6.1) + spree_core (4.6.3) + actionpack (>= 6.1, < 7.1) + actionview (>= 6.1, < 7.1) active_storage_validations (~> 0.9, <= 0.9.5) - activejob (>= 6.1) + activejob (>= 6.1, < 7.1) activemerchant (~> 1.67) - activemodel (>= 6.1) - activerecord (>= 6.1) + activemodel (>= 6.1, < 7.1) + activerecord (>= 6.1, < 7.1) activerecord-typedstore - activestorage (>= 6.1) - activesupport (>= 6.1) + activestorage (>= 6.1, < 7.1) + activesupport (>= 6.1, < 7.1) acts_as_list (>= 0.8) auto_strip_attributes (~> 2.6) awesome_nested_set (~> 3.3, >= 3.3.1) cancancan (~> 3.2) carmen (>= 1.0) friendly_id (~> 5.2, >= 5.2.1) + friendly_id-mobility (~> 1.0.4) highline (~> 2.0) image_processing (~> 1.2) kaminari (~> 1.2) + mobility (~> 1.2.9) + mobility-ransack (~> 1.2.1) monetize (~> 1.9) money (~> 6.13) paranoia (~> 2.4) - railties (>= 6.1) + railties (>= 6.1, < 7.1) ransack (>= 2.3, < 3.0) rexml state_machines-activemodel (~> 0.7) diff --git a/app/models/spree_cm_commissioner/option_type_decorator.rb b/app/models/spree_cm_commissioner/option_type_decorator.rb index 30db3c45b..f195d5bb3 100644 --- a/app/models/spree_cm_commissioner/option_type_decorator.rb +++ b/app/models/spree_cm_commissioner/option_type_decorator.rb @@ -6,7 +6,6 @@ def self.prepended(base) base.include SpreeCmCommissioner::ParameterizeName base.enum kind: %i[variant product vendor] - base.validates :name, presence: true base.validates :attr_type, inclusion: { in: ATTRIBUTE_TYPES } base.validates :attr_type, presence: true, if: :travel? base.validate :kind_has_updated, on: :update, if: :kind_changed? diff --git a/app/services/spree_cm_commissioner/webhooks/subscribers/handle_request_decorator.rb b/app/services/spree_cm_commissioner/webhooks/subscribers/handle_request_decorator.rb index 599a87053..d28a42ffe 100644 --- a/app/services/spree_cm_commissioner/webhooks/subscribers/handle_request_decorator.rb +++ b/app/services/spree_cm_commissioner/webhooks/subscribers/handle_request_decorator.rb @@ -9,7 +9,12 @@ def self.prepended(_base) # override def request @request ||= - SpreeCmCommissioner::Webhooks::Subscribers::MakeRequest.new(url: url, api_key: api_key, webhook_payload_body: body_with_event_metadata) + Spree::Webhooks::Subscribers::MakeRequest.new( + signature: event.signature_for(body_with_event_metadata), + url: url, + api_key: api_key, + webhook_payload_body: body_with_event_metadata + ) end end end diff --git a/app/services/spree_cm_commissioner/webhooks/subscribers/make_request.rb b/app/services/spree_cm_commissioner/webhooks/subscribers/make_request.rb index d542edc1d..db0e6ca98 100644 --- a/app/services/spree_cm_commissioner/webhooks/subscribers/make_request.rb +++ b/app/services/spree_cm_commissioner/webhooks/subscribers/make_request.rb @@ -6,9 +6,9 @@ module Subscribers class MakeRequest < Spree::Webhooks::Subscribers::MakeRequest attr_reader :api_key - def initialize(url:, api_key:, webhook_payload_body:) + def initialize(signature:, url:, api_key:, webhook_payload_body:) @api_key = api_key - super(url: url, webhook_payload_body: webhook_payload_body) + super(signature: signature, url: url, webhook_payload_body: webhook_payload_body) end def headers @@ -16,6 +16,7 @@ def headers headers['Content-Type'] = 'application/json' headers['X-Api-Key'] = api_key if api_key.present? + headers['X-Spree-Hmac-SHA256'] = @signature headers end diff --git a/config/initializers/spree_permitted_attributes.rb b/config/initializers/spree_permitted_attributes.rb index 0f19efc82..7ef508a02 100644 --- a/config/initializers/spree_permitted_attributes.rb +++ b/config/initializers/spree_permitted_attributes.rb @@ -5,6 +5,7 @@ module PermittedAttributes from_date to_date ] + @@user_attributes += %i[ first_name last_name @@ -12,6 +13,7 @@ module PermittedAttributes phone_number profile ] + @@taxon_attributes += %i[ category_icon custom_redirect_url @@ -19,11 +21,12 @@ module PermittedAttributes from_date kind ] - @@store_attributes = [ + @@store_attributes += [ :preferred_telegram_order_alert_chat_id, :preferred_telegram_order_request_alert_chat_id, { default_notification_image_attributes: {} } ] + @@checkout_attributes += %i[ phone_number country_code diff --git a/lib/spree_cm_commissioner/test_helper/factories/option_type_factory .rb b/lib/spree_cm_commissioner/test_helper/factories/option_type_factory .rb index fd49f0839..44fb60770 100644 --- a/lib/spree_cm_commissioner/test_helper/factories/option_type_factory .rb +++ b/lib/spree_cm_commissioner/test_helper/factories/option_type_factory .rb @@ -18,6 +18,11 @@ presentation { 'payment-option' } end - initialize_with { Spree::OptionType.where(name: name).first_or_initialize } + to_create do |instance| + Spree::OptionType.first_or_initialize(name: instance.name) do |object| + object.attributes = instance.attributes + object.save + end + end end end diff --git a/lib/spree_cm_commissioner/test_helper/factories/option_value_factory.rb b/lib/spree_cm_commissioner/test_helper/factories/option_value_factory.rb index f1b3b8fc6..aa4fd1f90 100644 --- a/lib/spree_cm_commissioner/test_helper/factories/option_value_factory.rb +++ b/lib/spree_cm_commissioner/test_helper/factories/option_value_factory.rb @@ -4,6 +4,11 @@ sequence(:name) { |n| "Size-#{n}" } presentation { 'S' } - initialize_with { Spree::OptionValue.where(name: name, option_type: option_type).first_or_initialize } + to_create do |instance| + Spree::OptionValue.first_or_initialize(name: instance.name) do |object| + object.attributes = instance.attributes + object.save + end + end end end diff --git a/spec/models/spree/webhooks/subscriber_spec.rb b/spec/models/spree/webhooks/subscriber_spec.rb index 56afa3460..6a1f7c042 100644 --- a/spec/models/spree/webhooks/subscriber_spec.rb +++ b/spec/models/spree/webhooks/subscriber_spec.rb @@ -25,6 +25,20 @@ end end + describe '#secret_key' do + it 'does not generate secret_key on build' do + subscriber = build(:cm_webhook_subscriber) + + expect(subscriber.secret_key).to be_nil + end + + it 'generate secret_key on create' do + subscriber = create(:cm_webhook_subscriber) + + expect(subscriber.secret_key).not_to be_nil + end + end + describe '#matches?' do let(:order) { build(:order) } let(:rule1) { build(:cm_webhook_subscriber_order_vendors_rule) } diff --git a/spec/serializers/spree/v2/storefront/product_serializer_spec.rb b/spec/serializers/spree/v2/storefront/product_serializer_spec.rb index ff27ea26c..390475dea 100644 --- a/spec/serializers/spree/v2/storefront/product_serializer_spec.rb +++ b/spec/serializers/spree/v2/storefront/product_serializer_spec.rb @@ -47,6 +47,7 @@ :kyc, :product_type, :reveal_description, + :localized_slugs, ) end diff --git a/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb b/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb index 2a1ed5162..e7412d066 100644 --- a/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb +++ b/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb @@ -6,8 +6,8 @@ let!(:taxon) { create(:taxon, category_icon: category_icon) } let!(:home_banner) { create(:cm_asset, viewable: taxon) } - subject { - described_class.new(taxon, include: [ + subject do + described_class.new(taxon, params: { 'store': Spree::Store.default }, include: [ :parent, :taxonomy, :children, @@ -17,7 +17,7 @@ :web_banner, :home_banner, ]).serializable_hash - } + end it 'returns exact taxon attributes' do expect(subject[:data][:attributes].keys).to contain_exactly( @@ -40,6 +40,7 @@ :is_leaf, :custom_redirect_url, :kind, + :localized_slugs, ) end diff --git a/spec/serializers/spree/v2/storefront/user_serializer_spec.rb b/spec/serializers/spree/v2/storefront/user_serializer_spec.rb index 7dc572031..cc2834774 100644 --- a/spec/serializers/spree/v2/storefront/user_serializer_spec.rb +++ b/spec/serializers/spree/v2/storefront/user_serializer_spec.rb @@ -23,7 +23,8 @@ :completed_orders, :phone_number, :intel_phone_number, - :country_code + :country_code, + :selected_locale, ) end diff --git a/spec/services/spree/webhooks/subscribers/handle_request_spec.rb b/spec/services/spree/webhooks/subscribers/handle_request_spec.rb new file mode 100644 index 000000000..680b000ea --- /dev/null +++ b/spec/services/spree/webhooks/subscribers/handle_request_spec.rb @@ -0,0 +1,26 @@ +require "spec_helper" + +RSpec.describe Spree::Webhooks::Subscribers::HandleRequest do + let(:subscriber) { create(:cm_webhook_subscriber) } + + describe '#request' do + subject { described_class.new(event_name: 'any.event', subscriber: subscriber, webhook_payload_body: '{"id": "fake"}') } + + let(:fake_payload_body) { subject.send(:body_with_event_metadata) } + let(:event) { subject.send(:event) } + + it 'construct make_request instance with singature' do + signature = event.signature_for(fake_payload_body) + + expect(Spree::Webhooks::Subscribers::MakeRequest).to receive(:new) + .with( + signature: signature, + url: subscriber.url, + api_key: subscriber.api_key, + webhook_payload_body: fake_payload_body + ) + + subject.request + end + end +end diff --git a/spec/services/spree_cm_commissioner/webhooks/subscribers/make_request_spec.rb b/spec/services/spree_cm_commissioner/webhooks/subscribers/make_request_spec.rb new file mode 100644 index 000000000..5a2d76f05 --- /dev/null +++ b/spec/services/spree_cm_commissioner/webhooks/subscribers/make_request_spec.rb @@ -0,0 +1,13 @@ +require "spec_helper" + +RSpec.describe SpreeCmCommissioner::Webhooks::Subscribers::MakeRequest do + describe '#headers' do + let(:request ) { described_class.new(signature: 'fake-signature', url: 'fake-url.com/create', api_key: 'fake-api-key', webhook_payload_body: {"id": "fake"}) } + + subject { request.headers } + + it { is_expected.to include('Content-Type' => 'application/json') } + it { is_expected.to include('X-Api-Key' => 'fake-api-key') } + it { is_expected.to include('X-Spree-Hmac-SHA256' => 'fake-signature') } + end +end diff --git a/spree_cm_commissioner.gemspec b/spree_cm_commissioner.gemspec index b1cf1645d..dcef02f7a 100644 --- a/spree_cm_commissioner.gemspec +++ b/spree_cm_commissioner.gemspec @@ -20,9 +20,9 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - spree_opts = '>= 4.5.0' + spree_opts = '>= 4.6.0' s.add_dependency 'spree', spree_opts - s.add_dependency 'spree_api_v1', spree_opts # latest spree_multi_vendor 2.4.0 still depends on the Spree v1 API + s.add_dependency 'spree_api_v1', '~> 4.5.0' # latest spree_multi_vendor 2.4.0 still depends on the Spree v1 API s.add_dependency 'spree_auth_devise', spree_opts s.add_dependency 'spree_backend', spree_opts s.add_dependency 'spree_multi_vendor', '>= 2.4.1'