valvat 0.9.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.github/workflows/ruby.yml +35 -0
  5. data/.gitignore +3 -1
  6. data/.rubocop.yml +15 -0
  7. data/CHANGES.md +36 -1
  8. data/Gemfile +4 -3
  9. data/Guardfile +6 -4
  10. data/README.md +99 -47
  11. data/Rakefile +3 -1
  12. data/certs/yolk.pem +21 -21
  13. data/gemfiles/activemodel-5 +4 -5
  14. data/gemfiles/activemodel-6 +7 -0
  15. data/gemfiles/standalone +3 -5
  16. data/lib/active_model/validations/valvat_validator.rb +70 -22
  17. data/lib/valvat.rb +7 -3
  18. data/lib/valvat/checksum.rb +24 -28
  19. data/lib/valvat/checksum/at.rb +3 -1
  20. data/lib/valvat/checksum/be.rb +3 -1
  21. data/lib/valvat/checksum/bg.rb +17 -17
  22. data/lib/valvat/checksum/cy.rb +2 -0
  23. data/lib/valvat/checksum/de.rb +5 -3
  24. data/lib/valvat/checksum/dk.rb +3 -1
  25. data/lib/valvat/checksum/ee.rb +2 -0
  26. data/lib/valvat/checksum/es.rb +14 -16
  27. data/lib/valvat/checksum/fi.rb +3 -1
  28. data/lib/valvat/checksum/fr.rb +4 -2
  29. data/lib/valvat/checksum/gb.rb +53 -21
  30. data/lib/valvat/checksum/gr.rb +4 -2
  31. data/lib/valvat/checksum/hr.rb +2 -0
  32. data/lib/valvat/checksum/hu.rb +2 -0
  33. data/lib/valvat/checksum/ie.rb +5 -6
  34. data/lib/valvat/checksum/it.rb +7 -5
  35. data/lib/valvat/checksum/lt.rb +2 -0
  36. data/lib/valvat/checksum/lu.rb +3 -1
  37. data/lib/valvat/checksum/mt.rb +2 -0
  38. data/lib/valvat/checksum/nl.rb +5 -3
  39. data/lib/valvat/checksum/pl.rb +3 -1
  40. data/lib/valvat/checksum/pt.rb +3 -1
  41. data/lib/valvat/checksum/ro.rb +2 -0
  42. data/lib/valvat/checksum/se.rb +5 -3
  43. data/lib/valvat/checksum/si.rb +4 -2
  44. data/lib/valvat/error.rb +31 -0
  45. data/lib/valvat/local.rb +8 -5
  46. data/lib/valvat/locales/de.yml +2 -1
  47. data/lib/valvat/locales/en.yml +2 -1
  48. data/lib/valvat/locales/es.yml +1 -0
  49. data/lib/valvat/locales/fr.yml +1 -0
  50. data/lib/valvat/locales/hu.yml +1 -0
  51. data/lib/valvat/locales/nl.yml +1 -0
  52. data/lib/valvat/locales/pl.yml +1 -0
  53. data/lib/valvat/locales/pt.yml +28 -28
  54. data/lib/valvat/lookup.rb +14 -45
  55. data/lib/valvat/lookup/fault.rb +31 -0
  56. data/lib/valvat/lookup/request.rb +37 -11
  57. data/lib/valvat/lookup/response.rb +37 -0
  58. data/lib/valvat/syntax.rb +31 -30
  59. data/lib/valvat/utils.rb +20 -9
  60. data/lib/valvat/version.rb +3 -1
  61. data/spec/active_model/validations/valvat_validator_spec.rb +132 -100
  62. data/spec/spec_helper.rb +8 -8
  63. data/spec/valvat/checksum/at_spec.rb +8 -6
  64. data/spec/valvat/checksum/be_spec.rb +8 -6
  65. data/spec/valvat/checksum/bg_spec.rb +9 -6
  66. data/spec/valvat/checksum/cy_spec.rb +5 -3
  67. data/spec/valvat/checksum/de_spec.rb +8 -6
  68. data/spec/valvat/checksum/dk_spec.rb +8 -6
  69. data/spec/valvat/checksum/ee_spec.rb +5 -3
  70. data/spec/valvat/checksum/es_spec.rb +9 -6
  71. data/spec/valvat/checksum/fi_spec.rb +8 -6
  72. data/spec/valvat/checksum/fr_spec.rb +8 -6
  73. data/spec/valvat/checksum/gb_spec.rb +9 -5
  74. data/spec/valvat/checksum/gr_spec.rb +8 -6
  75. data/spec/valvat/checksum/hr_spec.rb +5 -3
  76. data/spec/valvat/checksum/hu_spec.rb +5 -3
  77. data/spec/valvat/checksum/ie_spec.rb +13 -5
  78. data/spec/valvat/checksum/it_spec.rb +12 -10
  79. data/spec/valvat/checksum/lt_spec.rb +5 -3
  80. data/spec/valvat/checksum/lu_spec.rb +8 -6
  81. data/spec/valvat/checksum/mt_spec.rb +5 -3
  82. data/spec/valvat/checksum/nl_spec.rb +9 -7
  83. data/spec/valvat/checksum/pl_spec.rb +10 -8
  84. data/spec/valvat/checksum/pt_spec.rb +12 -13
  85. data/spec/valvat/checksum/ro_spec.rb +5 -3
  86. data/spec/valvat/checksum/se_spec.rb +10 -8
  87. data/spec/valvat/checksum/si_spec.rb +12 -10
  88. data/spec/valvat/checksum_spec.rb +25 -10
  89. data/spec/valvat/lookup/fault_spec.rb +34 -0
  90. data/spec/valvat/lookup/request_spec.rb +19 -0
  91. data/spec/valvat/lookup/response_spec.rb +29 -0
  92. data/spec/valvat/lookup_spec.rb +202 -84
  93. data/spec/valvat/syntax_spec.rb +59 -280
  94. data/spec/valvat/utils_spec.rb +76 -43
  95. data/spec/valvat_spec.rb +140 -152
  96. data/valvat.gemspec +18 -17
  97. metadata +61 -41
  98. metadata.gz.sig +0 -0
  99. data/.travis.yml +0 -33
  100. data/gemfiles/activemodel-3-2 +0 -8
  101. data/gemfiles/activemodel-4 +0 -8
  102. data/gemfiles/before-ruby21/activemodel-3-2 +0 -9
  103. data/gemfiles/before-ruby21/activemodel-4 +0 -9
  104. data/gemfiles/before-ruby21/standalone +0 -8
  105. data/lib/valvat/lookup/request_with_id.rb +0 -31
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::LU do
4
- %w(LU13669580 LU25361352 LU23124018 LU17560609).each do |valid_vat|
5
- it "returns true on valid vat #{valid_vat}" do
6
- expect(Valvat::Checksum.validate(valid_vat)).to eql(true)
6
+ %w[LU13669580 LU25361352 LU23124018 LU17560609].each do |valid_vat|
7
+ it "returns true on valid VAT #{valid_vat}" do
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[-2]}#{valid_vat[-3]}#{valid_vat[-1]}"
10
12
 
