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,243 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::Reporting do
4
+ let(:dummy_api) { instance_double ::SuperGood::SolidusTaxjar::Api }
5
+ let(:order) { build :order, completed_at: 1.days.ago }
6
+ let(:reporting) { described_class.new(api: dummy_api) }
7
+ let(:test_order_transaction_id) { "R1234-transaction" }
8
+ let(:test_refund_transaction_id) { "R1234-old-transaction-REFUND" }
9
+ let(:test_transaction_date) { Date.new(2022, 1, 1) }
10
+ let(:taxjar_order_response_double) {
11
+ double(
12
+ "Taxjar::Order",
13
+ transaction_id: test_order_transaction_id,
14
+ transaction_date: test_transaction_date
15
+ )
16
+ }
17
+ let(:taxjar_refund_response_double) {
18
+ double(
19
+ "Taxjar::Refund",
20
+ transaction_id: test_refund_transaction_id,
21
+ transaction_date: test_transaction_date
22
+ )
23
+ }
24
+
25
+ describe "#refund_and_create_transaction" do
26
+ subject { reporting.refund_and_create_new_transaction(order) }
27
+
28
+ let(:order) { create :order_with_totals, completed_at: 1.days.ago, number: "R1234-old-transaction" }
29
+ let!(:order_transaction_to_refund) {
30
+ create(
31
+ :taxjar_order_transaction,
32
+ transaction_id: "R1234-old-transaction",
33
+ transaction_date: Date.new(2021, 1, 1),
34
+ order: order
35
+ )
36
+ }
37
+
38
+ before do
39
+ allow(dummy_api)
40
+ .to receive(:create_refund_transaction_for)
41
+ .and_return(taxjar_refund_response_double)
42
+
43
+ allow(dummy_api)
44
+ .to receive(:create_transaction_for)
45
+ .and_return(taxjar_order_response_double)
46
+ end
47
+
48
+ it "refunds the transaction and creates a new one in TaxJar" do
49
+ subject
50
+
51
+ expect(dummy_api)
52
+ .to have_received(:create_refund_transaction_for)
53
+ .with(order)
54
+ expect(dummy_api)
55
+ .to have_received(:create_transaction_for)
56
+ .with(order)
57
+ end
58
+
59
+ it "creates a refund transaction record" do
60
+ expect { subject }
61
+ .to change { SuperGood::SolidusTaxjar::RefundTransaction.count }
62
+ .from(0)
63
+ .to(1)
64
+
65
+ expect(SuperGood::SolidusTaxjar::RefundTransaction.last).to have_attributes(
66
+ transaction_id: test_refund_transaction_id,
67
+ transaction_date: test_transaction_date,
68
+ order_transaction: order_transaction_to_refund
69
+ )
70
+ end
71
+
72
+ it "creates and returns a transaction for the order" do
73
+ expect(subject).to be_a(SuperGood::SolidusTaxjar::OrderTransaction)
74
+ expect(subject.persisted?).to be_truthy
75
+ expect(subject).to have_attributes(
76
+ transaction_id: test_order_transaction_id,
77
+ transaction_date: test_transaction_date
78
+ )
79
+ end
80
+
81
+ context "when the refund is for the full amount of the order" do
82
+ let(:order) { create :order, completed_at: 1.days.ago, number: "R1234-old-transaction" }
83
+
84
+ it "refunds the transaction" do
85
+ subject
86
+
87
+ expect(dummy_api)
88
+ .to have_received(:create_refund_transaction_for)
89
+ .with(order)
90
+ end
91
+
92
+ it "doesn't create a new transaction in taxjar" do
93
+ subject
94
+
95
+ expect(dummy_api)
96
+ .not_to have_received(:create_transaction_for)
97
+ end
98
+
99
+ it "creates a refund transaction record" do
100
+ expect { subject }
101
+ .to change { SuperGood::SolidusTaxjar::RefundTransaction.count }
102
+ .from(0)
103
+ .to(1)
104
+
105
+ expect(SuperGood::SolidusTaxjar::RefundTransaction.last).to have_attributes(
106
+ transaction_id: test_refund_transaction_id,
107
+ transaction_date: test_transaction_date,
108
+ order_transaction: order_transaction_to_refund
109
+ )
110
+ end
111
+
112
+ it "returns nil" do
113
+ expect(subject).to be_nil
114
+ end
115
+ end
116
+
117
+ context "when Taxjar cannot create a refund transaction", :vcr do
118
+ let(:reporting) { described_class.new }
119
+ let(:order) { create(:completed_order_with_totals, number: "R1234-old-transaction") }
120
+ let!(:tax_rate) { create(:tax_rate, name: "Sales Tax") }
121
+
122
+ # We ensure that TaxJar cannot create a refund transaction refunding it
123
+ # *before* the test scenario.
124
+ before do
125
+ taxjar_api_client = ::SuperGood::SolidusTaxjar::Api.default_taxjar_client
126
+ begin
127
+ taxjar_api_client.show_order("R1234-old-transaction")
128
+ rescue Taxjar::Error::NotFound
129
+ taxjar_client.create_order(
130
+ ApiParams.transaction_params(order, "R1234-old-transaction")
131
+ )
132
+ end
133
+
134
+ begin
135
+ taxjar_api_client.show_refund("R1234-old-transaction-REFUND")
136
+ rescue Taxjar::Error::NotFound
137
+ taxjar_client.create_refund(
138
+ ApiParams.refund_transaction_params(order, order_transaction_to_refund)
139
+ )
140
+ end
141
+ end
142
+
143
+ it "raises an error" do
144
+ expect { subject }.to raise_error(
145
+ Taxjar::Error::UnprocessableEntity,
146
+ "Provider tranx already imported for your user account"
147
+ )
148
+ end
149
+
150
+ it "doesn't create a new transaction" do
151
+ expect(SuperGood::SolidusTaxjar.api)
152
+ .not_to receive(:create_transaction_for)
153
+
154
+ begin
155
+ subject
156
+ rescue Taxjar::Error::UnprocessableEntity
157
+ nil
158
+ end
159
+ end
160
+ end
161
+
162
+ context "when a partially completed refund exists in the database" do
163
+ before do
164
+ create :taxjar_refund_transaction, order_transaction: order_transaction_to_refund
165
+ end
166
+
167
+ it "skips creating the refund transaction and model" do
168
+ expect { subject }
169
+ .not_to change { SuperGood::SolidusTaxjar::RefundTransaction.count }
170
+
171
+ expect(dummy_api)
172
+ .not_to have_received(:create_refund_transaction_for)
173
+ end
174
+
175
+ it "creates a new order transaction" do
176
+ expect { subject }
177
+ .to change { SuperGood::SolidusTaxjar::OrderTransaction.count }
178
+ .from(1)
179
+ .to(2)
180
+
181
+ expect(dummy_api)
182
+ .to have_received(:create_transaction_for)
183
+ end
184
+ end
185
+ end
186
+
187
+ describe "#show_or_create_transaction" do
188
+ subject { reporting.show_or_create_transaction(order) }
189
+
190
+ context "the order has an existing transaction" do
191
+ before do
192
+ create :taxjar_order_transaction, transaction_id: test_order_transaction_id, transaction_date: test_transaction_date
193
+ end
194
+
195
+ it "returns the existing taxjar order transaction record" do
196
+ allow(dummy_api)
197
+ .to receive(:show_latest_transaction_for)
198
+ .and_return(taxjar_order_response_double)
199
+
200
+ subject
201
+
202
+ expect(dummy_api)
203
+ .to have_received(:show_latest_transaction_for)
204
+ .with(order)
205
+
206
+ expect(subject).to be_a(SuperGood::SolidusTaxjar::OrderTransaction)
207
+ expect(subject.persisted?).to be_truthy
208
+ expect(subject).to have_attributes(
209
+ transaction_id: test_order_transaction_id,
210
+ transaction_date: test_transaction_date
211
+ )
212
+ end
213
+ end
214
+
215
+ context "order doesn't have a transaction" do
216
+ let(:order) { create :order, completed_at: 1.days.ago }
217
+
218
+ context "TaxJar has no record of the transaction" do
219
+ it "creates a transaction for the order" do
220
+ allow(dummy_api)
221
+ .to receive(:show_latest_transaction_for)
222
+ .and_return(nil)
223
+
224
+ allow(dummy_api)
225
+ .to receive(:create_transaction_for)
226
+ .and_return(taxjar_order_response_double)
227
+
228
+ subject
229
+
230
+ expect(dummy_api).to have_received(:show_latest_transaction_for).with(order)
231
+ expect(dummy_api).to have_received(:create_transaction_for).with(order)
232
+
233
+ expect(subject).to be_a(SuperGood::SolidusTaxjar::OrderTransaction)
234
+ expect(subject.persisted?).to be_truthy
235
+ expect(subject).to have_attributes(
236
+ transaction_id: test_order_transaction_id,
237
+ transaction_date: test_transaction_date
238
+ )
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
@@ -77,7 +77,6 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
77
77
  context "when the order has an incomplete tax address" do
