valvat 1.0.1 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.github/workflows/ruby.yml +41 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +15 -0
- data/CHANGES.md +27 -1
- data/Gemfile +2 -0
- data/Guardfile +6 -4
- data/README.md +6 -4
- data/Rakefile +3 -1
- data/certs/yolk.pem +25 -24
- data/gemfiles/activemodel-5 +1 -1
- data/gemfiles/activemodel-6 +1 -1
- data/gemfiles/{activemodel-3-2 → activemodel-7} +2 -2
- data/lib/active_model/validations/valvat_validator.rb +65 -32
- data/lib/valvat/checksum/at.rb +3 -1
- data/lib/valvat/checksum/be.rb +3 -1
- data/lib/valvat/checksum/bg.rb +17 -17
- data/lib/valvat/checksum/cy.rb +2 -0
- data/lib/valvat/checksum/de.rb +5 -3
- data/lib/valvat/checksum/dk.rb +3 -1
- data/lib/valvat/checksum/ee.rb +2 -0
- data/lib/valvat/checksum/es.rb +14 -16
- data/lib/valvat/checksum/fi.rb +3 -1
- data/lib/valvat/checksum/fr.rb +4 -2
- data/lib/valvat/checksum/gb.rb +46 -31
- data/lib/valvat/checksum/gr.rb +4 -2
- data/lib/valvat/checksum/hr.rb +2 -0
- data/lib/valvat/checksum/hu.rb +2 -0
- data/lib/valvat/checksum/ie.rb +5 -6
- data/lib/valvat/checksum/it.rb +7 -5
- data/lib/valvat/checksum/lt.rb +2 -0
- data/lib/valvat/checksum/lu.rb +3 -1
- data/lib/valvat/checksum/mt.rb +2 -0
- data/lib/valvat/checksum/nl.rb +5 -3
- data/lib/valvat/checksum/pl.rb +3 -1
- data/lib/valvat/checksum/pt.rb +3 -1
- data/lib/valvat/checksum/ro.rb +2 -0
- data/lib/valvat/checksum/se.rb +5 -3
- data/lib/valvat/checksum/si.rb +4 -2
- data/lib/valvat/checksum.rb +24 -28
- data/lib/valvat/error.rb +8 -2
- data/lib/valvat/local.rb +6 -4
- data/lib/valvat/locales/de.yml +2 -1
- data/lib/valvat/locales/en.yml +2 -1
- data/lib/valvat/locales/es.yml +1 -0
- data/lib/valvat/locales/fr.yml +1 -0
- data/lib/valvat/locales/hu.yml +1 -0
- data/lib/valvat/locales/nl.yml +1 -0
- data/lib/valvat/locales/pl.yml +1 -0
- data/lib/valvat/lookup/fault.rb +29 -21
- data/lib/valvat/lookup/request.rb +21 -16
- data/lib/valvat/lookup/response.rb +11 -10
- data/lib/valvat/lookup.rb +5 -4
- data/lib/valvat/syntax.rb +31 -30
- data/lib/valvat/utils.rb +12 -9
- data/lib/valvat/version.rb +3 -1
- data/lib/valvat.rb +4 -2
- data/spec/active_model/validations/valvat_validator_spec.rb +99 -93
- data/spec/spec_helper.rb +8 -8
- data/spec/valvat/checksum/at_spec.rb +6 -4
- data/spec/valvat/checksum/be_spec.rb +6 -4
- data/spec/valvat/checksum/bg_spec.rb +7 -4
- data/spec/valvat/checksum/cy_spec.rb +3 -1
- data/spec/valvat/checksum/de_spec.rb +6 -4
- data/spec/valvat/checksum/dk_spec.rb +6 -4
- data/spec/valvat/checksum/ee_spec.rb +3 -1
- data/spec/valvat/checksum/es_spec.rb +7 -4
- data/spec/valvat/checksum/fi_spec.rb +6 -4
- data/spec/valvat/checksum/fr_spec.rb +6 -4
- data/spec/valvat/checksum/gb_spec.rb +4 -2
- data/spec/valvat/checksum/gr_spec.rb +6 -4
- data/spec/valvat/checksum/hr_spec.rb +3 -1
- data/spec/valvat/checksum/hu_spec.rb +3 -1
- data/spec/valvat/checksum/ie_spec.rb +8 -6
- data/spec/valvat/checksum/it_spec.rb +10 -8
- data/spec/valvat/checksum/lt_spec.rb +3 -1
- data/spec/valvat/checksum/lu_spec.rb +6 -4
- data/spec/valvat/checksum/mt_spec.rb +3 -1
- data/spec/valvat/checksum/nl_spec.rb +9 -7
- data/spec/valvat/checksum/pl_spec.rb +8 -6
- data/spec/valvat/checksum/pt_spec.rb +10 -11
- data/spec/valvat/checksum/ro_spec.rb +3 -1
- data/spec/valvat/checksum/se_spec.rb +8 -6
- data/spec/valvat/checksum/si_spec.rb +10 -8
- data/spec/valvat/checksum_spec.rb +24 -9
- data/spec/valvat/lookup/fault_spec.rb +34 -0
- data/spec/valvat/lookup/request_spec.rb +32 -0
- data/spec/valvat/lookup/response_spec.rb +29 -0
- data/spec/valvat/lookup_spec.rb +171 -152
- data/spec/valvat/syntax_spec.rb +59 -280
- data/spec/valvat/utils_spec.rb +64 -47
- data/spec/valvat_spec.rb +140 -152
- data/valvat.gemspec +17 -17
- data.tar.gz.sig +1 -2
- metadata +48 -50
- metadata.gz.sig +0 -0
- data/.travis.yml +0 -47
- data/gemfiles/activemodel-4 +0 -7
- data/gemfiles/before-ruby21/activemodel-3-2 +0 -8
- data/gemfiles/before-ruby21/activemodel-4 +0 -8
- data/gemfiles/before-ruby21/standalone +0 -7
- data/spec/valvat/lockup/fault_spec.rb +0 -32
- data/spec/valvat/lockup/request_spec.rb +0 -15
- data/spec/valvat/lockup/response_spec.rb +0 -27
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Valvat
|
2
4
|
class Lookup
|
3
5
|
class Response
|
@@ -10,27 +12,26 @@ class Valvat
|
|
10
12
|
end
|
11
13
|
|
12
14
|
def to_hash
|
13
|
-
@
|
15
|
+
@to_hash ||= self.class.cleanup(@raw.to_hash)
|
14
16
|
end
|
15
17
|
|
16
|
-
private
|
17
|
-
|
18
18
|
def self.cleanup(hash)
|
19
|
-
(
|
20
|
-
hash[
|
21
|
-
|
19
|
+
(
|
20
|
+
hash[:check_vat_approx_response] || hash[:check_vat_response] || {}
|
21
|
+
).each_with_object({}) do |(key, value), result|
|
22
|
+
result[cleanup_key(key)] = cleanup_value(value) unless key == :"@xmlns"
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
25
|
-
TRADER_PREFIX = /\Atrader_
|
26
|
+
TRADER_PREFIX = /\Atrader_/.freeze
|
26
27
|
|
27
28
|
def self.cleanup_key(key)
|
28
|
-
key.to_s.sub(TRADER_PREFIX,
|
29
|
+
key.to_s.sub(TRADER_PREFIX, '').to_sym
|
29
30
|
end
|
30
31
|
|
31
32
|
def self.cleanup_value(value)
|
32
|
-
value ==
|
33
|
+
value == '---' ? nil : value
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
36
|
-
end
|
37
|
+
end
|
data/lib/valvat/lookup.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Valvat
|
2
4
|
class Lookup
|
3
|
-
|
4
|
-
def initialize(vat, options={})
|
5
|
+
def initialize(vat, options = {})
|
5
6
|
@vat = Valvat(vat)
|
6
7
|
@options = options || {}
|
7
8
|
@options[:requester] ||= @options[:requester_vat]
|
@@ -15,7 +16,7 @@ class Valvat
|
|
15
16
|
end
|
16
17
|
|
17
18
|
class << self
|
18
|
-
def validate(vat, options={})
|
19
|
+
def validate(vat, options = {})
|
19
20
|
new(vat, options).validate
|
20
21
|
end
|
21
22
|
end
|
@@ -35,7 +36,7 @@ class Valvat
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def handle_vies_error(error)
|
38
|
-
if ViesMaintenanceError
|
39
|
+
if error.is_a?(ViesMaintenanceError)
|
39
40
|
raise error if @options[:raise_error]
|
40
41
|
else
|
41
42
|
raise error unless @options[:raise_error] == false
|
data/lib/valvat/syntax.rb
CHANGED
@@ -1,36 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Valvat
|
2
4
|
module Syntax
|
3
|
-
|
4
5
|
VAT_PATTERNS = {
|
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
|
-
|
6
|
+
'AT' => /\AATU[0-9]{8}\Z/, # Austria
|
7
|
+
'BE' => /\ABE0[0-9]{9}\Z/, # Belgium
|
8
|
+
'BG' => /\ABG[0-9]{9,10}\Z/, # Bulgaria
|
9
|
+
'CY' => /\ACY(?!12)[0-59][0-9]{7}[A-Z]\Z/, # Cyprus
|
10
|
+
'CZ' => /\ACZ[0-9]{8,10}\Z/, # Czech Republic
|
11
|
+
'DE' => /\ADE[0-9]{9}\Z/, # Germany
|
12
|
+
'DK' => /\ADK[0-9]{8}\Z/, # Denmark
|
13
|
+
'EE' => /\AEE10[0-9]{7}\Z/, # Estonia
|
14
|
+
'GR' => /\AEL[0-9]{9}\Z/, # Greece
|
15
|
+
'ES' => /\AES([A-Z][0-9]{8}|[0-9]{8}[A-Z]|[A-Z][0-9]{7}[A-Z])\Z/, # Spain
|
16
|
+
'FI' => /\AFI[0-9]{8}\Z/, # Finland
|
17
|
+
'FR' => /\AFR[A-Z0-9]{2}[0-9]{9}\Z/, # France
|
18
|
+
'GB' => /\A(GB|XI)([0-9]{9}|[0-9]{12}|(HA|GD)[0-9]{3})\Z/, # United Kingdom
|
19
|
+
'HR' => /\AHR[0-9]{11}\Z/, # Croatia
|
20
|
+
'HU' => /\AHU[0-9]{8}\Z/, # Hungary
|
21
|
+
'IE' => /\AIE([0-9][A-Z][0-9]{5}|[0-9]{7}[A-Z]?)[A-Z]\Z/, # Ireland
|
22
|
+
'IT' => /\AIT[0-9]{11}\Z/, # Italy
|
23
|
+
'LT' => /\ALT([0-9]{7}1[0-9]|[0-9]{10}1[0-9])\Z/, # Lithuania
|
24
|
+
'LU' => /\ALU[0-9]{8}\Z/, # Luxembourg
|
25
|
+
'LV' => /\ALV[0-9]{11}\Z/, # Latvia
|
26
|
+
'MT' => /\AMT[0-9]{8}\Z/, # Malta
|
27
|
+
'NL' => /\ANL[0-9]{9}B[0-9]{2}\Z/, # Netherlands
|
28
|
+
'PL' => /\APL[0-9]{10}\Z/, # Poland
|
29
|
+
'PT' => /\APT[0-9]{9}\Z/, # Portugal
|
30
|
+
'RO' => /\ARO[1-9][0-9]{1,9}\Z/, # Romania
|
31
|
+
'SE' => /\ASE[0-9]{10}01\Z/, # Sweden
|
32
|
+
'SI' => /\ASI[0-9]{8}\Z/, # Slovenia
|
33
|
+
'SK' => /\ASK[0-9]{10}\Z/ # Slovakia
|
34
|
+
}.freeze
|
34
35
|
|
35
36
|
def self.validate(vat)
|
36
37
|
vat = Valvat(vat)
|
data/lib/valvat/utils.rb
CHANGED
@@ -1,31 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'date'
|
2
4
|
|
3
5
|
class Valvat
|
4
6
|
module Utils
|
5
|
-
|
6
|
-
|
7
|
-
SUPPORTED_STATES = EU_MEMBER_STATES + %w(GB)
|
7
|
+
EU_MEMBER_STATES = %w[AT BE BG CY CZ DE DK EE ES FI FR GR HR HU IE IT LT LU LV MT NL PL PT RO SE SI SK].freeze
|
8
|
+
SUPPORTED_STATES = EU_MEMBER_STATES + %w[GB]
|
8
9
|
EU_COUNTRIES = EU_MEMBER_STATES # TODO: Remove constant
|
9
|
-
COUNTRY_PATTERN = /\A([A-Z]{2})(.+)\Z
|
10
|
-
NORMALIZE_PATTERN = /[[:
|
10
|
+
COUNTRY_PATTERN = /\A([A-Z]{2})(.+)\Z/.freeze
|
11
|
+
NORMALIZE_PATTERN = /([[:punct:][:cntrl:]]|[[:space:]])+/.freeze
|
12
|
+
CONVERT_VAT_TO_ISO_COUNTRY = { 'EL' => 'GR', 'XI' => 'GB' }.freeze
|
13
|
+
CONVERT_ISO_TO_VAT_COUNTRY = CONVERT_VAT_TO_ISO_COUNTRY.invert.freeze
|
11
14
|
|
12
15
|
def self.split(vat)
|
13
16
|
COUNTRY_PATTERN =~ vat
|
14
|
-
result = [
|
17
|
+
result = [Regexp.last_match(1), Regexp.last_match(2)]
|
15
18
|
iso_country = vat_country_to_iso_country(result[0])
|
16
19
|
country_is_supported?(iso_country) ? result : [nil, nil]
|
17
20
|
end
|
18
21
|
|
19
22
|
def self.normalize(vat)
|
20
|
-
vat.to_s.upcase.gsub(NORMALIZE_PATTERN,
|
23
|
+
vat.to_s.upcase.gsub(NORMALIZE_PATTERN, '')
|
21
24
|
end
|
22
25
|
|
23
26
|
def self.vat_country_to_iso_country(vat_country)
|
24
|
-
vat_country
|
27
|
+
CONVERT_VAT_TO_ISO_COUNTRY[vat_country] || vat_country
|
25
28
|
end
|
26
29
|
|
27
30
|
def self.iso_country_to_vat_country(iso_country)
|
28
|
-
iso_country
|
31
|
+
CONVERT_ISO_TO_VAT_COUNTRY[iso_country] || iso_country
|
29
32
|
end
|
30
33
|
|
31
34
|
def self.country_is_supported?(iso_country)
|
data/lib/valvat/version.rb
CHANGED
data/lib/valvat.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'valvat/error'
|
2
4
|
require 'valvat/local'
|
3
5
|
require 'valvat/lookup'
|
@@ -7,8 +9,8 @@ require 'valvat/lookup/fault'
|
|
7
9
|
require 'active_model/validations/valvat_validator' if defined?(ActiveModel)
|
8
10
|
|
9
11
|
class Valvat
|
10
|
-
def exists?(options={})
|
12
|
+
def exists?(options = {})
|
11
13
|
Valvat::Lookup.validate(self, options)
|
12
14
|
end
|
13
|
-
|
15
|
+
alias exist? exists?
|
14
16
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
if defined?(ActiveModel)
|
@@ -6,15 +8,15 @@ if defined?(ActiveModel)
|
|
6
8
|
end
|
7
9
|
|
8
10
|
class InvoiceWithLookup < ModelBase
|
9
|
-
validates :vat_number, valvat: {lookup: true}
|
11
|
+
validates :vat_number, valvat: { lookup: true }
|
10
12
|
end
|
11
13
|
|
12
14
|
class InvoiceWithLookupAndFailIfDown < ModelBase
|
13
|
-
validates :vat_number, valvat: {lookup: :fail_if_down}
|
15
|
+
validates :vat_number, valvat: { lookup: :fail_if_down }
|
14
16
|
end
|
15
17
|
|
16
18
|
class InvoiceAllowBlank < ModelBase
|
17
|
-
validates :vat_number, valvat: {allow_blank: true}
|
19
|
+
validates :vat_number, valvat: { allow_blank: true }
|
18
20
|
end
|
19
21
|
|
20
22
|
class InvoiceAllowBlankOnAll < ModelBase
|
@@ -22,7 +24,7 @@ if defined?(ActiveModel)
|
|
22
24
|
end
|
23
25
|
|
24
26
|
class InvoiceCheckCountry < ModelBase
|
25
|
-
validates :vat_number, valvat: {match_country: :country}
|
27
|
+
validates :vat_number, valvat: { match_country: :country }
|
26
28
|
|
27
29
|
def country
|
28
30
|
@attributes[:country]
|
@@ -30,7 +32,7 @@ if defined?(ActiveModel)
|
|
30
32
|
end
|
31
33
|
|
32
34
|
class InvoiceCheckCountryWithLookup < ModelBase
|
33
|
-
validates :vat_number, valvat: {match_country: :country, lookup: true}
|
35
|
+
validates :vat_number, valvat: { match_country: :country, lookup: true }
|
34
36
|
|
35
37
|
def country
|
36
38
|
@attributes[:country]
|
@@ -38,7 +40,7 @@ if defined?(ActiveModel)
|
|
38
40
|
end
|
39
41
|
|
40
42
|
class InvoiceWithChecksum < ModelBase
|
41
|
-
validates :vat_number, valvat: {checksum: true}
|
43
|
+
validates :vat_number, valvat: { checksum: true }
|
42
44
|
end
|
43
45
|
|
44
46
|
describe Invoice do
|
@@ -46,190 +48,194 @@ if defined?(ActiveModel)
|
|
46
48
|
I18n.locale = :en
|
47
49
|
end
|
48
50
|
|
49
|
-
context
|
50
|
-
it
|
51
|
-
expect(
|
51
|
+
context 'with valid VAT number' do
|
52
|
+
it 'is valid' do
|
53
|
+
expect(described_class.new(vat_number: 'DE259597697')).to be_valid
|
52
54
|
end
|
53
55
|
end
|
54
56
|
|
55
|
-
context
|
56
|
-
let(:invoice) {
|
57
|
+
context 'with invalid VAT number' do
|
58
|
+
let(:invoice) { described_class.new(vat_number: 'DE259597697123') }
|
57
59
|
|
58
|
-
it
|
60
|
+
it 'is not valid' do
|
59
61
|
expect(invoice).not_to be_valid
|
60
62
|
end
|
61
63
|
|
62
|
-
it
|
64
|
+
it 'adds default (country specific) error message' do
|
63
65
|
invoice.valid?
|
64
|
-
expect(invoice.errors[:vat_number]).to eql([
|
66
|
+
expect(invoice.errors[:vat_number]).to eql(['is not a valid German VAT number'])
|
65
67
|
end
|
66
68
|
|
67
|
-
context
|
69
|
+
context 'with DE locale' do
|
68
70
|
before do
|
69
71
|
I18n.locale = :de
|
70
72
|
end
|
71
73
|
|
72
|
-
it
|
74
|
+
it 'adds translated error message' do
|
73
75
|
invoice.valid?
|
74
|
-
expect(invoice.errors[:vat_number]).to eql([
|
76
|
+
expect(invoice.errors[:vat_number]).to eql(['ist keine gültige deutsche USt-IdNr.'])
|
75
77
|
end
|
76
78
|
end
|
77
79
|
|
78
|
-
context
|
80
|
+
context 'with PT locale' do
|
79
81
|
before do
|
80
82
|
I18n.locale = :pt
|
81
83
|
end
|
82
84
|
|
83
|
-
it
|
85
|
+
it 'adds translated error message' do
|
84
86
|
invoice.valid?
|
85
|
-
expect(invoice.errors[:vat_number]).to eql([
|
87
|
+
expect(invoice.errors[:vat_number]).to eql(['O NIF alemão não é válido.'])
|
86
88
|
end
|
87
89
|
end
|
88
90
|
|
89
|
-
context
|
91
|
+
context 'with i18n translation in place' do
|
90
92
|
before do
|
91
93
|
I18n.backend.store_translations(:en, activemodel: {
|
92
|
-
|
93
|
-
|
94
|
+
errors: { models: { invoice: { invalid_vat: 'is ugly.' } } }
|
95
|
+
})
|
94
96
|
end
|
95
97
|
|
96
98
|
after { I18n.reload! }
|
97
99
|
|
98
|
-
it
|
100
|
+
it 'uses translation' do
|
99
101
|
invoice.valid?
|
100
|
-
expect(invoice.errors[:vat_number]).to eql([
|
102
|
+
expect(invoice.errors[:vat_number]).to eql(['is ugly.'])
|
101
103
|
end
|
102
104
|
end
|
103
105
|
|
104
|
-
context
|
106
|
+
context 'with i18n translation with country_adjective placeholder in place' do
|
105
107
|
before do
|
106
|
-
|
107
|
-
|
108
|
-
|
108
|
+
msg = 'is not a %{country_adjective} vat' # rubocop:disable Style/FormatStringToken
|
109
|
+
translation = { activemodel: { errors: { models: { invoice: { invalid_vat: msg } } } } }
|
110
|
+
I18n.backend.store_translations(:en, translation)
|
109
111
|
end
|
110
112
|
|
111
113
|
after { I18n.reload! }
|
112
114
|
|
113
|
-
it
|
114
|
-
invoice =
|
115
|
+
it 'replaces country_adjective placeholder' do
|
116
|
+
invoice = described_class.new(vat_number: 'IE123')
|
115
117
|
invoice.valid?
|
116
|
-
expect(invoice.errors[:vat_number]).to eql([
|
118
|
+
expect(invoice.errors[:vat_number]).to eql(['is not a Irish vat'])
|
117
119
|
end
|
118
120
|
|
119
|
-
it "
|
120
|
-
invoice =
|
121
|
+
it "falls back to 'European' if country is missing" do
|
122
|
+
invoice = described_class.new(vat_number: 'XX123')
|
121
123
|
invoice.valid?
|
122
|
-
expect(invoice.errors[:vat_number]).to eql([
|
124
|
+
expect(invoice.errors[:vat_number]).to eql(['is not a European vat'])
|
123
125
|
end
|
124
126
|
end
|
125
127
|
end
|
126
128
|
|
127
|
-
context
|
128
|
-
it
|
129
|
-
expect(
|
130
|
-
expect(
|
129
|
+
context 'with blank VAT number' do
|
130
|
+
it 'is not valid' do
|
131
|
+
expect(described_class.new(vat_number: '')).not_to be_valid
|
132
|
+
expect(described_class.new(vat_number: nil)).not_to be_valid
|
131
133
|
end
|
132
134
|
end
|
133
135
|
end
|
134
136
|
|
135
137
|
describe InvoiceWithLookup do
|
136
|
-
context
|
138
|
+
context 'with valid but not existing VAT number' do
|
137
139
|
before do
|
138
140
|
allow(Valvat::Syntax).to receive_messages(validate: true)
|
139
141
|
allow(Valvat::Lookup).to receive_messages(validate: false)
|
140
142
|
end
|
141
143
|
|
142
|
-
it
|
143
|
-
expect(
|
144
|
+
it 'is not valid' do
|
145
|
+
expect(described_class.new(vat_number: 'DE123')).not_to be_valid
|
144
146
|
end
|
145
147
|
end
|
146
148
|
|
147
|
-
context
|
149
|
+
context 'with valid and existing VAT number' do
|
148
150
|
before do
|
149
151
|
allow(Valvat::Syntax).to receive_messages(validate: true)
|
150
152
|
allow(Valvat::Lookup).to receive_messages(validate: true)
|
151
153
|
end
|
152
154
|
|
153
|
-
it
|
154
|
-
expect(
|
155
|
+
it 'is valid' do
|
156
|
+
expect(described_class.new(vat_number: 'DE123')).to be_valid
|
155
157
|
end
|
156
158
|
end
|
157
159
|
|
158
|
-
context
|
160
|
+
context 'with valid VAT number and VIES country service down' do
|
159
161
|
before do
|
160
162
|
allow(Valvat::Syntax).to receive_messages(validate: true)
|
161
163
|
allow(Valvat::Lookup).to receive_messages(validate: nil)
|
162
164
|
end
|
163
165
|
|
164
|
-
it
|
165
|
-
expect(
|
166
|
+
it 'is valid' do
|
167
|
+
expect(described_class.new(vat_number: 'DE123')).to be_valid
|
166
168
|
end
|
167
169
|
end
|
168
170
|
end
|
169
171
|
|
170
172
|
describe InvoiceWithLookupAndFailIfDown do
|
171
|
-
|
173
|
+
let(:record) { described_class.new }
|
174
|
+
|
175
|
+
context 'with valid VAT number and VIES country service down' do
|
172
176
|
before do
|
173
177
|
allow(Valvat::Syntax).to receive_messages(validate: true)
|
174
178
|
allow(Valvat::Lookup).to receive_messages(validate: nil)
|
175
179
|
end
|
176
180
|
|
177
|
-
it
|
178
|
-
expect(
|
181
|
+
it 'is not valid' do
|
182
|
+
expect(record).not_to be_valid
|
183
|
+
msg = 'Unable to validate your VAT number: the VIES service is down. Please try again later.'
|
184
|
+
expect(record.errors[:vat_number]).to eql([msg])
|
179
185
|
end
|
180
186
|
end
|
181
187
|
end
|
182
188
|
|
183
189
|
describe InvoiceAllowBlank do
|
184
|
-
context
|
185
|
-
it
|
186
|
-
expect(
|
187
|
-
expect(
|
190
|
+
context 'with blank VAT number' do
|
191
|
+
it 'is valid' do
|
192
|
+
expect(described_class.new(vat_number: '')).to be_valid
|
193
|
+
expect(described_class.new(vat_number: nil)).to be_valid
|
188
194
|
end
|
189
195
|
end
|
190
196
|
end
|
191
197
|
|
192
198
|
describe InvoiceAllowBlankOnAll do
|
193
|
-
context
|
194
|
-
it
|
195
|
-
expect(
|
196
|
-
expect(
|
199
|
+
context 'with blank VAT number' do
|
200
|
+
it 'is valid' do
|
201
|
+
expect(described_class.new(vat_number: '')).to be_valid
|
202
|
+
expect(described_class.new(vat_number: nil)).to be_valid
|
197
203
|
end
|
198
204
|
end
|
199
205
|
end
|
200
206
|
|
201
207
|
describe InvoiceCheckCountry do
|
202
|
-
it
|
203
|
-
expect(
|
204
|
-
expect(
|
208
|
+
it 'is not valid on blank country' do
|
209
|
+
expect(described_class.new(country: nil, vat_number: 'DE259597697')).not_to be_valid
|
210
|
+
expect(described_class.new(country: '', vat_number: 'DE259597697')).not_to be_valid
|
205
211
|
end
|
206
212
|
|
207
|
-
it
|
208
|
-
expect(
|
209
|
-
expect(
|
213
|
+
it 'is not valid on wired country' do
|
214
|
+
expect(described_class.new(country: 'XAXXX', vat_number: 'DE259597697')).not_to be_valid
|
215
|
+
expect(described_class.new(country: 'ZO', vat_number: 'DE259597697')).not_to be_valid
|
210
216
|
end
|
211
217
|
|
212
|
-
it
|
213
|
-
expect(
|
214
|
-
expect(
|
215
|
-
expect(
|
218
|
+
it 'is not valid on mismatching (eu) country' do
|
219
|
+
expect(described_class.new(country: 'FR', vat_number: 'DE259597697')).not_to be_valid
|
220
|
+
expect(described_class.new(country: 'AT', vat_number: 'DE259597697')).not_to be_valid
|
221
|
+
expect(described_class.new(country: 'DE', vat_number: 'ATU65931334')).not_to be_valid
|
216
222
|
end
|
217
223
|
|
218
|
-
it
|
219
|
-
expect(
|
220
|
-
expect(
|
224
|
+
it 'is valid on matching country' do
|
225
|
+
expect(described_class.new(country: 'DE', vat_number: 'DE259597697')).to be_valid
|
226
|
+
expect(described_class.new(country: 'AT', vat_number: 'ATU65931334')).to be_valid
|
221
227
|
end
|
222
228
|
|
223
|
-
it
|
224
|
-
invoice =
|
229
|
+
it 'gives back error message with country from :country_match' do
|
230
|
+
invoice = described_class.new(country: 'FR', vat_number: 'DE259597697')
|
225
231
|
invoice.valid?
|
226
|
-
expect(invoice.errors[:vat_number]).to eql([
|
232
|
+
expect(invoice.errors[:vat_number]).to eql(['is not a valid French VAT number'])
|
227
233
|
end
|
228
234
|
|
229
|
-
it
|
230
|
-
invoice =
|
235
|
+
it 'gives back error message with country from :country_match even on invalid VAT number' do
|
236
|
+
invoice = described_class.new(country: 'FR', vat_number: 'DE259597697123')
|
231
237
|
invoice.valid?
|
232
|
-
expect(invoice.errors[:vat_number]).to eql([
|
238
|
+
expect(invoice.errors[:vat_number]).to eql(['is not a valid French VAT number'])
|
233
239
|
end
|
234
240
|
end
|
235
241
|
|
@@ -239,30 +245,30 @@ if defined?(ActiveModel)
|
|
239
245
|
allow(Valvat::Lookup).to receive_messages(validate: true)
|
240
246
|
end
|
241
247
|
|
242
|
-
it
|
243
|
-
|
244
|
-
expect(Valvat::
|
245
|
-
|
248
|
+
it 'avoids lookup or syntax check on failed because of mismatching country' do
|
249
|
+
described_class.new(country: 'FR', vat_number: 'DE259597697').valid?
|
250
|
+
expect(Valvat::Syntax).not_to have_received(:validate)
|
251
|
+
expect(Valvat::Lookup).not_to have_received(:validate)
|
246
252
|
end
|
247
253
|
|
248
|
-
it
|
249
|
-
|
250
|
-
expect(Valvat::
|
251
|
-
|
254
|
+
it 'check syntax and looup on matching country' do
|
255
|
+
described_class.new(country: 'DE', vat_number: 'DE259597697').valid?
|
256
|
+
expect(Valvat::Syntax).to have_received(:validate)
|
257
|
+
expect(Valvat::Lookup).to have_received(:validate)
|
252
258
|
end
|
253
259
|
end
|
254
260
|
|
255
261
|
describe InvoiceWithChecksum do
|
256
|
-
context
|
257
|
-
it
|
258
|
-
expect(
|
262
|
+
context 'with valid VAT number' do
|
263
|
+
it 'is valid' do
|
264
|
+
expect(described_class.new(vat_number: 'DE259597697')).to be_valid
|
259
265
|
end
|
260
266
|
end
|
261
267
|
|
262
|
-
context
|
263
|
-
it
|
264
|
-
expect(
|
268
|
+
context 'with invalid VAT number' do
|
269
|
+
it 'is not valid' do
|
270
|
+
expect(described_class.new(vat_number: 'DE259597687')).not_to be_valid
|
265
271
|
end
|
266
272
|
end
|
267
273
|
end
|
268
|
-
end
|
274
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rspec'
|
2
4
|
begin
|
3
5
|
require 'active_model'
|
4
|
-
rescue LoadError
|
5
|
-
puts
|
6
|
+
rescue LoadError
|
7
|
+
puts 'Running specs without active_model extension'
|
6
8
|
end
|
7
9
|
|
8
|
-
require File.dirname(__FILE__)
|
10
|
+
require "#{File.dirname(__FILE__)}/../lib/valvat.rb"
|
9
11
|
|
10
12
|
RSpec.configure do |config|
|
11
13
|
config.mock_with :rspec
|
12
|
-
config.filter_run :
|
14
|
+
config.filter_run focus: true
|
13
15
|
config.run_all_when_everything_filtered = true
|
14
|
-
config.backtrace_exclusion_patterns = [/
|
16
|
+
config.backtrace_exclusion_patterns = [%r{rspec/(core|expectations)}]
|
15
17
|
end
|
16
18
|
|
17
|
-
if defined?(I18n)
|
18
|
-
I18n.enforce_available_locales = false
|
19
|
-
end
|
19
|
+
I18n.enforce_available_locales = false if defined?(I18n)
|
20
20
|
|
21
21
|
if defined?(ActiveModel)
|
22
22
|
class ModelBase
|
@@ -1,15 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Valvat::Checksum::AT do
|
4
|
-
%w
|
6
|
+
%w[ATU13585627 ATU66059506 ATU42403001].each do |valid_vat|
|
5
7
|
it "returns true on valid VAT #{valid_vat}" do
|
6
|
-
expect(Valvat::Checksum.validate(valid_vat)).to
|
8
|
+
expect(Valvat::Checksum.validate(valid_vat)).to be(true)
|
7
9
|
end
|
8
10
|
|
9
11
|
invalid_vat = "#{valid_vat[0..-4]}#{valid_vat[-1]}#{valid_vat[-2]}#{valid_vat[-3]}"
|
10
12
|
|
11
13
|
it "returns false on invalid VAT #{invalid_vat}" do
|
12
|
-
expect(Valvat::Checksum.validate(invalid_vat)).to
|
14
|
+
expect(Valvat::Checksum.validate(invalid_vat)).to be(false)
|
13
15
|
end
|
14
16
|
end
|
15
|
-
end
|
17
|
+
end
|