11
- it "returns false on invalid vat #{invalid_vat}" do
12
- expect(Valvat::Checksum.validate(invalid_vat)).to eql(false)
13
+ it "returns false on invalid VAT #{invalid_vat}" do
14
+ expect(Valvat::Checksum.validate(invalid_vat)).to be(false)
13
15
  end
14
16
  end
15
- end
17
+ end
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::MT do
4
- %w(MT11407334 MT10126313 MT11539237).each do |valid_vat|
5
- it "returns true on valid vat #{valid_vat}" do
6
+ %w[MT11407334 MT10126313 MT11539237].each do |valid_vat|
7
+ it "returns true on valid VAT #{valid_vat}" do
6
8
  expect(Valvat::Checksum.validate(valid_vat)).to be true
7
9
  end
8
10
 
9
11
  invalid_vat = "#{valid_vat[0..-3]}#{valid_vat[-1]}#{valid_vat[-2]}"
10
12
 
11
- it "returns false on invalid vat #{invalid_vat}" do
13
+ it "returns false on invalid VAT #{invalid_vat}" do
12
14
  expect(Valvat::Checksum.validate(invalid_vat)).to be false
13
15
  end
14
16
  end
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::NL do
4
- %w(NL123456782B12 NL802549391B01 NL808661863B01 NL820893559B01 NL000099998B57 NL123456789B13).each do |valid_vat|
5
- it "returns true on valid vat #{valid_vat}" do
6
- expect(Valvat::Checksum.validate(valid_vat)).to eql(true)
6
+ %w[NL123456782B12 NL802549391B01 NL808661863B01 NL820893559B01 NL000099998B57 NL123456789B13].each do |valid|
7
+ it "returns true on valid VAT #{valid}" do
8
+ expect(Valvat::Checksum.validate(valid)).to be(true)
7
9
  end
8
10
 
