solidus_subscriptions 1.0.0.rc1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +57 -9
  3. data/.github/dependabot.yml +7 -0
  4. data/.rubocop.yml +3 -0
  5. data/CHANGELOG.md +201 -163
  6. data/Gemfile +14 -1
  7. data/README.md +17 -7
  8. data/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb +25 -1
  9. data/app/controllers/spree/admin/subscriptions_controller.rb +32 -4
  10. data/app/decorators/models/solidus_subscriptions/spree/variant/auto_delete_from_subscriptions.rb +2 -2
  11. data/app/decorators/models/solidus_subscriptions/spree/wallet_payment_source/report_default_change_to_subscriptions.rb +2 -2
  12. data/app/jobs/solidus_subscriptions/create_subscription_job.rb +11 -0
  13. data/app/jobs/solidus_subscriptions/process_installment_job.rb +1 -1
  14. data/app/models/solidus_subscriptions/subscription.rb +75 -31
  15. data/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb +1 -0
  16. data/app/subscribers/solidus_subscriptions/event_storage_subscriber.rb +1 -0
  17. data/app/subscribers/solidus_subscriptions/order_subscriber.rb +16 -0
  18. data/app/views/spree/admin/shared/_subscription_actions.html.erb +27 -8
  19. data/app/views/spree/admin/shared/_subscription_sidebar.html.erb +2 -0
  20. data/app/views/spree/admin/subscriptions/_state_pill.html.erb +3 -2
  21. data/app/views/spree/admin/subscriptions/index.html.erb +50 -13
  22. data/bin/sandbox +1 -6
  23. data/config/locales/en.yml +16 -0
  24. data/config/routes.rb +9 -3
  25. data/db/migrate/20210905145955_add_paused_to_subscriptions.rb +5 -0
  26. data/{app/decorators → lib/decorators/api}/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb +14 -0
  27. data/lib/generators/solidus_subscriptions/install/install_generator.rb +16 -0
  28. data/lib/generators/solidus_subscriptions/install/templates/app/controllers/concerns/create_subscription.rb +17 -0
  29. data/lib/generators/solidus_subscriptions/install/templates/app/views/cart_line_items/_subscription_fields.html.erb +30 -0
  30. data/lib/generators/solidus_subscriptions/install/templates/initializer.rb +16 -1
  31. data/lib/solidus_subscriptions/checkout.rb +2 -9
  32. data/lib/solidus_subscriptions/configuration.rb +37 -8
  33. data/lib/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb +2 -2
  34. data/lib/solidus_subscriptions/dispatcher/success_dispatcher.rb +2 -2
  35. data/lib/solidus_subscriptions/engine.rb +28 -0
  36. data/lib/solidus_subscriptions/order_creator.rb +29 -0
  37. data/lib/solidus_subscriptions/permission_sets/default_customer.rb +1 -1
  38. data/lib/solidus_subscriptions/processing_error_handlers/rails_logger.rb +25 -0
  39. data/lib/solidus_subscriptions/subscription_generator.rb +6 -0
  40. data/lib/solidus_subscriptions/version.rb +1 -1
  41. data/lib/solidus_subscriptions.rb +1 -0
  42. data/solidus_subscriptions.gemspec +6 -6
  43. data/spec/controllers/spree/api/line_items_controller_spec.rb +63 -28
  44. data/spec/controllers/spree/api/users_controller_spec.rb +3 -0
  45. data/spec/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items_spec.rb +4 -4
  46. data/spec/decorators/models/solidus_subscriptions/spree/variant/auto_delete_from_subscriptions_spec.rb +2 -2
  47. data/spec/features/admin/subscriptions_spec.rb +13 -3
  48. data/spec/jobs/solidus_subscriptions/create_subscription_job_spec.rb +20 -0
  49. data/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb +17 -0
  50. data/spec/lib/solidus_subscriptions/dispatcher/payment_failed_dispatcher_spec.rb +3 -3
  51. data/spec/lib/solidus_subscriptions/dispatcher/success_dispatcher_spec.rb +3 -3
  52. data/spec/lib/solidus_subscriptions/subscription_generator_spec.rb +1 -1
  53. data/spec/models/solidus_subscriptions/installment_spec.rb +1 -1
  54. data/spec/models/solidus_subscriptions/subscription_spec.rb +893 -16
  55. data/spec/models/spree/wallet_payment_source_spec.rb +3 -3
  56. data/spec/requests/api/v1/subscriptions_spec.rb +229 -0
  57. data/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb +8 -8
  58. data/spec/subscribers/solidus_subscriptions/order_subscriber_spec.rb +13 -0
  59. metadata +31 -23
  60. data/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb +0 -23
  61. data/config/initializers/subscribers.rb +0 -9
  62. data/spec/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions_spec.rb +0 -32
  63. /data/{app/decorators → lib/decorators/frontend}/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb +0 -0
  64. /data/{app → lib}/views/spree/frontend/products/_subscription_line_item_fields.html.erb +0 -0
@@ -25,7 +25,7 @@ RSpec.describe 'Subscriptions admin' do
25
25
  expect(subscription.billing_address.zipcode).to eq('33167')
26
26
  end
27
27
 
28
- it 'Creates a subscription' do
28
+ it 'Creates a subscription' do # rubocop:disable RSpec/MultipleExpectations
29
29
  variant = create(:variant, subscribable: true)
30
30
  create(:user)
31
31
  create(:store)
@@ -53,10 +53,20 @@ RSpec.describe 'Subscriptions admin' do
53
53
  end
54
54
  end
55
55
 
56
- select variant.name, from: 'Subscribable'
56
+ select variant.pretty_name, from: 'Subscribable'
57
57
  fill_in 'Quantity', with: 1
58
58
 
