super_good-solidus_taxjar 0.18.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +48 -6
  3. data/CHANGELOG.md +103 -1
  4. data/Gemfile +26 -10
  5. data/PULL_REQUEST_TEMPLATE.md +0 -1
  6. data/README.md +299 -39
  7. data/Rakefile +3 -0
  8. data/app/controllers/spree/admin/taxjar_settings_controller.rb +86 -1
  9. data/app/controllers/spree/admin/taxjar_transactions_controller.rb +37 -0
  10. data/app/controllers/spree/admin/transaction_sync_batches_controller.rb +18 -0
  11. data/app/jobs/super_good/solidus_taxjar/backfill_transaction_sync_batch_job.rb +23 -0
  12. data/app/jobs/super_good/solidus_taxjar/replace_transaction_job.rb +26 -0
  13. data/app/jobs/super_good/solidus_taxjar/report_transaction_job.rb +22 -0
  14. data/app/models/super_good/solidus_taxjar/configuration.rb +27 -0
  15. data/app/models/super_good/solidus_taxjar/order_transaction.rb +16 -0
  16. data/app/models/super_good/solidus_taxjar/refund_transaction.rb +13 -0
  17. data/app/models/super_good/solidus_taxjar/transaction_sync_batch.rb +11 -0
  18. data/app/models/super_good/solidus_taxjar/transaction_sync_log.rb +8 -0
  19. data/app/overrides/spree/admin/orders_controller_override.rb +12 -0
  20. data/app/overrides/spree/admin/shared/_order_submenu/add_taxjar_sync_history_tab.html.erb.deface +6 -0
  21. data/app/overrides/spree/admin/shared/_order_summary/add_taxjar_reported_at.html.erb.deface +30 -0
  22. data/app/overrides/spree/admin/shared/_taxes_tabs/add_configuration_menu_items.html.erb.deface +5 -0
  23. data/app/overrides/super_good/solidus_taxjar/spree/order_override.rb +21 -0
  24. data/app/views/spree/admin/orders/taxjar_transactions.html.erb +4 -0
  25. data/app/views/spree/admin/shared/_transaction_sync_log_table.html.erb +35 -0
  26. data/app/views/spree/admin/taxjar_settings/_nexus_regions.html.erb +23 -0
  27. data/app/views/spree/admin/taxjar_settings/_tax_categories.html.erb +41 -0
  28. data/app/views/spree/admin/taxjar_settings/edit.html.erb +17 -0
  29. data/app/views/spree/admin/taxjar_settings/edit_no_api_key.html.erb +21 -0
  30. data/app/views/spree/admin/transaction_sync_batches/index.html.erb +50 -0
  31. data/app/views/spree/admin/transaction_sync_batches/show.html.erb +7 -0
  32. data/bin/console +2 -0
  33. data/bin/rails-engine +1 -1
  34. data/bin/sandbox +43 -36
  35. data/bin/setup +3 -3
  36. data/config/routes.rb +13 -1
  37. data/db/migrate/20210908205201_create_taxjar_order_transactions.rb +16 -0
  38. data/db/migrate/20211008175113_create_taxjar_refund_transaction.rb +15 -0
  39. data/db/migrate/20211008183858_add_transaction_date_to_order_transaction.rb +5 -0
  40. data/db/migrate/20211119143354_create_configuration.rb +8 -0
  41. data/db/migrate/20220405213958_create_transaction_sync_batches.rb +7 -0
  42. data/db/migrate/20220405215225_create_transaction_sync_logs.rb +14 -0
  43. data/db/migrate/20220908181655_add_dates_to_transaction_sync_batch.rb +6 -0
  44. data/db/migrate/20220912182210_allow_null_transaction_sync_batches_on_logs.rb +5 -0
  45. data/db/migrate/20230320211309_add_refund_transaction_to_sync_logs.rb +5 -0
  46. data/lib/generators/super_good/solidus_taxjar/install/install_generator.rb +68 -0
  47. data/lib/super_good/solidus_taxjar/api.rb +41 -9
  48. data/lib/super_good/solidus_taxjar/api_params.rb +92 -25
  49. data/lib/super_good/solidus_taxjar/backfill_transactions.rb +11 -0
  50. data/lib/super_good/solidus_taxjar/cached_api.rb +23 -0
  51. data/lib/super_good/solidus_taxjar/calculator_helper.rb +33 -4
  52. data/lib/super_good/solidus_taxjar/discount_calculator.rb +1 -1
  53. data/lib/super_good/solidus_taxjar/overrides/request_override.rb +15 -0
  54. data/lib/super_good/solidus_taxjar/reportable.rb +91 -0
  55. data/lib/super_good/solidus_taxjar/reporting.rb +44 -0
  56. data/lib/super_good/solidus_taxjar/spree/legacy_reporting_subscriber.rb +41 -0
  57. data/lib/super_good/solidus_taxjar/spree/reporting_subscriber.rb +40 -0
  58. data/lib/super_good/solidus_taxjar/tax_calculator.rb +7 -1
  59. data/lib/super_good/solidus_taxjar/testing_support/factories/address_factory.rb +11 -0
  60. data/lib/super_good/solidus_taxjar/testing_support/factories/configuration_factory.rb +11 -0
  61. data/lib/super_good/solidus_taxjar/testing_support/factories/order_transaction_factory.rb +22 -0
  62. data/lib/super_good/solidus_taxjar/testing_support/factories/refund_transaction_factory.rb +7 -0
  63. data/lib/super_good/solidus_taxjar/testing_support/factories/transaction_sync_batch_factory.rb +9 -0
  64. data/lib/super_good/solidus_taxjar/testing_support/factories/transaction_sync_log_factory.rb +18 -0
  65. data/lib/super_good/solidus_taxjar/transaction_id_generator.rb +45 -0
  66. data/lib/super_good/solidus_taxjar/version.rb +1 -1
  67. data/lib/super_good/solidus_taxjar.rb +29 -2
  68. data/spec/features/spree/admin/backfill_transactions_spec.rb +138 -0
  69. data/spec/features/spree/admin/refund_spec.rb +167 -0
  70. data/spec/features/spree/admin/reporting_to_taxjar_spec.rb +156 -0
  71. data/spec/features/spree/admin/taxjar_settings_spec.rb +58 -16
  72. data/spec/features/spree/checkout_spec.rb +58 -0
  73. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/GET_sync_nexus_regions/Taxjar_API_token_is_not_set/doesn_t_make_a_request_for_the_nexus_regions.yml +57 -0
  74. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/GET_sync_tax_categories/Taxjar_API_token_is_not_set/doesn_t_make_a_request_for_the_tax_categories.yml +57 -0
  75. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/Taxjar_settings_tab/Taxjar_API_token_is_set/shows_the_settings_page.yml +2437 -0
  76. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/Taxjar_settings_tab/Taxjar_API_token_isn_t_set/doesn_t_show_any_other_TaxJar_features.yml +57 -0
  77. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/Taxjar_settings_tab/Taxjar_API_token_isn_t_set/shows_a_descriptive_error_message.yml +57 -0
  78. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/Taxjar_settings_tab/Taxjar_reporting_is_enabled/shows_that_reporting_is_enabled.yml +2382 -0
  79. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/Taxjar_settings_tab/order_is_shipped/the_user_backfills_their_transactions.yml +2511 -0
  80. data/spec/fixtures/cassettes/Admin_TaxJar_Settings/Taxjar_settings_tab/the_user_navigates_to_the_TaxJar_Settings.yml +2382 -0
  81. data/spec/fixtures/cassettes/Admin_Transaction_Sync_Batches/user_has_a_shipped_order/starts_a_transaction_backfill.yml +370 -0
  82. data/spec/fixtures/cassettes/Reporting_orders_to_TaxJar/shipping_a_complete_and_paid_order.yml +310 -0
  83. data/spec/fixtures/cassettes/Reporting_orders_to_TaxJar/updating_an_order_which_was_not_reported_due_to_failure/it_reports_the_order_instead_of_trying_to_replace_it.yml +794 -0
  84. data/spec/fixtures/cassettes/Reporting_orders_to_TaxJar/with_an_order_with_invalid_zipcode/retry_of_a_previously_failed_transaction_sync.yml +418 -0
  85. data/spec/fixtures/cassettes/Spree_Admin_TransactionSyncBatchesController/_create/creates_a_batch.yml +250 -0
  86. data/spec/fixtures/cassettes/Spree_Admin_TransactionSyncBatchesController/_create/creates_a_log_in_the_batch_with_an_order.yml +250 -0
  87. data/spec/fixtures/cassettes/Spree_Admin_TransactionSyncBatchesController/_create/user_supplies_a_start_date/creates_a_batch.yml +250 -0
  88. data/spec/fixtures/cassettes/Spree_Admin_TransactionSyncBatchesController/_create/user_supplies_a_start_date/user_supplies_start_and_end_date/creates_a_batch.yml +250 -0
  89. data/spec/fixtures/cassettes/SuperGood_SolidusTaxjar_CalculatorHelper/_taxable_address_/when_taxable_address_check_returns_true/with_US_address/when_the_address_is_not_within_a_nexus_region/1_3_2_2_2_1.yml +58 -0
  90. data/spec/fixtures/cassettes/SuperGood_SolidusTaxjar_CalculatorHelper/_taxable_address_/when_taxable_address_check_returns_true/with_US_address/when_the_address_is_within_a_nexus_region/1_3_2_2_1_1.yml +58 -0
  91. data/spec/fixtures/cassettes/SuperGood_SolidusTaxjar_Reporting/_refund_and_create_transaction/when_Taxjar_cannot_create_a_refund_transaction/doesn_t_create_a_new_transaction.yml +393 -0
  92. data/spec/fixtures/cassettes/SuperGood_SolidusTaxjar_Reporting/_refund_and_create_transaction/when_Taxjar_cannot_create_a_refund_transaction/raises_an_error.yml +393 -0
  93. data/spec/fixtures/cassettes/Taxjar_API_Request/logging_is_disabled/doesn_t_call_the_logger.yml +158 -0
  94. data/spec/fixtures/cassettes/Taxjar_API_Request/logging_is_enabled/calls_the_logger.yml +158 -0
  95. data/spec/fixtures/cassettes/features/spree/admin/checkout.yml +238 -0
  96. data/spec/fixtures/cassettes/features/spree/admin/refund.yml +1162 -0
  97. data/spec/jobs/super_good/solidus_taxjar/backfill_transaction_sync_batch_job_spec.rb +117 -0
  98. data/spec/jobs/super_good/solidus_taxjar/replace_transaction_job_spec.rb +95 -0
  99. data/spec/jobs/super_good/solidus_taxjar/report_transaction_job_spec.rb +76 -0
  100. data/spec/models/super_good/solidus_taxjar/configuration_spec.rb +79 -0
  101. data/spec/models/super_good/solidus_taxjar/order_transaction_spec.rb +36 -0
  102. data/spec/models/super_good/solidus_taxjar/transaction_sync_batch_spec.rb +48 -0
  103. data/spec/requests/spree/admin/order_request_spec.rb +121 -0
  104. data/spec/requests/spree/admin/taxjar_settings_request_spec.rb +198 -0
  105. data/spec/requests/spree/admin/taxjar_transactions_request_spec.rb +62 -0
  106. data/spec/requests/spree/admin/transaction_sync_batches_request_spec.rb +82 -0
  107. data/spec/spec_helper.rb +46 -3
  108. data/spec/subscribers/super_good/solidus_taxjar/spree/reporting_subscriber_spec.rb +278 -0
  109. data/spec/super_good/solidus_taxjar/addresses_spec.rb +0 -14
  110. data/spec/super_good/solidus_taxjar/api_params_spec.rb +261 -89
  111. data/spec/super_good/solidus_taxjar/api_spec.rb +152 -29
  112. data/spec/super_good/solidus_taxjar/backfill_transactions_spec.rb +24 -0
  113. data/spec/super_good/solidus_taxjar/cached_api_spec.rb +58 -0
  114. data/spec/super_good/solidus_taxjar/calculator_helper_spec.rb +131 -0
  115. data/spec/super_good/solidus_taxjar/discount_calculator_spec.rb +19 -2
  116. data/spec/super_good/solidus_taxjar/reportable_spec.rb +194 -0
  117. data/spec/super_good/solidus_taxjar/reporting_spec.rb +243 -0
  118. data/spec/super_good/solidus_taxjar/tax_calculator_spec.rb +19 -19
  119. data/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb +8 -3
  120. data/spec/super_good/solidus_taxjar/transaction_id_generator_spec.rb +77 -0
  121. data/spec/super_good/solidus_taxjar_spec.rb +84 -0
  122. data/spec/support/checkoutable_store_shared_context.rb +19 -0
  123. data/spec/support/solidus_events_helper.rb +26 -0
  124. data/spec/taxjar/api/request_spec.rb +52 -0
  125. data/super_good-solidus_taxjar.gemspec +3 -2
  126. metadata +169 -17
  127. data/app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb +0 -18
  128. data/app/overrides/spree/admin/shared/_configuration_menu.rb +0 -11
  129. data/app/views/spree/admin/taxjar_settings/show.html.erb +0 -13
  130. data/spec/models/spree/order_updater_spec.rb +0 -12