9
- invalid_vat = "#{valid_vat[0..-5]}#{valid_vat[-2]}#{valid_vat[-3]}#{valid_vat[-4]}#{valid_vat[-1]}#{valid_vat[-2]}#{valid_vat[-3]}"
11
+ invalid = "#{valid[0..-5]}#{valid[-2]}#{valid[-3]}#{valid[-4]}#{valid[-1]}#{valid[-2]}#{valid[-3]}"
10
12
 
11
- it "returns false on invalid vat #{invalid_vat}" do
12
- expect(Valvat::Checksum.validate(invalid_vat)).to eql(false)
13
+ it "returns false on invalid VAT #{invalid}" do
14
+ expect(Valvat::Checksum.validate(invalid)).to be(false)
13
15
  end
14
16
  end
15
- end
17
+ end
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::PL do
4
- %w(PL8567346215 PL5260211587 PL9720575348 PL5272650022).each do |valid_vat|
5
- it "returns true on valid vat #{valid_vat}" do
6
- expect(Valvat::Checksum.validate(valid_vat)).to eql(true)
6
+ %w[PL8567346215 PL5260211587 PL9720575348 PL5272650022].each do |valid_vat|
7
+ it "returns true on valid VAT #{valid_vat}" do
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
- it "returns false on invalid vat #{invalid_vat}" do
12
- expect(Valvat::Checksum.validate(invalid_vat)).to eql(false)
13
+ it "returns false on invalid VAT #{invalid_vat}" do
14
+ expect(Valvat::Checksum.validate(invalid_vat)).to be(false)
13
15
  end
14
16
  end
15
17
 
16
- it "returns false on special case invalid vat PL8566445330" do
17
- expect(Valvat::Checksum.validate("PL8566445330")).to eql(false)
18
+ it 'returns false on special case invalid VAT PL8566445330' do
19
+ expect(Valvat::Checksum.validate('PL8566445330')).to be(false)
18
20
  end
19
- end
21
+ end
@@ -1,31 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::PT do
4
-
5
- subject { Valvat::Checksum }
6
-
7
- %w(PT136695973 PT501413197 PT503037753 PT500243590 PT500100144 PT502921838).each do |valid_vat|
8
- it "returns true on valid vat #{valid_vat}" do
9
- expect(subject.validate(valid_vat)).to eql(true)
6
+ %w[PT136695973 PT501413197 PT503037753 PT500243590 PT500100144 PT502921838].each do |valid_vat|
7
+ it "returns true on valid VAT #{valid_vat}" do
8
+ expect(Valvat::Checksum.validate(valid_vat)).to be(true)
10
9
  end
11
10
 
12
11
  invalid_vat = "#{valid_vat[0..-5]}#{valid_vat[-1]}#{valid_vat[-4]}#{valid_vat[-2]}#{valid_vat[-3]}"
13
12
 
14
- it "returns false on invalid vat #{invalid_vat}" do
15
- expect(subject.validate(invalid_vat)).to eql(false)
13
+ it "returns false on invalid VAT #{invalid_vat}" do
14
+ expect(Valvat::Checksum.validate(invalid_vat)).to be(false)
16
15
  end
17
16
  end
18
17
 
19
18
  # Portuguese business VAT should start with the following numbers
20
19
  # 1 or 2 (pessoa singular) - singular person
21
20
  # 5 (pessoa colectiva) - collective person
22
- # 6 (pessoa colectiva pública) - public collective person
23
- # 8 (empresário em nome individual) - individual enterpreneur
24
- # 9 (pessoa colectiva irregular ou número provisório) - provisional number
21
+ # 6 (pessoa colectiva publica) - public collective person
22
+ # 8 (empresario em nome individual) - individual enterpreneur
23
+ # 9 (pessoa colectiva irregular ou numero provisorio) - provisional number
25
24
  # http://www.nif.pt/nif-das-empresas/
26
- %w(PT148166644 PT111623448 PT204874866 PT292261314 PT579104222 PT628910002 PT812627318 PT943935784).each do |number|
25
+ %w[PT148166644 PT111623448 PT204874866 PT292261314 PT579104222 PT628910002 PT812627318 PT943935784].each do |number|
27
26
  it "returns true on a valid number - #{number}" do
28
- expect(subject.validate(number)).to eql(true)
27
+ expect(Valvat::Checksum.validate(number)).to be(true)
29
28
  end
30
29
  end
