-
Notifications
You must be signed in to change notification settings - Fork 100
/
eu_auto_taxes_service_spec.rb
136 lines (107 loc) · 3.56 KB
/
eu_auto_taxes_service_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# frozen_string_literal: true
require 'rails_helper'
require 'valvat'
RSpec.describe Customers::EuAutoTaxesService, type: :service do
subject(:eu_tax_service) { described_class.new(customer:) }
let(:organization) { create(:organization, country: 'FR') }
let(:customer) { create(:customer, organization:) }
describe '.call' do
context 'with B2B organization' do
let(:vies_service) { instance_double(Valvat) }
before do
allow(Valvat).to receive(:new).and_return(vies_service)
allow(vies_service).to receive(:exists?).and_return(vies_response)
end
context 'with same country as the organization' do
let(:vies_response) do
{
country_code: 'FR'
}
end
it 'returns the organization country tax code' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_fr_standard')
end
it 'enqueues a SendWebhookJob' do
eu_tax_service.call
expect(SendWebhookJob).to have_been_enqueued
.with('customer.vies_check', customer, vies_check: vies_response)
end
end
context 'with a different country from the organization one' do
let(:vies_response) do
{
country_code: 'DE'
}
end
it 'returns the reverse charge tax' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_reverse_charge')
end
end
context 'when country has exceptions' do
let(:vies_response) do
{
country_code: 'FR'
}
end
context 'when customer has no zipcode' do
it 'returns the customer country standard tax' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_fr_standard')
end
end
context 'when customer has a zipcode' do
context 'when zipcode has applicable exceptions' do
before do
customer.update(zipcode: '97412')
end
it 'returns the exception tax code' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_fr_exception_reunion')
end
end
context 'when zipcode has no applicable exceptions' do
before do
customer.update(zipcode: '12345')
end
it 'returns the customer counrty standard tax' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_fr_standard')
end
end
end
end
end
context 'with non B2B' do
let(:vies_response) { false }
context 'when the customer has no country' do
before do
customer.update(country: nil)
end
it 'returns the organization country tax code' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_fr_standard')
end
end
context 'when the customer country is in europe' do
before do
customer.update(country: 'DE')
end
it 'returns the customer country tax code' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_de_standard')
end
end
context 'when the customer country is out of europe' do
before do
customer.update(country: 'US')
end
it 'returns the tax exempt tax code' do
tax_code = eu_tax_service.call
expect(tax_code).to eq('lago_eu_tax_exempt')
end
end
end
end
end