59
- expect { click_on 'Create' }.to change { SolidusSubscriptions::Subscription.count }.by(1)
59
+ expect(SolidusSubscriptions::Subscription.count).to eq(0)
60
+
61
+ # The State field is controlled by JS, so we need to set it at the last moment
62
+ # available.
63
+ state_id = Spree::State.last.id.to_s
64
+ find('input#subscription_shipping_address_attributes_state_id', visible: :all).set(state_id)
65
+ find('input#subscription_billing_address_attributes_state_id', visible: :all).set(state_id)
66
+
67
+ click_on 'Create'
68
+
69
+ expect(SolidusSubscriptions::Subscription.count).to eq(1)
60
70
 
61
71
  expect(page).to have_text('Subscription has been successfully created!')
62
72
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe SolidusSubscriptions::CreateSubscriptionJob do
4
+ describe '#perform' do
5
+ it 'creates new subscriptions for an order' do
6
+ order = create(:order, :with_subscription_line_items)
7
+ subscription_line_item = order.subscription_line_items.last
8
+
9
+ described_class.perform_now(order)
10
+
11
+ expect(SolidusSubscriptions::Subscription.count).to eq(order.subscription_line_items.count)
12
+ subscription = SolidusSubscriptions::Subscription.last
13
+ expect(subscription).to have_attributes(
14
+ user_id: order.user_id,
15
+ actionable_date: Time.zone.today + subscription.interval,
16
+ line_items: [subscription_line_item]
17
+ )
18
+ end
19
+ end
20
+ end
@@ -12,7 +12,24 @@ RSpec.describe SolidusSubscriptions::ProcessInstallmentJob do
12
12
  end
13
13
 
14
14
  context 'when handling #perform errors' do
15
+ it 'by default logs exception data without raising exceptions' do # rubocop:disable RSpec/MultipleExpectations
16
+ installment = build_stubbed(:installment)
17
+ checkout = instance_double(SolidusSubscriptions::Checkout).tap do |c|
18
+ allow(c).to receive(:process).and_raise('test error')
19
+ end
20
+ allow(SolidusSubscriptions::Checkout).to receive(:new).and_return(checkout)
21
+ allow(Rails.logger).to receive(:error)
22
+
23
+ expect {
24
+ described_class.perform_now(installment)
25
+ }.not_to raise_error
26
+
27
+ expect(Rails.logger).to have_received(:error).with("Error processing installment with ID=#{installment.id}:").ordered
28
+ expect(Rails.logger).to have_received(:error).with("test error").ordered
29
+ end
30
+
15
31
  it 'swallows error when a proc is not configured' do
32
+ stub_config(processing_error_handler: nil )
16
33
  checkout = instance_double(SolidusSubscriptions::Checkout).tap do |c|
17
34
  allow(c).to receive(:process).and_raise('test error')
18
35
  end
@@ -27,15 +27,15 @@ RSpec.describe SolidusSubscriptions::Dispatcher::PaymentFailedDispatcher do
27
27
  end
28
28
 
29
29
  it 'fires an installments_failed_payment event' do
30
- stub_const('Spree::Event', class_spy(Spree::Event))
30
+ stub_const('SolidusSupport::LegacyEventCompat::Bus', class_spy(SolidusSupport::LegacyEventCompat::Bus))
31
31
  installment = instance_spy(SolidusSubscriptions::Installment)
32
32
  order = create(:order_with_line_items)
33
33
 
34
34
  dispatcher = described_class.new(installment, order)
35
35
  dispatcher.dispatch
36
36
 
37
- expect(Spree::Event).to have_received(:fire).with(
38
- 'solidus_subscriptions.installment_failed_payment',
37
+ expect(SolidusSupport::LegacyEventCompat::Bus).to have_received(:publish).with(
38
+ :'solidus_subscriptions.installment_failed_payment',
39
39
  installment: installment,
40
40
  order: order,
41
41
  )
@@ -13,15 +13,15 @@ RSpec.describe SolidusSubscriptions::Dispatcher::SuccessDispatcher do
13
13
  end
14
14
 
15
15
  it 'fires an installments_succeeded event' do
16
- stub_const('Spree::Event', class_spy(Spree::Event))
16
+ stub_const('SolidusSupport::LegacyEventCompat::Bus', class_spy(SolidusSupport::LegacyEventCompat::Bus))
17
17
  installment = instance_spy(SolidusSubscriptions::Installment)
18
18
  order = create(:order_with_line_items)
19
19
 
20
20
  dispatcher = described_class.new(installment, order)
21
21
  dispatcher.dispatch
22
22
 
23
- expect(Spree::Event).to have_received(:fire).with(
24
- 'solidus_subscriptions.installment_succeeded',
23
+ expect(SolidusSupport::LegacyEventCompat::Bus).to have_received(:publish).with(
24
+ :'solidus_subscriptions.installment_succeeded',
25
25
  installment: installment,
26
26
  order: order,
27
27
  )
@@ -75,7 +75,7 @@ RSpec.describe SolidusSubscriptions::SubscriptionGenerator do
75
75
  weekly_subscriptions,
76
76
  expiring_subscriptions,
77
77
  ]
78
- grouping_result = described_class.group(subscriptions.sum)
78
+ grouping_result = described_class.group(subscriptions.flatten)
79
79
 
80
80
  expect(grouping_result).to match_array(subscriptions)
81
81
  end
@@ -46,7 +46,7 @@ RSpec.describe SolidusSubscriptions::Installment, type: :model do
46
46
 
47
47
  it 'creates a new installment detail' do
48
48
  expect { success }.
49
- to change { SolidusSubscriptions::InstallmentDetail.count }.
49
+ to change(SolidusSubscriptions::InstallmentDetail, :count).
50
50
  by(1)
51
51
  end
52
52