31
30
  end
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::RO do
4
- %w(RO123456789 RO99908 RO19 RO124 RO13182060).each do |valid_vat|
5
- it "returns true on valid vat #{valid_vat}" do
6
+ %w[RO123456789 RO99908 RO19 RO124 RO13182060].each do |valid_vat|
7
+ it "returns true on valid VAT #{valid_vat}" do
6
8
  expect(Valvat::Checksum.validate(valid_vat)).to be true
7
9
  end
8
10
 
9
11
  invalid_vat = "#{valid_vat[0..-3]}#{valid_vat[-1]}#{valid_vat[-2]}"
10
12
 
11
- it "returns false on invalid vat #{invalid_vat}" do
13
+ it "returns false on invalid VAT #{invalid_vat}" do
12
14
  expect(Valvat::Checksum.validate(invalid_vat)).to be false
13
15
  end
14
16
  end
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::SE do
4
- %w(SE136695975501 SE556464687401 SE502052817901 SE556555952201 SE556084097601).each do |valid_vat|
5
- it "returns true on valid vat #{valid_vat}" do
6
- expect(Valvat::Checksum.validate(valid_vat)).to eql(true)
6
+ %w[SE136695975501 SE556464687401 SE502052817901 SE556555952201 SE556084097601].each do |valid_vat|
7
+ it "returns true on valid VAT #{valid_vat}" do
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
- it "returns false on invalid vat #{invalid_vat}" do
12
- expect(Valvat::Checksum.validate(invalid_vat)).to eql(false)
13
+ it "returns false on invalid VAT #{invalid_vat}" do
14
+ expect(Valvat::Checksum.validate(invalid_vat)).to be(false)
13
15
  end
14
16
  end
15
17
 
16
- it "returns false on special case invalid vat SE556464687400" do
17
- expect(Valvat::Checksum.validate("SE556464687400")).to eql(false)
18
+ it 'returns false on special case invalid VAT SE556464687400' do
19
+ expect(Valvat::Checksum.validate('SE556464687400')).to be(false)
18
20
  end
19
- end
21
+ end
@@ -1,23 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Checksum::SI do
4
- %w(SI59082437 SI51049406 SI86154575 SI47431857 SI22511822 SI26833921).each do |valid_vat|
5
- it "returns true on valid vat #{valid_vat}" do
6
- expect(Valvat::Checksum.validate(valid_vat)).to eql(true)
6
+ %w[SI59082437 SI51049406 SI86154575 SI47431857 SI22511822 SI26833921].each do |valid_vat|
7
+ it "returns true on valid VAT #{valid_vat}" do
8
+ expect(Valvat::Checksum.validate(valid_vat)).to be(true)
7
9
  end
8
10
 
9
- invalid_vat = "#{valid_vat[0..-2]}#{valid_vat[-1].to_i+1}"
11
+ invalid_vat = "#{valid_vat[0..-2]}#{valid_vat[-1].to_i + 1}"
10
12
 
11
- it "returns false on invalid vat #{invalid_vat}" do
12
- expect(Valvat::Checksum.validate(invalid_vat)).to eql(false)
13
+ it "returns false on invalid VAT #{invalid_vat}" do
14
+ expect(Valvat::Checksum.validate(invalid_vat)).to be(false)
13
15
  end
14
16
  end
15
17
 
16
- it "returns false on special case invalid vat SI11111107" do
17
- expect(Valvat::Checksum.validate("SI11111107")).to eql(false)
18
+ it 'returns false on special case invalid VAT SI11111107' do
19
+ expect(Valvat::Checksum.validate('SI11111107')).to be(false)
18
20
  end
19
21
 
20
- it "returns false on special case invalid vat SI01111108" do
21
- expect(Valvat::Checksum.validate("SI01111108")).to eql(false)
22
+ it 'returns false on special case invalid VAT SI01111108' do
23
+ expect(Valvat::Checksum.validate('SI01111108')).to be(false)
22
24
  end
23
25
  end
@@ -1,25 +1,40 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  begin
4
- require File.dirname(__FILE__) + '/../../tmp/valid_vat_numbers.rb'
6
+ require "#{File.dirname(__FILE__)}/../../tmp/valid_vat_numbers.rb"
5
7
  rescue LoadError
6
- VALID_VAT_NUMBERS = []
8
+ VALID_VAT_NUMBERS = [].freeze
7
9
  end
8
10
 
11
+ # Syntax is correct, but checksum invalid
12
+ INVALID_VAT_NUMBERS = %w[
13
+ DE002768611 DE161216774 DE192970834 DE223361232 DE226095231
14
+ DE227411313 DE230928917 DE232123248 DE267748405 DE275615509
15
+ DE311927825 DE329214395 DE815842425 DE821530465 DE999999999
16
+ ].freeze
17
+
9
18
  describe Valvat::Checksum do