78
78
  let(:address) do
79
79
  ::Spree::Address.new(
80
- first_name: "Ronnie James",
81
80
  zipcode: nil,
82
81
  address1: nil,
83
82
  city: "Beverly Hills",
@@ -106,7 +105,6 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
106
105
  context "when the order has no line items" do
107
106
  let(:address) do
108
107
  ::Spree::Address.new(
109
- first_name: "Ronnie James",
110
108
  zipcode: "90210",
111
109
  address1: "9900 Wilshire Blvd",
112
110
  city: "Beverly Hills",
@@ -127,7 +125,6 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
127
125
  context "when the API encounters an error" do
128
126
  let(:address) do
129
127
  ::Spree::Address.new(
130
- first_name: "Ronnie James",
131
128
  zipcode: "90210",
132
129
  address1: "9900 Wilshire Blvd",
133
130
  city: "Beverly Hills",
@@ -137,7 +134,13 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
137
134
  end
138
135
 
139
136
  before do
140
- allow(dummy_api).to receive(:tax_for).with(order).and_raise("A bad thing happened.")
137
+ allow(calculator)
138
+ .to receive(:taxable_address?).with(address)
139
+ .and_return(true)
140
+ allow(dummy_api)
141
+ .to receive(:tax_for)
142
+ .with(order)
143
+ .and_raise("A bad thing happened.")
141
144
  end
142
145
 
143
146
  it "calls the configured error handler" do
@@ -159,7 +162,6 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
159
162
  context "when the order has a non-empty tax address" do
160
163
  let(:address) do
161
164
  ::Spree::Address.new(
162
- first_name: "Ronnie James",
163
165
  zipcode: "90210",
164
166
  address1: "9900 Wilshire Blvd",
165
167
  city: "Beverly Hills",
@@ -178,14 +180,6 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
178
180
  end
179
181
 
180
182
  context "and there is tax" do
181
- let!(:tax_rate) do
182
- ::Spree::TaxRate.create!(
183
- name: "Sales Tax",
184
- amount: 0.5,
185
- calculator: ::Spree::Calculator.new
186
- )
187
- end
188
-
189
183
  let(:breakdown) do
190
184
  instance_double ::Taxjar::Breakdown,
191
185
  line_items: [taxjar_line_item],
@@ -199,6 +193,12 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
199
193
 
200
194
  let(:shipping_tax_breakdown) { nil }
201
195
 
196
+ before do
197
+ allow(calculator)
198
+ .to receive(:taxable_address?).with(address)
199
+ .and_return(true)
200
+ end
201
+
202
202
  it "returns the taxes" do
203
203
  expect(subject.order_id).to eq order.id
204
204
  expect(subject.shipment_taxes).to be_empty
@@ -209,7 +209,7 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
209
209
  aggregate_failures do
210
210
  expect(item_tax.item_id).to eq 33
211
211
  expect(item_tax.label).to eq "Sales Tax"
212
- expect(item_tax.tax_rate).to eq tax_rate
212
+ expect(item_tax.tax_rate).to be_a(Spree::TaxRate)
213
213
  expect(item_tax.amount).to eq 6.66
214
214
  expect(item_tax.included_in_price).to eq false
215
215
  end
@@ -231,8 +231,8 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
231
231
 
232
232
  context "but the taxable address check returns false" do
233
233
  before do
234
- allow(SuperGood::SolidusTaxjar.taxable_address_check)
235
- .to receive(:call).with(address)
234
+ allow(calculator)
235
+ .to receive(:taxable_address?).with(address)
236
236
  .and_return(false)
237
237
  end
238
238
 
@@ -269,19 +269,19 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxCalculator do
269
269
  aggregate_failures do
270
270
  expect(shipment_taxes[0].item_id).to eq 1
271
271
  expect(shipment_taxes[0].label).to eq "Sales Tax"
272
- expect(shipment_taxes[0].tax_rate).to eq tax_rate
272
+ expect(shipment_taxes[0].tax_rate).to be_a(Spree::TaxRate)
273
273
  expect(shipment_taxes[0].amount).to eq 2.33
274
274
  expect(shipment_taxes[0].included_in_price).to eq false
275
275
 
276
276
  expect(shipment_taxes[1].item_id).to eq 2
277
277
  expect(shipment_taxes[1].label).to eq "Sales Tax"
278
- expect(shipment_taxes[1].tax_rate).to eq tax_rate
278
+ expect(shipment_taxes[1].tax_rate).to be_a(Spree::TaxRate)
279
279
  expect(shipment_taxes[1].amount).to eq 4.33
280
280
  expect(shipment_taxes[1].included_in_price).to eq false
281
281
 
282
282
  expect(shipment_taxes[2].item_id).to eq 3
283
283
  expect(shipment_taxes[2].label).to eq "Sales Tax"
284
- expect(shipment_taxes[2].tax_rate).to eq tax_rate
284
+ expect(shipment_taxes[2].tax_rate).to be_a(Spree::TaxRate)
285
285
  expect(shipment_taxes[2].amount).to eq 3.34
286
286
  expect(shipment_taxes[2].included_in_price).to eq false
287
287
  end
@@ -18,7 +18,6 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxRateCalculator do
18
18
 
19
19
  let(:incomplete_address) do
20
20
  ::Spree::Address.new(
21
- first_name: "Ronnie James",
22
21
  zipcode: nil,
23
22
  address1: nil,
24
23
  city: "Beverly Hills",
@@ -64,8 +63,8 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxRateCalculator do
64
63
 
65
64
  context "when the address is not taxable" do
66
65
  before do
67
- allow(SuperGood::SolidusTaxjar.taxable_address_check)
68
- .to receive(:call).with(address)
66
+ allow(calculator)
67
+ .to receive(:taxable_address?).with(address)
69
68
  .and_return(false)
70
69
  end
71
70
 
@@ -76,6 +75,9 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxRateCalculator do
76
75
  let(:tax_rate) { 0.03 }
77
76
 
78
77
  before do
78
+ allow(calculator)
79
+ .to receive(:taxable_address?).with(address)
80
+ .and_return(true)
79
81
  allow(dummy_api).to receive(:tax_rate_for) { tax_rate }
80
82
  end
81
83
 
@@ -89,6 +91,9 @@ RSpec.describe ::SuperGood::SolidusTaxjar::TaxRateCalculator do
89
91
  let(:address) { complete_address }
90
92
 
91
93
  before do
94
+ allow(calculator)
95
+ .to receive(:taxable_address?).with(address)
96
+ .and_return(true)
92
97
  allow(dummy_api).to receive(:tax_rate_for).and_raise("A bad thing happened.")
93
98
  end
94
99
 
@@ -0,0 +1,77 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperGood::SolidusTaxjar::TransactionIdGenerator do
4
+ describe ".next_transaction_id" do
5
+ subject {
6
+ described_class.next_transaction_id(
7
+ order: order,
8
+ current_transaction_id: current_transaction_id
9
+ )
10
+ }
11
+
12
+ let(:order) { build :order, number: order_number }
13
+ let(:order_number) { "R1234567890" }
14
+
15
+ context "without a `current_transaction_id`" do
16
+ subject { described_class.next_transaction_id(order: order) }
17
+
18
+ it "uses the order.number as the transaction_id" do
19
+ expect(subject).to eq("R1234567890")
20
+ end
21
+
22
+ context "and there are dashes in the order number" do
23
+ let(:order_number) { "R123-456-7890" }
24
+
25
+ it "uses the order.number as the transaction_id" do
26
+ expect(subject).to eq("R123-456-7890")
27
+ end
28
+ end
29
+ end
30
+
31
+ context "with a `current_transaction_id`" do
32
+ context "when the current transaction ID does not have a suffix" do
33
+ let(:order_number) { "R1234567890" }
34
+ let(:current_transaction_id) { "R1234567890" }
35
+
36
+ it "generates the next sequential transaction_id" do
37
+ expect(subject).to eq("R1234567890-1")
38
+ end
39
+ end
40
+
41
+ context "when the current transaction ID has a suffix" do
42
+ let(:order_number) { "R1234567890" }
43
+ let(:current_transaction_id) { "R1234567890-2" }
44
+
45
+ it "generates the next sequential transaction_id" do
46
+ expect(subject).to eq("R1234567890-3")
47
+ end
48
+ end
49
+
50
+ context "when the current transaction ID contains more than one '-'" do
51
+ let(:order_number) { "R123-456-789-0" }
52
+ let(:current_transaction_id) { "R123-456-789-0-2" }
53
+
54
+ it "generates the next sequential transaction_id" do
55
+ expect(subject).to eq("R123-456-789-0-3")
56
+ end
57
+ end
58
+
59
+ context "when the current transaction ID suffix rolls over" do
60
+ let(:order_number) { "R1234567890" }
61
+ let(:current_transaction_id) { "R1234567890-99" }
62
+
63
+ it "generates the next sequential transaction_id" do
64
+ expect(subject).to eq("R1234567890-100")
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ describe ".refund_transaction_id" do
71
+ subject { described_class.refund_transaction_id(transaction_id) }
72
+
73
+ let(:transaction_id) { "R1234567890" }
74
+
75
+ it { is_expected.to eq("R1234567890-REFUND") }
76
+ end
77
+ end
@@ -5,6 +5,54 @@ RSpec.describe SuperGood::SolidusTaxjar do
5
5
  expect(SuperGood::SolidusTaxjar::VERSION).not_to be nil
6
6
  end
7
7
 
8
+ describe ".table_name_prefix" do
9
+ subject { described_class.table_name_prefix }
10
+
11
+ it { is_expected.to eq("solidus_taxjar_") }
12
+ end
13
+
14
+ describe ".api" do
15
+ subject { described_class.api }
16
+
17
+ it "returns an instance of the api client" do
18
+ expect(subject).to be_a(SuperGood::SolidusTaxjar::Api)
19
+ end
20
+ end
21
+
22
+ describe ".reporting" do
23
+ subject { described_class.reporting }
24
+
25
+ it "creates a new reporting" do
26
+ expect(subject).to be_a(::SuperGood::SolidusTaxjar::Reporting)
27
+ end
28
+ end
29
+
30
+ describe ".logger" do
31
+ subject { described_class.logger }
32
+
33
+ let(:logger_double) { instance_double(Logger) }
34
+
35
+ context "logger is set" do
36
+ before do
37
+ described_class.logger = logger_double
38
+ end
39
+
40
+ after do
41
+ described_class.logger = nil
42
+ end
43
+
44
+ it "returns the logger" do
45
+ expect(subject).to be(logger_double)
46
+ end
47
+ end
48
+
49
+ context "no logger is set" do
50
+ it "returns the Rails logger" do
51
+ expect(subject).to be(Rails.logger)
52
+ end
53
+ end
54
+ end
55
+
8
56
  describe "configuration" do
9
57
  describe ".cache_key" do
10
58
  subject { described_class.cache_key.call(order) }
@@ -21,6 +69,28 @@ RSpec.describe SuperGood::SolidusTaxjar do
21
69
  end
22
70
  end
23
71
 
72
+ describe ".cache_duration" do
73
+ subject { described_class.cache_duration }
74
+
75
+ it "returns the default cache duration" do
76
+ expect(subject).to eq(3.hours)
77
+ end
78
+
79
+ context "when set to another value" do
80
+ before do
81
+ described_class.cache_duration = 1.hour
82
+ end
83
+
84
+ it "returns the correct cache duration" do
85
+ expect(subject).to eq(1.hour)
86
+ end
87
+
88
+ after do
89
+ described_class.cache_duration = 3.hours
90
+ end
91
+ end
92
+ end
93
+
24
94
  describe ".discount_calculator" do
25
95
  subject { described_class.discount_calculator }
26
96
  it { is_expected.to eq SuperGood::SolidusTaxjar::DiscountCalculator }
@@ -88,5 +158,19 @@ RSpec.describe SuperGood::SolidusTaxjar do
88
158
  expect(subject).to eq(10)
89
159
  end
90
160
  end
161
+
162
+ describe ".job_queue" do
163
+ subject { described_class.job_queue }
164
+
165
+ it { is_expected.to eq :default }
166
+ end
167
+
168
+ describe ".configuration" do
169
+ subject { described_class.configuration }
170
+
171
+ it "returns a Configuration instance" do
172
+ expect(subject).to be_instance_of(SuperGood::SolidusTaxjar::Configuration)
173
+ end
174
+ end
91
175
  end
92
176
  end
@@ -0,0 +1,19 @@
1
+ # Set up some data that a store needs to work and be realistic.
2
+ #
3
+ RSpec.shared_context "checkoutable store" do
4
+ let!(:default_store) { create :store, default: true }
5
+ let!(:taxjar_configuration) {
6
+ create :taxjar_configuration, :reporting_enabled
7
+ }
8
+
9
+ let!(:california) {
10
+ create :state,
11
+ country: create(:country, states_required: true),
12
+ state_code: "CA"
13
+ }
14
+
15
+ let!(:check_payment_method) { create :check_payment_method }
16
+ let!(:shipping_method) { create :shipping_method }
17
+ let!(:stock_location) { create :stock_location }
18
+ let!(:zone) { create :zone }
19
+ end
@@ -0,0 +1,26 @@
1
+ module SolidusEventsHelper
2
+ # We can use this helper method to ensure that events are not run until after
3
+ # the test setup has been completed. This helps us more easily test side
4
+ # effects from events or control unintended side effects when attempting to
5
+ # test functionality that is not event-driven but would otherwise emit events.
6
+ #
7
+ # @yield Any test setup you'd like to run without events being emitted.
8
+ # @return The return value of the test setup in your block.
9
+ def with_events_disabled(&block)
10
+ if SolidusSupport::LegacyEventCompat.using_legacy?
11
+ allow(Spree::Event).to receive(:fire).and_return(nil)
12
+ else
13
+ allow(Spree::Bus).to receive(:publish).and_return(nil)
14
+ end
15
+
16
+ object = yield block
17
+
18
+ if SolidusSupport::LegacyEventCompat.using_legacy?
19
+ allow(Spree::Event).to receive(:fire).and_call_original
20
+ else
21
+ allow(Spree::Bus).to receive(:publish).and_call_original
22
+ end
23
+
24
+ object
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ RSpec.describe Taxjar::API::Request, :vcr do
2
+ subject { Taxjar::API::Request.new(client, :get, '/v2/summary_rates', 'summary_rates').perform }
3
+
4
+ let(:logger) { double(Logger) }
5
+ let(:client) { Taxjar::Client.new(api_key: ENV["TAXJAR_API_KEY"], api_url: "https://api.sandbox.taxjar.com") }
6
+
7
+ before do
8
+ allow(SuperGood::SolidusTaxjar).to receive(:logger).and_return(logger)
9
+ end
10
+
11
+
12
+ context "logging is enabled" do
13
+ before do
14
+ allow(SuperGood::SolidusTaxjar).to receive(:logging_enabled).and_return(true)
15
+ end
16
+
17
+ it "calls the logger", :aggregate_failures do
18
+ allow(logger).to receive(:debug)
19
+ allow(logger).to receive(:info)
20
+
21
+ subject
22
+
23
+ # When recording a cassette with VCR for this spec, one extra log
24
+ # statement is generated. In general we expect this to be called twice,
25
+ # but specify at least 2 times so cassettes can be re-recorded.
26
+ expect(logger).to have_received(:debug).at_least(2).times do |&block|
27
+ expect(block.call)
28
+ .to match("Host: api.sandbox.taxjar.com")
29
+ .or(match(/"summary_rates":/))
30
+ end
31
+
32
+ expect(logger).to have_received(:info).at_least(2).times do |&block|
33
+ expect(block.call)
34
+ .to match("> GET https://api.sandbox.taxjar.com/v2/summary_rates")
35
+ .or(match("< 200 OK"))
36
+ end
37
+ end
38
+ end
39
+
40
+ context "logging is disabled" do
41
+ before do
42
+ allow(SuperGood::SolidusTaxjar).to receive(:logging_enabled).and_return(false)
43
+ end
44
+
45
+ it "doesn't call the logger" do
46
+ expect(logger).to_not receive(:debug)
47
+ expect(logger).to_not receive(:info)
48
+
49
+ subject
50
+ end
51
+ end
52
+ end
@@ -25,13 +25,14 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
27
 
28
+ spec.add_dependency "deface", ">= 1"
28
29
  spec.add_dependency "solidus_core", ">= 2.4.0"
29
- spec.add_dependency "solidus_support"
30
+ spec.add_dependency "solidus_support", ">= 0.9.0"
30
31
  spec.add_dependency "taxjar-ruby"
31
32
 
32
33
  spec.add_development_dependency "solidus_dev_support"
33
34
  spec.add_development_dependency "bundler"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rake"
35
36
  spec.add_development_dependency "rspec", "~> 3.0"
36
37
  spec.add_development_dependency "rspec-rails"
37
38
  spec.add_development_dependency "vcr", "~> 4.0"