diff options
author | Jared Norman <jared@super.gd> | 2020-07-14 13:27:54 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-14 13:27:54 -0700 |
commit | 6073b524f5bfe73d6526cb19900897a2096351d5 (patch) | |
tree | f1df8217b05c7b97a1f23415c248a693b6b1a649 | |
parent | 15e30c62c721bd6f5e17dcd3d2f610f4b164269c (diff) | |
parent | fa3abe77f2e05ccabda975c82eec73929e1b3bee (diff) |
Merge pull request #22 from SuperGoodSoft/address-validation
Support for TaxJar Address Validation API
-rwxr-xr-x | bin/rails-engine | 12 | ||||
-rwxr-xr-x | bin/rails-sandbox | 8 | ||||
-rw-r--r-- | lib/super_good-solidus_taxjar.rb | 2 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar.rb | 11 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/addresses.rb | 61 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/api.rb | 6 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/api_params.rb | 20 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/calculator_helper.rb | 8 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_calculator.rb | 20 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_rate_calculator.rb | 9 | ||||
-rw-r--r-- | spec/spec_helper.rb | 10 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/addresses_spec.rb | 264 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/api_params_spec.rb | 40 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/api_spec.rb | 72 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/discount_calculator_spec.rb | 2 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/tax_calculator_spec.rb | 2 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb | 4 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar_spec.rb | 16 |
18 files changed, 470 insertions, 97 deletions
diff --git a/bin/rails-engine b/bin/rails-engine index 3684b30..5853e4d 100755 --- a/bin/rails-engine +++ b/bin/rails-engine @@ -2,12 +2,12 @@ # This command will automatically be run when you run "rails" with Rails gems # installed from the root of your application. -ENGINE_ROOT = File.expand_path('..', __dir__) -ENGINE_PATH = File.expand_path('../lib/solidus_taxjar/engine', __dir__) +ENGINE_ROOT = File.expand_path("..", __dir__) +ENGINE_PATH = File.expand_path("../lib/solidus_taxjar/engine", __dir__) # Set up gems listed in the Gemfile. -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) -require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) +require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) -require 'rails/all' -require 'rails/engine/commands' +require "rails/all" +require "rails/engine/commands" diff --git a/bin/rails-sandbox b/bin/rails-sandbox index c89b2ae..78020af 100755 --- a/bin/rails-sandbox +++ b/bin/rails-sandbox @@ -1,16 +1,16 @@ #!/usr/bin/env ruby -app_root = 'sandbox' +app_root = "sandbox" unless File.exist? "#{app_root}/bin/rails" - warn 'Creating the sandbox app...' + warn "Creating the sandbox app..." Dir.chdir "#{__dir__}/.." do system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr - warn 'Automatic creation of the sandbox app failed' + warn "Automatic creation of the sandbox app failed" exit 1 end end end Dir.chdir app_root -exec 'bin/rails', *ARGV +exec "bin/rails", *ARGV diff --git a/lib/super_good-solidus_taxjar.rb b/lib/super_good-solidus_taxjar.rb index e41f8a4..f7f97c7 100644 --- a/lib/super_good-solidus_taxjar.rb +++ b/lib/super_good-solidus_taxjar.rb @@ -1 +1 @@ -require 'super_good/solidus_taxjar' +require "super_good/solidus_taxjar" diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb index 2c47dda..7bdeb03 100644 --- a/lib/super_good/solidus_taxjar.rb +++ b/lib/super_good/solidus_taxjar.rb @@ -1,6 +1,6 @@ -require 'solidus_core' -require 'solidus_support' -require 'taxjar' +require "solidus_core" +require "solidus_support" +require "taxjar" require "super_good/solidus_taxjar/version" require "super_good/solidus_taxjar/api_params" @@ -9,6 +9,7 @@ require "super_good/solidus_taxjar/calculator_helper" require "super_good/solidus_taxjar/tax_calculator" require "super_good/solidus_taxjar/tax_rate_calculator" require "super_good/solidus_taxjar/discount_calculator" +require "super_good/solidus_taxjar/addresses" module SuperGood module SolidusTaxJar @@ -25,6 +26,10 @@ module SuperGood attr_accessor :taxable_address_check attr_accessor :taxable_order_check attr_accessor :test_mode + + def api + ::SuperGood::SolidusTaxJar::API.new + end end self.cache_duration = 3.hours diff --git a/lib/super_good/solidus_taxjar/addresses.rb b/lib/super_good/solidus_taxjar/addresses.rb new file mode 100644 index 0000000..0993a03 --- /dev/null +++ b/lib/super_good/solidus_taxjar/addresses.rb @@ -0,0 +1,61 @@ +module SuperGood + module SolidusTaxJar + class Addresses + class << self + def normalize(spree_address) + new.normalize(spree_address) + end + + def possibilities(spree_address) + new.possibilities(spree_address) + end + end + + def initialize(api: ::SuperGood::SolidusTaxJar.api) + @api = api + end + + def normalize(spree_address) + taxjar_address = taxjar_addresses(spree_address).first + + return if taxjar_address.nil? + + Spree::Address.immutable_merge(spree_address, { + country: us, # TaxJar only supports the US currently. + state: state(taxjar_address.state), + zipcode: taxjar_address.zip, + city: taxjar_address.city, + address1: taxjar_address.street + }) + end + + def possibilities(spree_address) + taxjar_addresses(spree_address).map { |taxjar_address| + Spree::Address.immutable_merge(spree_address, { + country: us, # TaxJar only supports the US currently. + state: state(taxjar_address.state), + zipcode: taxjar_address.zip, + city: taxjar_address.city, + address1: taxjar_address.street + }) + } + end + + private + + attr_reader :api + + def taxjar_addresses(spree_address) + api.validate_spree_address(spree_address) + end + + def us + Spree::Country.find_by iso: "US" + end + + def state(abbr) + us.states.find_by_abbr abbr + end + end + end +end diff --git a/lib/super_good/solidus_taxjar/api.rb b/lib/super_good/solidus_taxjar/api.rb index 601a2ae..9a8ba90 100644 --- a/lib/super_good/solidus_taxjar/api.rb +++ b/lib/super_good/solidus_taxjar/api.rb @@ -4,7 +4,7 @@ module SuperGood def self.default_taxjar_client ::Taxjar::Client.new( api_key: ENV.fetch("TAXJAR_API_KEY"), - api_url: ENV.fetch("TAXJAR_API_URL") { 'https://api.taxjar.com' } # Sandbox URL: https://api.sandbox.taxjar.com + api_url: ENV.fetch("TAXJAR_API_URL") { "https://api.taxjar.com" } # Sandbox URL: https://api.sandbox.taxjar.com ) end @@ -46,6 +46,10 @@ module SuperGood taxjar_client.create_refund APIParams.refund_params(reimbursement) end + def validate_spree_address(spree_address) + taxjar_client.validate_address APIParams.validate_address_params(spree_address) + end + private attr_reader :taxjar_client diff --git a/lib/super_good/solidus_taxjar/api_params.rb b/lib/super_good/solidus_taxjar/api_params.rb index b3c02fe..a7d1a5b 100644 --- a/lib/super_good/solidus_taxjar/api_params.rb +++ b/lib/super_good/solidus_taxjar/api_params.rb @@ -8,7 +8,7 @@ module SuperGood .merge(order_address_params(order.tax_address)) .merge(line_items_params(order.line_items)) .merge(shipping: shipping(order)) - .merge(SuperGood::SolidusTaxJar.custom_order_params.(order)) + .merge(SuperGood::SolidusTaxJar.custom_order_params.call(order)) .tap do |params| next unless SuperGood::SolidusTaxJar.logging_enabled @@ -33,7 +33,7 @@ module SuperGood def tax_rate_address_params(address) { amount: 100, - shipping: 0, + shipping: 0 }.merge(order_address_params(address)) end @@ -66,12 +66,22 @@ module SuperGood ) end + def validate_address_params(spree_address) + { + country: spree_address.country&.iso, + state: spree_address.state&.abbr || adddress.state_name, + zip: spree_address.zipcode, + city: spree_address.city, + street: spree_address.address1 + } + end + private def customer_params(order) return {} unless order.user_id - { customer_id: order.user_id.to_s } + {customer_id: order.user_id.to_s} end def order_address_params(address) @@ -80,7 +90,7 @@ module SuperGood to_zip: address.zipcode, to_city: address.city, to_state: address&.state&.abbr || address.state_name, - to_street: address.address1, + to_street: address.address1 } end @@ -127,7 +137,7 @@ module SuperGood end def shipping(order) - SuperGood::SolidusTaxJar.shipping_calculator.(order) + SuperGood::SolidusTaxJar.shipping_calculator.call(order) end def sales_tax(order) diff --git a/lib/super_good/solidus_taxjar/calculator_helper.rb b/lib/super_good/solidus_taxjar/calculator_helper.rb index dbde950..c4803a4 100644 --- a/lib/super_good/solidus_taxjar/calculator_helper.rb +++ b/lib/super_good/solidus_taxjar/calculator_helper.rb @@ -3,12 +3,6 @@ module SuperGood module CalculatorHelper extend ActiveSupport::Concern - class_methods do - def default_api - ::SuperGood::SolidusTaxJar::API.new - end - end - def incomplete_address?(address) return true if address.is_a?(Spree::Tax::TaxLocation) @@ -22,7 +16,7 @@ module SuperGood end def taxable_address?(address) - SuperGood::SolidusTaxJar.taxable_address_check.(address) + SuperGood::SolidusTaxJar.taxable_address_check.call(address) end def cache diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index 7fac1bd..88eee2a 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -3,7 +3,7 @@ module SuperGood class TaxCalculator include CalculatorHelper - def initialize(order, api: self.class.default_api) + def initialize(order, api: SuperGood::SolidusTaxJar.api) @order = order @api = api end @@ -23,8 +23,8 @@ module SuperGood shipment_taxes: shipment_taxes ) end - rescue StandardError => e - exception_handler.(e) + rescue => e + exception_handler.call(e) no_tax end @@ -34,7 +34,7 @@ module SuperGood def line_item_taxes @line_item_taxes ||= - taxjar_breakdown.line_items.map do |taxjar_line_item| + taxjar_breakdown.line_items.map { |taxjar_line_item| spree_line_item_id = taxjar_line_item.id.to_i # Searching in memory because this association is loaded and most @@ -48,13 +48,13 @@ module SuperGood amount: taxjar_line_item.tax_collectable, included_in_price: false ) - end + } end def shipment_taxes @shipment_taxes ||= if taxjar_breakdown.shipping? && - (total_shipping_tax = taxjar_breakdown.shipping.tax_collectable) != 0 + (total_shipping_tax = taxjar_breakdown.shipping.tax_collectable) != 0 # Distribute shipping tax across shipments: # TaxJar does not provide a breakdown of shipping taxes, so we have @@ -114,22 +114,22 @@ module SuperGood end def cache_key - SuperGood::SolidusTaxJar.cache_key.(order) + SuperGood::SolidusTaxJar.cache_key.call(order) end def taxable_order?(order) - SuperGood::SolidusTaxJar.taxable_order_check.(order) + SuperGood::SolidusTaxJar.taxable_order_check.call(order) end def shipping_tax_label(shipment, shipping_tax) - SuperGood::SolidusTaxJar.shipping_tax_label_maker.( + SuperGood::SolidusTaxJar.shipping_tax_label_maker.call( shipment, shipping_tax ) end def line_item_tax_label(taxjar_line_item, spree_line_item) - SuperGood::SolidusTaxJar.line_item_tax_label_maker.(taxjar_line_item, spree_line_item) + SuperGood::SolidusTaxJar.line_item_tax_label_maker.call(taxjar_line_item, spree_line_item) end end end diff --git a/lib/super_good/solidus_taxjar/tax_rate_calculator.rb b/lib/super_good/solidus_taxjar/tax_rate_calculator.rb index dc3d771..f9c996c 100644 --- a/lib/super_good/solidus_taxjar/tax_rate_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_rate_calculator.rb @@ -2,7 +2,7 @@ module SuperGood module SolidusTaxJar class TaxRateCalculator include CalculatorHelper - def initialize(address, api: self.class.default_api) + def initialize(address, api: SuperGood::SolidusTaxJar.api) @address = address @api = api end @@ -14,9 +14,8 @@ module SuperGood cache do api.tax_rate_for(address).to_d end - - rescue StandardError => e - exception_handler.(e) + rescue => e + exception_handler.call(e) no_rate end @@ -29,7 +28,7 @@ module SuperGood end def cache_key - SuperGood::SolidusTaxJar.cache_key.(address) + SuperGood::SolidusTaxJar.cache_key.call(address) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e55b5c5..ae3528d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,18 +1,18 @@ # frozen_string_literal: true # Configure Rails Environment -ENV['RAILS_ENV'] = 'test' +ENV["RAILS_ENV"] = "test" -require File.expand_path('dummy/config/environment.rb', __dir__).tap { |file| +require File.expand_path("dummy/config/environment.rb", __dir__).tap { |file| # Create the dummy app if it's still missing. - system 'bin/rake extension:test_app' unless File.exist? file + system "bin/rake extension:test_app" unless File.exist? file } -require 'solidus_dev_support/rspec/rails_helper' +require "solidus_dev_support/rspec/rails_helper" # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. -Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f } +Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f } RSpec.configure do |config| config.infer_spec_type_from_file_location! diff --git a/spec/super_good/solidus_taxjar/addresses_spec.rb b/spec/super_good/solidus_taxjar/addresses_spec.rb new file mode 100644 index 0000000..a5042d1 --- /dev/null +++ b/spec/super_good/solidus_taxjar/addresses_spec.rb @@ -0,0 +1,264 @@ +require "spec_helper" + +RSpec.describe SuperGood::SolidusTaxJar::Addresses do + describe "#normalize" do + subject { described_class.new(api: dummy_api).normalize(spree_address) } + + let(:spree_address) { + create( + :address, + address1: "475 North Beverly Drive", + city: "Los Angeles", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210" + ) + } + + let(:country_us) { + Spree::Country.create!( + iso3: "USA", + iso: "US", + iso_name: "UNITED STATES", + name: "United States", + numcode: 840, + states_required: true + ) + } + + let(:state_california) { + Spree::State.create!( + abbr: "CA", + country: country_us, + name: "California" + ) + } + + let(:dummy_api) { + instance_double ::SuperGood::SolidusTaxJar::API + } + + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_return(results) + end + + context "when there are no possibilities for the address" do + let(:results) { [] } + + it { is_expected.to be_nil } + end + + context "when there is one possibility for the address" do + let(:results) { + [ + Taxjar::Address.new( + country: "US", + state: "CA", + zip: "90210-4606", + city: "Beverly Hills", + street: "475 N Beverly Dr" + ) + ] + } + + it "returns a sanitized address" do + expect(subject).to eq( + build( + :address, + address1: "475 N Beverly Dr", + city: "Beverly Hills", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210-4606" + ) + ) + end + end + + context "when there are multiple possibilities for the address" do + let(:results) { + [ + Taxjar::Address.new( + country: "US", + state: "CA", + zip: "90210-4606", + city: "Beverly Hills", + street: "475 N Beverly Dr" + ), + Taxjar::Address.new( + country: "US", + state: "AZ", + zip: "90213-4606", + city: "Beverly Hills", + street: "475 N Beverly Dr" + ) + ] + } + + it "uses the first result to sanitize the addresses" do + expect(subject).to eq( + build( + :address, + address1: "475 N Beverly Dr", + city: "Beverly Hills", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210-4606" + ) + ) + end + end + end + + describe "#normalize" do + subject { described_class.new(api: dummy_api).possibilities(spree_address) } + + let(:spree_address) { + create( + :address, + address1: "475 North Beverly Drive", + city: "Los Angeles", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210" + ) + } + + let(:country_us) { + Spree::Country.create!( + iso3: "USA", + iso: "US", + iso_name: "UNITED STATES", + name: "United States", + numcode: 840, + states_required: true + ) + } + + let(:state_california) { + Spree::State.create!( + abbr: "CA", + country: country_us, + name: "California" + ) + } + + let(:dummy_api) { + instance_double ::SuperGood::SolidusTaxJar::API + } + + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_return(results) + end + + context "when there are no possibilities for the address" do + let(:results) { [] } + + it { is_expected.to be_empty } + end + + context "when there is one possibility for the address" do + let(:results) { + [ + Taxjar::Address.new( + country: "US", + state: "CA", + zip: "90210-4606", + city: "Beverly Hills", + street: "475 N Beverly Dr" + ) + ] + } + + it "returns the possibilities" do + expect(subject).to eq([ + build( + :address, + address1: "475 N Beverly Dr", + city: "Beverly Hills", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210-4606" + ) + ]) + end + end + + context "when there are multiple possibilities for the address" do + let(:results) { + [ + Taxjar::Address.new( + country: "US", + state: "CA", + zip: "90210-4606", + city: "Beverly Hills", + street: "475 N Beverly Dr" + ), + Taxjar::Address.new( + country: "US", + state: "AZ", + zip: "90213-1234", + city: "Phoenix", + street: "473 N Beverly Dr" + ) + ] + } + + let!(:state_arizona) { + Spree::State.create!( + abbr: "AZ", + country: country_us, + name: "Arizona" + ) + } + + it "returns all the possibilities" do + expect(subject).to eq([ + build( + :address, + address1: "475 N Beverly Dr", + city: "Beverly Hills", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210-4606" + ), + build( + :address, + address1: "473 N Beverly Dr", + city: "Phoenix", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_arizona, + zipcode: "90213-1234" + ) + ]) + end + end + end +end diff --git a/spec/super_good/solidus_taxjar/api_params_spec.rb b/spec/super_good/solidus_taxjar/api_params_spec.rb index 8e9536f..c4e3681 100644 --- a/spec/super_good/solidus_taxjar/api_params_spec.rb +++ b/spec/super_good/solidus_taxjar/api_params_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" RSpec.describe SuperGood::SolidusTaxJar::APIParams do let(:order) do @@ -144,15 +144,15 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do context "when custom params are used" do around do |example| default = SuperGood::SolidusTaxJar.custom_order_params - SuperGood::SolidusTaxJar.custom_order_params = -> (order) { + SuperGood::SolidusTaxJar.custom_order_params = ->(order) { { nexus_addresses: [ { - id: 'Main Location', - country: 'AU', - zip: 'NSW 2000', - city: 'Sydney', - street: '483 George St', + id: "Main Location", + country: "AU", + zip: "NSW 2000", + city: "Sydney", + street: "483 George St" } ] } @@ -172,11 +172,11 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do unit_price: 10.00 }], nexus_addresses: [{ - id: 'Main Location', - country: 'AU', - zip: 'NSW 2000', - city: 'Sydney', - street: '483 George St', + id: "Main Location", + country: "AU", + zip: "NSW 2000", + city: "Sydney", + street: "483 George St" }], shipping: 3.01, to_city: "Los Angeles", @@ -324,7 +324,7 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do to_street: "475 N Beverly Dr", to_zip: "90210", transaction_date: "2018-03-06T12:10:33Z", - transaction_id: "R111222333", + transaction_id: "R111222333" }) end end @@ -349,4 +349,18 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do }) end end + + describe "#validate_address_params" do + subject { described_class.validate_address_params(ship_address) } + + it "returns params for validating an address" do + expect(subject).to eq({ + country: "US", + state: "CA", + zip: "90210", + city: "Los Angeles", + street: "475 N Beverly Dr" + }) + end + end end diff --git a/spec/super_good/solidus_taxjar/api_spec.rb b/spec/super_good/solidus_taxjar/api_spec.rb index 8b0a5db..7fab505 100644 --- a/spec/super_good/solidus_taxjar/api_spec.rb +++ b/spec/super_good/solidus_taxjar/api_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" RSpec.describe SuperGood::SolidusTaxJar::API do describe "#tax_for" do @@ -12,15 +12,15 @@ RSpec.describe SuperGood::SolidusTaxJar::API do allow(SuperGood::SolidusTaxJar::APIParams) .to receive(:order_params) .with(order) - .and_return({ order: "params" }) + .and_return({order: "params"}) allow(dummy_client) .to receive(:tax_for_order) - .with({ order: "params" }) - .and_return({ some_kind_of: "response" }) + .with({order: "params"}) + .and_return({some_kind_of: "response"}) end - it { is_expected.to eq({ some_kind_of: "response" }) } + it { is_expected.to eq({some_kind_of: "response"}) } end describe "tax_rate_for" do @@ -36,11 +36,11 @@ RSpec.describe SuperGood::SolidusTaxJar::API do allow(SuperGood::SolidusTaxJar::APIParams) .to receive(:tax_rate_address_params) .with(address) - .and_return({ address: "params" }) + .and_return({address: "params"}) allow(dummy_client) .to receive(:tax_for_order) - .with({ address: "params" }) + .with({address: "params"}) .and_return(response) end @@ -58,15 +58,15 @@ RSpec.describe SuperGood::SolidusTaxJar::API do allow(SuperGood::SolidusTaxJar::APIParams) .to receive(:address_params) .with(address) - .and_return(["zipcode", { address: "params" }]) + .and_return(["zipcode", {address: "params"}]) allow(dummy_client) .to receive(:rates_for_location) - .with("zipcode", { address: "params" }) - .and_return({ some_kind_of: "response" }) + .with("zipcode", {address: "params"}) + .and_return({some_kind_of: "response"}) end - it { is_expected.to eq({ some_kind_of: "response" }) } + it { is_expected.to eq({some_kind_of: "response"}) } end describe "#create_transaction_for" do @@ -80,15 +80,15 @@ RSpec.describe SuperGood::SolidusTaxJar::API do allow(SuperGood::SolidusTaxJar::APIParams) .to receive(:transaction_params) .with(order) - .and_return({ transaction: "params" }) + .and_return({transaction: "params"}) allow(dummy_client) .to receive(:create_order) - .with({ transaction: "params" }) - .and_return({ some_kind_of: "response" }) + .with({transaction: "params"}) + .and_return({some_kind_of: "response"}) end - it { is_expected.to eq({ some_kind_of: "response" }) } + it { is_expected.to eq({some_kind_of: "response"}) } end describe "#update_transaction_for" do @@ -102,15 +102,15 @@ RSpec.describe SuperGood::SolidusTaxJar::API do allow(SuperGood::SolidusTaxJar::APIParams) .to receive(:transaction_params) .with(order) - .and_return({ transaction: "params" }) + .and_return({transaction: "params"}) allow(dummy_client) .to receive(:update_order) - .with({ transaction: "params" }) - .and_return({ some_kind_of: "response" }) + .with({transaction: "params"}) + .and_return({some_kind_of: "response"}) end - it { is_expected.to eq({ some_kind_of: "response" }) } + it { is_expected.to eq({some_kind_of: "response"}) } end describe "#update_transaction_for" do @@ -124,10 +124,10 @@ RSpec.describe SuperGood::SolidusTaxJar::API do allow(dummy_client) .to receive(:delete_order) .with("R111222333") - .and_return({ some_kind_of: "response" }) + .and_return({some_kind_of: "response"}) end - it { is_expected.to eq({ some_kind_of: "response" }) } + it { is_expected.to eq({some_kind_of: "response"}) } end describe "#create_refund_for" do @@ -141,14 +141,36 @@ RSpec.describe SuperGood::SolidusTaxJar::API do allow(SuperGood::SolidusTaxJar::APIParams) .to receive(:refund_params) .with(reimbursement) - .and_return({ refund: "params" }) + .and_return({refund: "params"}) allow(dummy_client) .to receive(:create_refund) - .with({ refund: "params" }) - .and_return({ some_kind_of: "response" }) + .with({refund: "params"}) + .and_return({some_kind_of: "response"}) end - it { is_expected.to eq({ some_kind_of: "response" }) } + it { is_expected.to eq({some_kind_of: "response"}) } + end + + describe "#validate_spree_address" do + subject { api.validate_spree_address spree_address } + + let(:api) { described_class.new(taxjar_client: dummy_client) } + let(:dummy_client) { instance_double ::Taxjar::Client } + let(:spree_address) { build :address } + + before do + allow(SuperGood::SolidusTaxJar::APIParams) + .to receive(:validate_address_params) + .with(spree_address) + .and_return({address: "params"}) + + allow(dummy_client) + .to receive(:validate_address) + .with({address: "params"}) + .and_return({some_kind_of: "response"}) + end + + it { is_expected.to eq({some_kind_of: "response"}) } end end diff --git a/spec/super_good/solidus_taxjar/discount_calculator_spec.rb b/spec/super_good/solidus_taxjar/discount_calculator_spec.rb index b89b4dc..65f3bc4 100644 --- a/spec/super_good/solidus_taxjar/discount_calculator_spec.rb +++ b/spec/super_good/solidus_taxjar/discount_calculator_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" RSpec.describe SuperGood::SolidusTaxJar::DiscountCalculator do describe "#discount" do diff --git a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb index c9ecbcd..c88be44 100644 --- a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb +++ b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do describe "#calculate" do diff --git a/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb b/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb index 3af89e5..057d58d 100644 --- a/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb +++ b/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" RSpec.describe ::SuperGood::SolidusTaxJar::TaxRateCalculator do describe "#calculate" do @@ -44,7 +44,7 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxRateCalculator do context "when we're not rescuing from errors" do around do |example| handler = SuperGood::SolidusTaxJar.exception_handler - SuperGood::SolidusTaxJar.exception_handler = -> (error) { raise error } + SuperGood::SolidusTaxJar.exception_handler = ->(error) { raise error } example.run SuperGood::SolidusTaxJar.exception_handler = handler end diff --git a/spec/super_good/solidus_taxjar_spec.rb b/spec/super_good/solidus_taxjar_spec.rb index 2b04e7b..30a2d01 100644 --- a/spec/super_good/solidus_taxjar_spec.rb +++ b/spec/super_good/solidus_taxjar_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" RSpec.describe SuperGood::SolidusTaxJar do it "has a version number" do @@ -7,7 +7,7 @@ RSpec.describe SuperGood::SolidusTaxJar do describe "configuration" do describe ".cache_key" do - subject { described_class.cache_key.(order) } + subject { described_class.cache_key.call(order) } let(:order) { Spree::Order.new } @@ -15,7 +15,7 @@ RSpec.describe SuperGood::SolidusTaxJar do allow(SuperGood::SolidusTaxJar::APIParams) .to receive(:order_params) .with(order) - .and_return({ some: "hash", with: "stuff", in: "it" }) + .and_return({some: "hash", with: "stuff", in: "it"}) expect(subject).to eq '{"some":"hash","with":"stuff","in":"it"}' end @@ -32,7 +32,7 @@ RSpec.describe SuperGood::SolidusTaxJar do end describe ".exception_handler" do - subject { described_class.exception_handler.(exception) } + subject { described_class.exception_handler.call(exception) } let(:exception) { StandardError.new("Something happened") } @@ -45,7 +45,7 @@ RSpec.describe SuperGood::SolidusTaxJar do end describe ".taxable_address_check" do - subject { described_class.taxable_address_check.(address) } + subject { described_class.taxable_address_check.call(address) } let(:address) { Spree::Address.new } @@ -53,7 +53,7 @@ RSpec.describe SuperGood::SolidusTaxJar do end describe ".taxable_order_check" do - subject { described_class.taxable_order_check.(order) } + subject { described_class.taxable_order_check.call(order) } let(:order) { Spree::Order.new } @@ -61,14 +61,14 @@ RSpec.describe SuperGood::SolidusTaxJar do end describe ".shipping_tax_label_maker" do - subject { described_class.shipping_tax_label_maker.(shipment, shipping_tax) } + subject { described_class.shipping_tax_label_maker.call(shipment, shipping_tax) } let(:shipment) { Spree::Shipment.new } let(:shipping_tax) { BigDecimal("3.25") } it { is_expected.to eq "Sales Tax" } end describe ".line_item_tax_label_maker" do - subject { described_class.line_item_tax_label_maker.(taxjar_line_item, spree_line_item) } + subject { described_class.line_item_tax_label_maker.call(taxjar_line_item, spree_line_item) } let(:taxjar_line_item) { instance_double Taxjar::BreakdownLineItem } let(:spree_line_item) { Spree::LineItem.new } it { is_expected.to eq "Sales Tax" } |