10
- describe "#validate" do
11
- it "returns true on vat number with unknown checksum algorithm" do
12
- expect(Valvat::Checksum.validate("CZ699001237")).to eql(true)
19
+ describe '#validate' do
20
+ it 'returns true on VAT number with unknown checksum algorithm' do
21
+ expect(described_class.validate('CZ699001237')).to be(true)
13
22
  end
14
23
 
15
- it "returns false on corrupt number (e.g checks syntax)" do
16
- expect(Valvat::Checksum.validate("FI1234567891")).to eql(false)
24
+ it 'returns false on corrupt number (e.g checks syntax)' do
25
+ expect(described_class.validate('FI1234567891')).to be(false)
17
26
  end
18
27
 
19
28
  VALID_VAT_NUMBERS.each do |valid_vat|
20
- it "returns true on valid vat number #{valid_vat}" do
21
- expect(Valvat::Checksum.validate(valid_vat)).to eql(true)
29
+ it "returns true on valid VAT number #{valid_vat}" do
30
+ expect(described_class.validate(valid_vat)).to be(true)
31
+ end
32
+ end
33
+
34
+ INVALID_VAT_NUMBERS.each do |invalid_vat|
35
+ it "returns false on invalid VAT number #{invalid_vat}" do
36
+ expect(described_class.validate(invalid_vat)).to be(false)
22
37
  end
23
38
  end
24
39
  end
25
- end
40
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Valvat::Lookup::Fault do
6
+ it "returns {valid: false} on fault 'INVALID_INPUT'" do
7
+ expect(described_class.new({
8
+ fault: { faultstring: 'INVALID_INPUT' }
9
+ }).to_hash).to eql({ valid: false })
10
+ end
11
+
12
+ {
13
+ 'SERVICE_UNAVAILABLE' => Valvat::ServiceUnavailable,
14
+ 'MS_UNAVAILABLE' => Valvat::MemberStateUnavailable,
15
+ 'INVALID_REQUESTER_INFO' => Valvat::InvalidRequester,
16
+ 'TIMEOUT' => Valvat::Timeout,
17
+ 'VAT_BLOCKED' => Valvat::BlockedError,
18
+ 'IP_BLOCKED' => Valvat::BlockedError,
19
+ 'GLOBAL_MAX_CONCURRENT_REQ' => Valvat::RateLimitError,
20
+ 'GLOBAL_MAX_CONCURRENT_REQ_TIME' => Valvat::RateLimitError,
21
+ 'MS_MAX_CONCURRENT_REQ' => Valvat::RateLimitError,
22
+ 'MS_MAX_CONCURRENT_REQ_TIME' => Valvat::RateLimitError,
23
+ 'ANYTHING ELSE' => Valvat::UnknownViesError,
24
+ 'REALLY ANYTHING' => Valvat::UnknownViesError
25
+ }.each do |fault, error|
26
+ it "returns error on fault '#{fault}'" do
27
+ expect(described_class.new({
28
+ fault: { faultstring: fault }
29
+ }).to_hash).to eql({
30
+ error: error.new(fault)
31
+ })
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Valvat::Lookup::Request do
6
+ it 'returns Response on success' do
7
+ response = described_class.new('IE6388047V', {}).perform
8
+ expect(response).to be_a(Valvat::Lookup::Response)
9
+
10
+ # Skip if VIES is down
11
+ expect(response.to_hash[:name]).to eql('GOOGLE IRELAND LIMITED') unless response.is_a?(Valvat::Lookup::Fault)
12
+ end
13
+
14
+ it 'returns Fault on failure' do
15
+ response = described_class.new('XC123123', {}).perform
16
+ expect(response).to be_a(Valvat::Lookup::Fault)
17
+ expect(response.to_hash).to eql({ valid: false })
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Valvat::Lookup::Response do
6
+ it 'removes @xmlns from :check_vat_response hash' do
7
+ expect(described_class.new({
8
+ check_vat_response: { :a => 1, :b => 2, :@xmlns => true }
9
+ }).to_hash).to eql({ a: 1, b: 2 })
10
+ end
11
+
12
+ it "removes 'trader_'-Prefixes :check_vat_response hash" do
13
+ expect(described_class.new({
14
+ check_vat_response: { a: 1, trader_b: 2 }
15
+ }).to_hash).to eql({ a: 1, b: 2 })
16
+ end
17
+
18
+ it 'accepts hash keyed as :check_vat_approx_response' do
19
+ expect(described_class.new({
20
+ check_vat_approx_response: { a: 1, b: 2 }
21
+ }).to_hash).to eql({ a: 1, b: 2 })
22
+ end
23
+
24
+ it 'allows direct access to hash via []' do
25
+ expect(described_class.new({
26
+ check_vat_response: { a: 123, b: 2 }
27
+ })[:a]).to be(123)
28
+ end
29
+ end
@@ -1,125 +1,243 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Valvat::Lookup do
4
- describe "#validate" do
5
- context "existing vat number" do
6
+ describe '#validate' do
7
+ context 'with existing VAT number' do
8
+ it 'returns true' do
9
+ result = described_class.validate('IE6388047V')
10
+ # Skip if VIES is down
11
+ expect(result).to be(true) unless result.nil?
12
+ end
6
13
 