@@ -0,0 +1,117 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::BackfillTransactionSyncBatchJob do
4
+ describe "#perform" do
5
+ subject do
6
+ perform_enqueued_jobs do
7
+ described_class.new.perform(batch)
8
+ end
9
+ end
10
+
11
+ let!(:shipped_order) { create :shipped_order }
12
+ let(:reporting_mock) { instance_double ::SuperGood::SolidusTaxjar::Reporting }
13
+ let(:test_transaction_id) { "R1234-transaction" }
14
+ let(:batch) { create :transaction_sync_batch }
15
+
16
+ around do |example|
17
+ ::SuperGood::SolidusTaxjar.test_mode = true
18
+ example.run
19
+ ::SuperGood::SolidusTaxjar.test_mode = false
20
+ end
21
+
22
+ before do
23
+ reported_order = create :shipped_order
24
+ create(:taxjar_order_transaction, order: reported_order)
25
+ create :order_ready_to_ship
26
+
27
+ unreported_transaction = build(:taxjar_order_transaction, order: shipped_order, transaction_id: test_transaction_id)
28
+
29
+ allow(SuperGood::SolidusTaxjar).to receive(:reporting).and_return(reporting_mock)
30
+ allow(reporting_mock).to receive(:show_or_create_transaction) do
31
+ unreported_transaction.save!
32
+ unreported_transaction
33
+ end
34
+ end
35
+
36
+ it "reports the transaction to TaxJar" do
37
+ subject
38
+
39
+ expect(reporting_mock).to have_received(:show_or_create_transaction).with(shipped_order)
40
+ end
41
+
42
+ it "creates a log of each synced order in the database" do
43
+ subject
44
+ expect(batch.transaction_sync_logs.count).to eq(1)
45
+ expect(batch.transaction_sync_logs.last.order).to eq(shipped_order)
46
+ end
47
+
48
+ it "records the associated order, taxjar transaction, and status on each log" do
49
+ subject
50
+ sync_log = batch.transaction_sync_logs.last
51
+
52
+ expect(sync_log).to have_attributes(
53
+ order: shipped_order,
54
+ status: "success"
55
+ )
56
+ expect(sync_log.order_transaction).not_to be_nil
57
+ end
58
+
59
+ context "when the transaction cannot be created on TaxJar" do
60
+ before do
61
+ allow(reporting_mock)
62
+ .to receive(:show_or_create_transaction)
63
+ .and_raise(Taxjar::Error.new("api down"))
64
+ end
65
+
66
+ it "records a failure status in the log" do
67
+ subject
68
+ sync_log = batch.transaction_sync_logs.last
69
+
70
+ expect(sync_log).to have_attributes(
71
+ order: shipped_order,
72
+ status: "error",
73
+ error_message: /api down/
74
+ )
75
+
76
+ expect(sync_log.order_transaction).to be_nil
77
+ end
78
+ end
79
+
80
+ context "batch has start date" do
81
+ let(:batch) { create :transaction_sync_batch, start_date: 2.days.ago.to_date }
82
+ let(:start_date) { 2.days.ago.to_date }
83
+
84
+ before do
85
+ old_order = create :shipped_order
86
+ old_order.update_column(:completed_at, 3.days.ago)
87
+
88
+ shipped_order.update_column(:completed_at, Date.today.end_of_day)
89
+ end
90
+
91
+ it "syncs orders in the date range" do
92
+ subject
93
+ expect(batch.orders).to contain_exactly(shipped_order)
94
+ end
95
+
96
+ end
97
+
98
+ context "batch has start and end date" do
99
+ let(:batch) { create :transaction_sync_batch, start_date: 3.days.ago.to_date, end_date: 2.days.ago.to_date }
100
+
101
+ before do
102
+ old_order = create :shipped_order
103
+ old_order.update_column(:completed_at, 4.days.ago)
104
+
105
+ new_order = create :shipped_order
106
+ new_order.update_column(:completed_at, Date.today.end_of_day)
107
+
108
+ shipped_order.update_column(:completed_at, 2.days.ago)
109
+ end
110
+
111
+ it "syncs orders in the date range" do
112
+ subject
113
+ expect(batch.orders).to contain_exactly(shipped_order)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,95 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::ReplaceTransactionJob do
4
+ describe ".perform_later" do
5
+ subject { described_class.perform_later(order) }
6
+
7
+ let(:order) { create :order }
8
+ let(:mock_reporting) { instance_double ::SuperGood::SolidusTaxjar::Reporting }
9
+ let(:fake_order_transaction) { create :taxjar_order_transaction }
10
+ let!(:fake_refund_transaction) { create :taxjar_refund_transaction, order_transaction: fake_order_transaction }
11
+
12
+ before do
13
+ allow(mock_reporting)
14
+ .to receive(:refund_and_create_new_transaction)
15
+ .and_return(fake_order_transaction)
16
+ allow(SuperGood::SolidusTaxjar)
17
+ .to receive(:reporting)
18
+ .and_return(mock_reporting)
19
+ end
20
+
21
+ it "enqueues the job" do
22
+ assert_enqueued_with(job: described_class, args: [order]) do
23
+ subject
24
+ end
25
+ end
26
+
27
+ it "replaces the transaction when it performs the job" do
28
+ perform_enqueued_jobs do
29
+ subject
30
+ end
31
+
32
+ expect(mock_reporting)
33
+ .to have_received(:refund_and_create_new_transaction)
34
+ .with(order)
35
+ end
36
+
37
+ it "creates multiple transaction sync logs with the order transactions and refund transactions" do
38
+ expect { perform_enqueued_jobs { subject } }
39
+ .to change { ::SuperGood::SolidusTaxjar::TransactionSyncLog.count }
40
+ .by(1)
41
+
42
+ expect(::SuperGood::SolidusTaxjar::TransactionSyncLog.last)
43
+ .to have_attributes(
44
+ status: "success",
45
+ refund_transaction: fake_refund_transaction,
46
+ order_transaction: fake_order_transaction
47
+ )
48
+ end
49
+
50
+ context "when a replacing the transaction has already partially succeeded" do
51
+ let(:fake_order_transaction) { create :taxjar_order_transaction }
52
+ let(:fake_refund_transaction) { nil }
53
+
54
+ before do
55
+ allow(mock_reporting)
56
+ .to receive(:refund_and_create_new_transaction)
57
+ .and_return(fake_order_transaction)
58
+ end
59
+
60
+ it "creates a transaction sync log with the order transaction" do
61
+ expect { perform_enqueued_jobs { subject } }
62
+ .to change { ::SuperGood::SolidusTaxjar::TransactionSyncLog.count }
63
+ .by(1)
64
+
65
+ expect(::SuperGood::SolidusTaxjar::TransactionSyncLog.last)
66
+ .to have_attributes(
67
+ status: "success",
68
+ order_transaction: fake_order_transaction,
69
+ refund_transaction: nil
70
+ )
71
+ end
72
+ end
73
+
74
+ context "when replacing the transaction throws an error" do
75
+ before do
76
+ allow(mock_reporting)
77
+ .to receive(:refund_and_create_new_transaction)
78
+ .and_raise(Taxjar::Error.new("something bad"))
79
+ end
80
+
81
+ it "creates a transaction sync log with the error message" do
82
+ expect { perform_enqueued_jobs {subject} }
83
+ .to change { ::SuperGood::SolidusTaxjar::TransactionSyncLog.count }
84
+ .from(0)
85
+ .to(1)
86
+
87
+ expect(::SuperGood::SolidusTaxjar::TransactionSyncLog.last)
88
+ .to have_attributes(
89
+ status: "error",
90
+ error_message: "something bad"
91
+ )
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,76 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::ReportTransactionJob do
4
+ describe ".perform_now" do
5
+ subject { described_class.perform_now(order) }
6
+
7
+ let(:order) { create :order }
8
+ let(:mock_reporting) { instance_double ::SuperGood::SolidusTaxjar::Reporting }
9
+ let(:fake_order_transaction) { create :taxjar_order_transaction }
10
+
11
+ before do
12
+ allow(mock_reporting)
13
+ .to receive(:show_or_create_transaction)
14
+ .and_return(fake_order_transaction)
15
+ allow(SuperGood::SolidusTaxjar).to receive(:reporting).and_return(mock_reporting)
16
+ end
17
+
18
+ it "reports the transaction when it performs the job" do
19
+ subject
20
+
21
+ expect(mock_reporting).to have_received(:show_or_create_transaction).with(order)
22
+ end
23
+
24
+ it "creates a transaction sync log with the order transaction" do
25
+ expect { subject }
26
+ .to change { ::SuperGood::SolidusTaxjar::TransactionSyncLog.count }
27
+ .from(0)
28
+ .to(1)
29
+
30
+ expect(::SuperGood::SolidusTaxjar::TransactionSyncLog.last)
31
+ .to have_attributes(
32
+ status: "success",
33
+ order_transaction: fake_order_transaction
34
+ )
35
+ end
36
+
37
+ context "when a transaction sync batch is passed" do
38
+ subject { described_class.perform_now(order, transaction_sync_batch) }
39
+
40
+ let(:transaction_sync_batch) { create :transaction_sync_batch }
41
+
42
+ it "creates a transaction sync log associated with the given batch" do
43
+ expect { subject }
44
+ .to change { ::SuperGood::SolidusTaxjar::TransactionSyncLog.count }
45
+ .from(0)
46
+ .to(1)
47
+
48
+ expect(::SuperGood::SolidusTaxjar::TransactionSyncLog.last)
49
+ .to have_attributes(
50
+ transaction_sync_batch: transaction_sync_batch
51
+ )
52
+ end
53
+ end
54
+
55
+ context "when syncing to taxjar fails" do
56
+ before do
57
+ allow(mock_reporting)
58
+ .to receive(:show_or_create_transaction)
59
+ .and_raise(Taxjar::Error.new("something bad"))
60
+ end
61
+
62
+ it "creates a transaction sync log with the error message" do
63
+ expect { subject }
64
+ .to change { ::SuperGood::SolidusTaxjar::TransactionSyncLog.count }
65
+ .from(0)
66
+ .to(1)
67
+
68
+ expect(::SuperGood::SolidusTaxjar::TransactionSyncLog.last)
69
+ .to have_attributes(
70
+ status: "error",
71
+ error_message: "something bad"
72
+ )
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,79 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::Configuration do
4
+ describe "is valid" do
5
+ subject { taxjar_configuration.valid? }
6
+
7
+ let(:taxjar_configuration) { create(:taxjar_configuration) }
8
+
9
+ it "is valid" do
10
+ expect(subject).to be_truthy
11
+ end
12
+ end
13
+
14
+ describe "#preferred_reporting_enabled" do
15
+ subject { taxjar_configuration.preferred_reporting_enabled }
16
+
17
+ let(:taxjar_configuration) { create(:taxjar_configuration) }
18
+
19
+ it "has the default value" do
20
+ expect(subject).to be_falsey
21
+ end
22
+
23
+ context "reporting_enabled_at is set in the past" do
24
+ let(:taxjar_configuration) { create(:taxjar_configuration, preferred_reporting_enabled_at_integer: DateTime.now.to_i) }
25
+
26
+ it "is true" do
27
+ expect(subject).to be_truthy
28
+ end
29
+ end
30
+
31
+ context "reporting_enabled_at is set in the future" do
32
+ let(:taxjar_configuration) { create(:taxjar_configuration, preferred_reporting_enabled_at_integer: (DateTime.now + 1.day).to_i) }
33
+
34
+ it "is false" do
35
+ expect(subject).to be_falsy
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "#preferred_reporting_enabled_at" do
41
+ subject { taxjar_configuration.preferred_reporting_enabled_at }
42
+
43
+ let(:taxjar_configuration) { create(:taxjar_configuration, preferred_reporting_enabled_at_integer: datetime_integer) }
44
+ let(:datetime_integer) { 1675116247 }
45
+
46
+ it "parses the integer datetime" do
47
+ expect(subject).to eq DateTime.parse("Mon, 30 Jan 2023 22:04:07 +0000")
48
+ end
49
+ end
50
+
51
+ describe ".default" do
52
+ subject { described_class.default }
53
+
54
+ context "configuration exists" do
55
+ let!(:taxjar_configuration) { create :taxjar_configuration }
56
+
57
+ it "returns the default configuration" do
58
+ expect(subject).to eq(taxjar_configuration)
59
+ end
60
+ end
61
+
62
+ context "configuration doesn't exist" do
63
+ it "creates the default config" do
64
+ expect { subject }.to change { described_class.count }.from(0).to(1)
65
+ end
66
+
67
+ it "returns a config" do
68
+ expect(subject).to be_instance_of(described_class)
69
+ end
70
+
71
+ context "calling default more than once" do
72
+ it "creates only one config" do
73
+ described_class.default
74
+ expect { subject }.to_not change { described_class.count }.from(1)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::OrderTransaction do
4
+ describe ".latest_for" do
5
+ subject { described_class.latest_for(order) }
6
+
7
+ let(:order) { create(:order) }
8
+
9
+ context "when there are no order transactions" do
10
+ it { is_expected.to be_nil }
11
+ end
12
+
13
+ context "when there are one or more order transactions" do
14
+ let(:transaction_date) { 1.day.ago }
15
+
16
+ let!(:first_order_transaction) {
17
+ create(
18
+ :taxjar_order_transaction,
19
+ transaction_date: transaction_date,
20
+ order: order
21
+ )
22
+ }
23
+ let!(:latest_order_transaction) {
24
+ create(
25
+ :taxjar_order_transaction,
26
+ transaction_date: transaction_date,
27
+ order: order
28
+ )
29
+ }
30
+
31
+ it "returns the most recent order transaction" do
32
+ expect(subject).to eq latest_order_transaction
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::TransactionSyncBatch do
4
+ describe "#status" do
5
+ subject { transaction_sync_batch.status }
6
+
7
+ let(:transaction_sync_batch) { create(:transaction_sync_batch, transaction_sync_logs: transaction_sync_logs) }
8
+
9
+ context "all logs have status 'success'" do
10
+ let(:transaction_sync_logs) { build_list(:transaction_sync_log, 2, status: :success) }
11
+
12
+ it "returns 'success'" do
13
+ expect(subject).to eq "success"
14
+ end
15
+ end
16
+
17
+ context "one or more logs have status 'error'" do
18
+ let(:transaction_sync_logs) { [
19
+ build(:transaction_sync_log, status: :success),
20
+ build(:transaction_sync_log, status: :error)
21
+ ] }
22
+
23
+ it "returns 'error'" do
24
+ expect(subject).to eq "error"
25
+ end
26
+ end
27
+
28
+ context "one or more logs have status 'processing'" do
29
+ let(:transaction_sync_logs) { [
30
+ build(:transaction_sync_log, status: :success),
31
+ build(:transaction_sync_log, status: :processing),
32
+ build(:transaction_sync_log, status: :error)
33
+ ] }
34
+
35
+ it "returns 'processing'" do
36
+ expect(subject).to eq "processing"
37
+ end
38
+ end
39
+
40
+ context "with an empty batch" do
41
+ let(:transaction_sync_logs) { [] }
42
+
43
+ it "returns 'success'" do
44
+ expect(subject).to eq "success"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Spree::Admin::OrdersController, :type => :request do
4
+ include Capybara::RSpecMatchers
5
+
6
+ extend Spree::TestingSupport::AuthorizationHelpers::Request
7
+ stub_authorization!
8
+
9
+ describe "#edit" do
10
+ subject { get spree.edit_admin_order_path(order) }
11
+
12
+ let!(:order) { create(:shipped_order) }
13
+ let(:reporting_enabled_at) { 1.day.ago.to_i }
14
+
15
+ around do |example|
16
+ begin
17
+ old_value = SuperGood::SolidusTaxjar
18
+ .configuration
19
+ .preferred_reporting_enabled_at_integer
20
+ SuperGood::SolidusTaxjar.configuration.update!(
21
+ preferred_reporting_enabled_at_integer: reporting_enabled_at
22
+ )
23
+ example.run
24
+ ensure
25
+ SuperGood::SolidusTaxjar.configuration.update!(
26
+ preferred_reporting_enabled_at_integer: old_value
27
+ )
28
+ end
29
+ end
30
+
31
+ it "displays an empty value for the TaxJar reported at time" do
32
+ subject
33
+ expect(response.body).to have_text("Reported to TaxJar at: -", normalize_ws: true)
34
+ end
35
+
36
+ it "displays a 'pending' status" do
37
+ subject
38
+ expect(response.body).to have_text("TaxJar Sync: Pending", normalize_ws: true)
39
+ end
40
+
41
+ it "displays a link to view the TaxJar Sync History" do
42
+ subject
43
+ expect(response.body).to have_link("TaxJar Sync History", href: spree.taxjar_transactions_admin_order_path(order))
44
+ end
45
+
46
+ context "if the order has reported transactions" do
47
+ let(:latest_sync_log_status) { :success }
48
+
49
+ before do
50
+ create :transaction_sync_log, :success, order: order, created_at: DateTime.new(2022,01,01,12,12)
51
+ create(
52
+ :transaction_sync_log,
53
+ order: order,
54
+ created_at: DateTime.new(2022,06,06,12,12),
55
+ order_transaction: create(:taxjar_order_transaction, order: order),
56
+ status: latest_sync_log_status
57
+ )
58
+ end
59
+
60
+ it "displays the date and time of the most recent transaction sync" do
61
+ subject
62
+ expect(response.body).to have_text("Reported to TaxJar at: June 06, 2022 12:12 PM", normalize_ws: true)
63
+ end
64
+
65
+ it "displays the status of the transaction sync log" do
66
+ subject
67
+ expect(response.body).to have_text("TaxJar Sync: #{latest_sync_log_status.capitalize}", normalize_ws: true)
68
+ end
69
+ end
70
+
71
+ context "when transaction syncing is turned off" do
72
+ let(:reporting_enabled_at) { nil }
73
+
74
+ it "displays a 'disabled' status" do
75
+ subject
76
+ expect(response.body).to have_text("TaxJar Sync: Disabled", normalize_ws: true)
77
+ end
78
+
79
+ context "and a transaction sync log exists" do
80
+ let(:latest_sync_log_status) { :success }
81
+
82
+ before do
83
+ create(
84
+ :transaction_sync_log,
85
+ order: order,
86
+ created_at: DateTime.new(2022,06,06,12,12),
87
+ order_transaction: create(:taxjar_order_transaction, order: order),
88
+ status: latest_sync_log_status
89
+ )
90
+ end
91
+
92
+ it "displays the status of the transaction sync log" do
93
+ subject
94
+ expect(response.body).to have_text("TaxJar Sync: #{latest_sync_log_status.capitalize}", normalize_ws: true)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#taxjar_transactions" do
101
+ subject { get spree.taxjar_transactions_admin_order_path(order) }
102
+
103
+ let!(:order) { create(:shipped_order) }
104
+ let(:order_transaction) { create :taxjar_order_transaction, transaction_id: "Test-123", refund_transaction: refund_transaction}
105
+ let(:refund_transaction) { create :taxjar_refund_transaction, transaction_id: "Test-123-refund"}
106
+
107
+ before do
108
+ create :transaction_sync_log, order: order, order_transaction: order_transaction, status: :success
109
+ end
110
+
111
+ it "renders the taxjar transactions page for the order" do
112
+ subject
113
+ expect(response.body).to have_text(/TaxJar Sync History - #?#{order.number} - Orders/)
114
+ end
115
+
116
+ it "renders the transaction sync logs" do
117
+ subject
118
+ expect(response.body).to have_text("#{order.number} Test-123 Test-123-refund Success", normalize_ws: true)
119
+ end
120
+ end
121
+ end