7
- it "returns true" do
8
- result = Valvat::Lookup.validate("IE6388047V")
14
+ it 'allows Valvat instance as input' do
15
+ result = described_class.validate(Valvat.new('IE6388047V'))
16
+ # Skip if VIES is down
17
+ expect(result).to be(true) unless result.nil?
18
+ end
19
+ end
9
20
 
10
- unless result.nil?
11
- expect(result).to eql(true)
12
- else
13
- puts "Skipping IE vies lookup spec"
14
- end
21
+ context 'with not existing VAT number' do
22
+ it 'returns false' do
23
+ result = described_class.validate('IE6388048V')
24
+ # Skip if VIES is down
25
+ expect(result).to be(false) unless result.nil?
15
26
  end
27
+ end
16
28
 
17
- it "allows Valvat instance as input" do
18
- result = Valvat::Lookup.validate(Valvat.new("IE6388047V"))
29
+ context 'with invalid country code / input' do
30
+ it 'returns false' do
31
+ expect(described_class.validate('AE259597697')).to be(false)
32
+ expect(described_class.validate('')).to be(false)
33
+ end
34
+ end
35
+
36
+ context 'with details' do
37
+ let(:details) do
38
+ {
39
+ country_code: 'IE',
40
+ vat_number: '6388047V',
41
+ name: 'GOOGLE IRELAND LIMITED',
42
+ address: '3RD FLOOR, GORDON HOUSE, BARROW STREET, DUBLIN 4',
43
+ valid: true
44
+ }
45
+ end
19
46
 
20
- unless result.nil?
21
- expect(result).to eql(true)
22
- else
23
- puts "Skipping IE vies lookup spec"
47
+ it 'returns hash of details instead of true' do
48
+ result = described_class.validate('IE6388047V', detail: true)
49
+ # Skip if VIES is down
50
+ if result
51
+ expect(result.delete(:request_date)).to be_kind_of(Date)
52
+ expect(result).to eql(details)
24
53
  end
25
54
  end
55
+
56
+ it 'still returns false on not existing VAT number' do
57
+ result = described_class.validate('LU21416128', detail: true)
58
+ # Skip if VIES is down
59
+ expect(result).to be(false) unless result.nil?
60
+ end
26
61
  end
27
62
 
28
- context "not existing vat number" do
29
- it "returns false" do
30
- result = Valvat::Lookup.validate("IE6388048V")
63
+ context 'with request identifier' do
64
+ let(:response) { described_class.validate('IE6388047V', requester: 'IE6388047V') }
65
+ let(:details) do
66
+ {
67
+ country_code: 'IE',
68
+ vat_number: '6388047V',
69
+ name: 'GOOGLE IRELAND LIMITED',
70
+ company_type: nil,
71
+ address: '3RD FLOOR, GORDON HOUSE, BARROW STREET, DUBLIN 4',
72
+ valid: true
73
+ }
74
+ end
31
75
 
32
- unless result.nil?
33
- expect(result).to eql(false)
34
- else
35
- puts "Skipping IE vies lookup spec"
76
+ it 'returns hash of details instead of true' do
77
+ # Skip if VIES is down
78
+ if response
79
+ expect(response.delete(:request_identifier).size).to be(16)
80
+ expect(response.delete(:request_date)).to be_kind_of(Date)
81
+ expect(response).to eql(details)
36
82
  end
37
83
  end
84
+
85
+ it 'supports old :requester_vat option for backwards stability' do
86
+ expect(
87
+ described_class.new('IE6388047V', requester_vat: 'LU21416127').instance_variable_get(:@options)[:requester]
88
+ ).to eql('LU21416127')
89
+ end
38
90
  end
91
+ end
39
92
 
40
- context "invalid country code / input" do
41
- it "returns false" do
42
- expect(Valvat::Lookup.validate("AE259597697")).to eql(false)
43
- expect(Valvat::Lookup.validate("")).to eql(false)
93
+ describe '#validate with VIES test enviroment' do
94
+ let(:options) do
95
+ { savon: { wsdl: 'https://ec.europa.eu/taxation_customs/vies/checkVatTestService.wsdl' },
96
+ skip_local_validation: true }
97
+ end
98
+
99
+ context 'with valid request with valid VAT number' do
100
+ subject(:result) { described_class.validate('DE100', options) }
101
+
102
+ it 'returns true' do
103
+ expect(result).to be(true)
44
104
  end
45
105
  end
46
106
 
47
- context "with details" do
48
- it "returns hash of details instead of true" do
49
- result = Valvat::Lookup.validate("IE6388047V", :detail => true)
107
+ context 'with valid request with an invalid VAT number' do
108
+ subject(:result) { described_class.validate('DE200', options) }
50
109
 
51
- if result
52
- expect(result.delete(:request_date)).to be_kind_of(Date)
53
- expect(result).to eql({
54
- :country_code=>"IE",
55
- :vat_number=>"6388047V",
56
- :name=>"GOOGLE IRELAND LIMITED",
57
- :address=>"3RD FLOOR, GORDON HOUSE, BARROW STREET, DUBLIN 4"
58
- })
59
- else
60
- puts "Skipping IE vies lookup spec; result = #{result.inspect}"
61
- end
110
+ it 'returns false' do
111
+ expect(result).to be(false)
62
112
  end
113
+ end
63
114
 
64
- it "still returns false on not existing vat number" do
65
- result = Valvat::Lookup.validate("LU21416128", :detail => true)
115
+ describe 'Error : INVALID_INPUT' do
116
+ subject(:result) { described_class.validate('DE201', options) }
66
117
 
67
- unless result.nil?
68
- expect(result).to eql(false)
69
- else
70
- puts "Skipping LU vies lookup spec"
71
- end
118
+ it 'returns false' do
119
+ expect(result).to be(false)
72
120
  end
73
121
  end
74
122
 
75
- context "with request identifier" do
76
- it "returns hash of details instead of true" do
77
- response = Valvat::Lookup.validate("IE6388047V", :requester_vat => "IE6388047V")
123
+ describe 'Error : INVALID_REQUESTER_INFO' do
124
+ subject(:result) { described_class.validate('DE202', options) }
78
125
 
79
- if response
80
- expect(response[:request_identifier].size).to eql(16)
81
- request_identifier = response[:request_identifier]
82
- expect(response.delete(:request_date)).to be_kind_of(Date)
83
- expect(response).to eql({
84
- :country_code=>"IE",
85
- :vat_number=>"6388047V",
86
- :name => "GOOGLE IRELAND LIMITED",
87
- :company_type=>nil,
88
- :address => "3RD FLOOR, GORDON HOUSE, BARROW STREET, DUBLIN 4",
89
- :request_identifier=> request_identifier
90
- })
91
- else
92
- puts "Skipping IE vies lookup spec"
93
- end
126
+ it 'raises Valvat::InvalidRequester' do
127
+ expect { result }.to raise_error(Valvat::InvalidRequester)
128
+ end
129
+ end
130
+
131
+ describe 'Error : SERVICE_UNAVAILABLE' do
132
+ subject(:result) { described_class.validate('DE300', options) }
133
+
134
+ it 'returns nil' do
135
+ expect(result).to be(nil)
136
+ end
137
+
138
+ it 'raises error with raise_error: true' do
139
+ expect do
140
+ described_class.validate('DE300', options.merge(raise_error: true))
141
+ end.to raise_error(Valvat::ServiceUnavailable)
142
+ end
143
+ end
144
+
145
+ describe 'Error : MS_UNAVAILABLE' do
146
+ subject(:result) { described_class.validate('DE301', options) }
147
+
148
+ it 'returns nil' do
149
+ expect(result).to be(nil)
150
+ end
151
+
152
+ it 'raises error with raise_error: true' do
153
+ expect do
154
+ described_class.validate('DE301', options.merge(raise_error: true))
155
+ end.to raise_error(Valvat::MemberStateUnavailable)
94
156
  end
95
157
  end
96
158
 
97
- context "error on request" do
98
- before do
99
- @request = double("request")
100
- allow(Valvat::Lookup::Request).to receive_messages(:new => @request)
101
- allow(@request).to receive(:perform).and_raise(ArgumentError.new)
159
+ describe 'Error : TIMEOUT' do
160
+ subject(:result) { described_class.validate('DE302', options) }
161
+
162
+ it 'raises error' do
163
+ expect { result }.to raise_error(Valvat::Timeout)
102
164
  end
103
165
 
104
- it "should return nil" do
105
- expect(Valvat::Lookup.validate("LU21416127")).to eql(nil)
166
+ it 'returns nil with raise_error set to false' do
167
+ expect(described_class.validate('DE302', options.merge(raise_error: false))).to be(nil)
106
168
  end
169
+ end
170
+
171
+ describe 'Error : VAT_BLOCKED' do
172
+ subject(:result) { described_class.validate('DE400', options) }
107
173
 
108
- it "should raise error with raise_error option" do
109
- expect {Valvat::Lookup.validate("LU21416127", :raise_error => true)}.to raise_error(ArgumentError)
174
+ it 'raises error' do
175
+ expect { result }.to raise_error(Valvat::BlockedError, /VAT_BLOCKED/)
176
+ end
177
+
178
+ it 'returns nil with raise_error set to false' do
179
+ expect(described_class.validate('DE400', options.merge(raise_error: false))).to be(nil)
110
180
  end
111
181
  end
112
182
 
113
- # TODO : Reactivate with coorect "down" response
114
- # context "country web service down" do
115
- # before do
116
- # json = "{\"error_message\": \"Member State service unavailable.\", \"error_code\": 1, \"error\": true}"
117
- # FakeWeb.register_uri(:get, "http://isvat.appspot.com/DE/259597697/", :body => json)
118
- # end
183
+ describe 'Error : IP_BLOCKED' do
184
+ subject(:result) { described_class.validate('DE401', options) }
119
185
 
120
- # it "returns nil" do
121
- # Valvat::Lookup.validate("DE259597697").should eql(nil)
122
- # end
123
- # end
186
+ it 'raises error' do
187
+ expect { result }.to raise_error(Valvat::BlockedError, /IP_BLOCKED/)
188
+ end
189
+
190
+ it 'returns nil with raise_error set to false' do
191
+ expect(described_class.validate('DE401', options.merge(raise_error: false))).to be(nil)
192
+ end
193
+ end
194
+
195
+ describe 'Error : GLOBAL_MAX_CONCURRENT_REQ' do
196
+ subject(:result) { described_class.validate('DE500', options) }
197
+
198
+ it 'raises error' do
199
+ expect { result }.to raise_error(Valvat::RateLimitError, /GLOBAL_MAX_CONCURRENT_REQ/)
200
+ end
201
+
202
+ it 'returns nil with raise_error set to false' do
203
+ expect(described_class.validate('DE500', options.merge(raise_error: false))).to be(nil)
204
+ end
205
+ end
206
+
207
+ describe 'Error : GLOBAL_MAX_CONCURRENT_REQ_TIME' do
208
+ subject(:result) { described_class.validate('DE501', options) }
209
+
210
+ it 'raises error' do
211
+ expect { result }.to raise_error(Valvat::RateLimitError, /GLOBAL_MAX_CONCURRENT_REQ_TIME/)
212
+ end
213
+
214
+ it 'returns nil with raise_error set to false' do
215
+ expect(described_class.validate('DE501', options.merge(raise_error: false))).to be(nil)
216
+ end
217
+ end
218
+
219
+ describe 'Error : MS_MAX_CONCURRENT_REQ' do
220
+ subject(:result) { described_class.validate('DE600', options) }
221
+
222
+ it 'raises error' do
223
+ expect { result }.to raise_error(Valvat::RateLimitError, /MS_MAX_CONCURRENT_REQ/)
224
+ end
225
+
226
+ it 'returns nil with raise_error set to false' do
227
+ expect(described_class.validate('DE600', options.merge(raise_error: false))).to be(nil)
228
+ end
229
+ end
230
+
231
+ describe 'Error : MS_MAX_CONCURRENT_REQ_TIME' do
232
+ subject(:result) { described_class.validate('DE601', options) }
233
+
234
+ it 'raises error' do
235
+ expect { result }.to raise_error(Valvat::RateLimitError, /MS_MAX_CONCURRENT_REQ_TIME/)
236
+ end
237
+
238
+ it 'returns nil with raise_error set to false' do
239
+ expect(described_class.validate('DE601', options.merge(raise_error: false))).to be(nil)
240
+ end
241
+ end
124
242
  end
125
243
  end